diff --git a/plugins/chat/lib/chat/guardian_extensions.rb b/plugins/chat/lib/chat/guardian_extensions.rb index 75308556b4b..d31ed8d6a1b 100644 --- a/plugins/chat/lib/chat/guardian_extensions.rb +++ b/plugins/chat/lib/chat/guardian_extensions.rb @@ -13,7 +13,7 @@ module Chat def can_chat? return false if anonymous? - @user.staff? || @user.in_any_groups?(Chat.allowed_group_ids) + is_staff? || @user.bot? || @user.in_any_groups?(Chat.allowed_group_ids) end def can_direct_message? @@ -140,7 +140,7 @@ module Chat def can_flag_chat_messages? return false if @user.silenced? - return true if @user.staff? + return true if is_staff? @user.in_any_groups?(SiteSetting.chat_message_flag_allowed_groups_map) end diff --git a/plugins/chat/spec/lib/chat/guardian_extensions_spec.rb b/plugins/chat/spec/lib/chat/guardian_extensions_spec.rb index b392fc15dfc..8af2bd9ecd0 100644 --- a/plugins/chat/spec/lib/chat/guardian_extensions_spec.rb +++ b/plugins/chat/spec/lib/chat/guardian_extensions_spec.rb @@ -12,28 +12,52 @@ RSpec.describe Chat::GuardianExtensions do before { SiteSetting.chat_allowed_groups = [chatters] } - it "cannot chat if the user is not in the Chat.allowed_group_ids" do - SiteSetting.chat_allowed_groups = "" - expect(guardian.can_chat?).to eq(false) - end + describe "#can_chat?" do + context "when the user is not in allowed to chat" do + before { SiteSetting.chat_allowed_groups = "" } - it "staff can always chat regardless of chat_allowed_grups" do - SiteSetting.chat_allowed_groups = "" - expect(staff_guardian.can_chat?).to eq(true) - end + it "cannot chat" do + expect(guardian.can_chat?).to eq(false) + end - it "allows TL1 to chat by default and by extension higher trust levels" do - expect(guardian.can_chat?).to eq(true) - user.change_trust_level!(TrustLevel[3]) - expect(guardian.can_chat?).to eq(true) - end + context "when the user is a bot" do + let(:guardian) { Discourse.system_user.guardian } - it "allows user in specific group to chat" do - SiteSetting.chat_allowed_groups = chat_group.id - expect(guardian.can_chat?).to eq(false) - chat_group.add(user) - user.reload - expect(guardian.can_chat?).to eq(true) + it "can chat" do + expect(guardian.can_chat?).to eq(true) + end + end + + context "when user is staff" do + let(:guardian) { staff_guardian } + + it "can chat" do + expect(guardian.can_chat?).to eq(true) + end + end + end + + context "when user is anonymous" do + let(:guardian) { Guardian.new } + + it "cannot chat" do + expect(guardian.can_chat?).to eq(false) + end + end + + it "allows TL1 to chat by default and by extension higher trust levels" do + expect(guardian.can_chat?).to eq(true) + user.change_trust_level!(TrustLevel[3]) + expect(guardian.can_chat?).to eq(true) + end + + it "allows user in specific group to chat" do + SiteSetting.chat_allowed_groups = chat_group.id + expect(guardian.can_chat?).to eq(false) + chat_group.add(user) + user.reload + expect(guardian.can_chat?).to eq(true) + end end describe "chat channel" do diff --git a/plugins/chat/spec/serializer/chat/direct_message_serializer_spec.rb b/plugins/chat/spec/serializer/chat/direct_message_serializer_spec.rb index 3908ebb7878..01f218de91b 100644 --- a/plugins/chat/spec/serializer/chat/direct_message_serializer_spec.rb +++ b/plugins/chat/spec/serializer/chat/direct_message_serializer_spec.rb @@ -3,9 +3,9 @@ describe Chat::DirectMessageSerializer do describe "#user" do it "returns you when there are two of us" do - me = Fabricate.build(:user) - you = Fabricate.build(:user) - direct_message = Fabricate.build(:direct_message, users: [me, you]) + me = Fabricate(:user) + you = Fabricate(:user) + direct_message = Fabricate(:direct_message, users: [me, you]) serializer = described_class.new(direct_message, scope: Guardian.new(me), root: false) json = serializer.as_json @@ -14,10 +14,10 @@ describe Chat::DirectMessageSerializer do end it "returns you both if there are three of us" do - me = Fabricate.build(:user) - you = Fabricate.build(:user) - other_you = Fabricate.build(:user) - direct_message = Fabricate.build(:direct_message, users: [me, you, other_you]) + me = Fabricate(:user) + you = Fabricate(:user) + other_you = Fabricate(:user) + direct_message = Fabricate(:direct_message, users: [me, you, other_you]) serializer = described_class.new(direct_message, scope: Guardian.new(me), root: false) json = serializer.as_json @@ -28,8 +28,8 @@ describe Chat::DirectMessageSerializer do end it "returns me if there is only me" do - me = Fabricate.build(:user) - direct_message = Fabricate.build(:direct_message, users: [me]) + me = Fabricate(:user) + direct_message = Fabricate(:direct_message, users: [me]) serializer = described_class.new(direct_message, scope: Guardian.new(me), root: false) json = serializer.as_json