<commit_before># frozen_string_literal: true

require "rails_helper"

describe Ajax::QuestionController, :ajax_controller, type: :controller do
  describe "#create" do
    shared_examples "creates the question" do |check_for_inbox = true|
      it "creates the question" do
        expect { subject }.to(change { Question.count }.by(1))
        expect(Question.last.content).to eq(question_content)
        expect(Question.last.author_is_anonymous).to be(expected_question_anonymous)
        expect(Question.last.user).to eq(expected_question_user)
      end

      if check_for_inbox
        it "adds the question to the target users' inbox" do
          expect { subject }.to(change { target_user.inboxes.count }.by(1))
          expect(target_user.inboxes.last.question.content).to eq(question_content)
        end
      end

      include_examples "returns the expected response"
    end

    shared_examples "does not create the question" do |check_for_inbox = true|
      it "does not create the question" do
        expect { subject }.not_to(change { Question.count })
      end

      if check_for_inbox
        it "does not add the question to the target users' inbox" do
          expect { subject }.not_to(change { target_user.inboxes.count })
        end
      end

      include_examples "returns the expected response"
    end

    shared_examples "enqueues a QuestionWorker job" do |expected_rcpt|
      it "enqueues a QuestionWorker job" do
        allow(QuestionWorker).to receive(:perform_async)
        subject
        expect(QuestionWorker).to have_received(:perform_async).with(user.id, Question.last.id)
      end

      include_examples "returns the expected response"
    end

    shared_examples "does not enqueue a QuestionWorker job" do
      it "does not enqueue a QuestionWorker job" do
        allow(QuestionWorker).to receive(:perform_async)
        subject
        expect(QuestionWorker).not_to have_received(:perform_async)
      end

      include_examples "returns the expected response"
    end

    let(:target_user) { FactoryBot.create(:user, privacy_allow_anonymous_questions: user_allows_anonymous_questions) }
    let(:params) do
      {
        question: question_content,
        anonymousQuestion: anonymous_question,
        rcpt: rcpt
      }
    end

    subject { post(:create, params: params) }

    context "when user is signed in" do
      let(:question_content) { "Was letzte Preis?" }
      let(:expected_response) do
        {
          "success" => true,
          "status" => "okay",
          "message" => anything
        }
      end
      let(:expected_question_user) { user }

      before(:each) { sign_in(user) }

      context "when rcpt is a valid user" do
        let(:rcpt) { target_user.id }

        context "when user allows anonymous questions" do
          let(:user_allows_anonymous_questions) { true }

          context "when anonymousQuestion is true" do
            let(:anonymous_question) { "true" }
            let(:expected_question_anonymous) { true }

            include_examples "creates the question"
          end

          context "when anonymousQuestion is false" do
            let(:anonymous_question) { "false" }
            let(:expected_question_anonymous) { false }

            include_examples "creates the question"
          end
        end

        context "when user does not allow anonymous questions" do
          let(:user_allows_anonymous_questions) { false }

          context "when anonymousQuestion is true" do
            let(:anonymous_question) { "true" }
            let(:expected_response) do
              {
                "success" => false,
                "status" => "unknown",
                "message" => anything
              }
            end

            include_examples "does not create the question"
          end

          context "when anonymousQuestion is false" do
            let(:anonymous_question) { "false" }
            let(:expected_question_anonymous) { false }

            include_examples "creates the question"
          end
        end
      end

      context "when rcpt is followers" do
        let(:rcpt) { "followers" }

        context "when anonymousQuestion is true" do
          let(:anonymous_question) { "true" }
          let(:expected_question_anonymous) { false }

          include_examples "creates the question", false
          include_examples "enqueues a QuestionWorker job", "followers"
        end

        context "when anonymousQuestion is false" do
          let(:anonymous_question) { "false" }
          let(:expected_question_anonymous) { false }

          include_examples "creates the question", false
          include_examples "enqueues a QuestionWorker job", "followers"
        end
      end

      context "when rcpt is a non-existent user" do
        let(:rcpt) { "tripmeister_eder" }
        let(:anonymous_question) { "false" }
        let(:expected_response) do
          {
            "success" => false,
            "status" => "not_found",
            "message" => anything
          }
        end

        include_examples "does not create the question", false

        include_examples "returns the expected response"
      end
    end

    context "when user is not signed in" do
      let(:target_user) { FactoryBot.create(:user, privacy_allow_anonymous_questions: user_allows_anonymous_questions) }
      let(:question_content) { "Was letzte Preis?" }
      let(:anonymous_question) { "true" }
      let(:expected_response) do
        {
          "success" => true,
          "status" => "okay",
          "message" => anything
        }
      end
      let(:expected_question_anonymous) { true }
      let(:expected_question_user) { nil }

      context "when rcpt is a valid user" do
        let(:rcpt) { target_user.id }

        context "when user allows anonymous questions" do
          let(:user_allows_anonymous_questions) { true }
<commit_msg>Test if direct flag is being assigned correctly when a question is created<commit_after># frozen_string_literal: true

require "rails_helper"

describe Ajax::QuestionController, :ajax_controller, type: :controller do
  describe "#create" do
    shared_examples "creates the question" do |check_for_inbox = true|
      it "creates the question" do
        expect { subject }.to(change { Question.count }.by(1))
        expect(Question.last.content).to eq(question_content)
        expect(Question.last.author_is_anonymous).to be(expected_question_anonymous)
        expect(Question.last.user).to eq(expected_question_user)
        expect(Question.last.direct).to eq(expected_question_direct)
      end

      if check_for_inbox
        it "adds the question to the target users' inbox" do
          expect { subject }.to(change { target_user.inboxes.count }.by(1))
          expect(target_user.inboxes.last.question.content).to eq(question_content)
        end
      end

      include_examples "returns the expected response"
    end

    shared_examples "does not create the question" do |check_for_inbox = true|
      it "does not create the question" do
        expect { subject }.not_to(change { Question.count })
      end

      if check_for_inbox
        it "does not add the question to the target users' inbox" do
          expect { subject }.not_to(change { target_user.inboxes.count })
        end
      end

      include_examples "returns the expected response"
    end

    shared_examples "enqueues a QuestionWorker job" do |expected_rcpt|
      it "enqueues a QuestionWorker job" do
        allow(QuestionWorker).to receive(:perform_async)
        subject
        expect(QuestionWorker).to have_received(:perform_async).with(user.id, Question.last.id)
      end

      include_examples "returns the expected response"
    end

    shared_examples "does not enqueue a QuestionWorker job" do
      it "does not enqueue a QuestionWorker job" do
        allow(QuestionWorker).to receive(:perform_async)
        subject
        expect(QuestionWorker).not_to have_received(:perform_async)
      end

      include_examples "returns the expected response"
    end

    let(:target_user) { FactoryBot.create(:user, privacy_allow_anonymous_questions: user_allows_anonymous_questions) }
    let(:params) do
      {
        question: question_content,
        anonymousQuestion: anonymous_question,
        rcpt: rcpt
      }
    end

    subject { post(:create, params: params) }

    context "when user is signed in" do
      let(:question_content) { "Was letzte Preis?" }
      let(:expected_response) do
        {
          "success" => true,
          "status" => "okay",
          "message" => anything
        }
      end
      let(:expected_question_user) { user }

      before(:each) { sign_in(user) }

      context "when rcpt is a valid user" do
        let(:rcpt) { target_user.id }

        context "when user allows anonymous questions" do
          let(:user_allows_anonymous_questions) { true }
          let(:expected_question_direct) { true }

          context "when anonymousQuestion is true" do
            let(:anonymous_question) { "true" }
            let(:expected_question_anonymous) { true }

            include_examples "creates the question"
          end

          context "when anonymousQuestion is false" do
            let(:anonymous_question) { "false" }
            let(:expected_question_anonymous) { false }

            include_examples "creates the question"
          end
        end

        context "when user does not allow anonymous questions" do
          let(:user_allows_anonymous_questions) { false }

          context "when anonymousQuestion is true" do
            let(:anonymous_question) { "true" }
            let(:expected_response) do
              {
                "success" => false,
                "status" => "unknown",
                "message" => anything
              }
            end

            include_examples "does not create the question"
          end

          context "when anonymousQuestion is false" do
            let(:anonymous_question) { "false" }
            let(:expected_question_anonymous) { false }
            let(:expected_question_direct) { true }

            include_examples "creates the question"
          end
        end
      end

      context "when rcpt is followers" do
        let(:rcpt) { "followers" }
        let(:expected_question_direct) { false }

        context "when anonymousQuestion is true" do
          let(:anonymous_question) { "true" }
          let(:expected_question_anonymous) { false }

          include_examples "creates the question", false
          include_examples "enqueues a QuestionWorker job", "followers"
        end

        context "when anonymousQuestion is false" do
          let(:anonymous_question) { "false" }
          let(:expected_question_anonymous) { false }

          include_examples "creates the question", false
          include_examples "enqueues a QuestionWorker job", "followers"
        end
      end

      context "when rcpt is a non-existent user" do
        let(:rcpt) { "tripmeister_eder" }
        let(:anonymous_question) { "false" }
        let(:expected_response) do
          {
            "success" => false,
            "status" => "not_found",
            "message" => anything
          }
        end

        include_examples "does not create the question", false

        include_examples "returns the expected response"
      end
    end

    context "when user is not signed in" do
      let(:target_user) { FactoryBot.create(:user, privacy_allow_anonymous_questions: user_allows_anonymous_questions) }
      let(:question_content) { "Was letzte Preis?" }
      let(:anonymous_question) { "true" }
      let(:expected_response) do
        {
          "success" => true,
          "status" => "okay",
          "message" => anything
        }
      end
      let(:expected_question_anonymous) { true }
      let(:expected_question_user) { nil }

      context "when rcpt is a valid user" do
        let(:rcpt) { target_user.id }

        context "when user allows anonymous questions" do
          let(:user_allows_anonymous_questions) { true }
          let(:expected_question_direct) { true }

{'fraction_non_alphanumeric': 0.08385829992636833, 'fraction_numerical': 0.00032725190215168124, 'mean_word_length': 2.3435448577680527, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, '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': '3394740', 'n_tokens_mistral': 3380, 'n_tokens_neox': 3231, 'n_words': 916}