Update tests for single-recipient QuestionWorker behaviour

This commit is contained in:
Karina Kwiatek 2023-11-01 22:40:41 +01:00
parent 1dd1c828eb
commit 2327b2ce52
3 changed files with 50 additions and 43 deletions

View File

@ -48,11 +48,13 @@ describe Ajax::QuestionController, :ajax_controller, type: :controller do
include_examples "returns the expected response" include_examples "returns the expected response"
end end
shared_examples "enqueues a QuestionWorker job" do |_expected_rcpt| shared_examples "enqueues QuestionWorker jobs" do
it "enqueues a QuestionWorker job" do it "enqueues a QuestionWorker job" do
allow(QuestionWorker).to receive(:perform_async) allow(QuestionWorker).to receive(:perform_bulk)
subject 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 end
include_examples "returns the expected response" 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 shared_examples "does not enqueue a QuestionWorker job" do
it "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_async)
allow(QuestionWorker).to receive(:perform_bulk)
subject subject
expect(QuestionWorker).not_to have_received(:perform_async) expect(QuestionWorker).not_to have_received(:perform_async)
expect(QuestionWorker).not_to have_received(:perform_bulk)
end end
include_examples "returns the expected response" include_examples "returns the expected response"
@ -194,13 +198,18 @@ describe Ajax::QuestionController, :ajax_controller, type: :controller do
context "when rcpt is followers" do context "when rcpt is followers" do
let(:rcpt) { "followers" } 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 context "when anonymousQuestion is true" do
let(:anonymous_question) { "true" } let(:anonymous_question) { "true" }
let(:expected_question_anonymous) { false } let(:expected_question_anonymous) { false }
include_examples "creates the question", false include_examples "creates the question", false
include_examples "enqueues a QuestionWorker job", "followers" include_examples "enqueues QuestionWorker jobs"
end end
context "when anonymousQuestion is false" do context "when anonymousQuestion is false" do
@ -208,7 +217,7 @@ describe Ajax::QuestionController, :ajax_controller, type: :controller do
let(:expected_question_anonymous) { false } let(:expected_question_anonymous) { false }
include_examples "creates the question", false include_examples "creates the question", false
include_examples "enqueues a QuestionWorker job", "followers" include_examples "enqueues QuestionWorker jobs"
end end
end end

View File

@ -6,13 +6,20 @@ describe UseCase::Question::CreateFollowers do
subject do subject do
UseCase::Question::CreateFollowers.call( UseCase::Question::CreateFollowers.call(
source_user_id: source_user.id, source_user_id: source_user.id,
content: content, content:,
author_identifier: author_identifier author_identifier:,
) )
end end
context "user is logged in" do 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(:source_user) { create(:user) }
let(:followers) { create_list(:user, 5) }
let(:content) { "content" } let(:content) { "content" }
let(:author_identifier) { nil } let(:author_identifier) { nil }
@ -21,7 +28,9 @@ describe UseCase::Question::CreateFollowers do
end end
it "enqueues a QuestionWorker job" do 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 end
it "increments the asked count" do it "increments the asked count" do

View File

@ -9,22 +9,21 @@ describe QuestionWorker do
let(:content) { Faker::Lorem.sentence } let(:content) { Faker::Lorem.sentence }
let(:question) { FactoryBot.create(:question, content:, user:) } let(:question) { FactoryBot.create(:question, content:, user:) }
let(:question_id) { question.id } let(:question_id) { question.id }
let(:follower) { FactoryBot.create(:user) }
let(:follower_id) { follower.id }
before do before do
5.times do follower.follow(user)
other_user = FactoryBot.create(:user)
other_user.follow(user)
end
end 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 it "places the question in the inbox of the user's followers" do
expect { subject } expect { subject }
.to( .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) .from(0)
.to(5) .to(1)
) )
end end
@ -32,56 +31,41 @@ describe QuestionWorker do
question.content = "Some spicy question text" question.content = "Some spicy question text"
question.save question.save
MuteRule.create(user_id: user.followers.first.id, muted_phrase: "spicy") MuteRule.create(user_id: follower_id, muted_phrase: "spicy")
expect { subject } subject
.to( expect(Inbox.where(user_id: follower_id, question_id:, new: true).count).to eq(0)
change { Inbox.where(user_id: user.followers.ids, question_id:, new: true).count }
.from(0)
.to(4)
)
end end
it "respects inbox locks" do it "respects inbox locks" do
user.followers.first.update(privacy_lock_inbox: true) follower.update(privacy_lock_inbox: true)
expect { subject } subject
.to( expect(Inbox.where(user_id: follower_id, question_id:, new: true).count).to eq(0)
change { Inbox.where(user_id: user.followers.ids, question_id:, new: true).count }
.from(0)
.to(4)
)
end end
it "does not send questions to banned users" do it "does not send questions to banned users" do
user.followers.first.ban follower.ban
expect { subject } subject
.to( expect(Inbox.where(user_id: follower_id, question_id:, new: true).count).to eq(0)
change { Inbox.where(user_id: user.followers.ids, question_id:, new: true).count }
.from(0)
.to(4)
)
end end
context "receiver has push notifications enabled" do context "receiver has push notifications enabled" do
let(:receiver) { FactoryBot.create(:user) }
before do before do
Rpush::Webpush::App.create( Rpush::Webpush::App.create(
name: "webpush", name: "webpush",
certificate: { public_key: "AAAA", private_key: "AAAA", subject: "" }.to_json, certificate: { public_key: "AAAA", private_key: "AAAA", subject: "" }.to_json,
connections: 1 connections: 1,
) )
WebPushSubscription.create!( WebPushSubscription.create!(
user: receiver, user: follower,
subscription: { subscription: {
endpoint: "This will not be used", endpoint: "This will not be used",
keys: {}, keys: {},
} }
) )
receiver.follow(user)
end end
it "sends notifications" do it "sends notifications" do
@ -98,15 +82,20 @@ describe QuestionWorker do
let(:content) { "x" * 1000 } let(:content) { "x" * 1000 }
it "sends to recipients who allow long questions" do 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 } expect { subject }
.to( .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) .from(0)
.to(1) .to(1)
) )
end 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 end
end end