Commit fc6442e2 authored by Sean McGivern's avatar Sean McGivern

Merge branch 'afontaine/add-username-to-email-from' into 'master'

Add Username to Email From Header in Notifications

See merge request gitlab-org/gitlab!56588
parents 3973ece4 a1881c25
...@@ -70,7 +70,7 @@ class Notify < ApplicationMailer ...@@ -70,7 +70,7 @@ class Notify < ApplicationMailer
return unless sender = User.find(sender_id) return unless sender = User.find(sender_id)
address = default_sender_address address = default_sender_address
address.display_name = sender_name.presence || sender.name address.display_name = sender_name.presence || "#{sender.name} (#{sender.to_reference})"
if send_from_user_email && can_send_from_user_email?(sender) if send_from_user_email && can_send_from_user_email?(sender)
address.address = sender.email address.address = sender.email
......
---
title: Add Username to Email From Header in Notifications
merge_request: 56588
author:
type: changed
...@@ -106,9 +106,7 @@ RSpec.describe Notify do ...@@ -106,9 +106,7 @@ RSpec.describe Notify do
it_behaves_like 'an unsubscribeable thread' it_behaves_like 'an unsubscribeable thread'
it 'is sent as the last approver' do it 'is sent as the last approver' do
sender = subject.header[:from].addrs[0] expect_sender(last_approver)
expect(sender.display_name).to eq(last_approver.name)
expect(sender.address).to eq(gitlab_sender)
end end
it 'has the correct subject' do it 'has the correct subject' do
...@@ -170,9 +168,7 @@ RSpec.describe Notify do ...@@ -170,9 +168,7 @@ RSpec.describe Notify do
it_behaves_like 'an unsubscribeable thread' it_behaves_like 'an unsubscribeable thread'
it 'is sent as the last unapprover' do it 'is sent as the last unapprover' do
sender = subject.header[:from].addrs[0] expect_sender(last_unapprover)
expect(sender.display_name).to eq(last_unapprover.name)
expect(sender.address).to eq(gitlab_sender)
end end
it 'has the correct subject' do it 'has the correct subject' do
...@@ -397,4 +393,10 @@ RSpec.describe Notify do ...@@ -397,4 +393,10 @@ RSpec.describe Notify do
is_expected.to have_body_text recipient.confirmation_token is_expected.to have_body_text recipient.confirmation_token
end end
end end
def expect_sender(user)
sender = subject.header[:from].addrs[0]
expect(sender.display_name).to eq("#{user.name} (@#{user.username})")
expect(sender.address).to eq(gitlab_sender)
end
end end
...@@ -55,9 +55,7 @@ RSpec.describe Emails::MergeRequests do ...@@ -55,9 +55,7 @@ RSpec.describe Emails::MergeRequests do
it_behaves_like 'appearance header and footer not enabled' it_behaves_like 'appearance header and footer not enabled'
it 'is sent as the merge request author' do it 'is sent as the merge request author' do
sender = subject.header[:from].addrs[0] expect_sender(merge_request.author)
expect(sender.display_name).to eq(merge_request.author.name)
expect(sender.address).to eq(gitlab_sender)
end end
it 'has the correct subject and body' do it 'has the correct subject and body' do
...@@ -85,9 +83,7 @@ RSpec.describe Emails::MergeRequests do ...@@ -85,9 +83,7 @@ RSpec.describe Emails::MergeRequests do
it_behaves_like 'appearance header and footer not enabled' it_behaves_like 'appearance header and footer not enabled'
it 'is sent as the author' do it 'is sent as the author' do
sender = subject.header[:from].addrs[0] expect_sender(current_user)
expect(sender.display_name).to eq(current_user.name)
expect(sender.address).to eq(gitlab_sender)
end end
it 'has the correct subject and body' do it 'has the correct subject and body' do
...@@ -120,9 +116,7 @@ RSpec.describe Emails::MergeRequests do ...@@ -120,9 +116,7 @@ RSpec.describe Emails::MergeRequests do
it_behaves_like 'appearance header and footer not enabled' it_behaves_like 'appearance header and footer not enabled'
it 'is sent as the merge author' do it 'is sent as the merge author' do
sender = subject.header[:from].addrs[0] expect_sender(merge_author)
expect(sender.display_name).to eq(merge_author.name)
expect(sender.address).to eq(gitlab_sender)
end end
it 'has the correct subject and body' do it 'has the correct subject and body' do
...@@ -153,9 +147,7 @@ RSpec.describe Emails::MergeRequests do ...@@ -153,9 +147,7 @@ RSpec.describe Emails::MergeRequests do
it_behaves_like 'appearance header and footer not enabled' it_behaves_like 'appearance header and footer not enabled'
it 'is sent as the author' do it 'is sent as the author' do
sender = subject.header[:from].addrs[0] expect_sender(current_user)
expect(sender.display_name).to eq(current_user.name)
expect(sender.address).to eq(gitlab_sender)
end end
it 'has the correct subject and body' do it 'has the correct subject and body' do
...@@ -229,4 +221,10 @@ RSpec.describe Emails::MergeRequests do ...@@ -229,4 +221,10 @@ RSpec.describe Emails::MergeRequests do
it { expect(subject).to have_content('attachment has been truncated to avoid exceeding the maximum allowed attachment size of 15 MB.') } it { expect(subject).to have_content('attachment has been truncated to avoid exceeding the maximum allowed attachment size of 15 MB.') }
end end
end end
def expect_sender(user)
sender = subject.header[:from].addrs[0]
expect(sender.display_name).to eq("#{user.name} (@#{user.username})")
expect(sender.address).to eq(gitlab_sender)
end
end end
...@@ -69,11 +69,8 @@ RSpec.describe Notify do ...@@ -69,11 +69,8 @@ RSpec.describe Notify do
it_behaves_like 'an email sent to a user' it_behaves_like 'an email sent to a user'
it 'is sent to the assignee as the author' do it 'is sent to the assignee as the author' do
sender = subject.header[:from].addrs.first
aggregate_failures do aggregate_failures do
expect(sender.display_name).to eq(current_user.name) expect_sender(current_user)
expect(sender.address).to eq(gitlab_sender)
expect(subject).to deliver_to(recipient.notification_email) expect(subject).to deliver_to(recipient.notification_email)
end end
end end
...@@ -146,9 +143,7 @@ RSpec.describe Notify do ...@@ -146,9 +143,7 @@ RSpec.describe Notify do
it_behaves_like 'appearance header and footer not enabled' it_behaves_like 'appearance header and footer not enabled'
it 'is sent as the author' do it 'is sent as the author' do
sender = subject.header[:from].addrs[0] expect_sender(current_user)
expect(sender.display_name).to eq(current_user.name)
expect(sender.address).to eq(gitlab_sender)
end end
it 'has the correct subject and body' do it 'has the correct subject and body' do
...@@ -187,9 +182,7 @@ RSpec.describe Notify do ...@@ -187,9 +182,7 @@ RSpec.describe Notify do
it_behaves_like 'appearance header and footer not enabled' it_behaves_like 'appearance header and footer not enabled'
it 'is sent as the author' do it 'is sent as the author' do
sender = subject.header[:from].addrs[0] expect_sender(current_user)
expect(sender.display_name).to eq(current_user.name)
expect(sender.address).to eq(gitlab_sender)
end end
it 'has the correct subject and body' do it 'has the correct subject and body' do
...@@ -251,9 +244,7 @@ RSpec.describe Notify do ...@@ -251,9 +244,7 @@ RSpec.describe Notify do
it_behaves_like 'appearance header and footer not enabled' it_behaves_like 'appearance header and footer not enabled'
it 'is sent as the author' do it 'is sent as the author' do
sender = subject.header[:from].addrs[0] expect_sender(current_user)
expect(sender.display_name).to eq(current_user.name)
expect(sender.address).to eq(gitlab_sender)
end end
it 'has the correct subject and body' do it 'has the correct subject and body' do
...@@ -389,9 +380,7 @@ RSpec.describe Notify do ...@@ -389,9 +380,7 @@ RSpec.describe Notify do
it_behaves_like 'appearance header and footer not enabled' it_behaves_like 'appearance header and footer not enabled'
it 'is sent as the author' do it 'is sent as the author' do
sender = subject.header[:from].addrs[0] expect_sender(current_user)
expect(sender.display_name).to eq(current_user.name)
expect(sender.address).to eq(gitlab_sender)
end end
it 'has the correct subject and body' do it 'has the correct subject and body' do
...@@ -456,9 +445,7 @@ RSpec.describe Notify do ...@@ -456,9 +445,7 @@ RSpec.describe Notify do
it_behaves_like 'appearance header and footer not enabled' it_behaves_like 'appearance header and footer not enabled'
it 'is sent as the author' do it 'is sent as the author' do
sender = subject.header[:from].addrs[0] expect_sender(current_user)
expect(sender.display_name).to eq(current_user.name)
expect(sender.address).to eq(gitlab_sender)
end end
it 'has the correct subject and body' do it 'has the correct subject and body' do
...@@ -486,10 +473,7 @@ RSpec.describe Notify do ...@@ -486,10 +473,7 @@ RSpec.describe Notify do
it_behaves_like 'appearance header and footer not enabled' it_behaves_like 'appearance header and footer not enabled'
it 'is sent as the push user' do it 'is sent as the push user' do
sender = subject.header[:from].addrs[0] expect_sender(push_user)
expect(sender.display_name).to eq(push_user.name)
expect(sender.address).to eq(gitlab_sender)
end end
it 'has the correct subject and body' do it 'has the correct subject and body' do
...@@ -1002,11 +986,8 @@ RSpec.describe Notify do ...@@ -1002,11 +986,8 @@ RSpec.describe Notify do
it_behaves_like 'it should have Gmail Actions links' it_behaves_like 'it should have Gmail Actions links'
it 'is sent to the given recipient as the author' do it 'is sent to the given recipient as the author' do
sender = subject.header[:from].addrs[0]
aggregate_failures do aggregate_failures do
expect(sender.display_name).to eq(note_author.name) expect_sender(note_author)
expect(sender.address).to eq(gitlab_sender)
expect(subject).to deliver_to(recipient.notification_email) expect(subject).to deliver_to(recipient.notification_email)
end end
end end
...@@ -1162,11 +1143,8 @@ RSpec.describe Notify do ...@@ -1162,11 +1143,8 @@ RSpec.describe Notify do
it_behaves_like 'it should have Gmail Actions links' it_behaves_like 'it should have Gmail Actions links'
it 'is sent to the given recipient as the author' do it 'is sent to the given recipient as the author' do
sender = subject.header[:from].addrs[0]
aggregate_failures do aggregate_failures do
expect(sender.display_name).to eq(note_author.name) expect_sender(note_author)
expect(sender.address).to eq(gitlab_sender)
expect(subject).to deliver_to(recipient.notification_email) expect(subject).to deliver_to(recipient.notification_email)
end end
end end
...@@ -1221,12 +1199,6 @@ RSpec.describe Notify do ...@@ -1221,12 +1199,6 @@ RSpec.describe Notify do
issue.issue_email_participants.create!(email: 'service.desk@example.com') issue.issue_email_participants.create!(email: 'service.desk@example.com')
end end
def expect_sender(username)
sender = subject.header[:from].addrs[0]
expect(sender.display_name).to eq(username)
expect(sender.address).to eq(gitlab_sender)
end
describe 'thank you email' do describe 'thank you email' do
subject { described_class.service_desk_thank_you_email(issue.id) } subject { described_class.service_desk_thank_you_email(issue.id) }
...@@ -1244,14 +1216,16 @@ RSpec.describe Notify do ...@@ -1244,14 +1216,16 @@ RSpec.describe Notify do
end end
it 'uses service bot name by default' do it 'uses service bot name by default' do
expect_sender(User.support_bot.name) expect_sender(User.support_bot)
end end
context 'when custom outgoing name is set' do context 'when custom outgoing name is set' do
let_it_be(:settings) { create(:service_desk_setting, project: project, outgoing_name: 'some custom name') } let_it_be(:settings) { create(:service_desk_setting, project: project, outgoing_name: 'some custom name') }
it 'uses custom name in "from" header' do it 'uses custom name in "from" header' do
expect_sender('some custom name') sender = subject.header[:from].addrs[0]
expect(sender.display_name).to eq('some custom name')
expect(sender.address).to eq(gitlab_sender)
end end
end end
...@@ -1259,7 +1233,7 @@ RSpec.describe Notify do ...@@ -1259,7 +1233,7 @@ RSpec.describe Notify do
let_it_be(:settings) { create(:service_desk_setting, project: project, outgoing_name: '') } let_it_be(:settings) { create(:service_desk_setting, project: project, outgoing_name: '') }
it 'uses service bot name' do it 'uses service bot name' do
expect_sender(User.support_bot.name) expect_sender(User.support_bot)
end end
end end
end end
...@@ -1276,7 +1250,7 @@ RSpec.describe Notify do ...@@ -1276,7 +1250,7 @@ RSpec.describe Notify do
end end
it 'uses author\'s name in "from" header' do it 'uses author\'s name in "from" header' do
expect_sender(first_note.author.name) expect_sender(first_note.author)
end end
it 'has the correct subject and body' do it 'has the correct subject and body' do
...@@ -1672,9 +1646,7 @@ RSpec.describe Notify do ...@@ -1672,9 +1646,7 @@ RSpec.describe Notify do
it_behaves_like 'appearance header and footer not enabled' it_behaves_like 'appearance header and footer not enabled'
it 'is sent as the author' do it 'is sent as the author' do
sender = subject.header[:from].addrs[0] expect_sender(user)
expect(sender.display_name).to eq(user.name)
expect(sender.address).to eq(gitlab_sender)
end end
it 'has the correct subject and body' do it 'has the correct subject and body' do
...@@ -1699,9 +1671,7 @@ RSpec.describe Notify do ...@@ -1699,9 +1671,7 @@ RSpec.describe Notify do
it_behaves_like 'appearance header and footer not enabled' it_behaves_like 'appearance header and footer not enabled'
it 'is sent as the author' do it 'is sent as the author' do
sender = subject.header[:from].addrs[0] expect_sender(user)
expect(sender.display_name).to eq(user.name)
expect(sender.address).to eq(gitlab_sender)
end end
it 'has the correct subject and body' do it 'has the correct subject and body' do
...@@ -1725,9 +1695,7 @@ RSpec.describe Notify do ...@@ -1725,9 +1695,7 @@ RSpec.describe Notify do
it_behaves_like 'appearance header and footer not enabled' it_behaves_like 'appearance header and footer not enabled'
it 'is sent as the author' do it 'is sent as the author' do
sender = subject.header[:from].addrs[0] expect_sender(user)
expect(sender.display_name).to eq(user.name)
expect(sender.address).to eq(gitlab_sender)
end end
it 'has the correct subject' do it 'has the correct subject' do
...@@ -1748,9 +1716,7 @@ RSpec.describe Notify do ...@@ -1748,9 +1716,7 @@ RSpec.describe Notify do
it_behaves_like 'appearance header and footer not enabled' it_behaves_like 'appearance header and footer not enabled'
it 'is sent as the author' do it 'is sent as the author' do
sender = subject.header[:from].addrs[0] expect_sender(user)
expect(sender.display_name).to eq(user.name)
expect(sender.address).to eq(gitlab_sender)
end end
it 'has the correct subject' do it 'has the correct subject' do
...@@ -1777,9 +1743,7 @@ RSpec.describe Notify do ...@@ -1777,9 +1743,7 @@ RSpec.describe Notify do
it_behaves_like 'appearance header and footer not enabled' it_behaves_like 'appearance header and footer not enabled'
it 'is sent as the author' do it 'is sent as the author' do
sender = subject.header[:from].addrs[0] expect_sender(user)
expect(sender.display_name).to eq(user.name)
expect(sender.address).to eq(gitlab_sender)
end end
it 'has the correct subject and body' do it 'has the correct subject and body' do
...@@ -1870,9 +1834,7 @@ RSpec.describe Notify do ...@@ -1870,9 +1834,7 @@ RSpec.describe Notify do
it_behaves_like 'appearance header and footer not enabled' it_behaves_like 'appearance header and footer not enabled'
it 'is sent as the author' do it 'is sent as the author' do
sender = subject.header[:from].addrs[0] expect_sender(user)
expect(sender.display_name).to eq(user.name)
expect(sender.address).to eq(gitlab_sender)
end end
it 'has the correct subject and body' do it 'has the correct subject and body' do
...@@ -1962,12 +1924,8 @@ RSpec.describe Notify do ...@@ -1962,12 +1924,8 @@ RSpec.describe Notify do
it_behaves_like 'an unsubscribeable thread' it_behaves_like 'an unsubscribeable thread'
it 'is sent to the given recipient as the author' do it 'is sent to the given recipient as the author' do
sender = subject.header[:from].addrs[0]
aggregate_failures do aggregate_failures do
expect(sender.display_name).to eq(review.author_name) expect_sender(review.author)
expect(sender.address).to eq(gitlab_sender)
expect(subject).to deliver_to(recipient.notification_email)
end end
end end
...@@ -2002,4 +1960,10 @@ RSpec.describe Notify do ...@@ -2002,4 +1960,10 @@ RSpec.describe Notify do
end end
end end
end end
def expect_sender(user)
sender = subject.header[:from].addrs[0]
expect(sender.display_name).to eq("#{user.name} (@#{user.username})")
expect(sender.address).to eq(gitlab_sender)
end
end end
...@@ -1662,7 +1662,7 @@ RSpec.describe NotificationService, :mailer do ...@@ -1662,7 +1662,7 @@ RSpec.describe NotificationService, :mailer do
notification.issue_due(issue) notification.issue_due(issue)
email = find_email_for(@subscriber) email = find_email_for(@subscriber)
expect(email.header[:from].display_names).to eq([issue.author.name]) expect(email.header[:from].display_names).to eq(["#{issue.author.name} (@#{issue.author.username})"])
end end
it_behaves_like 'participating notifications' do it_behaves_like 'participating notifications' do
......
...@@ -225,7 +225,7 @@ RSpec.shared_examples 'a note email' do ...@@ -225,7 +225,7 @@ RSpec.shared_examples 'a note email' do
sender = subject.header[:from].addrs[0] sender = subject.header[:from].addrs[0]
aggregate_failures do aggregate_failures do
expect(sender.display_name).to eq(note_author.name) expect(sender.display_name).to eq("#{note_author.name} (@#{note_author.username})")
expect(sender.address).to eq(gitlab_sender) expect(sender.address).to eq(gitlab_sender)
expect(subject).to deliver_to(recipient.notification_email) expect(subject).to deliver_to(recipient.notification_email)
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment