<commit_before># frozen_string_literal: true

# Tests for abilities related to Question object.
require "rails_helper"

describe "abilities for questions" do
  context "for coordinator role" do
    before do
      @user = create(:user, role_name: "coordinator")
      @ability = Ability.new(user: @user, mode: "mission", mission: get_mission)
    end

    it "should be able to create and index" do
      %i[create index].each { |op| expect(@ability).to be_able_to(op, Question) }
    end

    context "when unpublished" do
      before do
        @form = create(:form, question_types: %w[text])
        @question = @form.questions.first
      end

      it "should be able to do all" do
        %i[show update update_code update_core destroy].each { |op| expect(@ability).to be_able_to(op, @question) }
      end

      context "with answers" do
        before do
          create(:response, form: @form, answer_values: ["foo"])
        end

        it "should be able to do all but destroy and update core" do
          %i[show update update_code].each { |op| expect(@ability).to be_able_to(op, @question) }
          %i[update_core destroy].each { |op| expect(@ability).not_to be_able_to(op, @question) }
        end
      end
    end

    context "when published" do
      before do
        @form = create(:form, question_types: %w[text])
        @form.publish!
        @question = @form.questions.first
      end

      it "should be able show and update only" do
        %i[show update update_code].each { |op| expect(@ability).to be_able_to(op, @question) }
        %i[update_core destroy].each { |op| expect(@ability).not_to be_able_to(op, @question) }
      end
    end

    context "when unpublished std copy" do
      before do
        @std = create(:form, :standard, question_types: %w[text])
        @copy = @std.replicate(mode: :to_mission, dest_mission: get_mission)
        @question = @copy.questions.first
      end

      it "should be able to do all except update_code" do
        %i[show update update_core destroy].each { |op| expect(@ability).to be_able_to(op, @question) }
        %i[update_code].each { |op| expect(@ability).not_to be_able_to(op, @question) }
      end
    end
  end
end<commit_msg>10156: Fix question ability spec<commit_after># frozen_string_literal: true

# Tests for abilities related to Question object.
require "rails_helper"

describe "abilities for questions" do
  context "for coordinator role" do
    let(:ability) { Ability.new(user: user, mode: "mission", mission: get_mission) }
    let(:user) { create(:user, role_name: "coordinator") }

    it "should be able to create and index" do
      %i[create index].each { |op| expect(ability).to be_able_to(op, Question) }
    end

    context "when draft" do
      let(:form) { create(:form, question_types: %w[text]) }
      let(:question) { form.questions.first }

      it "should be able to do all" do
        %i[show update update_code update_core destroy].each do |op|
          expect(ability).to be_able_to(op, question)
        end
      end

      context "with answers" do
        before do
          create(:response, form: form, answer_values: ["foo"])
        end

        it "should be able to do all but destroy and update core" do
          %i[show update update_code].each { |op| expect(ability).to be_able_to(op, question) }
          %i[update_core destroy].each { |op| expect(ability).not_to be_able_to(op, question) }
        end
      end
    end

    context "when live" do
      let(:form) { create(:form, :live, question_types: %w[text]) }
      let(:question) { form.questions.first }

      it "should be able show and update only" do
        %i[show update update_code].each { |op| expect(ability).to be_able_to(op, question) }
        %i[update_core destroy].each { |op| expect(ability).not_to be_able_to(op, question) }
      end
    end

    context "when std copy" do
      let(:std) { create(:form, :standard, question_types: %w[text]) }
      let(:copy) { std.replicate(mode: :to_mission, dest_mission: get_mission) }
      let(:question) { copy.questions.first }

      it "should be able to do all except update_code" do
        %i[show update update_core destroy].each { |op| expect(ability).to be_able_to(op, question) }
        %i[update_code].each { |op| expect(ability).not_to be_able_to(op, question) }
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.11616739080722616, 'fraction_numerical': 0.001143379830779785, 'mean_word_length': 2.9193548387096775, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3303844', 'n_tokens_mistral': 1377, 'n_tokens_neox': 1341, 'n_words': 412}