2020-05-25 18:33:09 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "rails_helper"
|
|
|
|
|
|
|
|
describe QuestionWorker do
|
|
|
|
describe "#perform" do
|
|
|
|
let(:user) { FactoryBot.create(:user) }
|
|
|
|
let(:user_id) { user.id }
|
2023-01-08 19:22:00 +01:00
|
|
|
let(:content) { Faker::Lorem.sentence }
|
|
|
|
let(:question) { FactoryBot.create(:question, content:, user:) }
|
2020-05-25 18:33:09 +02:00
|
|
|
let(:question_id) { question.id }
|
|
|
|
|
|
|
|
before do
|
|
|
|
5.times do
|
|
|
|
other_user = FactoryBot.create(:user)
|
|
|
|
other_user.follow(user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { described_class.new.perform(user_id, question_id) }
|
|
|
|
|
|
|
|
it "places the question in the inbox of the user's followers" do
|
|
|
|
expect { subject }
|
|
|
|
.to(
|
2024-01-27 13:02:58 +01:00
|
|
|
change { InboxEntry.where(user_id: user.followers.ids, question_id:, new: true).count }
|
2020-05-25 18:33:09 +02:00
|
|
|
.from(0)
|
|
|
|
.to(5)
|
|
|
|
)
|
|
|
|
end
|
2021-12-22 19:21:09 +01:00
|
|
|
|
|
|
|
it "respects mute rules" do
|
2022-01-23 19:25:56 +01:00
|
|
|
question.content = "Some spicy question text"
|
2021-12-22 19:21:09 +01:00
|
|
|
question.save
|
|
|
|
|
2022-01-23 19:25:56 +01:00
|
|
|
MuteRule.create(user_id: user.followers.first.id, muted_phrase: "spicy")
|
2021-12-22 19:21:09 +01:00
|
|
|
|
|
|
|
expect { subject }
|
|
|
|
.to(
|
2024-01-27 13:02:58 +01:00
|
|
|
change { InboxEntry.where(user_id: user.followers.ids, question_id:, new: true).count }
|
2021-12-22 19:21:09 +01:00
|
|
|
.from(0)
|
|
|
|
.to(4)
|
|
|
|
)
|
|
|
|
end
|
2022-11-06 15:02:12 +01:00
|
|
|
|
|
|
|
it "respects inbox locks" do
|
|
|
|
user.followers.first.update(privacy_lock_inbox: true)
|
2022-12-23 14:03:28 +01:00
|
|
|
|
2022-11-12 13:39:35 +01:00
|
|
|
expect { subject }
|
|
|
|
.to(
|
2024-01-27 13:02:58 +01:00
|
|
|
change { InboxEntry.where(user_id: user.followers.ids, question_id:, new: true).count }
|
2022-11-12 13:39:35 +01:00
|
|
|
.from(0)
|
|
|
|
.to(4)
|
|
|
|
)
|
|
|
|
end
|
2022-12-23 14:03:28 +01:00
|
|
|
|
2022-11-12 12:13:51 +01:00
|
|
|
it "does not send questions to banned users" do
|
|
|
|
user.followers.first.ban
|
2022-11-06 15:02:12 +01:00
|
|
|
|
|
|
|
expect { subject }
|
|
|
|
.to(
|
2024-01-27 13:02:58 +01:00
|
|
|
change { InboxEntry.where(user_id: user.followers.ids, question_id:, new: true).count }
|
2022-11-06 15:02:12 +01:00
|
|
|
.from(0)
|
|
|
|
.to(4)
|
|
|
|
)
|
|
|
|
end
|
2022-12-23 14:03:28 +01:00
|
|
|
|
|
|
|
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,
|
2022-12-26 10:59:56 +01:00
|
|
|
connections: 1
|
2022-12-23 14:03:28 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
WebPushSubscription.create!(
|
|
|
|
user: receiver,
|
|
|
|
subscription: {
|
|
|
|
endpoint: "This will not be used",
|
2023-03-04 17:49:24 +01:00
|
|
|
keys: {},
|
2022-12-26 10:59:56 +01:00
|
|
|
}
|
2022-12-23 14:03:28 +01:00
|
|
|
)
|
|
|
|
receiver.follow(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "sends notifications" do
|
|
|
|
expect { subject }
|
|
|
|
.to(
|
|
|
|
change { Rpush::Webpush::Notification.count }
|
|
|
|
.from(0)
|
|
|
|
.to(1)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2023-01-08 19:22:00 +01:00
|
|
|
|
|
|
|
context "long question" do
|
|
|
|
let(:content) { "x" * 1000 }
|
|
|
|
|
|
|
|
it "sends to recipients who allow long questions" do
|
|
|
|
user.followers.first.profile.update(allow_long_questions: true)
|
|
|
|
|
|
|
|
expect { subject }
|
|
|
|
.to(
|
2024-01-27 13:02:58 +01:00
|
|
|
change { InboxEntry.where(user_id: user.followers.ids, question_id:, new: true).count }
|
2023-01-08 19:22:00 +01:00
|
|
|
.from(0)
|
|
|
|
.to(1)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2020-05-25 18:33:09 +02:00
|
|
|
end
|
|
|
|
end
|