Skip to content

Commit

Permalink
fix!: Raise error when adding duplicate email address in Personalizat…
Browse files Browse the repository at this point in the history
…ions (#437)
  • Loading branch information
pranc1ngpegasus authored Sep 23, 2020
1 parent 840bf00 commit 7c19b84
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 13 deletions.
20 changes: 20 additions & 0 deletions lib/sendgrid/helpers/mail/personalization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,20 @@ def initialize
end

def add_to(to)
raise DuplicatePersonalizationError if duplicate?(to)

@tos << to.to_json
end

def add_cc(cc)
raise DuplicatePersonalizationError if duplicate?(cc)

@ccs << cc.to_json
end

def add_bcc(bcc)
raise DuplicatePersonalizationError if duplicate?(bcc)

@bccs << bcc.to_json
end

Expand Down Expand Up @@ -63,5 +69,19 @@ def to_json(*)
'send_at' => send_at
}.delete_if { |_, value| value.to_s.strip == '' || value == [] || value == {} }
end

private

def duplicate?(addition)
additional_email = addition.email.downcase

[@tos, @ccs, @bccs].flatten.each do |elm|
return true if elm&.dig('email') == additional_email
end

false
end
end

class DuplicatePersonalizationError < StandardError; end
end
28 changes: 15 additions & 13 deletions test/sendgrid/helpers/mail/test_mail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ def test_kitchen_sink
mail.from = Email.new(email: '[email protected]')
mail.subject = 'Hello World from the Twilio SendGrid Ruby Library'
personalization = Personalization.new
personalization.add_to(Email.new(email: 'test@example.com', name: 'Example User'))
personalization.add_to(Email.new(email: 'test@example.com', name: 'Example User'))
personalization.add_cc(Email.new(email: 'test@example.com', name: 'Example User'))
personalization.add_cc(Email.new(email: 'test@example.com', name: 'Example User'))
personalization.add_bcc(Email.new(email: 'test@example.com', name: 'Example User'))
personalization.add_bcc(Email.new(email: 'test@example.com', name: 'Example User'))
personalization.add_to(Email.new(email: 'test1@example.com', name: 'Example User 1'))
personalization.add_to(Email.new(email: 'test2@example.com', name: 'Example User 2'))
personalization.add_cc(Email.new(email: 'test3@example.com', name: 'Example User 3'))
personalization.add_cc(Email.new(email: 'test4@example.com', name: 'Example User 4'))
personalization.add_bcc(Email.new(email: 'test5@example.com', name: 'Example User 5'))
personalization.add_bcc(Email.new(email: 'test6@example.com', name: 'Example User 6'))
personalization.subject = 'Hello World from the Personalized Twilio SendGrid Ruby Library'
personalization.add_header(Header.new(key: 'X-Test', value: 'True'))
personalization.add_header(Header.new(key: 'X-Mock', value: 'False'))
Expand All @@ -39,12 +39,12 @@ def test_kitchen_sink
mail.add_personalization(personalization)

personalization2 = Personalization.new
personalization2.add_to(Email.new(email: 'test@example.com', name: 'Example User'))
personalization2.add_to(Email.new(email: 'test@example.com', name: 'Example User'))
personalization2.add_cc(Email.new(email: 'test@example.com', name: 'Example User'))
personalization2.add_cc(Email.new(email: 'test@example.com', name: 'Example User'))
personalization2.add_bcc(Email.new(email: 'test@example.com', name: 'Example User'))
personalization2.add_bcc(Email.new(email: 'test@example.com', name: 'Example User'))
personalization2.add_to(Email.new(email: 'test1@example.com', name: 'Example User 1'))
personalization2.add_to(Email.new(email: 'test2@example.com', name: 'Example User 2'))
personalization2.add_cc(Email.new(email: 'test3@example.com', name: 'Example User 3'))
personalization2.add_cc(Email.new(email: 'test4@example.com', name: 'Example User 4'))
personalization2.add_bcc(Email.new(email: 'test5@example.com', name: 'Example User 5'))
personalization2.add_bcc(Email.new(email: 'test6@example.com', name: 'Example User 6'))
personalization2.subject = 'Hello World from the Personalized Twilio SendGrid Ruby Library'
personalization2.add_header(Header.new(key: 'X-Test', value: 'True'))
personalization2.add_header(Header.new(key: 'X-Mock', value: 'False'))
Expand Down Expand Up @@ -114,7 +114,9 @@ def test_kitchen_sink

mail.reply_to = Email.new(email: '[email protected]')

assert_equal(mail.to_json, JSON.parse('{"asm":{"group_id":99,"groups_to_display":[4,5,6,7,8]},"attachments":[{"content":"TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12","content_id":"Balance Sheet","disposition":"attachment","filename":"balance_001.pdf","type":"application/pdf"},{"content":"BwdW","content_id":"Banner","disposition":"inline","filename":"banner.png","type":"image/png"}],"batch_id":"sendgrid_batch_id","categories":["May","2016"],"content":[{"type":"text/plain","value":"some text here"},{"type":"text/html","value":"<html><body>some text here</body></html>"}],"custom_args":{"campaign":"welcome","weekday":"morning"},"from":{"email":"[email protected]"},"headers":{"X-Test3":"test3","X-Test4":"test4"},"ip_pool_name":"23","mail_settings":{"bcc":{"email":"[email protected]","enable":true},"bypass_list_management":{"enable":true},"footer":{"enable":true,"html":"<html><body>Footer Text</body></html>","text":"Footer Text"},"sandbox_mode":{"enable":true},"spam_check":{"enable":true,"post_to_url":"https://spamcatcher.sendgrid.com","threshold":1}},"personalizations":[{"bcc":[{"email":"[email protected]","name":"Example User"},{"email":"[email protected]","name":"Example User"}],"cc":[{"email":"[email protected]","name":"Example User"},{"email":"[email protected]","name":"Example User"}],"custom_args":{"type":"marketing","user_id":"343"},"headers":{"X-Mock":"False","X-Test":"True"},"send_at":1443636843,"subject":"Hello World from the Personalized Twilio SendGrid Ruby Library","substitutions":{"%city%":"Denver","%name%":"Example User"},"to":[{"email":"[email protected]","name":"Example User"},{"email":"[email protected]","name":"Example User"}]},{"bcc":[{"email":"[email protected]","name":"Example User"},{"email":"[email protected]","name":"Example User"}],"cc":[{"email":"[email protected]","name":"Example User"},{"email":"[email protected]","name":"Example User"}],"custom_args":{"type":"marketing","user_id":"343"},"headers":{"X-Mock":"False","X-Test":"True"},"send_at":1443636843,"subject":"Hello World from the Personalized Twilio SendGrid Ruby Library","substitutions":{"%city%":"Denver","%name%":"Example User"},"to":[{"email":"[email protected]","name":"Example User"},{"email":"[email protected]","name":"Example User"}]}],"reply_to":{"email":"[email protected]"},"sections":{"%section1%":"Substitution Text for Section 1","%section2%":"Substitution Text for Section 2"},"send_at":1443636842,"subject":"Hello World from the Twilio SendGrid Ruby Library","template_id":"13b8f94f-bcae-4ec6-b752-70d6cb59f932","tracking_settings":{"click_tracking":{"enable":false,"enable_text":false},"ganalytics":{"enable":true,"utm_campaign":"some campaign","utm_content":"some content","utm_medium":"some medium","utm_source":"some source","utm_term":"some term"},"open_tracking":{"enable":true,"substitution_tag":"Optional tag to replace with the open image in the body of the message"},"subscription_tracking":{"enable":true,"html":"html to insert into the text/html portion of the message","substitution_tag":"Optional tag to replace with the open image in the body of the message","text":"text to insert into the text/plain portion of the message"}}}'))
# rubocop:disable Layout/LineLength
assert_equal(mail.to_json, JSON.parse('{"asm":{"group_id":99,"groups_to_display":[4,5,6,7,8]},"attachments":[{"content":"TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12","content_id":"Balance Sheet","disposition":"attachment","filename":"balance_001.pdf","type":"application/pdf"},{"content":"BwdW","content_id":"Banner","disposition":"inline","filename":"banner.png","type":"image/png"}],"batch_id":"sendgrid_batch_id","categories":["May","2016"],"content":[{"type":"text/plain","value":"some text here"},{"type":"text/html","value":"<html><body>some text here</body></html>"}],"custom_args":{"campaign":"welcome","weekday":"morning"},"from":{"email":"[email protected]"},"headers":{"X-Test3":"test3","X-Test4":"test4"},"ip_pool_name":"23","mail_settings":{"bcc":{"email":"[email protected]","enable":true},"bypass_list_management":{"enable":true},"footer":{"enable":true,"html":"<html><body>Footer Text</body></html>","text":"Footer Text"},"sandbox_mode":{"enable":true},"spam_check":{"enable":true,"post_to_url":"https://spamcatcher.sendgrid.com","threshold":1}},"personalizations":[{"bcc":[{"email":"[email protected]","name":"Example User 5"},{"email":"[email protected]","name":"Example User 6"}],"cc":[{"email":"[email protected]","name":"Example User 3"},{"email":"[email protected]","name":"Example User 4"}],"custom_args":{"type":"marketing","user_id":"343"},"headers":{"X-Mock":"False","X-Test":"True"},"send_at":1443636843,"subject":"Hello World from the Personalized Twilio SendGrid Ruby Library","substitutions":{"%city%":"Denver","%name%":"Example User"},"to":[{"email":"[email protected]","name":"Example User 1"},{"email":"[email protected]","name":"Example User 2"}]},{"bcc":[{"email":"[email protected]","name":"Example User 5"},{"email":"[email protected]","name":"Example User 6"}],"cc":[{"email":"[email protected]","name":"Example User 3"},{"email":"[email protected]","name":"Example User 4"}],"custom_args":{"type":"marketing","user_id":"343"},"headers":{"X-Mock":"False","X-Test":"True"},"send_at":1443636843,"subject":"Hello World from the Personalized Twilio SendGrid Ruby Library","substitutions":{"%city%":"Denver","%name%":"Example User"},"to":[{"email":"[email protected]","name":"Example User 1"},{"email":"[email protected]","name":"Example User 2"}]}],"reply_to":{"email":"[email protected]"},"sections":{"%section1%":"Substitution Text for Section 1","%section2%":"Substitution Text for Section 2"},"send_at":1443636842,"subject":"Hello World from the Twilio SendGrid Ruby Library","template_id":"13b8f94f-bcae-4ec6-b752-70d6cb59f932","tracking_settings":{"click_tracking":{"enable":false,"enable_text":false},"ganalytics":{"enable":true,"utm_campaign":"some campaign","utm_content":"some content","utm_medium":"some medium","utm_source":"some source","utm_term":"some term"},"open_tracking":{"enable":true,"substitution_tag":"Optional tag to replace with the open image in the body of the message"},"subscription_tracking":{"enable":true,"html":"html to insert into the text/html portion of the message","substitution_tag":"Optional tag to replace with the open image in the body of the message","text":"text to insert into the text/plain portion of the message"}}}'))
# rubocop:enable all
end

def test_that_personalizations_is_empty_initially
Expand Down
39 changes: 39 additions & 0 deletions test/sendgrid/helpers/mail/test_personalizations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ def test_add_to
assert_equal @personalization.to_json, expected_json
end

def test_duplicate_add_to
@personalization = Personalization.new
@personalization.add_to(Email.new(email: '[email protected]', name: 'Example User'))

assert_raises(DuplicatePersonalizationError) do
@personalization.add_to(Email.new(email: '[email protected]', name: 'Duplicate User'))
end

assert_raises(DuplicatePersonalizationError) do
@personalization.add_to(Email.new(email: '[email protected]', name: 'Duplicate User'))
end
end

def test_add_cc
@personalization = Personalization.new
@personalization.add_cc(Email.new(email: '[email protected]', name: 'Example User'))
Expand All @@ -42,6 +55,19 @@ def test_add_cc
assert_equal @personalization.to_json, expected_json
end

def test_duplicate_add_cc
@personalization = Personalization.new
@personalization.add_cc(Email.new(email: '[email protected]', name: 'Example User'))

assert_raises(DuplicatePersonalizationError) do
@personalization.add_cc(Email.new(email: '[email protected]', name: 'Duplicate User'))
end

assert_raises(DuplicatePersonalizationError) do
@personalization.add_cc(Email.new(email: '[email protected]', name: 'Duplicate User'))
end
end

def test_add_bcc
@personalization = Personalization.new
@personalization.add_bcc(Email.new(email: '[email protected]', name: 'Example User'))
Expand All @@ -61,6 +87,19 @@ def test_add_bcc
assert_equal @personalization.to_json, expected_json
end

def test_duplicate_add_bcc
@personalization = Personalization.new
@personalization.add_bcc(Email.new(email: '[email protected]', name: 'Example User'))

assert_raises(DuplicatePersonalizationError) do
@personalization.add_bcc(Email.new(email: '[email protected]', name: 'Duplicate User'))
end

assert_raises(DuplicatePersonalizationError) do
@personalization.add_bcc(Email.new(email: '[email protected]', name: 'Duplicate User'))
end
end

def test_add_header
@personalization = Personalization.new
@personalization.add_header(Header.new(key: 'X-Test', value: 'True'))
Expand Down

0 comments on commit 7c19b84

Please sign in to comment.