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