2020-03-12 08:16:00 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
RSpec.describe BookmarkReminderNotificationHandler do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:user)
|
2020-03-12 08:16:00 +08:00
|
|
|
|
2020-05-23 12:56:13 +08:00
|
|
|
before { Discourse.redis.flushdb }
|
2020-03-12 08:16:00 +08:00
|
|
|
|
2020-03-13 08:44:39 +08:00
|
|
|
describe "#send_notification" do
|
2023-06-21 22:00:19 +08:00
|
|
|
subject(:send_notification) { handler.send_notification }
|
|
|
|
|
|
|
|
let(:handler) { described_class.new(bookmark) }
|
2022-05-09 07:37:23 +08:00
|
|
|
let!(:bookmark) do
|
2022-05-23 08:07:15 +08:00
|
|
|
Fabricate(:bookmark_next_business_day_reminder, user: user, bookmarkable: Fabricate(:post))
|
2020-03-12 08:16:00 +08:00
|
|
|
end
|
|
|
|
|
2020-03-13 08:44:39 +08:00
|
|
|
it "creates a bookmark reminder notification with the correct details" do
|
2023-06-21 22:00:19 +08:00
|
|
|
send_notification
|
2020-03-13 08:44:39 +08:00
|
|
|
notif = bookmark.user.notifications.last
|
|
|
|
expect(notif.notification_type).to eq(Notification.types[:bookmark_reminder])
|
2022-05-23 08:07:15 +08:00
|
|
|
expect(notif.topic_id).to eq(bookmark.bookmarkable.topic_id)
|
|
|
|
expect(notif.post_number).to eq(bookmark.bookmarkable.post_number)
|
2020-03-13 08:44:39 +08:00
|
|
|
data = JSON.parse(notif.data)
|
2022-05-23 08:07:15 +08:00
|
|
|
expect(data["title"]).to eq(bookmark.bookmarkable.topic.title)
|
2020-03-13 08:44:39 +08:00
|
|
|
expect(data["display_username"]).to eq(bookmark.user.username)
|
|
|
|
expect(data["bookmark_name"]).to eq(bookmark.name)
|
2022-05-23 08:07:15 +08:00
|
|
|
expect(data["bookmarkable_url"]).to eq(bookmark.bookmarkable.url)
|
2020-03-13 08:44:39 +08:00
|
|
|
end
|
|
|
|
|
2022-05-23 08:07:15 +08:00
|
|
|
context "when the bookmarkable is deleted" do
|
2021-09-15 08:16:54 +08:00
|
|
|
before do
|
2022-05-23 08:07:15 +08:00
|
|
|
bookmark.bookmarkable.trash!
|
2021-09-15 08:16:54 +08:00
|
|
|
bookmark.reload
|
|
|
|
end
|
|
|
|
|
2022-03-09 01:44:18 +08:00
|
|
|
it "does not send a notification and updates last notification attempt time" do
|
2023-06-21 22:00:19 +08:00
|
|
|
expect { send_notification }.not_to change { Notification.count }
|
2022-03-09 01:44:18 +08:00
|
|
|
expect(bookmark.reload.reminder_last_sent_at).not_to be_blank
|
2021-09-15 08:16:54 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-21 08:00:39 +08:00
|
|
|
context "when the auto_delete_preference is when_reminder_sent" do
|
2020-07-29 07:43:32 +08:00
|
|
|
before do
|
2022-05-23 08:07:15 +08:00
|
|
|
TopicUser.create!(topic: bookmark.bookmarkable.topic, user: user, bookmarked: true)
|
2020-07-21 08:00:39 +08:00
|
|
|
bookmark.update(
|
|
|
|
auto_delete_preference: Bookmark.auto_delete_preferences[:when_reminder_sent],
|
|
|
|
)
|
2020-07-29 07:43:32 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "deletes the bookmark after the reminder gets sent" do
|
2023-06-21 22:00:19 +08:00
|
|
|
send_notification
|
2020-05-07 11:37:39 +08:00
|
|
|
expect(Bookmark.find_by(id: bookmark.id)).to eq(nil)
|
|
|
|
end
|
2020-07-29 07:43:32 +08:00
|
|
|
|
|
|
|
it "changes the TopicUser bookmarked column to false" do
|
2023-06-21 22:00:19 +08:00
|
|
|
send_notification
|
2022-05-23 08:07:15 +08:00
|
|
|
expect(TopicUser.find_by(topic: bookmark.bookmarkable.topic, user: user).bookmarked).to eq(
|
|
|
|
false,
|
|
|
|
)
|
2020-07-29 07:43:32 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context "if there are still other bookmarks in the topic" do
|
|
|
|
before do
|
2022-05-23 08:07:15 +08:00
|
|
|
Fabricate(
|
|
|
|
:bookmark,
|
|
|
|
bookmarkable: Fabricate(:post, topic: bookmark.bookmarkable.topic),
|
|
|
|
user: user,
|
|
|
|
)
|
2020-07-29 07:43:32 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not change the TopicUser bookmarked column to false" do
|
2023-06-21 22:00:19 +08:00
|
|
|
send_notification
|
2022-05-23 08:07:15 +08:00
|
|
|
expect(
|
|
|
|
TopicUser.find_by(topic: bookmark.bookmarkable.topic, user: user).bookmarked,
|
|
|
|
).to eq(true)
|
2020-07-29 07:43:32 +08:00
|
|
|
end
|
|
|
|
end
|
2020-05-07 11:37:39 +08:00
|
|
|
end
|
|
|
|
|
2022-03-09 01:44:18 +08:00
|
|
|
context "when the auto_delete_preference is clear_reminder" do
|
|
|
|
before do
|
2022-05-23 08:07:15 +08:00
|
|
|
TopicUser.create!(topic: bookmark.bookmarkable.topic, user: user, bookmarked: true)
|
2022-03-09 01:44:18 +08:00
|
|
|
bookmark.update(auto_delete_preference: Bookmark.auto_delete_preferences[:clear_reminder])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "resets reminder_at after the reminder gets sent" do
|
2023-06-21 22:00:19 +08:00
|
|
|
send_notification
|
2022-03-09 01:44:18 +08:00
|
|
|
expect(Bookmark.find_by(id: bookmark.id).reminder_at).to eq(nil)
|
|
|
|
end
|
|
|
|
end
|
2020-03-13 08:44:39 +08:00
|
|
|
end
|
2020-03-12 08:16:00 +08:00
|
|
|
end
|