From e5c705af8a62907342f64f90febce18082e99955 Mon Sep 17 00:00:00 2001 From: Andrei Prigorshnev Date: Wed, 14 Jun 2023 19:34:38 +0400 Subject: [PATCH] DEV: group tests related to mentions (#22017) --- .../components/chat/message_creator_spec.rb | 224 +++++++++--------- .../components/chat/message_updater_spec.rb | 72 +++--- 2 files changed, 149 insertions(+), 147 deletions(-) diff --git a/plugins/chat/spec/components/chat/message_creator_spec.rb b/plugins/chat/spec/components/chat/message_creator_spec.rb index 91055313cee..15ebed5e4df 100644 --- a/plugins/chat/spec/components/chat/message_creator_spec.rb +++ b/plugins/chat/spec/components/chat/message_creator_spec.rb @@ -288,6 +288,114 @@ describe Chat::MessageCreator do end end + context "with group mentions" do + it "creates chat mentions for group mentions where the group is mentionable" do + expect { + described_class.create( + chat_channel: public_chat_channel, + user: user1, + content: "hello @#{admin_group.name}", + ) + }.to change { admin1.chat_mentions.count }.by(1).and change { + admin2.chat_mentions.count + }.by(1) + end + + it "doesn't mention users twice if they are direct mentioned and group mentioned" do + expect { + described_class.create( + chat_channel: public_chat_channel, + user: user1, + content: "hello @#{admin_group.name} @#{admin1.username} and @#{admin2.username}", + ) + }.to change { admin1.chat_mentions.count }.by(1).and change { + admin2.chat_mentions.count + }.by(1) + end + + it "creates chat mentions for group mentions and direct mentions" do + expect { + described_class.create( + chat_channel: public_chat_channel, + user: user1, + content: "hello @#{admin_group.name} @#{user2.username}", + ) + }.to change { admin1.chat_mentions.count }.by(1).and change { + admin2.chat_mentions.count + }.by(1).and change { user2.chat_mentions.count }.by(1) + end + + it "creates chat mentions for group mentions and direct mentions" do + expect { + described_class.create( + chat_channel: public_chat_channel, + user: user1, + content: "hello @#{admin_group.name} @#{user_group.name}", + ) + }.to change { admin1.chat_mentions.count }.by(1).and change { + admin2.chat_mentions.count + }.by(1).and change { user2.chat_mentions.count }.by(1).and change { + user3.chat_mentions.count + }.by(1) + end + + it "doesn't create chat mentions for group mentions where the group is un-mentionable" do + admin_group.update(mentionable_level: Group::ALIAS_LEVELS[:nobody]) + expect { + described_class.create( + chat_channel: public_chat_channel, + user: user1, + content: "hello @#{admin_group.name}", + ) + }.not_to change { Chat::Mention.count } + end + + it "creates a chat mention without notification for acting user" do + message = + described_class.create( + chat_channel: public_chat_channel, + user: user1, + content: "@#{user_group.name}", + ).chat_message + + mention = user1.chat_mentions.where(chat_message: message).first + expect(mention).to be_present + expect(mention.notification).to be_blank + end + end + + context "when ignore_channel_wide_mention is enabled" do + before { user2.user_option.update(ignore_channel_wide_mention: true) } + + it "when mentioning @all creates a mention without notification" do + message = + described_class.create( + chat_channel: public_chat_channel, + user: user1, + content: "hi! @all", + ).chat_message + + mention = user2.chat_mentions.where(chat_message: message).first + expect(mention).to be_present + expect(mention.notification).to be_nil + end + + it "when mentioning @here creates a mention without notification" do + user2.update(last_seen_at: Time.now) + + message = + described_class.create( + chat_channel: public_chat_channel, + user: user1, + content: "@here", + ).chat_message + + mention = user2.chat_mentions.where(chat_message: message).first + expect(mention).to be_present + expect(mention.notification).to be_nil + end + end + it "doesn't create mention notifications for users without a membership record" do message = described_class.create( @@ -476,48 +584,16 @@ describe Chat::MessageCreator do expect(mentioned_user["status"]).to be_blank end - end - it "creates a chat_mention record without notification when self mentioning" do - message = - described_class.create( - chat_channel: direct_message_channel, - user: user1, - content: "hello @#{user1.username}", - ).chat_message - - mention = user1.chat_mentions.where(chat_message: message).first - expect(mention).to be_present - expect(mention.notification).to be_nil - end - - context "when ignore_channel_wide_mention is enabled" do - before { user2.user_option.update(ignore_channel_wide_mention: true) } - - it "when mentioning @all creates a mention without notification" do + it "creates a chat_mention record without notification when self mentioning" do message = described_class.create( - chat_channel: public_chat_channel, + chat_channel: direct_message_channel, user: user1, - content: "hi! @all", + content: "hello @#{user1.username}", ).chat_message - mention = user2.chat_mentions.where(chat_message: message).first - expect(mention).to be_present - expect(mention.notification).to be_nil - end - - it "when mentioning @here creates a mention without notification" do - user2.update(last_seen_at: Time.now) - - message = - described_class.create( - chat_channel: public_chat_channel, - user: user1, - content: "@here", - ).chat_message - - mention = user2.chat_mentions.where(chat_message: message).first + mention = user1.chat_mentions.where(chat_message: message).first expect(mention).to be_present expect(mention.notification).to be_nil end @@ -976,82 +1052,6 @@ describe Chat::MessageCreator do end end - describe "group mentions" do - it "creates chat mentions for group mentions where the group is mentionable" do - expect { - described_class.create( - chat_channel: public_chat_channel, - user: user1, - content: "hello @#{admin_group.name}", - ) - }.to change { admin1.chat_mentions.count }.by(1).and change { - admin2.chat_mentions.count - }.by(1) - end - - it "doesn't mention users twice if they are direct mentioned and group mentioned" do - expect { - described_class.create( - chat_channel: public_chat_channel, - user: user1, - content: "hello @#{admin_group.name} @#{admin1.username} and @#{admin2.username}", - ) - }.to change { admin1.chat_mentions.count }.by(1).and change { - admin2.chat_mentions.count - }.by(1) - end - - it "creates chat mentions for group mentions and direct mentions" do - expect { - described_class.create( - chat_channel: public_chat_channel, - user: user1, - content: "hello @#{admin_group.name} @#{user2.username}", - ) - }.to change { admin1.chat_mentions.count }.by(1).and change { - admin2.chat_mentions.count - }.by(1).and change { user2.chat_mentions.count }.by(1) - end - - it "creates chat mentions for group mentions and direct mentions" do - expect { - described_class.create( - chat_channel: public_chat_channel, - user: user1, - content: "hello @#{admin_group.name} @#{user_group.name}", - ) - }.to change { admin1.chat_mentions.count }.by(1).and change { - admin2.chat_mentions.count - }.by(1).and change { user2.chat_mentions.count }.by(1).and change { - user3.chat_mentions.count - }.by(1) - end - - it "doesn't create chat mentions for group mentions where the group is un-mentionable" do - admin_group.update(mentionable_level: Group::ALIAS_LEVELS[:nobody]) - expect { - described_class.create( - chat_channel: public_chat_channel, - user: user1, - content: "hello @#{admin_group.name}", - ) - }.not_to change { Chat::Mention.count } - end - - it "creates a chat mention without notification for acting user" do - message = - described_class.create( - chat_channel: public_chat_channel, - user: user1, - content: "@#{user_group.name}", - ).chat_message - - mention = user1.chat_mentions.where(chat_message: message).first - expect(mention).to be_present - expect(mention.notification).to be_blank - end - end - describe "push notifications" do before do Chat::UserChatChannelMembership.where( diff --git a/plugins/chat/spec/components/chat/message_updater_spec.rb b/plugins/chat/spec/components/chat/message_updater_spec.rb index 5e587d54b0b..c6a67af24e4 100644 --- a/plugins/chat/spec/components/chat/message_updater_spec.rb +++ b/plugins/chat/spec/components/chat/message_updater_spec.rb @@ -347,46 +347,48 @@ describe Chat::MessageUpdater do expect(user2.chat_mentions.where(chat_message: chat_message).count).to eq(1) end end - end - describe "group mentions" do - it "creates group mentions on update" do - chat_message = create_chat_message(user1, "ping nobody", public_chat_channel) - expect { - Chat::MessageUpdater.update( - guardian: guardian, - chat_message: chat_message, - new_content: "ping @#{admin_group.name}", - ) - }.to change { Chat::Mention.where(chat_message: chat_message).count }.by(2) + describe "with group mentions" do + it "creates group mentions on update" do + chat_message = create_chat_message(user1, "ping nobody", public_chat_channel) + expect { + Chat::MessageUpdater.update( + guardian: guardian, + chat_message: chat_message, + new_content: "ping @#{admin_group.name}", + ) + }.to change { Chat::Mention.where(chat_message: chat_message).count }.by(2) - expect(admin1.chat_mentions.where(chat_message: chat_message)).to be_present - expect(admin2.chat_mentions.where(chat_message: chat_message)).to be_present - end + expect(admin1.chat_mentions.where(chat_message: chat_message)).to be_present + expect(admin2.chat_mentions.where(chat_message: chat_message)).to be_present + end - it "doesn't duplicate mentions when the user is already direct mentioned and then group mentioned" do - chat_message = create_chat_message(user1, "ping @#{admin2.username}", public_chat_channel) - expect { - Chat::MessageUpdater.update( - guardian: guardian, - chat_message: chat_message, - new_content: "ping @#{admin_group.name} @#{admin2.username}", - ) - }.to change { admin1.chat_mentions.count }.by(1).and not_change { admin2.chat_mentions.count } - end + it "doesn't duplicate mentions when the user is already direct mentioned and then group mentioned" do + chat_message = create_chat_message(user1, "ping @#{admin2.username}", public_chat_channel) + expect { + Chat::MessageUpdater.update( + guardian: guardian, + chat_message: chat_message, + new_content: "ping @#{admin_group.name} @#{admin2.username}", + ) + }.to change { admin1.chat_mentions.count }.by(1).and not_change { + admin2.chat_mentions.count + } + end - it "deletes old mentions when group mention is removed" do - chat_message = create_chat_message(user1, "ping @#{admin_group.name}", public_chat_channel) - expect { - Chat::MessageUpdater.update( - guardian: guardian, - chat_message: chat_message, - new_content: "ping nobody anymore!", - ) - }.to change { Chat::Mention.where(chat_message: chat_message).count }.by(-2) + it "deletes old mentions when group mention is removed" do + chat_message = create_chat_message(user1, "ping @#{admin_group.name}", public_chat_channel) + expect { + Chat::MessageUpdater.update( + guardian: guardian, + chat_message: chat_message, + new_content: "ping nobody anymore!", + ) + }.to change { Chat::Mention.where(chat_message: chat_message).count }.by(-2) - expect(admin1.chat_mentions.where(chat_message: chat_message)).not_to be_present - expect(admin2.chat_mentions.where(chat_message: chat_message)).not_to be_present + expect(admin1.chat_mentions.where(chat_message: chat_message)).not_to be_present + expect(admin2.chat_mentions.where(chat_message: chat_message)).not_to be_present + end end end