A platform for community discussion. Free, open, simple.
DEV: more tests for mentions when updating chat messages (#21185)
Andrei Prigorshneve7292e1682bb
diff --git a/plugins/chat/spec/components/chat/message_updater_spec.rb b/plugins/chat/spec/components/chat/message_updater_spec.rbindex 9017657bbdd..19247162dc3 100644--- a/plugins/chat/spec/components/chat/message_updater_spec.rb+++ b/plugins/chat/spec/components/chat/message_updater_spec.rb@@ -30,8 +30,6 @@ describe Chat::MessageUpdater doFabricate(:user_chat_channel_membership, chat_channel: public_chat_channel, user: user)endGroup.refresh_automatic_groups!- @direct_message_channel =- Chat::DirectMessageChannelCreator.create!(acting_user: user1, target_users: [user1, user2])enddef create_chat_message(user, message, channel, upload_ids: nil)@@ -126,82 +124,126 @@ describe Chat::MessageUpdater doexpect(events.map { _1[:event_name] }).to include(:chat_message_edited)end- it "sends notifications if a message was updated with new mentions" do- message = create_chat_message(user1, "Mentioning @#{user2.username}", public_chat_channel)+ context "with mentions" do+ it "sends notifications if a message was updated with new mentions" do+ message = create_chat_message(user1, "Mentioning @#{user2.username}", public_chat_channel)- Chat::MessageUpdater.update(- guardian: guardian,- chat_message: message,- new_content: "Mentioning @#{user2.username} and @#{user3.username}",- )+ Chat::MessageUpdater.update(+ guardian: guardian,+ chat_message: message,+ new_content: "Mentioning @#{user2.username} and @#{user3.username}",+ )- mention = user3.chat_mentions.where(chat_message: message).first- expect(mention.notification).to be_present- end+ mention = user3.chat_mentions.where(chat_message: message).first+ expect(mention.notification).to be_present+ end++ it "doesn't create mentions for already mentioned users" do+ message = "ping @#{user2.username} @#{user3.username}"+ chat_message = create_chat_message(user1, message, public_chat_channel)+ expect {+ Chat::MessageUpdater.update(+ guardian: guardian,+ chat_message: chat_message,+ new_content: message + " editedddd",+ )+ }.not_to change { Chat::Mention.count }+ end++ it "doesn't create mention notification for users without access" do+ message = "ping"+ chat_message = create_chat_message(user1, message, public_chat_channel)- it "doesn't create mentions for already mentioned users" do- message = "ping @#{user2.username} @#{user3.username}"- chat_message = create_chat_message(user1, message, public_chat_channel)- expect {Chat::MessageUpdater.update(guardian: guardian,chat_message: chat_message,- new_content: message + " editedddd",+ new_content: message + " @#{user_without_memberships.username}",)- }.not_to change { Chat::Mention.count }- end-- it "doesn't create mention notification for users without access" do- message = "ping"- chat_message = create_chat_message(user1, message, public_chat_channel)- Chat::MessageUpdater.update(- guardian: guardian,- chat_message: chat_message,- new_content: message + " @#{user_without_memberships.username}",- )+ mention = user_without_memberships.chat_mentions.where(chat_message: chat_message).first+ expect(mention.notification).to be_nil+ end- mention = user_without_memberships.chat_mentions.where(chat_message: chat_message).first- expect(mention.notification).to be_nil- end+ it "destroys mentions that should be removed" do+ chat_message =+ create_chat_message(+ user1,+ "ping @#{user2.username} @#{user3.username}",+ public_chat_channel,+ )+ expect {+ Chat::MessageUpdater.update(+ guardian: guardian,+ chat_message: chat_message,+ new_content: "ping @#{user3.username}",+ )+ }.to change { user2.chat_mentions.count }.by(-1).and not_change { user3.chat_mentions.count }+ end- it "destroys mention notifications that should be removed" do- chat_message =- create_chat_message(user1, "ping @#{user2.username} @#{user3.username}", public_chat_channel)- expect {+ it "creates new, leaves existing, and removes old mentions all at once" do+ chat_message =+ create_chat_message(+ user1,+ "ping @#{user2.username} @#{user3.username}",+ public_chat_channel,+ )Chat::MessageUpdater.update(guardian: guardian,chat_message: chat_message,- new_content: "ping @#{user3.username}",+ new_content: "ping @#{user3.username} @#{user4.username}",)- }.to change { user2.chat_mentions.count }.by(-1).and not_change { user3.chat_mentions.count }- end- it "creates new, leaves existing, and removes old mentions all at once" do- chat_message =- create_chat_message(user1, "ping @#{user2.username} @#{user3.username}", public_chat_channel)- Chat::MessageUpdater.update(- guardian: guardian,- chat_message: chat_message,- new_content: "ping @#{user3.username} @#{user4.username}",- )+ expect(user2.chat_mentions.where(chat_message: chat_message)).not_to be_present+ expect(user3.chat_mentions.where(chat_message: chat_message)).to be_present+ expect(user4.chat_mentions.where(chat_message: chat_message)).to be_present+ end- expect(user2.chat_mentions.where(chat_message: chat_message)).not_to be_present- expect(user3.chat_mentions.where(chat_message: chat_message)).to be_present- expect(user4.chat_mentions.where(chat_message: chat_message)).to be_present- end+ it "doesn't create mention notification in direct message for users without access" do+ direct_message_channel =+ Chat::DirectMessageChannelCreator.create!(acting_user: user1, target_users: [user1, user2])+ message = create_chat_message(user1, "ping nobody", direct_message_channel)- it "doesn't create mention notification in direct message for users without access" do- message = create_chat_message(user1, "ping nobody", @direct_message_channel)+ Chat::MessageUpdater.update(+ guardian: guardian,+ chat_message: message,+ new_content: "ping @#{admin1.username}",+ )- Chat::MessageUpdater.update(- guardian: guardian,- chat_message: message,- new_content: "ping @#{admin1.username}",- )+ mention = admin1.chat_mentions.where(chat_message: message).first+ expect(mention.notification).to be_nil+ end- mention = admin1.chat_mentions.where(chat_message: message).first- expect(mention.notification).to be_nil+ context "when updating a mentioned user" do+ it "updates the mention record" do+ chat_message = create_chat_message(user1, "ping @#{user2.username}", public_chat_channel)++ Chat::MessageUpdater.update(+ guardian: guardian,+ chat_message: chat_message,+ new_content: "ping @#{user3.username}",+ )++ user2_mentions = user2.chat_mentions.where(chat_message: chat_message)+ expect(user2_mentions.length).to be(0)++ user3_mentions = user3.chat_mentions.where(chat_message: chat_message)+ expect(user3_mentions.length).to be(1)+ end+ end++ context "when there are duplicate mentions" do+ it "creates a single mention record per user" do+ chat_message = create_chat_message(user1, "ping @#{user2.username}", public_chat_channel)++ Chat::MessageUpdater.update(+ guardian: guardian,+ chat_message: chat_message,+ new_content: "ping @#{user2.username} @#{user2.username} edited",+ )++ expect(user2.chat_mentions.where(chat_message: chat_message).count).to eq(1)+ end+ endenddescribe "group mentions" do