require 'rails_helper'

feature "Categorize workflow instances as other scenario", js: true do
  let(:user_params) { {} }
  let(:user) { create :user, user_params }
  background { i_am_logged_in_as user }
  context "with permissions" do
    let(:user_params) { {role_names: ["read-class-WorkflowInstance", "categorize-class-WorkflowInstance"] } }

    scenario "nothing selected" do
      create 'workflow_instance/scenario1', bucket: 'B1'
      create 'workflow_instance/scenario1', bucket: 'B2'
      visit "/workflow_instance/scenario1s"
      js_click "#categorize-as-button"
      page.accept_alert "Nothing selected" # will fail if no modal is present
    end

    scenario "from scenario1 to 2" do
      create 'workflow_instance/scenario1', bucket: 'B1'
      create 'workflow_instance/scenario1', bucket: 'B2'
      visit "/workflow_instance/scenario1s"
      select_all_workflow_instances
      js_click "#categorize-as-button"
      accept_confirm "Weet je dit zeker? Stappen die niet aanwezig zijn in SC2 zullen verdwijnen." do
        sleep 0.3
        WorkflowInstance.pluck(:sti_type).uniq.should eq ["WorkflowInstance::Scenario2"]
        page.current_path.should eq "/workflow_instance/scenario2s/selection"
        page.should have_content "B1"
        page.should have_content "B2"
      end
    end
  end

  context "without permissions" do
    let(:user_params) { {role_names: ["read-class-WorkflowInstance"] } }
    scenario "there is no button" do
      create 'workflow_instance/scenario1', bucket: 'B1'
      create 'workflow_instance/scenario1', bucket: 'B2'
      page.should_not have_selector "#categorize-as-button"
    end

    scenario "fabricate hack request", js: false do
      wi = create 'workflow_instance/scenario1', bucket: 'B1'
      page.driver.post "/dunlop/workflow_instances/categorize_as?scenario=scenario2&ids=#{wi.id}"
      page.status_code.should eq 403

      WorkflowInstance.pluck(:sti_type).should eq ["WorkflowInstance::Scenario1"]
    end
  end
end
