2022-05-08 19:37:23 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "rails_helper"
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe PostBookmarkable do
|
2023-06-21 10:00:19 -04:00
|
|
|
subject(:registered_bookmarkable) { RegisteredBookmarkable.new(PostBookmarkable) }
|
|
|
|
|
2022-05-08 19:37:23 -04:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
fab!(:private_category) { Fabricate(:private_category, group: Fabricate(:group)) }
|
|
|
|
|
2023-06-21 10:00:19 -04:00
|
|
|
let(:guardian) { Guardian.new(user) }
|
|
|
|
|
2022-05-08 19:37:23 -04:00
|
|
|
let!(:post1) { Fabricate(:post) }
|
|
|
|
let!(:post2) { Fabricate(:post) }
|
|
|
|
let!(:bookmark1) do
|
|
|
|
Fabricate(:bookmark, user: user, bookmarkable: post1, name: "something i gotta do")
|
2023-01-09 06:18:21 -05:00
|
|
|
end
|
2022-05-08 19:37:23 -04:00
|
|
|
let!(:bookmark2) { Fabricate(:bookmark, user: user, bookmarkable: post2) }
|
|
|
|
let!(:bookmark3) { Fabricate(:bookmark) }
|
|
|
|
let!(:topic_user1) { Fabricate(:topic_user, user: user, topic: post1.topic) }
|
|
|
|
let!(:topic_user2) { Fabricate(:topic_user, user: user, topic: post2.topic) }
|
|
|
|
|
|
|
|
describe "#perform_list_query" do
|
|
|
|
it "returns all the user's bookmarks" do
|
2023-06-21 10:00:19 -04:00
|
|
|
expect(registered_bookmarkable.perform_list_query(user, guardian).map(&:id)).to match_array(
|
2022-05-08 19:37:23 -04:00
|
|
|
[bookmark1.id, bookmark2.id],
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not return bookmarks for posts where the user does not have access to the topic category" do
|
|
|
|
bookmark1.bookmarkable.topic.update(category: private_category)
|
2023-06-21 10:00:19 -04:00
|
|
|
expect(registered_bookmarkable.perform_list_query(user, guardian).map(&:id)).to match_array(
|
|
|
|
[bookmark2.id],
|
|
|
|
)
|
2022-05-08 19:37:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not return bookmarks for posts where the user does not have access to the private message" do
|
|
|
|
bookmark1.bookmarkable.update(topic: Fabricate(:private_message_topic))
|
2023-06-21 10:00:19 -04:00
|
|
|
expect(registered_bookmarkable.perform_list_query(user, guardian).map(&:id)).to match_array(
|
|
|
|
[bookmark2.id],
|
|
|
|
)
|
2022-05-08 19:37:23 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#perform_search_query" do
|
|
|
|
before { SearchIndexer.enable }
|
|
|
|
|
|
|
|
it "returns bookmarks that match by name" do
|
|
|
|
ts_query = Search.ts_query(term: "gotta", ts_config: "simple")
|
|
|
|
expect(
|
2023-06-21 10:00:19 -04:00
|
|
|
registered_bookmarkable.perform_search_query(
|
|
|
|
registered_bookmarkable.perform_list_query(user, guardian),
|
2022-05-08 19:37:23 -04:00
|
|
|
"%gotta%",
|
|
|
|
ts_query,
|
|
|
|
).map(&:id),
|
|
|
|
).to match_array([bookmark1.id])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns bookmarks that match by post search data (topic title or post content)" do
|
|
|
|
post2.update(raw: "some post content")
|
|
|
|
post2.topic.update(title: "a great topic title")
|
|
|
|
|
|
|
|
ts_query = Search.ts_query(term: "post content", ts_config: "simple")
|
|
|
|
expect(
|
2023-06-21 10:00:19 -04:00
|
|
|
registered_bookmarkable.perform_search_query(
|
|
|
|
registered_bookmarkable.perform_list_query(user, guardian),
|
2022-05-08 19:37:23 -04:00
|
|
|
"%post content%",
|
|
|
|
ts_query,
|
|
|
|
).map(&:id),
|
|
|
|
).to match_array([bookmark2.id])
|
|
|
|
|
|
|
|
ts_query = Search.ts_query(term: "great topic", ts_config: "simple")
|
|
|
|
expect(
|
2023-06-21 10:00:19 -04:00
|
|
|
registered_bookmarkable.perform_search_query(
|
|
|
|
registered_bookmarkable.perform_list_query(user, guardian),
|
2022-05-08 19:37:23 -04:00
|
|
|
"%great topic%",
|
|
|
|
ts_query,
|
|
|
|
).map(&:id),
|
|
|
|
).to match_array([bookmark2.id])
|
|
|
|
|
|
|
|
ts_query = Search.ts_query(term: "blah", ts_config: "simple")
|
|
|
|
expect(
|
2023-06-21 10:00:19 -04:00
|
|
|
registered_bookmarkable.perform_search_query(
|
|
|
|
registered_bookmarkable.perform_list_query(user, guardian),
|
2022-05-08 19:37:23 -04:00
|
|
|
"%blah%",
|
|
|
|
ts_query,
|
|
|
|
).map(&:id),
|
|
|
|
).to eq([])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#can_send_reminder?" do
|
|
|
|
it "cannot send reminder if the post or topic is deleted" do
|
2023-06-21 10:00:19 -04:00
|
|
|
expect(registered_bookmarkable.can_send_reminder?(bookmark1)).to eq(true)
|
2022-05-08 19:37:23 -04:00
|
|
|
bookmark1.bookmarkable.trash!
|
|
|
|
bookmark1.reload
|
2023-06-21 10:00:19 -04:00
|
|
|
expect(registered_bookmarkable.can_send_reminder?(bookmark1)).to eq(false)
|
2022-05-08 19:37:23 -04:00
|
|
|
Post.with_deleted.find_by(id: bookmark1.bookmarkable_id).recover!
|
|
|
|
bookmark1.reload
|
|
|
|
bookmark1.bookmarkable.topic.trash!
|
|
|
|
bookmark1.reload
|
2023-06-21 10:00:19 -04:00
|
|
|
expect(registered_bookmarkable.can_send_reminder?(bookmark1)).to eq(false)
|
2022-05-08 19:37:23 -04:00
|
|
|
end
|
2023-10-12 21:41:10 -04:00
|
|
|
|
|
|
|
it "cannot send reminder if the user cannot access the topic" do
|
|
|
|
expect(registered_bookmarkable.can_send_reminder?(bookmark1)).to eq(true)
|
|
|
|
bookmark1.bookmarkable.topic.update!(category: private_category)
|
|
|
|
bookmark1.reload
|
|
|
|
expect(registered_bookmarkable.can_send_reminder?(bookmark1)).to eq(false)
|
|
|
|
end
|
2022-05-08 19:37:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "#reminder_handler" do
|
|
|
|
it "creates a notification for the user with the correct details" do
|
2023-06-21 10:00:19 -04:00
|
|
|
expect { registered_bookmarkable.send_reminder_notification(bookmark1) }.to change {
|
|
|
|
Notification.count
|
|
|
|
}.by(1)
|
2022-05-08 19:37:23 -04:00
|
|
|
notif = user.notifications.last
|
|
|
|
expect(notif.notification_type).to eq(Notification.types[:bookmark_reminder])
|
|
|
|
expect(notif.topic_id).to eq(bookmark1.bookmarkable.topic_id)
|
|
|
|
expect(notif.post_number).to eq(bookmark1.bookmarkable.post_number)
|
|
|
|
expect(notif.data).to eq(
|
|
|
|
{
|
|
|
|
title: bookmark1.bookmarkable.topic.title,
|
2022-07-17 22:51:57 -04:00
|
|
|
bookmarkable_url: bookmark1.bookmarkable.url,
|
2022-05-08 19:37:23 -04:00
|
|
|
display_username: bookmark1.user.username,
|
|
|
|
bookmark_name: bookmark1.name,
|
2022-07-17 22:51:57 -04:00
|
|
|
bookmark_id: bookmark1.id,
|
2022-05-08 19:37:23 -04:00
|
|
|
}.to_json,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#can_see?" do
|
|
|
|
it "returns false if the post is in a private category or private message the user cannot see" do
|
2023-06-21 10:00:19 -04:00
|
|
|
expect(registered_bookmarkable.can_see?(guardian, bookmark1)).to eq(true)
|
2022-05-08 19:37:23 -04:00
|
|
|
bookmark1.bookmarkable.topic.update(category: private_category)
|
2023-06-21 10:00:19 -04:00
|
|
|
expect(registered_bookmarkable.can_see?(guardian, bookmark1)).to eq(false)
|
2022-05-08 19:37:23 -04:00
|
|
|
bookmark1.bookmarkable.update(topic: Fabricate(:private_message_topic))
|
2023-06-21 10:00:19 -04:00
|
|
|
expect(registered_bookmarkable.can_see?(guardian, bookmark1)).to eq(false)
|
2022-05-08 19:37:23 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|