0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-12 05:37:26 +08:00
discourse/plugins/discourse-assign/spec/serializers/user_bookmark_base_serializer_spec.rb
Alan Guo Xiang Tan 7ce1992ec1
FIX: Serialize only a bookmark target's active assignment (#41397)
This PR makes a bookmark's "Assigned to …" badge reflect only the
current, active assignment of the exact item that was bookmarked.

In discourse-assign, a topic or an individual post can be assigned to a
user or group, and bookmark lists show a badge for it. Previously,
`BookmarkQuery.on_preload` loaded every assignment belonging to a
bookmarked topic — without filtering on `active`, and keyed by
`topic_id` regardless of `target_type`. Bookmarks kept showing "Assigned
to …" after an assignment was deactivated (for example by
`unassign_on_close`), and when a topic carried both a direct assignment
and post-level assignments, whichever row loaded last won, so a bookmark
could show an assignee that was never assigned the bookmarked item at
all. Post bookmarks had a second problem: the
`UserBookmarkBaseSerializer` extension read the topic's assignment for
them, so a bookmark on an assigned post never showed that post's own
assignee.
2026-07-06 06:34:22 +08:00

59 lines
2.1 KiB
Ruby
Vendored

# frozen_string_literal: true
require_relative "../support/assign_allowed_group"
describe UserBookmarkBaseSerializer do
include_context "with group that is allowed to assign"
before do
SiteSetting.assign_enabled = true
add_to_assign_allowed_group(user)
end
fab!(:user)
fab!(:topic)
fab!(:post) { Fabricate(:post, topic: topic) }
let(:guardian) { Guardian.new(user) }
context "for Topic bookmarkable" do
let!(:bookmark) { Fabricate(:bookmark, user: user, bookmarkable: post.topic) }
it "includes assigned user in serializer" do
Assigner.new(topic, user).assign(user)
serializer = UserTopicBookmarkSerializer.new(bookmark, scope: guardian)
bookmark = serializer.as_json[:user_topic_bookmark]
expect(bookmark[:assigned_to_user][:id]).to eq(user.id)
expect(bookmark[:assigned_to_group]).to be(nil)
end
it "includes assigned group in serializer" do
Assigner.new(topic, user).assign(assign_allowed_group)
serializer = UserTopicBookmarkSerializer.new(bookmark, scope: guardian)
bookmark = serializer.as_json[:user_topic_bookmark]
expect(bookmark[:assigned_to_group][:id]).to eq(assign_allowed_group.id)
expect(bookmark[:assigned_to_user]).to be(nil)
end
end
context "for Post bookmarkable" do
let!(:bookmark) { Fabricate(:bookmark, user: user, bookmarkable: post) }
it "includes assigned user in serializer" do
Assigner.new(post, user).assign(user)
serializer = UserPostBookmarkSerializer.new(bookmark, scope: guardian)
bookmark = serializer.as_json[:user_post_bookmark]
expect(bookmark[:assigned_to_user][:id]).to eq(user.id)
expect(bookmark[:assigned_to_group]).to be(nil)
end
it "includes assigned group in serializer" do
Assigner.new(post, user).assign(assign_allowed_group)
serializer = UserPostBookmarkSerializer.new(bookmark, scope: guardian)
bookmark = serializer.as_json[:user_post_bookmark]
expect(bookmark[:assigned_to_group][:id]).to eq(assign_allowed_group.id)
expect(bookmark[:assigned_to_user]).to be(nil)
end
end
end