2020-03-11 19:16:00 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
RSpec.describe Jobs::BookmarkReminderNotifications do
|
2023-06-21 09:00:19 -05:00
|
|
|
subject(:job) { described_class.new }
|
2020-03-11 19:16:00 -05:00
|
|
|
|
2023-11-09 16:47:59 -06:00
|
|
|
fab!(:user)
|
2020-03-11 19:16:00 -05:00
|
|
|
let(:five_minutes_ago) { Time.zone.now - 5.minutes }
|
2022-05-08 18:37:23 -05:00
|
|
|
let(:bookmark1) { Fabricate(:bookmark, user: user) }
|
|
|
|
let(:bookmark2) { Fabricate(:bookmark, user: user) }
|
|
|
|
let(:bookmark3) { Fabricate(:bookmark, user: user) }
|
2023-01-09 05:18:21 -06:00
|
|
|
let!(:bookmarks) { [bookmark1, bookmark2, bookmark3] }
|
2020-03-11 19:16:00 -05:00
|
|
|
|
|
|
|
before do
|
|
|
|
# this is done to avoid model validations on Bookmark
|
|
|
|
bookmark1.update_column(:reminder_at, five_minutes_ago - 10.minutes)
|
|
|
|
bookmark2.update_column(:reminder_at, five_minutes_ago - 5.minutes)
|
|
|
|
bookmark3.update_column(:reminder_at, five_minutes_ago)
|
2020-05-22 23:56:13 -05:00
|
|
|
Discourse.redis.flushdb
|
2020-03-11 19:16:00 -05:00
|
|
|
end
|
|
|
|
|
2022-03-08 11:44:18 -06:00
|
|
|
it "sends every reminder and sets the reminder_last_sent_at" do
|
2023-06-21 09:00:19 -05:00
|
|
|
job.execute
|
2020-03-11 19:16:00 -05:00
|
|
|
bookmark1.reload
|
|
|
|
bookmark2.reload
|
|
|
|
bookmark3.reload
|
|
|
|
expect(bookmark1.reminder_last_sent_at).not_to eq(nil)
|
|
|
|
expect(bookmark2.reminder_last_sent_at).not_to eq(nil)
|
|
|
|
expect(bookmark3.reminder_last_sent_at).not_to eq(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "will not send a reminder for a bookmark in the future" do
|
2022-05-08 18:37:23 -05:00
|
|
|
freeze_time
|
2020-03-11 19:16:00 -05:00
|
|
|
bookmark4 = Fabricate(:bookmark, reminder_at: Time.zone.now + 1.day)
|
2023-06-21 09:00:19 -05:00
|
|
|
expect { job.execute }.to change { Notification.where(user: user).count }.by(3)
|
2022-05-08 18:37:23 -05:00
|
|
|
expect(bookmark1.reload.reminder_last_sent_at).to eq_time(Time.zone.now)
|
|
|
|
expect(bookmark2.reload.reminder_last_sent_at).to eq_time(Time.zone.now)
|
|
|
|
expect(bookmark3.reload.reminder_last_sent_at).to eq_time(Time.zone.now)
|
2020-03-11 19:16:00 -05:00
|
|
|
expect(bookmark4.reload.reminder_at).not_to eq(nil)
|
2022-05-08 18:37:23 -05:00
|
|
|
expect(bookmark4.reload.reminder_last_sent_at).to eq(nil)
|
2020-03-11 19:16:00 -05:00
|
|
|
end
|
|
|
|
|
2021-04-08 22:06:35 -05:00
|
|
|
context "when a user is over the bookmark limit" do
|
|
|
|
it "clearing their reminder does not error and hold up the rest" do
|
|
|
|
other_bookmark = Fabricate(:bookmark, user: bookmark1.user)
|
|
|
|
other_bookmark.update_column(:reminder_at, five_minutes_ago)
|
|
|
|
SiteSetting.max_bookmarks_per_user = 2
|
2023-06-21 09:00:19 -05:00
|
|
|
expect { job.execute }.not_to raise_error
|
2021-04-08 22:06:35 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-11 19:16:00 -05:00
|
|
|
context "when the number of notifications exceed max_reminder_notifications_per_run" do
|
|
|
|
it "does not send them in the current run, but will send them in the next" do
|
|
|
|
begin
|
|
|
|
Jobs::BookmarkReminderNotifications.max_reminder_notifications_per_run = 2
|
2023-06-21 09:00:19 -05:00
|
|
|
job.execute
|
2022-03-08 11:44:18 -06:00
|
|
|
expect(bookmark1.reload.reminder_last_sent_at).not_to eq(nil)
|
|
|
|
expect(bookmark2.reload.reminder_last_sent_at).not_to eq(nil)
|
|
|
|
expect(bookmark3.reload.reminder_last_sent_at).to eq(nil)
|
2020-03-11 19:16:00 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-08-30 18:15:36 -05:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "will not send notification when topic is not available" do
|
2022-05-22 19:07:15 -05:00
|
|
|
bookmark1.bookmarkable.topic.destroy
|
|
|
|
bookmark2.bookmarkable.topic.destroy
|
|
|
|
bookmark3.bookmarkable.topic.destroy
|
2023-06-21 09:00:19 -05:00
|
|
|
expect { job.execute }.not_to change {
|
2023-01-09 05:18:21 -06:00
|
|
|
Notification.where(notification_type: Notification.types[:bookmark_reminder]).count
|
|
|
|
}
|
2020-08-30 18:15:36 -05:00
|
|
|
end
|
2020-03-11 19:16:00 -05:00
|
|
|
end
|