FIX: Include `assigned_messages_count` only when viewing assigned list.

This commit is contained in:
Guo Xiang Tan 2018-05-24 11:49:49 +08:00
parent e48ed99dfe
commit 4e53bf1962
2 changed files with 37 additions and 2 deletions

View File

@ -162,8 +162,10 @@ after_initialize do
add_to_serializer(:topic_list, 'include_assigned_messages_count?') do
options = object.instance_variable_get(:@opts)
scope.is_staff? ||
options.dig(:assigned)&.downcase == scope.current_user&.username_lower
if assigned_user = options.dig(:assigned)
scope.is_staff? ||
assigned_user.downcase == scope.current_user&.username_lower
end
end
add_to_serializer(:topic_view, :assigned_to_user, false) do

View File

@ -45,7 +45,40 @@ RSpec.describe TopicListSerializer do
.to eq(1)
end
describe 'when not viewing assigned list' do
let(:topic_list) do
TopicQuery.new(user).list_private_messages_assigned(user)
end
describe 'as an admin user' do
let(:guardian) { Guardian.new(Fabricate(:admin)) }
it 'should not include the attribute' do
expect(serializer.as_json[:topic_list][:assigned_messages_count])
.to eq(nil)
end
end
describe 'as an anon user' do
let(:guardian) { Guardian.new }
it 'should not include the attribute' do
expect(serializer.as_json[:topic_list][:assigned_messages_count])
.to eq(nil)
end
end
end
describe 'viewing another user' do
describe 'as an anon user' do
let(:guardian) { Guardian.new }
it 'should not include the attribute' do
expect(serializer.as_json[:topic_list][:assigned_messages_count])
.to eq(nil)
end
end
describe 'as a staff' do
let(:guardian) { Guardian.new(Fabricate(:admin)) }