diff --git a/spec/controllers/ajax/question_controller_spec.rb b/spec/controllers/ajax/question_controller_spec.rb index 021a4172..0fc8922b 100644 --- a/spec/controllers/ajax/question_controller_spec.rb +++ b/spec/controllers/ajax/question_controller_spec.rb @@ -48,11 +48,13 @@ describe Ajax::QuestionController, :ajax_controller, type: :controller do include_examples "returns the expected response" end - shared_examples "enqueues a QuestionWorker job" do |_expected_rcpt| + shared_examples "enqueues QuestionWorker jobs" do it "enqueues a QuestionWorker job" do - allow(QuestionWorker).to receive(:perform_async) + allow(QuestionWorker).to receive(:perform_bulk) subject - expect(QuestionWorker).to have_received(:perform_async).with(user.id, Question.last.id) + question_id = Question.last.id + bulk_args = followers.map { |f| [f.id, question_id] } + expect(QuestionWorker).to have_received(:perform_bulk).with(bulk_args) end include_examples "returns the expected response" @@ -61,8 +63,10 @@ describe Ajax::QuestionController, :ajax_controller, type: :controller do shared_examples "does not enqueue a QuestionWorker job" do it "does not enqueue a QuestionWorker job" do allow(QuestionWorker).to receive(:perform_async) + allow(QuestionWorker).to receive(:perform_bulk) subject expect(QuestionWorker).not_to have_received(:perform_async) + expect(QuestionWorker).not_to have_received(:perform_bulk) end include_examples "returns the expected response" @@ -194,13 +198,18 @@ describe Ajax::QuestionController, :ajax_controller, type: :controller do context "when rcpt is followers" do let(:rcpt) { "followers" } + let(:followers) { FactoryBot.create_list(:user, 3) } + + before do + followers.each { |follower| follower.follow(user) } + end 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" + include_examples "enqueues QuestionWorker jobs" end context "when anonymousQuestion is false" do @@ -208,7 +217,7 @@ describe Ajax::QuestionController, :ajax_controller, type: :controller do let(:expected_question_anonymous) { false } include_examples "creates the question", false - include_examples "enqueues a QuestionWorker job", "followers" + include_examples "enqueues QuestionWorker jobs" end end diff --git a/spec/lib/use_case/question/create_followers_spec.rb b/spec/lib/use_case/question/create_followers_spec.rb index 7b3e2d75..3f048370 100644 --- a/spec/lib/use_case/question/create_followers_spec.rb +++ b/spec/lib/use_case/question/create_followers_spec.rb @@ -6,13 +6,20 @@ describe UseCase::Question::CreateFollowers do subject do UseCase::Question::CreateFollowers.call( source_user_id: source_user.id, - content: content, - author_identifier: author_identifier + content:, + author_identifier:, ) end context "user is logged in" do + before do + followers.each do |target_user| + target_user.follow source_user + end + end + let(:source_user) { create(:user) } + let(:followers) { create_list(:user, 5) } let(:content) { "content" } let(:author_identifier) { nil } @@ -21,7 +28,9 @@ describe UseCase::Question::CreateFollowers do end it "enqueues a QuestionWorker job" do - expect(QuestionWorker).to have_enqueued_sidekiq_job(source_user.id, subject[:resource].id) + followers.each do |target_user| + expect(QuestionWorker).to have_enqueued_sidekiq_job(target_user.id, subject[:resource].id) + end end it "increments the asked count" do diff --git a/spec/workers/question_worker_spec.rb b/spec/workers/question_worker_spec.rb index 0795e55b..3d03b7e8 100644 --- a/spec/workers/question_worker_spec.rb +++ b/spec/workers/question_worker_spec.rb @@ -9,22 +9,21 @@ describe QuestionWorker do let(:content) { Faker::Lorem.sentence } let(:question) { FactoryBot.create(:question, content:, user:) } let(:question_id) { question.id } + let(:follower) { FactoryBot.create(:user) } + let(:follower_id) { follower.id } before do - 5.times do - other_user = FactoryBot.create(:user) - other_user.follow(user) - end + follower.follow(user) end - subject { described_class.new.perform(user_id, question_id) } + subject { described_class.new.perform(follower_id, question_id) } it "places the question in the inbox of the user's followers" do expect { subject } .to( - change { Inbox.where(user_id: user.followers.ids, question_id:, new: true).count } + change { Inbox.where(user_id: follower_id, question_id:, new: true).count } .from(0) - .to(5) + .to(1) ) end @@ -32,56 +31,41 @@ describe QuestionWorker do question.content = "Some spicy question text" question.save - MuteRule.create(user_id: user.followers.first.id, muted_phrase: "spicy") + MuteRule.create(user_id: follower_id, muted_phrase: "spicy") - expect { subject } - .to( - change { Inbox.where(user_id: user.followers.ids, question_id:, new: true).count } - .from(0) - .to(4) - ) + subject + expect(Inbox.where(user_id: follower_id, question_id:, new: true).count).to eq(0) end it "respects inbox locks" do - user.followers.first.update(privacy_lock_inbox: true) + follower.update(privacy_lock_inbox: true) - expect { subject } - .to( - change { Inbox.where(user_id: user.followers.ids, question_id:, new: true).count } - .from(0) - .to(4) - ) + subject + expect(Inbox.where(user_id: follower_id, question_id:, new: true).count).to eq(0) end it "does not send questions to banned users" do - user.followers.first.ban + follower.ban - expect { subject } - .to( - change { Inbox.where(user_id: user.followers.ids, question_id:, new: true).count } - .from(0) - .to(4) - ) + subject + expect(Inbox.where(user_id: follower_id, question_id:, new: true).count).to eq(0) end context "receiver has push notifications enabled" do - let(:receiver) { FactoryBot.create(:user) } - before do Rpush::Webpush::App.create( name: "webpush", certificate: { public_key: "AAAA", private_key: "AAAA", subject: "" }.to_json, - connections: 1 + connections: 1, ) WebPushSubscription.create!( - user: receiver, + user: follower, subscription: { endpoint: "This will not be used", keys: {}, } ) - receiver.follow(user) end it "sends notifications" do @@ -98,15 +82,20 @@ describe QuestionWorker do let(:content) { "x" * 1000 } it "sends to recipients who allow long questions" do - user.followers.first.profile.update(allow_long_questions: true) + follower.profile.update(allow_long_questions: true) expect { subject } .to( - change { Inbox.where(user_id: user.followers.ids, question_id:, new: true).count } + change { Inbox.where(user_id: follower_id, question_id:, new: true).count } .from(0) .to(1) ) end + + it "does not send to recipients who do not allow long questions" do + subject + expect(Inbox.where(user_id: follower_id, question_id:, new: true).count).to eq(0) + end end end end