From 0398271f87e1bb60a5069b0c56037f13a18e27f4 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Wed, 9 Sep 2020 08:58:21 +0800 Subject: [PATCH] FIX: Admin should always be able to view group messages. Follow-up to b0f22f25231d73500a5b17d31349bc419a1058b0. Before this fix, `SiteSetting.enable_personal_messages = false` will prevent admin from viewing group messages. --- lib/guardian/group_guardian.rb | 6 ++-- spec/requests/list_controller_spec.rb | 46 +++++++++++++++++++++++---- 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/lib/guardian/group_guardian.rb b/lib/guardian/group_guardian.rb index dfeb4bae838..7246f6c5e17 100644 --- a/lib/guardian/group_guardian.rb +++ b/lib/guardian/group_guardian.rb @@ -34,8 +34,8 @@ module GroupGuardian end def can_see_group_messages?(group) - SiteSetting.enable_personal_messages? && ( - is_admin? || group.users.include?(user) - ) + return true if is_admin? + + SiteSetting.enable_personal_messages? && group.users.include?(user) end end diff --git a/spec/requests/list_controller_spec.rb b/spec/requests/list_controller_spec.rb index c0299f2b904..0de5c8863b9 100644 --- a/spec/requests/list_controller_spec.rb +++ b/spec/requests/list_controller_spec.rb @@ -173,15 +173,44 @@ RSpec.describe ListController do end describe '#private_messages_group' do - let(:user) do - user = Fabricate(:user) - group.add(user) - sign_in(user) - user + let(:user) { Fabricate(:user) } + + describe 'with personal_messages disabled' do + let!(:topic) { Fabricate(:private_message_topic, allowed_groups: [group]) } + + before do + group.add(user) + SiteSetting.enable_personal_messages = false + end + + it 'should display group private messages for an admin' do + sign_in(Fabricate(:admin)) + + get "/topics/private-messages-group/#{user.username}/#{group.name}.json" + + expect(response.status).to eq(200) + + expect(response.parsed_body["topic_list"]["topics"].first["id"]) + .to eq(topic.id) + end + + it "should not display group private messages for a moderator's group" do + moderator = Fabricate(:moderator) + sign_in(moderator) + group.add(moderator) + + get "/topics/private-messages-group/#{user.username}/#{group.name}.json" + + expect(response.status).to eq(404) + end end describe 'with unicode_usernames' do - before { SiteSetting.unicode_usernames = false } + before do + group.add(user) + sign_in(user) + SiteSetting.unicode_usernames = false + end it 'should return the right response when user does not belong to group' do Fabricate(:private_message_topic, allowed_groups: [group]) @@ -205,7 +234,10 @@ RSpec.describe ListController do end describe 'with unicode_usernames' do - before { SiteSetting.unicode_usernames = true } + before do + sign_in(user) + SiteSetting.unicode_usernames = true + end it 'Returns a 200 with unicode group name' do unicode_group = Fabricate(:group, name: '群群组')