sendgrid-ruby 6.2.1 → 6.3.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.codeclimate.yml +1 -1
- data/.gitignore +1 -1
- data/.rubocop.yml +4 -0
- data/.rubocop_todo.yml +109 -0
- data/.travis.yml +3 -4
- data/CHANGELOG.md +69 -9
- data/CONTRIBUTING.md +11 -21
- data/FIRST_TIMERS.md +79 -0
- data/Gemfile +0 -1
- data/ISSUE_TEMPLATE.md +5 -1
- data/Makefile +4 -2
- data/PULL_REQUEST_TEMPLATE.md +1 -1
- data/README.md +22 -27
- data/Rakefile +2 -3
- data/TROUBLESHOOTING.md +5 -5
- data/USAGE.md +145 -38
- data/examples/accesssettings/accesssettings.rb +9 -12
- data/examples/alerts/alerts.rb +8 -11
- data/examples/apikeys/apikeys.rb +12 -15
- data/examples/asm/asm.rb +27 -30
- data/examples/browsers/browsers.rb +0 -3
- data/examples/campaigns/campaigns.rb +29 -32
- data/examples/categories/categories.rb +0 -3
- data/examples/clients/clients.rb +1 -4
- data/examples/contactdb/contactdb.rb +63 -66
- data/examples/devices/devices.rb +0 -3
- data/examples/emailactivity/emailactivity.rb +52 -0
- data/examples/geo/geo.rb +0 -3
- data/examples/helpers/eventwebhook/example.rb +16 -0
- data/examples/helpers/mail/example.rb +19 -13
- data/examples/helpers/settings/example.rb +1 -1
- data/examples/helpers/stats/example.rb +4 -4
- data/examples/ips/ips.rb +19 -22
- data/examples/mail/mail.rb +72 -75
- data/examples/mailboxproviders/mailboxproviders.rb +0 -3
- data/examples/mailsettings/mailsettings.rb +21 -24
- data/examples/partnersettings/partnersettings.rb +3 -6
- data/examples/scopes/scopes.rb +7 -9
- data/examples/senderauthentication/senderauthentication.rb +41 -44
- data/examples/senders/senders.rb +28 -31
- data/examples/stats/stats.rb +0 -3
- data/examples/subusers/subusers.rb +17 -20
- data/examples/suppression/suppression.rb +15 -18
- data/examples/templates/templates.rb +29 -31
- data/examples/trackingsettings/trackingsettings.rb +14 -17
- data/examples/user/user.rb +41 -44
- data/lib/rack/sendgrid_webhook_verification.rb +53 -0
- data/lib/sendgrid-ruby.rb +2 -0
- data/lib/sendgrid/base_interface.rb +1 -1
- data/lib/sendgrid/helpers/eventwebhook/eventwebhook.rb +50 -0
- data/lib/sendgrid/helpers/inbound/README.md +5 -5
- data/lib/sendgrid/helpers/inbound/app.rb +1 -1
- data/lib/sendgrid/helpers/inbound/public/index.html +1 -1
- data/lib/sendgrid/helpers/inbound/send.rb +2 -2
- data/lib/sendgrid/helpers/ip_management/ip_management.rb +1 -1
- data/lib/sendgrid/helpers/mail/README.md +3 -3
- data/lib/sendgrid/helpers/mail/asm.rb +6 -18
- data/lib/sendgrid/helpers/mail/attachment.rb +12 -42
- data/lib/sendgrid/helpers/mail/bcc_settings.rb +6 -18
- data/lib/sendgrid/helpers/mail/bypass_list_management.rb +8 -18
- data/lib/sendgrid/helpers/mail/category.rb +2 -2
- data/lib/sendgrid/helpers/mail/click_tracking.rb +6 -18
- data/lib/sendgrid/helpers/mail/content.rb +4 -3
- data/lib/sendgrid/helpers/mail/custom_arg.rb +6 -10
- data/lib/sendgrid/helpers/mail/email.rb +5 -4
- data/lib/sendgrid/helpers/mail/footer.rb +7 -27
- data/lib/sendgrid/helpers/mail/ganalytics.rb +10 -54
- data/lib/sendgrid/helpers/mail/header.rb +6 -10
- data/lib/sendgrid/helpers/mail/mail.rb +30 -48
- data/lib/sendgrid/helpers/mail/mail_settings.rb +9 -25
- data/lib/sendgrid/helpers/mail/open_tracking.rb +6 -18
- data/lib/sendgrid/helpers/mail/personalization.rb +34 -27
- data/lib/sendgrid/helpers/mail/section.rb +6 -10
- data/lib/sendgrid/helpers/mail/spam_check.rb +7 -27
- data/lib/sendgrid/helpers/mail/subscription_tracking.rb +8 -36
- data/lib/sendgrid/helpers/mail/substitution.rb +6 -10
- data/lib/sendgrid/helpers/mail/tracking_settings.rb +7 -20
- data/lib/sendgrid/helpers/permissions/scope.rb +1 -1
- data/lib/sendgrid/helpers/settings/README.md +2 -2
- data/lib/sendgrid/helpers/settings/settings.rb +1 -1
- data/lib/sendgrid/helpers/settings/tracking_settings_dto.rb +3 -5
- data/lib/sendgrid/helpers/stats/metrics.rb +5 -5
- data/lib/sendgrid/sendgrid.rb +1 -1
- data/lib/sendgrid/twilio_email.rb +1 -1
- data/lib/sendgrid/version.rb +1 -1
- data/mail_helper_v3.md +12 -12
- data/sendgrid-ruby.gemspec +8 -7
- data/spec/fixtures/event_webhook.rb +22 -0
- data/spec/rack/sendgrid_webhook_verification_spec.rb +116 -0
- data/spec/sendgrid/helpers/eventwebhook/eventwebhook_spec.rb +105 -0
- data/spec/sendgrid/helpers/settings/mail_settings_dto_spec.rb +1 -1
- data/spec/sendgrid/helpers/settings/partner_settings_dto_spec.rb +1 -1
- data/spec/sendgrid/helpers/settings/settings_spec.rb +2 -2
- data/spec/sendgrid/helpers/settings/tracking_settings_dto_spec.rb +1 -1
- data/spec/sendgrid/helpers/settings/user_settings_dto_spec.rb +1 -1
- data/spec/sendgrid/helpers/stats/email_stats_spec.rb +22 -23
- data/spec/sendgrid/helpers/stats/metrics_spec.rb +19 -20
- data/spec/sendgrid/helpers/stats/stats_response_spec.rb +22 -23
- data/spec/spec_helper.rb +3 -1
- data/static/img/github-fork.png +0 -0
- data/static/img/github-sign-up.png +0 -0
- data/test/sendgrid/helpers/mail/test_attachment.rb +4 -6
- data/test/sendgrid/helpers/mail/test_category.rb +0 -2
- data/test/sendgrid/helpers/mail/test_email.rb +9 -11
- data/test/sendgrid/helpers/mail/test_mail.rb +101 -102
- data/test/sendgrid/helpers/mail/test_personalizations.rb +106 -93
- data/test/sendgrid/permissions/test_scopes.rb +0 -2
- data/test/sendgrid/test_sendgrid-ruby.rb +1948 -1958
- data/twilio_sendgrid_logo.png +0 -0
- data/use-cases/README.md +16 -0
- data/use-cases/domain-authentication.md +5 -0
- data/use-cases/email-statistics.md +52 -0
- data/use-cases/legacy-templates.md +98 -0
- data/use-cases/sms.md +39 -0
- data/use-cases/transactional-templates.md +111 -0
- data/use-cases/twilio-email.md +13 -0
- data/use-cases/twilio-setup.md +54 -0
- metadata +73 -24
- data/USE_CASES.md +0 -405
- data/docker/Dockerfile +0 -12
- data/docker/README.md +0 -30
@@ -0,0 +1,105 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require './spec/fixtures/event_webhook'
|
3
|
+
|
4
|
+
describe SendGrid::EventWebhook do
|
5
|
+
describe '.verify_signature' do
|
6
|
+
it 'verifies a valid signature' do
|
7
|
+
unless skip_jruby
|
8
|
+
expect(verify(
|
9
|
+
Fixtures::EventWebhook::PUBLIC_KEY,
|
10
|
+
Fixtures::EventWebhook::PAYLOAD,
|
11
|
+
Fixtures::EventWebhook::SIGNATURE,
|
12
|
+
Fixtures::EventWebhook::TIMESTAMP
|
13
|
+
)).to be true
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'rejects a bad key' do
|
18
|
+
unless skip_jruby
|
19
|
+
expect(verify(
|
20
|
+
Fixtures::EventWebhook::FAILING_PUBLIC_KEY,
|
21
|
+
Fixtures::EventWebhook::PAYLOAD,
|
22
|
+
Fixtures::EventWebhook::SIGNATURE,
|
23
|
+
Fixtures::EventWebhook::TIMESTAMP
|
24
|
+
)).to be false
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'rejects a bad payload' do
|
29
|
+
unless skip_jruby
|
30
|
+
expect(verify(
|
31
|
+
Fixtures::EventWebhook::PUBLIC_KEY,
|
32
|
+
'payload',
|
33
|
+
Fixtures::EventWebhook::SIGNATURE,
|
34
|
+
Fixtures::EventWebhook::TIMESTAMP
|
35
|
+
)).to be false
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'rejects a bad signature' do
|
40
|
+
unless skip_jruby
|
41
|
+
expect(verify(
|
42
|
+
Fixtures::EventWebhook::PUBLIC_KEY,
|
43
|
+
Fixtures::EventWebhook::PAYLOAD,
|
44
|
+
Fixtures::EventWebhook::FAILING_SIGNATURE,
|
45
|
+
Fixtures::EventWebhook::TIMESTAMP
|
46
|
+
)).to be false
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'rejects a bad timestamp' do
|
51
|
+
unless skip_jruby
|
52
|
+
expect(verify(
|
53
|
+
Fixtures::EventWebhook::PUBLIC_KEY,
|
54
|
+
Fixtures::EventWebhook::PAYLOAD,
|
55
|
+
Fixtures::EventWebhook::SIGNATURE,
|
56
|
+
'timestamp'
|
57
|
+
)).to be false
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'rejects a missing signature' do
|
62
|
+
unless skip_jruby
|
63
|
+
expect(verify(
|
64
|
+
Fixtures::EventWebhook::PUBLIC_KEY,
|
65
|
+
Fixtures::EventWebhook::PAYLOAD,
|
66
|
+
nil,
|
67
|
+
Fixtures::EventWebhook::TIMESTAMP
|
68
|
+
)).to be false
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'throws an error when using jruby' do
|
73
|
+
if skip_jruby
|
74
|
+
expect do
|
75
|
+
verify(
|
76
|
+
Fixtures::EventWebhook::PUBLIC_KEY,
|
77
|
+
Fixtures::EventWebhook::PAYLOAD,
|
78
|
+
Fixtures::EventWebhook::SIGNATURE,
|
79
|
+
Fixtures::EventWebhook::TIMESTAMP
|
80
|
+
)
|
81
|
+
end .to raise_error(SendGrid::EventWebhook::NotSupportedError)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
describe SendGrid::EventWebhookHeader do
|
88
|
+
it 'sets the signature header constant' do
|
89
|
+
expect(SendGrid::EventWebhookHeader::SIGNATURE).to eq("HTTP_X_TWILIO_EMAIL_EVENT_WEBHOOK_SIGNATURE")
|
90
|
+
end
|
91
|
+
|
92
|
+
it 'sets the timestamp header constant' do
|
93
|
+
expect(SendGrid::EventWebhookHeader::TIMESTAMP).to eq("HTTP_X_TWILIO_EMAIL_EVENT_WEBHOOK_TIMESTAMP")
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def verify(public_key, payload, signature, timestamp)
|
98
|
+
ew = SendGrid::EventWebhook.new
|
99
|
+
ec_public_key = ew.convert_public_key_to_ecdsa(public_key)
|
100
|
+
ew.verify_signature(ec_public_key, payload, signature, timestamp)
|
101
|
+
end
|
102
|
+
|
103
|
+
def skip_jruby
|
104
|
+
RUBY_PLATFORM == 'java'
|
105
|
+
end
|
@@ -4,7 +4,7 @@ describe SendGrid::MailSettingsDto do
|
|
4
4
|
let(:sendgrid_client) { SendGrid::API.new(api_key: 'fake_key').client }
|
5
5
|
let(:mail_settings) { SendGrid::MailSettingsDto }
|
6
6
|
let(:setting_name) { 'bcc' }
|
7
|
-
let(:setting_params) { {email: Faker::Internet.email, enabled: rand(1..100).even?} }
|
7
|
+
let(:setting_params) { { email: Faker::Internet.email, enabled: rand(1..100).even? } }
|
8
8
|
|
9
9
|
it { should respond_to :bcc }
|
10
10
|
it { should respond_to :address_whitelist }
|
@@ -4,7 +4,7 @@ describe SendGrid::PartnerSettingsDto do
|
|
4
4
|
let(:sendgrid_client) { SendGrid::API.new(api_key: 'fake_key').client }
|
5
5
|
let(:partner_settings) { SendGrid::PartnerSettingsDto }
|
6
6
|
let(:setting_name) { 'new_relic' }
|
7
|
-
let(:setting_params) { {license_key: 'key', enabled: rand(1..100).even?} }
|
7
|
+
let(:setting_params) { { license_key: 'key', enabled: rand(1..100).even? } }
|
8
8
|
|
9
9
|
it { should respond_to :new_relic }
|
10
10
|
|
@@ -18,8 +18,8 @@ describe SendGrid::Settings do
|
|
18
18
|
|
19
19
|
describe '.update_bcc' do
|
20
20
|
it 'updates bcc' do
|
21
|
-
bcc_response = settings.update_bcc(enabled: true, email:
|
21
|
+
bcc_response = settings.update_bcc(enabled: true, email: 'email@example.com')
|
22
22
|
expect(bcc_response).to be_a SendGrid::Response
|
23
23
|
end
|
24
24
|
end
|
25
|
-
end
|
25
|
+
end
|
@@ -4,7 +4,7 @@ describe SendGrid::TrackingSettingsDto do
|
|
4
4
|
let(:sendgrid_client) { SendGrid::API.new(api_key: 'fake_key').client }
|
5
5
|
let(:tracking_settings) { SendGrid::TrackingSettingsDto }
|
6
6
|
let(:setting_name) { 'open_tracking' }
|
7
|
-
let(:setting_params) { {enabled: rand(1..100).even?} }
|
7
|
+
let(:setting_params) { { enabled: rand(1..100).even? } }
|
8
8
|
|
9
9
|
it { should respond_to :open_tracking }
|
10
10
|
it { should respond_to :click_tracking }
|
@@ -4,7 +4,7 @@ describe SendGrid::UserSettingsDto do
|
|
4
4
|
let(:sendgrid_client) { SendGrid::API.new(api_key: 'fake_key').client }
|
5
5
|
let(:user_settings) { SendGrid::UserSettingsDto }
|
6
6
|
let(:setting_name) { 'enforced_tls' }
|
7
|
-
let(:setting_params) { {require_tls: rand(1..100).even?} }
|
7
|
+
let(:setting_params) { { require_tls: rand(1..100).even? } }
|
8
8
|
|
9
9
|
it { should respond_to :enforced_tls }
|
10
10
|
|
@@ -7,37 +7,36 @@ describe SendGrid::EmailStats do
|
|
7
7
|
|
8
8
|
let(:sample_response) do
|
9
9
|
[{
|
10
|
-
|
11
|
-
|
12
|
-
{
|
10
|
+
'date' => '2017-10-01',
|
11
|
+
'stats' => [
|
12
|
+
{ 'metrics' =>
|
13
13
|
{
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
}
|
31
|
-
}
|
14
|
+
'blocks' => 101,
|
15
|
+
'bounce_drops' => 102,
|
16
|
+
'bounces' => 103,
|
17
|
+
'clicks' => 104,
|
18
|
+
'deferred' => 105,
|
19
|
+
'delivered' => 106,
|
20
|
+
'invalid_emails' => 107,
|
21
|
+
'opens' => 108,
|
22
|
+
'processed' => 109,
|
23
|
+
'requests' => 110,
|
24
|
+
'spam_report_drops' => 111,
|
25
|
+
'spam_reports' => 112,
|
26
|
+
'unique_clicks' => 113,
|
27
|
+
'unique_opens' => 114,
|
28
|
+
'unsubscribe_drops' => 115,
|
29
|
+
'unsubscribes' => 116
|
30
|
+
} }
|
32
31
|
]
|
33
32
|
}]
|
34
33
|
end
|
35
34
|
|
36
35
|
let(:error_response) do
|
37
36
|
{
|
38
|
-
|
37
|
+
'errors' => [
|
39
38
|
{
|
40
|
-
|
39
|
+
'message' => 'end_date should be a YYYY-MM-DD formatted date'
|
41
40
|
}
|
42
41
|
]
|
43
42
|
}
|
@@ -3,23 +3,23 @@ require 'spec_helper'
|
|
3
3
|
describe SendGrid::Metrics do
|
4
4
|
let(:params) do
|
5
5
|
{
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
6
|
+
'date' => '2017-10-01',
|
7
|
+
'blocks' => 101,
|
8
|
+
'bounce_drops' => 102,
|
9
|
+
'bounces' => 103,
|
10
|
+
'clicks' => 104,
|
11
|
+
'deferred' => 105,
|
12
|
+
'delivered' => 106,
|
13
|
+
'invalid_emails' => 107,
|
14
|
+
'opens' => 108,
|
15
|
+
'processed' => 109,
|
16
|
+
'requests' => 110,
|
17
|
+
'spam_report_drops' => 111,
|
18
|
+
'spam_reports' => 112,
|
19
|
+
'unique_clicks' => 113,
|
20
|
+
'unique_opens' => 114,
|
21
|
+
'unsubscribe_drops' => 115,
|
22
|
+
'unsubscribes' => 116
|
23
23
|
}
|
24
24
|
end
|
25
25
|
|
@@ -33,14 +33,13 @@ describe SendGrid::Metrics do
|
|
33
33
|
expect(subject.date).to be_a Date
|
34
34
|
end
|
35
35
|
|
36
|
-
%w
|
36
|
+
%w[
|
37
37
|
blocks bounce_drops bounces clicks deferred delivered invalid_emails
|
38
38
|
opens processed requests spam_report_drops spam_reports unique_clicks
|
39
39
|
unique_opens unsubscribe_drops unsubscribes
|
40
|
-
|
40
|
+
].each do |attribute|
|
41
41
|
it "responds to #{attribute}" do
|
42
42
|
expect(subject).to respond_to(attribute.to_sym)
|
43
43
|
end
|
44
44
|
end
|
45
|
-
|
46
45
|
end
|
@@ -3,28 +3,27 @@ require 'spec_helper'
|
|
3
3
|
describe SendGrid::StatsResponse do
|
4
4
|
let(:params) do
|
5
5
|
[{
|
6
|
-
|
7
|
-
|
8
|
-
{
|
6
|
+
'date' => '2017-10-01',
|
7
|
+
'stats' => [
|
8
|
+
{ 'metrics' =>
|
9
9
|
{
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
}
|
27
|
-
}
|
10
|
+
'blocks' => 101,
|
11
|
+
'bounce_drops' => 102,
|
12
|
+
'bounces' => 103,
|
13
|
+
'clicks' => 104,
|
14
|
+
'deferred' => 105,
|
15
|
+
'delivered' => 106,
|
16
|
+
'invalid_emails' => 107,
|
17
|
+
'opens' => 108,
|
18
|
+
'processed' => 109,
|
19
|
+
'requests' => 110,
|
20
|
+
'spam_report_drops' => 111,
|
21
|
+
'spam_reports' => 112,
|
22
|
+
'unique_clicks' => 113,
|
23
|
+
'unique_opens' => 114,
|
24
|
+
'unsubscribe_drops' => 115,
|
25
|
+
'unsubscribes' => 116
|
26
|
+
} }
|
28
27
|
]
|
29
28
|
}]
|
30
29
|
end
|
@@ -51,9 +50,9 @@ describe SendGrid::StatsResponse do
|
|
51
50
|
context 'errors' do
|
52
51
|
let(:error_params) do
|
53
52
|
{
|
54
|
-
|
53
|
+
'errors' => [
|
55
54
|
{
|
56
|
-
|
55
|
+
'message' => 'end_date should be a YYYY-MM-DD formatted date'
|
57
56
|
}
|
58
57
|
]
|
59
58
|
}
|
data/spec/spec_helper.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'simplecov'
|
2
|
+
SimpleCov.start
|
1
3
|
require 'rubygems'
|
2
4
|
require 'bundler/setup'
|
3
5
|
require 'pry'
|
@@ -7,4 +9,4 @@ RSpec.configure do |config|
|
|
7
9
|
Dir["#{File.dirname(__FILE__)}/../lib/sendgrid-ruby.rb"].sort.each { |ext| require ext }
|
8
10
|
|
9
11
|
config.color = true
|
10
|
-
end
|
12
|
+
end
|
Binary file
|
Binary file
|
@@ -1,19 +1,17 @@
|
|
1
|
-
# coding: utf-8
|
2
1
|
require_relative "../../../../lib/sendgrid/helpers/mail/attachment"
|
3
2
|
include SendGrid
|
4
3
|
require "json"
|
5
4
|
require "minitest/autorun"
|
6
5
|
|
7
6
|
class TestAttachment < Minitest::Test
|
8
|
-
SAMPLE_INPUT = "
|
7
|
+
SAMPLE_INPUT = "Es blüht so grün wie Blüten blüh'n im Frühling
|
9
8
|
Es blüht so grün wie Blüten blüh'n im Frühling
|
10
9
|
Es blüht so grün wie Blüten blüh'n im Frühling
|
11
10
|
Es blüht so grün wie Blüten blüh'n im Frühling
|
12
11
|
Es blüht so grün wie Blüten blüh'n im Frühling
|
13
|
-
"
|
14
|
-
|
15
|
-
def setup
|
16
|
-
end
|
12
|
+
".force_encoding('UTF-8').encode
|
13
|
+
|
14
|
+
def setup; end
|
17
15
|
|
18
16
|
def test_io_enocding
|
19
17
|
attachment = Attachment.new
|
@@ -2,7 +2,6 @@ require_relative '../../../../lib/sendgrid/helpers/mail/mail'
|
|
2
2
|
require 'minitest/autorun'
|
3
3
|
|
4
4
|
class TestCategory < Minitest::Test
|
5
|
-
|
6
5
|
include SendGrid
|
7
6
|
|
8
7
|
def setup
|
@@ -23,5 +22,4 @@ class TestCategory < Minitest::Test
|
|
23
22
|
}
|
24
23
|
assert_equal @category.to_json, expected_json
|
25
24
|
end
|
26
|
-
|
27
25
|
end
|
@@ -2,33 +2,31 @@ require_relative '../../../../lib/sendgrid/helpers/mail/email'
|
|
2
2
|
require 'minitest/autorun'
|
3
3
|
|
4
4
|
class TestEmail < Minitest::Test
|
5
|
-
|
6
5
|
include SendGrid
|
7
6
|
|
8
7
|
def test_split_email_full_email
|
9
|
-
@email = Email.new(email:
|
8
|
+
@email = Email.new(email: 'Example User <test1@example.com>')
|
10
9
|
expected_json = {
|
11
|
-
|
12
|
-
|
10
|
+
'email' => 'test1@example.com',
|
11
|
+
'name' => 'Example User'
|
13
12
|
}
|
14
13
|
assert_equal @email.to_json, expected_json
|
15
14
|
end
|
16
15
|
|
17
16
|
def test_split_email_only_email
|
18
|
-
@email = Email.new(email:
|
17
|
+
@email = Email.new(email: 'test1@example.com')
|
19
18
|
expected_json = {
|
20
|
-
|
19
|
+
'email' => 'test1@example.com'
|
21
20
|
}
|
22
21
|
assert_equal @email.to_json, expected_json
|
23
22
|
end
|
24
23
|
|
25
24
|
def test_split_email_name_and_email
|
26
|
-
@email = Email.new(name:
|
25
|
+
@email = Email.new(name: 'Example User', email: 'test1@example.com')
|
27
26
|
expected_json = {
|
28
|
-
|
29
|
-
|
27
|
+
'email' => 'test1@example.com',
|
28
|
+
'name' => 'Example User'
|
30
29
|
}
|
31
30
|
assert_equal @email.to_json, expected_json
|
32
31
|
end
|
33
|
-
|
34
|
-
end
|
32
|
+
end
|
@@ -1,12 +1,11 @@
|
|
1
|
-
require_relative
|
2
|
-
require_relative
|
1
|
+
require_relative '../../../../lib/sendgrid/helpers/mail/mail'
|
2
|
+
require_relative '../../../../lib/sendgrid/sendgrid'
|
3
3
|
include SendGrid
|
4
|
-
require
|
4
|
+
require 'json'
|
5
5
|
require 'minitest/autorun'
|
6
6
|
|
7
7
|
class TestMail < Minitest::Test
|
8
|
-
def setup
|
9
|
-
end
|
8
|
+
def setup; end
|
10
9
|
|
11
10
|
def test_hello_world
|
12
11
|
from = Email.new(email: 'test@example.com')
|
@@ -20,102 +19,104 @@ class TestMail < Minitest::Test
|
|
20
19
|
|
21
20
|
def test_kitchen_sink
|
22
21
|
mail = SendGrid::Mail.new
|
23
|
-
mail.from = Email.new(email:
|
24
|
-
mail.subject =
|
22
|
+
mail.from = Email.new(email: 'test@example.com')
|
23
|
+
mail.subject = 'Hello World from the Twilio SendGrid Ruby Library'
|
25
24
|
personalization = Personalization.new
|
26
|
-
personalization.add_to(Email.new(email:
|
27
|
-
personalization.add_to(Email.new(email:
|
28
|
-
personalization.add_cc(Email.new(email:
|
29
|
-
personalization.add_cc(Email.new(email:
|
30
|
-
personalization.add_bcc(Email.new(email:
|
31
|
-
personalization.add_bcc(Email.new(email:
|
32
|
-
personalization.subject =
|
33
|
-
personalization.add_header(Header.new(key:
|
34
|
-
personalization.add_header(Header.new(key:
|
35
|
-
personalization.add_substitution(Substitution.new(key:
|
36
|
-
personalization.add_substitution(Substitution.new(key:
|
37
|
-
personalization.add_custom_arg(CustomArg.new(key:
|
38
|
-
personalization.add_custom_arg(CustomArg.new(key:
|
39
|
-
personalization.send_at =
|
25
|
+
personalization.add_to(Email.new(email: 'test1@example.com', name: 'Example User 1'))
|
26
|
+
personalization.add_to(Email.new(email: 'test2@example.com', name: 'Example User 2'))
|
27
|
+
personalization.add_cc(Email.new(email: 'test3@example.com', name: 'Example User 3'))
|
28
|
+
personalization.add_cc(Email.new(email: 'test4@example.com', name: 'Example User 4'))
|
29
|
+
personalization.add_bcc(Email.new(email: 'test5@example.com', name: 'Example User 5'))
|
30
|
+
personalization.add_bcc(Email.new(email: 'test6@example.com', name: 'Example User 6'))
|
31
|
+
personalization.subject = 'Hello World from the Personalized Twilio SendGrid Ruby Library'
|
32
|
+
personalization.add_header(Header.new(key: 'X-Test', value: 'True'))
|
33
|
+
personalization.add_header(Header.new(key: 'X-Mock', value: 'False'))
|
34
|
+
personalization.add_substitution(Substitution.new(key: '%name%', value: 'Example User'))
|
35
|
+
personalization.add_substitution(Substitution.new(key: '%city%', value: 'Denver'))
|
36
|
+
personalization.add_custom_arg(CustomArg.new(key: 'user_id', value: '343'))
|
37
|
+
personalization.add_custom_arg(CustomArg.new(key: 'type', value: 'marketing'))
|
38
|
+
personalization.send_at = 1_443_636_843
|
40
39
|
mail.add_personalization(personalization)
|
41
40
|
|
42
41
|
personalization2 = Personalization.new
|
43
|
-
personalization2.add_to(Email.new(email:
|
44
|
-
personalization2.add_to(Email.new(email:
|
45
|
-
personalization2.add_cc(Email.new(email:
|
46
|
-
personalization2.add_cc(Email.new(email:
|
47
|
-
personalization2.add_bcc(Email.new(email:
|
48
|
-
personalization2.add_bcc(Email.new(email:
|
49
|
-
personalization2.subject =
|
50
|
-
personalization2.add_header(Header.new(key:
|
51
|
-
personalization2.add_header(Header.new(key:
|
52
|
-
personalization2.add_substitution(Substitution.new(key:
|
53
|
-
personalization2.add_substitution(Substitution.new(key:
|
54
|
-
personalization2.add_custom_arg(CustomArg.new(key:
|
55
|
-
personalization2.add_custom_arg(CustomArg.new(key:
|
56
|
-
personalization2.send_at =
|
42
|
+
personalization2.add_to(Email.new(email: 'test1@example.com', name: 'Example User 1'))
|
43
|
+
personalization2.add_to(Email.new(email: 'test2@example.com', name: 'Example User 2'))
|
44
|
+
personalization2.add_cc(Email.new(email: 'test3@example.com', name: 'Example User 3'))
|
45
|
+
personalization2.add_cc(Email.new(email: 'test4@example.com', name: 'Example User 4'))
|
46
|
+
personalization2.add_bcc(Email.new(email: 'test5@example.com', name: 'Example User 5'))
|
47
|
+
personalization2.add_bcc(Email.new(email: 'test6@example.com', name: 'Example User 6'))
|
48
|
+
personalization2.subject = 'Hello World from the Personalized Twilio SendGrid Ruby Library'
|
49
|
+
personalization2.add_header(Header.new(key: 'X-Test', value: 'True'))
|
50
|
+
personalization2.add_header(Header.new(key: 'X-Mock', value: 'False'))
|
51
|
+
personalization2.add_substitution(Substitution.new(key: '%name%', value: 'Example User'))
|
52
|
+
personalization2.add_substitution(Substitution.new(key: '%city%', value: 'Denver'))
|
53
|
+
personalization2.add_custom_arg(CustomArg.new(key: 'user_id', value: '343'))
|
54
|
+
personalization2.add_custom_arg(CustomArg.new(key: 'type', value: 'marketing'))
|
55
|
+
personalization2.send_at = 1_443_636_843
|
57
56
|
mail.add_personalization(personalization2)
|
58
57
|
|
59
|
-
mail.add_content(Content.new(type:
|
60
|
-
mail.add_content(Content.new(type:
|
58
|
+
mail.add_content(Content.new(type: 'text/plain', value: 'some text here'))
|
59
|
+
mail.add_content(Content.new(type: 'text/html', value: '<html><body>some text here</body></html>'))
|
61
60
|
|
62
61
|
attachment = Attachment.new
|
63
|
-
attachment.content =
|
64
|
-
attachment.type =
|
65
|
-
attachment.filename =
|
66
|
-
attachment.disposition =
|
67
|
-
attachment.content_id =
|
62
|
+
attachment.content = 'TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12'
|
63
|
+
attachment.type = 'application/pdf'
|
64
|
+
attachment.filename = 'balance_001.pdf'
|
65
|
+
attachment.disposition = 'attachment'
|
66
|
+
attachment.content_id = 'Balance Sheet'
|
68
67
|
|
69
68
|
mail.add_attachment(attachment)
|
70
69
|
|
71
70
|
attachment2 = Attachment.new
|
72
|
-
attachment2.content =
|
73
|
-
attachment2.type =
|
74
|
-
attachment2.filename =
|
75
|
-
attachment2.disposition =
|
76
|
-
attachment2.content_id =
|
71
|
+
attachment2.content = 'BwdW'
|
72
|
+
attachment2.type = 'image/png'
|
73
|
+
attachment2.filename = 'banner.png'
|
74
|
+
attachment2.disposition = 'inline'
|
75
|
+
attachment2.content_id = 'Banner'
|
77
76
|
mail.add_attachment(attachment2)
|
78
77
|
|
79
|
-
mail.template_id =
|
78
|
+
mail.template_id = '13b8f94f-bcae-4ec6-b752-70d6cb59f932'
|
80
79
|
|
81
|
-
mail.add_section(Section.new(key:
|
82
|
-
mail.add_section(Section.new(key:
|
80
|
+
mail.add_section(Section.new(key: '%section1%', value: 'Substitution Text for Section 1'))
|
81
|
+
mail.add_section(Section.new(key: '%section2%', value: 'Substitution Text for Section 2'))
|
83
82
|
|
84
|
-
mail.add_header(Header.new(key:
|
85
|
-
mail.add_header(Header.new(key:
|
83
|
+
mail.add_header(Header.new(key: 'X-Test3', value: 'test3'))
|
84
|
+
mail.add_header(Header.new(key: 'X-Test4', value: 'test4'))
|
86
85
|
|
87
|
-
mail.add_category(Category.new(name:
|
88
|
-
mail.add_category(Category.new(name:
|
86
|
+
mail.add_category(Category.new(name: 'May'))
|
87
|
+
mail.add_category(Category.new(name: '2016'))
|
89
88
|
|
90
|
-
mail.add_custom_arg(CustomArg.new(key:
|
91
|
-
mail.add_custom_arg(CustomArg.new(key:
|
89
|
+
mail.add_custom_arg(CustomArg.new(key: 'campaign', value: 'welcome'))
|
90
|
+
mail.add_custom_arg(CustomArg.new(key: 'weekday', value: 'morning'))
|
92
91
|
|
93
|
-
mail.send_at =
|
92
|
+
mail.send_at = 1_443_636_842
|
94
93
|
|
95
|
-
mail.batch_id =
|
94
|
+
mail.batch_id = 'sendgrid_batch_id'
|
96
95
|
|
97
|
-
mail.asm = ASM.new(group_id: 99, groups_to_display: [4,5,6,7,8])
|
96
|
+
mail.asm = ASM.new(group_id: 99, groups_to_display: [4, 5, 6, 7, 8])
|
98
97
|
|
99
|
-
mail.ip_pool_name =
|
98
|
+
mail.ip_pool_name = '23'
|
100
99
|
|
101
100
|
mail_settings = MailSettings.new
|
102
|
-
mail_settings.bcc = BccSettings.new(enable: true, email:
|
101
|
+
mail_settings.bcc = BccSettings.new(enable: true, email: 'test@example.com')
|
103
102
|
mail_settings.bypass_list_management = BypassListManagement.new(enable: true)
|
104
|
-
mail_settings.footer = Footer.new(enable: true, text:
|
103
|
+
mail_settings.footer = Footer.new(enable: true, text: 'Footer Text', html: '<html><body>Footer Text</body></html>')
|
105
104
|
mail_settings.sandbox_mode = SandBoxMode.new(enable: true)
|
106
|
-
mail_settings.spam_check = SpamCheck.new(enable: true, threshold: 1, post_to_url:
|
105
|
+
mail_settings.spam_check = SpamCheck.new(enable: true, threshold: 1, post_to_url: 'https://spamcatcher.sendgrid.com')
|
107
106
|
mail.mail_settings = mail_settings
|
108
107
|
|
109
108
|
tracking_settings = TrackingSettings.new
|
110
109
|
tracking_settings.click_tracking = ClickTracking.new(enable: false, enable_text: false)
|
111
|
-
tracking_settings.open_tracking = OpenTracking.new(enable: true, substitution_tag:
|
112
|
-
tracking_settings.subscription_tracking = SubscriptionTracking.new(enable: true, text:
|
113
|
-
tracking_settings.ganalytics = Ganalytics.new(enable: true, utm_source:
|
110
|
+
tracking_settings.open_tracking = OpenTracking.new(enable: true, substitution_tag: 'Optional tag to replace with the open image in the body of the message')
|
111
|
+
tracking_settings.subscription_tracking = SubscriptionTracking.new(enable: true, text: 'text to insert into the text/plain portion of the message', 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')
|
112
|
+
tracking_settings.ganalytics = Ganalytics.new(enable: true, utm_source: 'some source', utm_medium: 'some medium', utm_term: 'some term', utm_content: 'some content', utm_campaign: 'some campaign')
|
114
113
|
mail.tracking_settings = tracking_settings
|
115
114
|
|
116
|
-
mail.reply_to = Email.new(email:
|
115
|
+
mail.reply_to = Email.new(email: 'test@example.com')
|
117
116
|
|
118
|
-
|
117
|
+
# rubocop:disable Layout/LineLength
|
118
|
+
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":"test@example.com"},"headers":{"X-Test3":"test3","X-Test4":"test4"},"ip_pool_name":"23","mail_settings":{"bcc":{"email":"test@example.com","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":"test5@example.com","name":"Example User 5"},{"email":"test6@example.com","name":"Example User 6"}],"cc":[{"email":"test3@example.com","name":"Example User 3"},{"email":"test4@example.com","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":"test1@example.com","name":"Example User 1"},{"email":"test2@example.com","name":"Example User 2"}]},{"bcc":[{"email":"test5@example.com","name":"Example User 5"},{"email":"test6@example.com","name":"Example User 6"}],"cc":[{"email":"test3@example.com","name":"Example User 3"},{"email":"test4@example.com","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":"test1@example.com","name":"Example User 1"},{"email":"test2@example.com","name":"Example User 2"}]}],"reply_to":{"email":"test@example.com"},"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"}}}'))
|
119
|
+
# rubocop:enable all
|
119
120
|
end
|
120
121
|
|
121
122
|
def test_that_personalizations_is_empty_initially
|
@@ -154,17 +155,17 @@ class TestMail < Minitest::Test
|
|
154
155
|
mail = SendGrid::Mail.new
|
155
156
|
mail.add_section(Section.new(key: '%section1%', value: 'Substitution Text for Section 1'))
|
156
157
|
expected_json = {
|
157
|
-
|
158
|
-
|
159
|
-
|
158
|
+
'sections' => {
|
159
|
+
'%section1%' => 'Substitution Text for Section 1'
|
160
|
+
}
|
160
161
|
}
|
161
162
|
assert_equal mail.to_json, expected_json
|
162
163
|
mail.add_section(Section.new(key: '%section2%', value: 'Substitution Text for Section 2'))
|
163
164
|
expected_json = {
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
165
|
+
'sections' => {
|
166
|
+
'%section1%' => 'Substitution Text for Section 1',
|
167
|
+
'%section2%' => 'Substitution Text for Section 2'
|
168
|
+
}
|
168
169
|
}
|
169
170
|
assert_equal mail.to_json, expected_json
|
170
171
|
end
|
@@ -173,17 +174,17 @@ class TestMail < Minitest::Test
|
|
173
174
|
mail = SendGrid::Mail.new
|
174
175
|
mail.add_header(Header.new(key: 'X-Test3', value: 'test3'))
|
175
176
|
expected_json = {
|
176
|
-
|
177
|
-
|
178
|
-
|
177
|
+
'headers' => {
|
178
|
+
'X-Test3' => 'test3'
|
179
|
+
}
|
179
180
|
}
|
180
181
|
assert_equal mail.to_json, expected_json
|
181
182
|
mail.add_header(Header.new(key: 'X-Test4', value: 'test4'))
|
182
183
|
expected_json = {
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
184
|
+
'headers' => {
|
185
|
+
'X-Test3' => 'test3',
|
186
|
+
'X-Test4' => 'test4'
|
187
|
+
}
|
187
188
|
}
|
188
189
|
assert_equal mail.to_json, expected_json
|
189
190
|
end
|
@@ -192,32 +193,32 @@ class TestMail < Minitest::Test
|
|
192
193
|
mail = SendGrid::Mail.new
|
193
194
|
mail.add_custom_arg(CustomArg.new(key: 'campaign 1', value: 'welcome 1'))
|
194
195
|
expected_json = {
|
195
|
-
|
196
|
-
|
197
|
-
|
196
|
+
'custom_args' => {
|
197
|
+
'campaign 1' => 'welcome 1'
|
198
|
+
}
|
198
199
|
}
|
199
200
|
assert_equal mail.to_json, expected_json
|
200
201
|
mail.add_custom_arg(CustomArg.new(key: 'campaign 2', value: 'welcome 2'))
|
201
202
|
expected_json = {
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
203
|
+
'custom_args' => {
|
204
|
+
'campaign 1' => 'welcome 1',
|
205
|
+
'campaign 2' => 'welcome 2'
|
206
|
+
}
|
206
207
|
}
|
207
208
|
assert_equal mail.to_json, expected_json
|
208
209
|
end
|
209
210
|
|
210
211
|
def test_add_non_string_custom_arg
|
211
212
|
mail = Mail.new
|
212
|
-
mail.add_custom_arg(CustomArg.new(key:
|
213
|
-
mail.add_custom_arg(CustomArg.new(key:
|
214
|
-
mail.add_custom_arg(CustomArg.new(key:
|
213
|
+
mail.add_custom_arg(CustomArg.new(key: 'Integer', value: 1))
|
214
|
+
mail.add_custom_arg(CustomArg.new(key: 'Array', value: [1, 'a', true]))
|
215
|
+
mail.add_custom_arg(CustomArg.new(key: 'Hash', value: { 'a' => 1, 'b' => 2 }))
|
215
216
|
expected_json = {
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
217
|
+
'custom_args' => {
|
218
|
+
'Integer' => '1',
|
219
|
+
'Array' => '[1, "a", true]',
|
220
|
+
'Hash' => '{"a"=>1, "b"=>2}'
|
221
|
+
}
|
221
222
|
}
|
222
223
|
assert_equal mail.to_json, expected_json
|
223
224
|
end
|
@@ -237,11 +238,9 @@ class TestMail < Minitest::Test
|
|
237
238
|
|
238
239
|
def test_add_more_than_1_valid_category
|
239
240
|
mail = SendGrid::Mail.new
|
240
|
-
|
241
|
-
|
242
|
-
mail.
|
243
|
-
mail.add_category(category_2)
|
244
|
-
assert_equal(['foo', 'bar'], mail.categories)
|
241
|
+
mail.add_category(Category.new(name: 'foo'))
|
242
|
+
mail.add_category(Category.new(name: 'bar'))
|
243
|
+
assert_equal(%w[foo bar], mail.categories)
|
245
244
|
end
|
246
245
|
|
247
246
|
def test_add_invalid_category
|