mirror of
https://github.com/discourse/discourse.git
synced 2025-05-31 20:15:17 +08:00
FIX: Wrong scope used for notification levels user serializer (#13039)
This is a recent regression introduced by https://github.com/discourse/discourse/pull/12937 which makes it so that when looking at a user profile that is not your own, specifically the category and tag notification settings, you would see your own settings instead of the target user. This is only a problem for admins because regular users cannot see these details for other users. The issue was that we were using `scope` in the serializer, which refers to the current user, rather than using a scope for the target user via `Guardian.new(user)`. However, on further inspection the `notification_levels_for` method for `TagUser` and `CategoryUser` did not actually need to be accepting an instance of Guardian, all that it was using it for was to check guardian.anonymous? which is just a fancy way of saying user.blank?. Changed this method to just accept a user instead and send the user in from the serializer.
This commit is contained in:
@ -60,6 +60,7 @@ describe UserSerializer do
|
||||
end
|
||||
|
||||
context "with a user" do
|
||||
let(:admin_user) { Fabricate(:admin) }
|
||||
let(:scope) { Guardian.new }
|
||||
fab!(:user) { Fabricate(:user) }
|
||||
let(:serializer) { UserSerializer.new(user, scope: scope, root: false) }
|
||||
@ -67,6 +68,50 @@ describe UserSerializer do
|
||||
fab!(:upload) { Fabricate(:upload) }
|
||||
fab!(:upload2) { Fabricate(:upload) }
|
||||
|
||||
context "when the scope user is admin" do
|
||||
let(:scope) { Guardian.new(admin_user) }
|
||||
|
||||
it "returns the user's category notification levels, not the scope user's" do
|
||||
category1 = Fabricate(:category)
|
||||
category2 = Fabricate(:category)
|
||||
category3 = Fabricate(:category)
|
||||
category4 = Fabricate(:category)
|
||||
CategoryUser.create(category: category1, user: user, notification_level: CategoryUser.notification_levels[:muted])
|
||||
CategoryUser.create(category: Fabricate(:category), user: admin_user, notification_level: CategoryUser.notification_levels[:muted])
|
||||
CategoryUser.create(category: category2, user: user, notification_level: CategoryUser.notification_levels[:tracking])
|
||||
CategoryUser.create(category: Fabricate(:category), user: admin_user, notification_level: CategoryUser.notification_levels[:tracking])
|
||||
CategoryUser.create(category: category3, user: user, notification_level: CategoryUser.notification_levels[:watching])
|
||||
CategoryUser.create(category: Fabricate(:category), user: admin_user, notification_level: CategoryUser.notification_levels[:watching])
|
||||
CategoryUser.create(category: category4, user: user, notification_level: CategoryUser.notification_levels[:regular])
|
||||
CategoryUser.create(category: Fabricate(:category), user: admin_user, notification_level: CategoryUser.notification_levels[:regular])
|
||||
|
||||
expect(json[:muted_category_ids]).to eq([category1.id])
|
||||
expect(json[:tracked_category_ids]).to eq([category2.id])
|
||||
expect(json[:watched_category_ids]).to eq([category3.id])
|
||||
expect(json[:regular_category_ids]).to eq([category4.id])
|
||||
end
|
||||
|
||||
it "returns the user's tag notification levels, not the scope user's" do
|
||||
tag1 = Fabricate(:tag)
|
||||
tag2 = Fabricate(:tag)
|
||||
tag3 = Fabricate(:tag)
|
||||
tag4 = Fabricate(:tag)
|
||||
TagUser.create(tag: tag1, user: user, notification_level: TagUser.notification_levels[:muted])
|
||||
TagUser.create(tag: Fabricate(:tag), user: admin_user, notification_level: TagUser.notification_levels[:muted])
|
||||
TagUser.create(tag: tag2, user: user, notification_level: TagUser.notification_levels[:tracking])
|
||||
TagUser.create(tag: Fabricate(:tag), user: admin_user, notification_level: TagUser.notification_levels[:tracking])
|
||||
TagUser.create(tag: tag3, user: user, notification_level: TagUser.notification_levels[:watching])
|
||||
TagUser.create(tag: Fabricate(:tag), user: admin_user, notification_level: TagUser.notification_levels[:watching])
|
||||
TagUser.create(tag: tag4, user: user, notification_level: TagUser.notification_levels[:watching_first_post])
|
||||
TagUser.create(tag: Fabricate(:tag), user: admin_user, notification_level: TagUser.notification_levels[:watching_first_post])
|
||||
|
||||
expect(json[:muted_tags]).to eq([tag1.name])
|
||||
expect(json[:tracked_tags]).to eq([tag2.name])
|
||||
expect(json[:watched_tags]).to eq([tag3.name])
|
||||
expect(json[:watching_first_post_tags]).to eq([tag4.name])
|
||||
end
|
||||
end
|
||||
|
||||
context "with `enable_names` true" do
|
||||
before do
|
||||
SiteSetting.enable_names = true
|
||||
|
Reference in New Issue
Block a user