Commit 8bab486c authored by Andy Soiron's avatar Andy Soiron Committed by Peter Leitzen

Exclude duplicates from emails on push recipients

The worker would otherwise send to the same email
address multiple times
parent 47b30b7c
...@@ -96,6 +96,6 @@ class EmailsOnPushWorker # rubocop:disable Scalability/IdempotentWorker ...@@ -96,6 +96,6 @@ class EmailsOnPushWorker # rubocop:disable Scalability/IdempotentWorker
def valid_recipients(recipients) def valid_recipients(recipients)
recipients.split.select do |recipient| recipients.split.select do |recipient|
recipient.include?('@') recipient.include?('@')
end end.uniq(&:downcase)
end end
end end
---
title: Exclude duplicates from emails on push recipients
merge_request: 55588
author:
type: changed
...@@ -97,7 +97,7 @@ RSpec.describe EmailsOnPushWorker, :mailer do ...@@ -97,7 +97,7 @@ RSpec.describe EmailsOnPushWorker, :mailer do
end end
it "gracefully handles an input SMTP error" do it "gracefully handles an input SMTP error" do
expect(ActionMailer::Base.deliveries.count).to eq(0) expect(ActionMailer::Base.deliveries).to be_empty
end end
end end
...@@ -112,6 +112,16 @@ RSpec.describe EmailsOnPushWorker, :mailer do ...@@ -112,6 +112,16 @@ RSpec.describe EmailsOnPushWorker, :mailer do
end end
end end
context "with mixed-case recipient" do
let(:recipients) { user.email.upcase }
it "retains the case" do
perform
expect(email_recipients).to contain_exactly(recipients)
end
end
context "when the recipient addresses are a list of email addresses" do context "when the recipient addresses are a list of email addresses" do
let(:recipients) do let(:recipients) do
1.upto(5).map { |i| user.email.sub('@', "+#{i}@") }.join("\n") 1.upto(5).map { |i| user.email.sub('@', "+#{i}@") }.join("\n")
...@@ -120,7 +130,6 @@ RSpec.describe EmailsOnPushWorker, :mailer do ...@@ -120,7 +130,6 @@ RSpec.describe EmailsOnPushWorker, :mailer do
it "sends the mail to each of the recipients" do it "sends the mail to each of the recipients" do
perform perform
expect(ActionMailer::Base.deliveries.count).to eq(5)
expect(email_recipients).to contain_exactly(*recipients.split) expect(email_recipients).to contain_exactly(*recipients.split)
end end
...@@ -132,13 +141,22 @@ RSpec.describe EmailsOnPushWorker, :mailer do ...@@ -132,13 +141,22 @@ RSpec.describe EmailsOnPushWorker, :mailer do
end end
end end
context "when recipients are invalid" do
let(:recipients) { "invalid\n\nrecipients" }
it "ignores them" do
perform
expect(ActionMailer::Base.deliveries).to be_empty
end
end
context "when the recipient addresses contains angle brackets and are separated by spaces" do context "when the recipient addresses contains angle brackets and are separated by spaces" do
let(:recipients) { "John Doe <johndoe@example.com> Jane Doe <janedoe@example.com>" } let(:recipients) { "John Doe <johndoe@example.com> Jane Doe <janedoe@example.com>" }
it "accepts emails separated by whitespace" do it "accepts emails separated by whitespace" do
perform perform
expect(ActionMailer::Base.deliveries.count).to eq(2)
expect(email_recipients).to contain_exactly("johndoe@example.com", "janedoe@example.com") expect(email_recipients).to contain_exactly("johndoe@example.com", "janedoe@example.com")
end end
end end
...@@ -149,7 +167,6 @@ RSpec.describe EmailsOnPushWorker, :mailer do ...@@ -149,7 +167,6 @@ RSpec.describe EmailsOnPushWorker, :mailer do
it "accepts both kind of emails" do it "accepts both kind of emails" do
perform perform
expect(ActionMailer::Base.deliveries.count).to eq(2)
expect(email_recipients).to contain_exactly("johndoe@example.com", "janedoe@example.com") expect(email_recipients).to contain_exactly("johndoe@example.com", "janedoe@example.com")
end end
end end
...@@ -160,10 +177,19 @@ RSpec.describe EmailsOnPushWorker, :mailer do ...@@ -160,10 +177,19 @@ RSpec.describe EmailsOnPushWorker, :mailer do
it "accepts emails separated by newlines" do it "accepts emails separated by newlines" do
perform perform
expect(ActionMailer::Base.deliveries.count).to eq(2)
expect(email_recipients).to contain_exactly("johndoe@example.com", "janedoe@example.com") expect(email_recipients).to contain_exactly("johndoe@example.com", "janedoe@example.com")
end end
end end
context 'when the recipient addresses contains duplicates' do
let(:recipients) { 'non@dubplicate.com Duplic@te.com duplic@te.com Duplic@te.com duplic@Te.com' }
it 'deduplicates recipients while treating the domain part as case-insensitive' do
perform
expect(email_recipients).to contain_exactly('non@dubplicate.com', 'Duplic@te.com')
end
end
end end
end end
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