diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-06-10 00:09:51 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-06-10 00:09:51 +0300 |
commit | e374f6b2297582fde956350e92c19d1ae93ddfc8 (patch) | |
tree | 30be153d2883438955cfbf0d576052adc5875f67 /spec/services | |
parent | 1268cfeaf73a73c5593f13639530513716826e2b (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/services')
-rw-r--r-- | spec/services/notification_service_spec.rb | 182 |
1 files changed, 121 insertions, 61 deletions
diff --git a/spec/services/notification_service_spec.rb b/spec/services/notification_service_spec.rb index f63f982708d..0e1afaa8378 100644 --- a/spec/services/notification_service_spec.rb +++ b/spec/services/notification_service_spec.rb @@ -800,7 +800,21 @@ RSpec.describe NotificationService, :mailer, feature_category: :team_planning do let_it_be(:mentioned_issue) { create(:issue, assignees: issue.assignees) } let_it_be(:author) { create(:user) } - let(:note) { create(:note_on_issue, author: author, noteable: issue, project_id: issue.project_id, note: '@all mentioned') } + let(:user_mentions) do + other_members = [ + @unsubscribed_mentioned, + @u_guest_watcher, + @pg_watcher, + @u_mentioned, + @u_not_mentioned, + @u_disabled, + @pg_disabled + ] + + (issue.project.team.members + other_members).map(&:to_reference).join(' ') + end + + let(:note) { create(:note_on_issue, author: author, noteable: issue, project_id: issue.project_id, note: note_content) } before_all do build_team(project) @@ -815,73 +829,103 @@ RSpec.describe NotificationService, :mailer, feature_category: :team_planning do end describe '#new_note' do - it 'notifies the team members' do + it 'notifies parent group members with mention level' do + note = create(:note_on_issue, noteable: issue, project_id: issue.project_id, note: "@#{@pg_mention.username}") + notification.new_note(note) - # Make sure @unsubscribed_mentioned is part of the team - expect(note.project.team.members).to include(@unsubscribed_mentioned) + should_email_nested_group_user(@pg_mention) + end + + shared_examples 'correct team members are notified' do + it 'notifies the team members' do + notification.new_note(note) - # Notify all team members - note.project.team.members.each do |member| - # User with disabled notification should not be notified - next if member.id == @u_disabled.id - # Author should not be notified - next if member.id == note.author.id + # Make sure @unsubscribed_mentioned is part of the team + expect(note.project.team.members).to include(@unsubscribed_mentioned) - should_email(member) + # Notify all team members + note.project.team.members.each do |member| + # User with disabled notification should not be notified + next if member.id == @u_disabled.id + # Author should not be notified + next if member.id == note.author.id + + should_email(member) + end + + should_email(@u_guest_watcher) + should_email(note.noteable.author) + should_email(note.noteable.assignees.first) + should_email_nested_group_user(@pg_watcher) + should_email(@u_mentioned) + should_email(@u_not_mentioned) + should_not_email(note.author) + should_not_email(@u_disabled) + should_not_email_nested_group_user(@pg_disabled) end - should_email(@u_guest_watcher) - should_email(note.noteable.author) - should_email(note.noteable.assignees.first) - should_email_nested_group_user(@pg_watcher) - should_email(@u_mentioned) - should_email(@u_not_mentioned) - should_not_email(note.author) - should_not_email(@u_disabled) - should_not_email_nested_group_user(@pg_disabled) - end + it 'filters out "mentioned in" notes' do + mentioned_note = SystemNoteService.cross_reference(mentioned_issue, issue, issue.author) - it 'notifies parent group members with mention level' do - note = create(:note_on_issue, noteable: issue, project_id: issue.project_id, note: "@#{@pg_mention.username}") + expect(Notify).not_to receive(:note_issue_email) + notification.new_note(mentioned_note) + end - notification.new_note(note) + it_behaves_like 'project emails are disabled' do + let(:notification_target) { note } + let(:notification_trigger) { notification.new_note(note) } + end - should_email_nested_group_user(@pg_mention) - end + context 'when note is confidential' do + let(:note) { create(:note_on_issue, author: author, noteable: issue, project_id: issue.project_id, note: note_content, confidential: true) } + let(:guest) { create(:user) } - it 'filters out "mentioned in" notes' do - mentioned_note = SystemNoteService.cross_reference(mentioned_issue, issue, issue.author) + it 'does not notify users that cannot read note' do + project.add_guest(guest) + reset_delivered_emails! - expect(Notify).not_to receive(:note_issue_email) - notification.new_note(mentioned_note) - end + notification.new_note(note) - it_behaves_like 'project emails are disabled' do - let(:notification_target) { note } - let(:notification_trigger) { notification.new_note(note) } + should_not_email(guest) + end + end end - context 'when note is confidential' do - let(:note) { create(:note_on_issue, author: author, noteable: issue, project_id: issue.project_id, note: '@all mentioned', confidential: true) } - let(:guest) { create(:user) } + context 'when `@all` mention is used' do + let(:note_content) { "@all mentioned" } - it 'does not notify users that cannot read note' do - project.add_guest(guest) - reset_delivered_emails! + it_behaves_like 'correct team members are notified' + end - notification.new_note(note) + context 'when users are individually mentioned' do + # `user_mentions` is concatenanting individual user mentions + # so that the end result is the same as `@all`. + let(:note_content) { "#{user_mentions} mentioned" } - should_not_email(guest) - end + it_behaves_like 'correct team members are notified' end end end context 'project snippet note', :deliver_mails_inline do + let(:user_mentions) do + other_members = [ + @u_custom_global, + @u_guest_watcher, + snippet.author, # snippet = note.noteable's author + author, # note's author + @u_disabled, + @u_mentioned, + @u_not_mentioned + ] + + (snippet.project.team.members + other_members).map(&:to_reference).join(' ') + end + let(:snippet) { create(:project_snippet, project: project, author: create(:user)) } let(:author) { create(:user) } - let(:note) { create(:note_on_project_snippet, author: author, noteable: snippet, project_id: project.id, note: '@all mentioned') } + let(:note) { create(:note_on_project_snippet, author: author, noteable: snippet, project_id: project.id, note: note_content) } before do build_team(project) @@ -896,27 +940,43 @@ RSpec.describe NotificationService, :mailer, feature_category: :team_planning do end describe '#new_note' do - it 'notifies the team members' do - notification.new_note(note) - # Notify all team members - note.project.team.members.each do |member| - # User with disabled notification should not be notified - next if member.id == @u_disabled.id - # Author should not be notified - next if member.id == note.author.id + shared_examples 'correct team members are notified' do + it 'notifies the team members' do + notification.new_note(note) + # Notify all team members + note.project.team.members.each do |member| + # User with disabled notification should not be notified + next if member.id == @u_disabled.id + # Author should not be notified + next if member.id == note.author.id + + should_email(member) + end - should_email(member) + # it emails custom global users on mention + should_email(@u_custom_global) + + should_email(@u_guest_watcher) + should_email(note.noteable.author) + should_not_email(note.author) + should_email(@u_mentioned) + should_not_email(@u_disabled) + should_email(@u_not_mentioned) end + end - # it emails custom global users on mention - should_email(@u_custom_global) + context 'when `@all` mention is used' do + let(:note_content) { "@all mentioned" } - should_email(@u_guest_watcher) - should_email(note.noteable.author) - should_not_email(note.author) - should_email(@u_mentioned) - should_not_email(@u_disabled) - should_email(@u_not_mentioned) + it_behaves_like 'correct team members are notified' + end + + context 'when users are individually mentioned' do + # `user_mentions` is concatenanting individual user mentions + # so that the end result is the same as `@all`. + let(:note_content) { "#{user_mentions} mentioned" } + + it_behaves_like 'correct team members are notified' end end end |