2020-03-19 13:48:23 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
RSpec.describe BookmarkQuery do
|
2020-07-17 13:55:07 +08:00
|
|
|
before do
|
|
|
|
SearchIndexer.enable
|
|
|
|
end
|
|
|
|
|
2020-03-19 13:48:23 +08:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
let(:params) { {} }
|
|
|
|
|
2020-03-23 19:04:39 +08:00
|
|
|
def bookmark_query(user: nil, params: nil)
|
|
|
|
BookmarkQuery.new(user: user || self.user, params: params || self.params)
|
|
|
|
end
|
|
|
|
|
2020-03-19 13:48:23 +08:00
|
|
|
describe "#list_all" do
|
2022-05-23 08:07:15 +08:00
|
|
|
before do
|
|
|
|
Bookmark.reset_bookmarkables
|
|
|
|
register_test_bookmarkable
|
2020-05-01 14:14:20 +08:00
|
|
|
|
2022-05-23 08:07:15 +08:00
|
|
|
Fabricate(:topic_user, user: user, topic: post_bookmark.bookmarkable.topic)
|
|
|
|
Fabricate(:topic_user, user: user, topic: topic_bookmark.bookmarkable)
|
|
|
|
user_bookmark
|
2020-03-23 19:04:39 +08:00
|
|
|
end
|
|
|
|
|
2022-05-23 08:07:15 +08:00
|
|
|
let(:post_bookmark) { Fabricate(:bookmark, user: user, bookmarkable: Fabricate(:post)) }
|
|
|
|
let(:topic_bookmark) { Fabricate(:bookmark, user: user, bookmarkable: Fabricate(:topic)) }
|
|
|
|
let(:user_bookmark) { Fabricate(:bookmark, user: user, bookmarkable: Fabricate(:user, username: "bookmarkqueen")) }
|
|
|
|
|
|
|
|
after do
|
|
|
|
Bookmark.reset_bookmarkables
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns all the bookmarks for a user" do
|
|
|
|
expect(bookmark_query.list_all.count).to eq(3)
|
2020-03-23 19:04:39 +08:00
|
|
|
end
|
|
|
|
|
2022-05-23 08:07:15 +08:00
|
|
|
it "does not return deleted bookmarkables" do
|
|
|
|
post_bookmark.bookmarkable.trash!
|
|
|
|
topic_bookmark.bookmarkable.trash!
|
2020-03-23 19:04:39 +08:00
|
|
|
expect(bookmark_query.list_all.count).to eq(1)
|
2020-03-19 13:48:23 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "runs the on_preload block provided passing in bookmarks" do
|
|
|
|
preloaded_bookmarks = []
|
|
|
|
BookmarkQuery.on_preload do |bookmarks, bq|
|
|
|
|
(preloaded_bookmarks << bookmarks).flatten
|
|
|
|
end
|
2020-03-23 19:04:39 +08:00
|
|
|
bookmark_query.list_all
|
2020-03-19 13:48:23 +08:00
|
|
|
expect(preloaded_bookmarks.any?).to eq(true)
|
|
|
|
end
|
|
|
|
|
2022-05-23 08:07:15 +08:00
|
|
|
it "returns a mixture of post, topic, and custom bookmarkable type bookmarks" do
|
|
|
|
bookmarks = bookmark_query.list_all
|
|
|
|
expect(bookmarks.map(&:id)).to match_array([post_bookmark.id, topic_bookmark.id, user_bookmark.id])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "handles the user not having permission for all of the bookmarks of a certain bookmarkable" do
|
|
|
|
UserTestBookmarkable.expects(:list_query).returns(nil)
|
|
|
|
bookmarks = bookmark_query.list_all
|
|
|
|
expect(bookmarks.map(&:id)).to match_array([post_bookmark.id, topic_bookmark.id])
|
|
|
|
end
|
2021-09-21 11:49:56 +08:00
|
|
|
|
2022-05-23 08:07:15 +08:00
|
|
|
it "handles the user not having permission to see any of their bookmarks" do
|
|
|
|
topic_bookmark.bookmarkable.update(category: Fabricate(:private_category, group: Fabricate(:group)))
|
|
|
|
post_bookmark.bookmarkable.topic.update(category: topic_bookmark.bookmarkable.category)
|
|
|
|
UserTestBookmarkable.expects(:list_query).returns(nil)
|
|
|
|
bookmarks = bookmark_query.list_all
|
|
|
|
expect(bookmarks.map(&:id)).to eq([])
|
2021-09-21 11:49:56 +08:00
|
|
|
end
|
|
|
|
|
2022-05-23 08:07:15 +08:00
|
|
|
context "when q param is provided" do
|
|
|
|
let!(:post) { Fabricate(:post, raw: "Some post content here", topic: Fabricate(:topic, title: "Bugfix game for devs")) }
|
|
|
|
|
2020-07-17 13:55:07 +08:00
|
|
|
before do
|
2022-04-22 06:23:42 +08:00
|
|
|
Bookmark.reset_bookmarkables
|
2020-07-14 12:43:41 +08:00
|
|
|
end
|
|
|
|
|
2022-04-22 06:23:42 +08:00
|
|
|
after do
|
|
|
|
Bookmark.reset_bookmarkables
|
2020-07-17 13:55:07 +08:00
|
|
|
end
|
|
|
|
|
2022-05-23 08:07:15 +08:00
|
|
|
let(:bookmark3) { Fabricate(:bookmark, user: user, name: "Check up later", bookmarkable: Fabricate(:post)) }
|
|
|
|
let(:bookmark4) { Fabricate(:bookmark, user: user, bookmarkable: post) }
|
2022-05-12 08:29:01 +08:00
|
|
|
|
2022-05-23 08:07:15 +08:00
|
|
|
before do
|
|
|
|
Fabricate(:topic_user, user: user, topic: bookmark3.bookmarkable.topic)
|
|
|
|
Fabricate(:topic_user, user: user, topic: bookmark4.bookmarkable.topic)
|
2022-05-12 08:29:01 +08:00
|
|
|
end
|
|
|
|
|
2022-05-23 08:07:15 +08:00
|
|
|
it "can search by bookmark name" do
|
|
|
|
bookmarks = bookmark_query(params: { q: 'check' }).list_all
|
|
|
|
expect(bookmarks.map(&:id)).to eq([bookmark3.id])
|
2022-05-12 08:29:01 +08:00
|
|
|
end
|
2022-04-22 06:23:42 +08:00
|
|
|
|
2022-05-23 08:07:15 +08:00
|
|
|
it "can search by post content" do
|
|
|
|
bookmarks = bookmark_query(params: { q: 'content' }).list_all
|
|
|
|
expect(bookmarks.map(&:id)).to eq([bookmark4.id])
|
2022-04-22 06:23:42 +08:00
|
|
|
end
|
|
|
|
|
2022-05-23 08:07:15 +08:00
|
|
|
it "can search by topic title" do
|
|
|
|
bookmarks = bookmark_query(params: { q: 'bugfix' }).list_all
|
|
|
|
expect(bookmarks.map(&:id)).to eq([bookmark4.id])
|
|
|
|
end
|
2020-07-17 13:55:07 +08:00
|
|
|
|
2022-05-23 08:07:15 +08:00
|
|
|
context "with custom bookmarkable fitering" do
|
2022-04-22 06:23:42 +08:00
|
|
|
before do
|
2022-05-23 08:07:15 +08:00
|
|
|
register_test_bookmarkable
|
2022-04-22 06:23:42 +08:00
|
|
|
end
|
|
|
|
|
2022-05-23 08:07:15 +08:00
|
|
|
let!(:bookmark5) { Fabricate(:bookmark, user: user, bookmarkable: Fabricate(:user, username: "bookmarkking")) }
|
2022-04-22 06:23:42 +08:00
|
|
|
|
2022-05-23 08:07:15 +08:00
|
|
|
it "allows searching bookmarkables by fields in other tables" do
|
|
|
|
bookmarks = bookmark_query(params: { q: 'bookmarkk' }).list_all
|
|
|
|
expect(bookmarks.map(&:id)).to eq([bookmark5.id])
|
2022-04-22 06:23:42 +08:00
|
|
|
end
|
2020-07-14 12:43:41 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-23 19:04:39 +08:00
|
|
|
context "for a whispered post" do
|
|
|
|
before do
|
2022-05-23 08:07:15 +08:00
|
|
|
post_bookmark.bookmarkable.update(post_type: Post.types[:whisper])
|
2022-12-17 00:42:51 +08:00
|
|
|
SiteSetting.whispers_allowed_groups = "#{Group::AUTO_GROUPS[:staff]}"
|
2020-03-23 19:04:39 +08:00
|
|
|
end
|
2022-06-30 08:18:12 +08:00
|
|
|
fab!(:whisperers_group) { Fabricate(:group) }
|
|
|
|
|
2020-03-23 19:04:39 +08:00
|
|
|
context "when the user is moderator" do
|
|
|
|
it "does return the whispered post" do
|
2022-12-17 00:42:51 +08:00
|
|
|
user.grant_moderation!
|
2022-05-23 08:07:15 +08:00
|
|
|
expect(bookmark_query.list_all.count).to eq(3)
|
2020-03-23 19:04:39 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
context "when the user is admin" do
|
|
|
|
it "does return the whispered post" do
|
2022-12-17 00:42:51 +08:00
|
|
|
user.grant_admin!
|
2022-05-23 08:07:15 +08:00
|
|
|
expect(bookmark_query.list_all.count).to eq(3)
|
2020-03-23 19:04:39 +08:00
|
|
|
end
|
|
|
|
end
|
2022-06-30 08:18:12 +08:00
|
|
|
context "when the user is a member of whisperers group" do
|
|
|
|
it "returns the whispered post" do
|
|
|
|
SiteSetting.whispers_allowed_groups = "#{whisperers_group.id}"
|
|
|
|
user.update!(groups: [whisperers_group])
|
|
|
|
expect(bookmark_query.list_all.count).to eq(3)
|
|
|
|
end
|
|
|
|
end
|
2020-03-23 19:04:39 +08:00
|
|
|
context "when the user is not staff" do
|
|
|
|
it "does not return the whispered post" do
|
2022-05-23 08:07:15 +08:00
|
|
|
expect(bookmark_query.list_all.count).to eq(2)
|
2020-03-23 19:04:39 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "for a private message topic bookmark" do
|
|
|
|
let(:pm_topic) { Fabricate(:private_message_topic) }
|
|
|
|
before do
|
2022-05-23 08:07:15 +08:00
|
|
|
post_bookmark.update(bookmarkable: Fabricate(:post, topic: pm_topic))
|
2020-03-23 19:04:39 +08:00
|
|
|
TopicUser.change(user.id, pm_topic.id, total_msecs_viewed: 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the user is a topic_allowed_user" do
|
|
|
|
before do
|
|
|
|
TopicAllowedUser.create(topic: pm_topic, user: user)
|
|
|
|
end
|
|
|
|
it "shows the user the bookmark in the PM" do
|
2022-05-23 08:07:15 +08:00
|
|
|
expect(bookmark_query.list_all.map(&:id).count).to eq(3)
|
2020-03-23 19:04:39 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the user is in a topic_allowed_group" do
|
|
|
|
before do
|
|
|
|
group = Fabricate(:group)
|
|
|
|
GroupUser.create(group: group, user: user)
|
|
|
|
TopicAllowedGroup.create(topic: pm_topic, group: group)
|
|
|
|
end
|
|
|
|
it "shows the user the bookmark in the PM" do
|
2022-05-23 08:07:15 +08:00
|
|
|
expect(bookmark_query.list_all.map(&:id).count).to eq(3)
|
2020-03-23 19:04:39 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the user is not a topic_allowed_user" do
|
|
|
|
it "does not show the user a bookmarked post in a PM where they are not an allowed user" do
|
2022-05-23 08:07:15 +08:00
|
|
|
expect(bookmark_query.list_all.map(&:id).count).to eq(2)
|
2020-03-23 19:04:39 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the user is not in a topic_allowed_group" do
|
|
|
|
it "does not show the user a bookmarked post in a PM where they are not in an allowed group" do
|
2022-05-23 08:07:15 +08:00
|
|
|
expect(bookmark_query.list_all.map(&:id).count).to eq(2)
|
2020-03-23 19:04:39 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the topic category is private" do
|
|
|
|
let(:group) { Fabricate(:group) }
|
|
|
|
before do
|
2022-05-23 08:07:15 +08:00
|
|
|
post_bookmark.bookmarkable.topic.update(category: Fabricate(:private_category, group: group))
|
|
|
|
post_bookmark.reload
|
2020-03-23 19:04:39 +08:00
|
|
|
end
|
|
|
|
it "does not show the user a post/topic in a private category they cannot see" do
|
2022-05-23 08:07:15 +08:00
|
|
|
expect(bookmark_query.list_all.map(&:id)).not_to include(post_bookmark.id)
|
2020-03-23 19:04:39 +08:00
|
|
|
end
|
|
|
|
it "does show the user a post/topic in a private category they can see" do
|
|
|
|
GroupUser.create(user: user, group: group)
|
2022-05-23 08:07:15 +08:00
|
|
|
expect(bookmark_query.list_all.map(&:id)).to include(post_bookmark.id)
|
2020-03-23 19:04:39 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-19 13:48:23 +08:00
|
|
|
context "when the limit param is provided" do
|
|
|
|
let(:params) { { limit: 1 } }
|
|
|
|
it "is respected" do
|
2020-03-23 19:04:39 +08:00
|
|
|
expect(bookmark_query.list_all.count).to eq(1)
|
2020-03-19 13:48:23 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-05-01 14:14:20 +08:00
|
|
|
|
|
|
|
describe "#list_all ordering" do
|
2021-09-16 07:56:54 +08:00
|
|
|
let!(:bookmark1) { Fabricate(:bookmark, user: user, updated_at: 1.day.ago, reminder_at: nil) }
|
|
|
|
let!(:bookmark2) { Fabricate(:bookmark, user: user, updated_at: 2.days.ago, reminder_at: nil) }
|
|
|
|
let!(:bookmark3) { Fabricate(:bookmark, user: user, updated_at: 6.days.ago, reminder_at: nil) }
|
|
|
|
let!(:bookmark4) { Fabricate(:bookmark, user: user, updated_at: 4.days.ago, reminder_at: nil) }
|
|
|
|
let!(:bookmark5) { Fabricate(:bookmark, user: user, updated_at: 3.days.ago, reminder_at: nil) }
|
2021-03-22 07:50:22 +08:00
|
|
|
|
2021-09-21 11:49:56 +08:00
|
|
|
before do
|
|
|
|
[bookmark1, bookmark2, bookmark3, bookmark4, bookmark5].each do |bm|
|
2022-05-23 08:07:15 +08:00
|
|
|
Fabricate(:topic_user, topic: bm.bookmarkable.topic, user: user)
|
2021-09-21 11:49:56 +08:00
|
|
|
bm.reload
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-26 03:23:46 +08:00
|
|
|
it "order defaults to updated_at DESC" do
|
2020-05-01 14:14:20 +08:00
|
|
|
expect(bookmark_query.list_all.map(&:id)).to eq([
|
|
|
|
bookmark1.id,
|
|
|
|
bookmark2.id,
|
|
|
|
bookmark5.id,
|
|
|
|
bookmark4.id,
|
|
|
|
bookmark3.id
|
|
|
|
])
|
|
|
|
end
|
2021-03-22 07:50:22 +08:00
|
|
|
|
2021-05-26 03:23:46 +08:00
|
|
|
it "orders by reminder_at, then updated_at" do
|
|
|
|
bookmark4.update_column(:reminder_at, 1.day.from_now)
|
|
|
|
bookmark5.update_column(:reminder_at, 26.hours.from_now)
|
|
|
|
|
|
|
|
expect(bookmark_query.list_all.map(&:id)).to eq([
|
|
|
|
bookmark4.id,
|
|
|
|
bookmark5.id,
|
|
|
|
bookmark1.id,
|
|
|
|
bookmark2.id,
|
|
|
|
bookmark3.id
|
|
|
|
])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "shows pinned bookmarks first ordered by reminder_at ASC then updated_at DESC" do
|
2021-03-22 07:50:22 +08:00
|
|
|
bookmark3.update_column(:pinned, true)
|
2021-05-26 03:23:46 +08:00
|
|
|
bookmark3.update_column(:reminder_at, 1.day.from_now)
|
|
|
|
|
2021-03-22 07:50:22 +08:00
|
|
|
bookmark4.update_column(:pinned, true)
|
2021-05-26 03:23:46 +08:00
|
|
|
bookmark4.update_column(:reminder_at, 28.hours.from_now)
|
|
|
|
|
|
|
|
bookmark1.update_column(:pinned, true)
|
|
|
|
bookmark2.update_column(:pinned, true)
|
|
|
|
|
|
|
|
bookmark5.update_column(:reminder_at, 1.day.from_now)
|
|
|
|
|
2021-03-22 07:50:22 +08:00
|
|
|
expect(bookmark_query.list_all.map(&:id)).to eq([
|
|
|
|
bookmark3.id,
|
2021-05-26 03:23:46 +08:00
|
|
|
bookmark4.id,
|
2021-03-22 07:50:22 +08:00
|
|
|
bookmark1.id,
|
|
|
|
bookmark2.id,
|
|
|
|
bookmark5.id
|
|
|
|
])
|
|
|
|
end
|
2020-05-01 14:14:20 +08:00
|
|
|
end
|
2020-03-19 13:48:23 +08:00
|
|
|
end
|