A platform for community discussion. Free, open, simple.
DEV: group tests related to mentions (#22017)
Andrei Prigorshneve5c705af8a62
diff --git a/plugins/chat/spec/components/chat/message_creator_spec.rb b/plugins/chat/spec/components/chat/message_creator_spec.rbindex 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 doendend+ 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" domessage =described_class.create(@@ -476,48 +584,16 @@ describe Chat::MessageCreator doexpect(mentioned_user["status"]).to be_blankend- 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" domessage =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,+ chat_channel: direct_message_channel,user: user1,- content: "@here",+ content: "hello @#{user1.username}",).chat_message- mention = user2.chat_mentions.where(chat_message: message).first+ mention = user1.chat_mentions.where(chat_message: message).firstexpect(mention).to be_presentexpect(mention.notification).to be_nilend@@ -976,82 +1052,6 @@ describe Chat::MessageCreator doendend- 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" dobefore doChat::UserChatChannelMembership.where(diff --git a/plugins/chat/spec/components/chat/message_updater_spec.rb b/plugins/chat/spec/components/chat/message_updater_spec.rbindex 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 doexpect(user2.chat_mentions.where(chat_message: chat_message).count).to eq(1)endend- 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+ endendend