DEV: more tests for mentions when updating chat messages (#21185)

This adds these two new test cases:

    context "when updating a mentioned user" do
      it "updates the mention record" do

    # and
    context "when there are duplicate mentions" do
        it "creates a single mention record per mention" do

Apart from that, this groups mention related tests into a context, renames one test, and moves setup of another test into the test case itself from the before block (to make it more clear, that test is the only one that uses that setup). See the PR's commit history.
This commit is contained in:
Andrei Prigorshnev
2023-04-21 16:58:01 +04:00
committed by GitHub
parent 3e414996c2
commit e7292e1682

View File

@ -30,8 +30,6 @@ describe Chat::MessageUpdater do
Fabricate(:user_chat_channel_membership, chat_channel: public_chat_channel, user: user) Fabricate(:user_chat_channel_membership, chat_channel: public_chat_channel, user: user)
end end
Group.refresh_automatic_groups! Group.refresh_automatic_groups!
@direct_message_channel =
Chat::DirectMessageChannelCreator.create!(acting_user: user1, target_users: [user1, user2])
end end
def create_chat_message(user, message, channel, upload_ids: nil) def create_chat_message(user, message, channel, upload_ids: nil)
@ -126,82 +124,126 @@ describe Chat::MessageUpdater do
expect(events.map { _1[:event_name] }).to include(:chat_message_edited) expect(events.map { _1[:event_name] }).to include(:chat_message_edited)
end end
it "sends notifications if a message was updated with new mentions" do context "with mentions" do
message = create_chat_message(user1, "Mentioning @#{user2.username}", public_chat_channel) 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( Chat::MessageUpdater.update(
guardian: guardian, guardian: guardian,
chat_message: message, chat_message: message,
new_content: "Mentioning @#{user2.username} and @#{user3.username}", new_content: "Mentioning @#{user2.username} and @#{user3.username}",
) )
mention = user3.chat_mentions.where(chat_message: message).first mention = user3.chat_mentions.where(chat_message: message).first
expect(mention.notification).to be_present expect(mention.notification).to be_present
end 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( Chat::MessageUpdater.update(
guardian: guardian, guardian: guardian,
chat_message: chat_message, 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 mention = user_without_memberships.chat_mentions.where(chat_message: chat_message).first
message = "ping" expect(mention.notification).to be_nil
chat_message = create_chat_message(user1, message, public_chat_channel) end
Chat::MessageUpdater.update( it "destroys mentions that should be removed" do
guardian: guardian, chat_message =
chat_message: chat_message, create_chat_message(
new_content: message + " @#{user_without_memberships.username}", 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
mention = user_without_memberships.chat_mentions.where(chat_message: chat_message).first it "creates new, leaves existing, and removes old mentions all at once" do
expect(mention.notification).to be_nil chat_message =
end create_chat_message(
user1,
it "destroys mention notifications that should be removed" do "ping @#{user2.username} @#{user3.username}",
chat_message = public_chat_channel,
create_chat_message(user1, "ping @#{user2.username} @#{user3.username}", public_chat_channel) )
expect {
Chat::MessageUpdater.update( Chat::MessageUpdater.update(
guardian: guardian, guardian: guardian,
chat_message: chat_message, 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 expect(user2.chat_mentions.where(chat_message: chat_message)).not_to be_present
chat_message = expect(user3.chat_mentions.where(chat_message: chat_message)).to be_present
create_chat_message(user1, "ping @#{user2.username} @#{user3.username}", public_chat_channel) expect(user4.chat_mentions.where(chat_message: chat_message)).to be_present
Chat::MessageUpdater.update( end
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 it "doesn't create mention notification in direct message for users without access" do
expect(user3.chat_mentions.where(chat_message: chat_message)).to be_present direct_message_channel =
expect(user4.chat_mentions.where(chat_message: chat_message)).to be_present Chat::DirectMessageChannelCreator.create!(acting_user: user1, target_users: [user1, user2])
end message = create_chat_message(user1, "ping nobody", direct_message_channel)
it "doesn't create mention notification in direct message for users without access" do Chat::MessageUpdater.update(
message = create_chat_message(user1, "ping nobody", @direct_message_channel) guardian: guardian,
chat_message: message,
new_content: "ping @#{admin1.username}",
)
Chat::MessageUpdater.update( mention = admin1.chat_mentions.where(chat_message: message).first
guardian: guardian, expect(mention.notification).to be_nil
chat_message: message, end
new_content: "ping @#{admin1.username}",
)
mention = admin1.chat_mentions.where(chat_message: message).first context "when updating a mentioned user" do
expect(mention.notification).to be_nil 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
end
end end
describe "group mentions" do describe "group mentions" do