diff --git a/app/mailers/emails/admin_notification.rb b/app/mailers/emails/admin_notification.rb index 5c5497d8eb55601e01ff8d9760ca09b5e1aad2a7..35814eb5647b53dee9ca658f8c8a51cf47d32135 100644 --- a/app/mailers/emails/admin_notification.rb +++ b/app/mailers/emails/admin_notification.rb @@ -2,18 +2,18 @@ module Emails module AdminNotification - def send_admin_notification(user_id, subject, body) + def send_admin_notification(user_id, subj, body) user = User.find(user_id) email = user.notification_email_or_default @unsubscribe_url = unsubscribe_url(email: Base64.urlsafe_encode64(email)) @body = body - mail_with_locale to: email, subject: subject + mail_with_locale to: email, subject: subject(subj) end def send_unsubscribed_notification(user_id) user = User.find(user_id) email = user.notification_email_or_default - mail_with_locale to: email, subject: "Unsubscribed from GitLab administrator notifications" + mail_with_locale to: email, subject: subject("Unsubscribed from GitLab administrator notifications") end end end diff --git a/app/mailers/emails/projects.rb b/app/mailers/emails/projects.rb index ef555d69c4a27f60521da13f0e4af27174d6f6fd..dde6ee69a99a786159bd3e23f8400e80ab77ec4d 100644 --- a/app/mailers/emails/projects.rb +++ b/app/mailers/emails/projects.rb @@ -82,7 +82,7 @@ def repository_push_email(project_id, opts = {}) mail_with_locale( from: sender(@message.author_id, send_from_user_email: @message.send_from_committer_email?), reply_to: @message.reply_to, - subject: @message.subject + subject: subject_with_suffix([@message.subject]) ) end diff --git a/spec/mailers/emails/admin_notification_spec.rb b/spec/mailers/emails/admin_notification_spec.rb index 33b8558bfa3ab881885a0c77c136d232e8ea208b..3fc71e8d30e9d420d8f9435ee766b46a4833c7b0 100644 --- a/spec/mailers/emails/admin_notification_spec.rb +++ b/spec/mailers/emails/admin_notification_spec.rb @@ -11,4 +11,26 @@ expect(Notify).to be_respond_to(email_method) end end + + describe '#send_admin_notification' do + subject { Notify.send_admin_notification(recipient.id, 'Subject', 'Body') } + + it 'sends an email' do + expect(subject).to have_subject 'Subject' + expect(subject).to have_body_text 'Body' + end + + it_behaves_like 'an email with suffix' + end + + describe '#send_unsubscribed_notification' do + subject { Notify.send_unsubscribed_notification(recipient.id) } + + it 'sends an email' do + expect(subject).to have_subject 'Unsubscribed from GitLab administrator notifications' + expect(subject).to have_body_text 'You have been unsubscribed' + end + + it_behaves_like 'an email with suffix' + end end diff --git a/spec/mailers/emails/projects_spec.rb b/spec/mailers/emails/projects_spec.rb index fc33b7fb865b31d1020c98493c6e863a4edf0547..f39c2efd8a66d7b18ded8f3782a811371a1b34cf 100644 --- a/spec/mailers/emails/projects_spec.rb +++ b/spec/mailers/emails/projects_spec.rb @@ -180,6 +180,24 @@ end end + describe '#repository_push_email' do + let(:recipient) { user } + + subject { Notify.repository_push_email(project.id, { author_id: user.id, ref: 'main', action: :create }) } + + it_behaves_like 'it should not have Gmail Actions links' + it_behaves_like 'a user cannot unsubscribe through footer link' + it_behaves_like 'appearance header and footer enabled' + it_behaves_like 'appearance header and footer not enabled' + it_behaves_like 'an email with suffix' + + it 'has the correct subject and body' do + is_expected.to have_subject("[Git][#{project.full_path}] Pushed new branch main") + is_expected.to have_body_text(project.full_path) + is_expected.to have_body_text("#{user.name} pushed new branch main at") + end + end + describe '.inactive_project_deletion_warning_email' do let(:recipient) { user } let(:deletion_date) { "2022-01-10" } diff --git a/spec/support/shared_examples/mailers/notify_shared_examples.rb b/spec/support/shared_examples/mailers/notify_shared_examples.rb index 0ffab4b7a479b6ac9dfbed66c8bfc4429028b8d5..e965787cf2dfebace76f964bfa5a8537bdca0f74 100644 --- a/spec/support/shared_examples/mailers/notify_shared_examples.rb +++ b/spec/support/shared_examples/mailers/notify_shared_examples.rb @@ -36,6 +36,16 @@ end end +RSpec.shared_examples 'an email with suffix' do + before do + stub_config_setting(email_subject_suffix: 'Suffix') + end + + it 'adds a correct suffix to subject' do + expect(subject.subject).to end_with('| Suffix') + end +end + RSpec.shared_examples 'an email that contains a header with author username' do it 'has X-GitLab-Author header containing author\'s username' do is_expected.to have_header 'X-GitLab-Author', user.username