DEV: group tests related to mentions (#22017)

This commit is contained in:
Andrei Prigorshnev 2023-06-14 19:34:38 +04:00 committed by GitHub
parent 2652354da3
commit e5c705af8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 149 additions and 147 deletions

View File

@ -288,6 +288,114 @@ describe Chat::MessageCreator do
end end
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 it "doesn't create mention notifications for users without a membership record" do
message = message =
described_class.create( described_class.create(
@ -476,48 +584,16 @@ describe Chat::MessageCreator do
expect(mentioned_user["status"]).to be_blank expect(mentioned_user["status"]).to be_blank
end end
end
it "creates a chat_mention record without notification when self mentioning" do 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
message = message =
described_class.create( described_class.create(
chat_channel: public_chat_channel, chat_channel: direct_message_channel,
user: user1, user: user1,
content: "hi! @all", content: "hello @#{user1.username}",
).chat_message ).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
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).to be_present
expect(mention.notification).to be_nil expect(mention.notification).to be_nil
end end
@ -976,82 +1052,6 @@ describe Chat::MessageCreator do
end end
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 describe "push notifications" do
before do before do
Chat::UserChatChannelMembership.where( Chat::UserChatChannelMembership.where(

View File

@ -347,46 +347,48 @@ describe Chat::MessageUpdater do
expect(user2.chat_mentions.where(chat_message: chat_message).count).to eq(1) expect(user2.chat_mentions.where(chat_message: chat_message).count).to eq(1)
end end
end end
end
describe "group mentions" do describe "with group mentions" do
it "creates group mentions on update" do it "creates group mentions on update" do
chat_message = create_chat_message(user1, "ping nobody", public_chat_channel) chat_message = create_chat_message(user1, "ping nobody", public_chat_channel)
expect { expect {
Chat::MessageUpdater.update( Chat::MessageUpdater.update(
guardian: guardian, guardian: guardian,
chat_message: chat_message, chat_message: chat_message,
new_content: "ping @#{admin_group.name}", new_content: "ping @#{admin_group.name}",
) )
}.to change { Chat::Mention.where(chat_message: chat_message).count }.by(2) }.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(admin1.chat_mentions.where(chat_message: chat_message)).to be_present
expect(admin2.chat_mentions.where(chat_message: chat_message)).to be_present expect(admin2.chat_mentions.where(chat_message: chat_message)).to be_present
end end
it "doesn't duplicate mentions when the user is already direct mentioned and then group mentioned" do 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) chat_message = create_chat_message(user1, "ping @#{admin2.username}", public_chat_channel)
expect { expect {
Chat::MessageUpdater.update( Chat::MessageUpdater.update(
guardian: guardian, guardian: guardian,
chat_message: chat_message, chat_message: chat_message,
new_content: "ping @#{admin_group.name} @#{admin2.username}", new_content: "ping @#{admin_group.name} @#{admin2.username}",
) )
}.to change { admin1.chat_mentions.count }.by(1).and not_change { admin2.chat_mentions.count } }.to change { admin1.chat_mentions.count }.by(1).and not_change {
end admin2.chat_mentions.count
}
end
it "deletes old mentions when group mention is removed" do it "deletes old mentions when group mention is removed" do
chat_message = create_chat_message(user1, "ping @#{admin_group.name}", public_chat_channel) chat_message = create_chat_message(user1, "ping @#{admin_group.name}", public_chat_channel)
expect { expect {
Chat::MessageUpdater.update( Chat::MessageUpdater.update(
guardian: guardian, guardian: guardian,
chat_message: chat_message, chat_message: chat_message,
new_content: "ping nobody anymore!", new_content: "ping nobody anymore!",
) )
}.to change { Chat::Mention.where(chat_message: chat_message).count }.by(-2) }.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(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(admin2.chat_mentions.where(chat_message: chat_message)).not_to be_present
end
end end
end end