Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Accurate duplicate checking when adding email to Personalizations #446

Merged
merged 2 commits into from
Jan 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/sendgrid/helpers/mail/personalization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def duplicate?(addition)
additional_email = addition.email.downcase

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

false
Expand Down
27 changes: 27 additions & 0 deletions test/sendgrid/helpers/mail/test_personalizations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def test_add_to
def test_duplicate_add_to
@personalization = Personalization.new
@personalization.add_to(Email.new(email: 'test1@example.com', name: 'Example User'))
@personalization.add_to(Email.new(email: 'TEST2@EXAMPLE.COM', name: 'Example User 2'))

assert_raises(DuplicatePersonalizationError) do
@personalization.add_to(Email.new(email: 'test1@example.com', name: 'Duplicate User'))
Expand All @@ -34,6 +35,14 @@ def test_duplicate_add_to
assert_raises(DuplicatePersonalizationError) do
@personalization.add_to(Email.new(email: 'TEST1@EXAMPLE.COM', name: 'Duplicate User'))
end

assert_raises(DuplicatePersonalizationError) do
@personalization.add_to(Email.new(email: 'test2@example.com', name: 'Duplicate User 2'))
end

assert_raises(DuplicatePersonalizationError) do
@personalization.add_to(Email.new(email: 'TEST2@EXAMPLE.COM', name: 'Duplicate User 2'))
end
end

def test_add_cc
Expand All @@ -58,6 +67,7 @@ def test_add_cc
def test_duplicate_add_cc
@personalization = Personalization.new
@personalization.add_cc(Email.new(email: 'test1@example.com', name: 'Example User'))
@personalization.add_cc(Email.new(email: 'TEST2@EXAMPLE.COM', name: 'Example User 2'))

assert_raises(DuplicatePersonalizationError) do
@personalization.add_cc(Email.new(email: 'test1@example.com', name: 'Duplicate User'))
Expand All @@ -66,6 +76,14 @@ def test_duplicate_add_cc
assert_raises(DuplicatePersonalizationError) do
@personalization.add_cc(Email.new(email: 'TEST1@EXAMPLE.COM', name: 'Duplicate User'))
end

assert_raises(DuplicatePersonalizationError) do
@personalization.add_cc(Email.new(email: 'test2@example.com', name: 'Duplicate User 2'))
end

assert_raises(DuplicatePersonalizationError) do
@personalization.add_cc(Email.new(email: 'TEST2@EXAMPLE.COM', name: 'Duplicate User 2'))
end
end

def test_add_bcc
Expand All @@ -90,6 +108,7 @@ def test_add_bcc
def test_duplicate_add_bcc
@personalization = Personalization.new
@personalization.add_bcc(Email.new(email: 'test1@example.com', name: 'Example User'))
@personalization.add_bcc(Email.new(email: 'TEST2@EXAMPLE.COM', name: 'Example User 2'))

assert_raises(DuplicatePersonalizationError) do
@personalization.add_bcc(Email.new(email: 'test1@example.com', name: 'Duplicate User'))
Expand All @@ -98,6 +117,14 @@ def test_duplicate_add_bcc
assert_raises(DuplicatePersonalizationError) do
@personalization.add_bcc(Email.new(email: 'TEST1@EXAMPLE.COM', name: 'Duplicate User'))
end

assert_raises(DuplicatePersonalizationError) do
@personalization.add_bcc(Email.new(email: 'test2@example.com', name: 'Duplicate User 2'))
end

assert_raises(DuplicatePersonalizationError) do
@personalization.add_bcc(Email.new(email: 'TEST2@EXAMPLE.COM', name: 'Duplicate User 2'))
end
end

def test_add_header
Expand Down