send_grid_mailer 1.1.0 → 2.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.circleci/config.yml +102 -0
- data/.circleci/setup-rubygems.sh +3 -0
- data/.rubocop.yml +31 -591
- data/.ruby-version +1 -1
- data/CHANGELOG.md +28 -0
- data/README.md +46 -2
- data/lib/send_grid_mailer/api.rb +14 -9
- data/lib/send_grid_mailer/definition.rb +32 -14
- data/lib/send_grid_mailer/dev_deliverer.rb +72 -0
- data/lib/send_grid_mailer/engine.rb +4 -2
- data/lib/send_grid_mailer/logger.rb +10 -5
- data/lib/send_grid_mailer/mailer_base_ext.rb +13 -2
- data/lib/send_grid_mailer/version.rb +1 -1
- data/send_grid_mailer.gemspec +12 -6
- data/spec/dummy/Rakefile +1 -1
- data/spec/dummy/app/assets/config/manifest.js +3 -0
- data/spec/dummy/app/assets/stylesheets/application.css +3 -3
- data/spec/dummy/app/channels/application_cable/channel.rb +4 -0
- data/spec/dummy/app/channels/application_cable/connection.rb +4 -0
- data/spec/dummy/app/controllers/application_controller.rb +0 -3
- data/spec/dummy/app/{assets/javascripts → javascript/packs}/application.js +3 -1
- data/spec/dummy/app/jobs/application_job.rb +7 -0
- data/spec/dummy/app/mailers/application_mailer.rb +1 -1
- data/spec/dummy/app/mailers/test_mailer.rb +12 -0
- data/spec/dummy/app/models/application_record.rb +3 -0
- data/spec/dummy/app/views/layouts/application.html.erb +10 -9
- data/spec/dummy/bin/rails +3 -3
- data/spec/dummy/bin/rake +2 -2
- data/spec/dummy/bin/setup +18 -14
- data/spec/dummy/config/application.rb +12 -22
- data/spec/dummy/config/boot.rb +3 -3
- data/spec/dummy/config/cable.yml +10 -0
- data/spec/dummy/config/database.yml +2 -2
- data/spec/dummy/config/environment.rb +1 -1
- data/spec/dummy/config/environments/development.rb +48 -18
- data/spec/dummy/config/environments/production.rb +63 -22
- data/spec/dummy/config/environments/test.rb +29 -12
- data/spec/dummy/config/initializers/application_controller_renderer.rb +8 -0
- data/spec/dummy/config/initializers/assets.rb +4 -3
- data/spec/dummy/config/initializers/backtrace_silencers.rb +4 -3
- data/spec/dummy/config/initializers/content_security_policy.rb +28 -0
- data/spec/dummy/config/initializers/cookies_serializer.rb +2 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +3 -1
- data/spec/dummy/config/initializers/permissions_policy.rb +11 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +2 -2
- data/spec/dummy/config/locales/en.yml +11 -1
- data/spec/dummy/config/puma.rb +43 -0
- data/spec/dummy/config/routes.rb +1 -54
- data/spec/dummy/config/storage.yml +34 -0
- data/spec/dummy/config.ru +3 -1
- data/spec/dummy/public/404.html +6 -6
- data/spec/dummy/public/422.html +6 -6
- data/spec/dummy/public/500.html +6 -6
- data/spec/dummy/public/apple-touch-icon-precomposed.png +0 -0
- data/spec/dummy/public/apple-touch-icon.png +0 -0
- data/spec/dummy/spec/lib/send_grid_mailer/definition_spec.rb +52 -23
- data/spec/dummy/spec/mailers/test_mailer_spec.rb +547 -384
- data/spec/rails_helper.rb +5 -7
- metadata +124 -38
- data/.hound.yml +0 -4
- data/.travis.yml +0 -15
- data/spec/dummy/bin/bundle +0 -3
- data/spec/dummy/config/initializers/session_store.rb +0 -3
- data/spec/dummy/config/secrets.yml +0 -22
- data/spec/dummy/db/schema.rb +0 -16
- data/spec/dummy/spec/support/test_helpers.rb +0 -5
@@ -1,24 +1,30 @@
|
|
1
1
|
require "rails_helper"
|
2
2
|
|
3
3
|
describe TestMailer do
|
4
|
-
|
5
|
-
|
6
|
-
deliver
|
7
|
-
end
|
4
|
+
let(:error_code) { "404" }
|
5
|
+
let(:success_code) { "200" }
|
8
6
|
|
9
|
-
def
|
10
|
-
error_code = "404"
|
11
|
-
result = { errors: errors }.to_json
|
12
|
-
expect_sg_api_request(error_code, request_body, result)
|
7
|
+
def expect_sg_api_errors(errors)
|
13
8
|
error_messages = errors.map { |err| err['message'] }.join('. ')
|
14
9
|
message = "Sendgrid API error. Code: #{error_code}. Errors: #{error_messages}"
|
15
10
|
expect { deliver }.to raise_error(SendGridMailer::ApiError, message) do |e|
|
16
|
-
expect(e.error_code).to eq(error_code)
|
11
|
+
expect(e.error_code).to eq(error_code.to_i)
|
17
12
|
expect(e.errors).to eq(errors)
|
18
13
|
end
|
19
14
|
end
|
20
15
|
|
21
|
-
def
|
16
|
+
def expect_valid_sg_api_send_mail_request(request_body)
|
17
|
+
expect_sg_api_send_mail_request(success_code, request_body)
|
18
|
+
deliver
|
19
|
+
end
|
20
|
+
|
21
|
+
def expect_invalid_sg_api_send_mail_request(request_body, errors)
|
22
|
+
result = { errors: errors }.to_json
|
23
|
+
expect_sg_api_send_mail_request(error_code, request_body, result)
|
24
|
+
expect_sg_api_errors(errors)
|
25
|
+
end
|
26
|
+
|
27
|
+
def expect_sg_api_send_mail_request(status_code, request_body, result = nil)
|
22
28
|
result = double(status_code: status_code, body: result)
|
23
29
|
client2 = double(post: result)
|
24
30
|
client1 = double(_: client2)
|
@@ -26,466 +32,623 @@ describe TestMailer do
|
|
26
32
|
expect(client2).to receive(:post).with(request_body: request_body).and_return(result)
|
27
33
|
end
|
28
34
|
|
29
|
-
|
35
|
+
def expect_valid_sg_api_get_template_request(response)
|
36
|
+
expect_sg_api_get_template_request(success_code, response)
|
37
|
+
deliver
|
38
|
+
end
|
30
39
|
|
31
|
-
|
32
|
-
|
40
|
+
def expect_invalid_sg_api_get_template_request(errors)
|
41
|
+
result = { errors: errors }.to_json
|
42
|
+
expect_sg_api_get_template_request(error_code, result)
|
43
|
+
expect_sg_api_errors(errors)
|
44
|
+
end
|
33
45
|
|
34
|
-
|
35
|
-
|
46
|
+
def expect_sg_api_get_template_request(status_code, result = nil)
|
47
|
+
result = double(status_code: status_code, body: result)
|
48
|
+
client2 = double(post: result)
|
49
|
+
client1 = double(_: client2)
|
50
|
+
expect_any_instance_of(SendGrid::Client).to receive(:_).with(:templates).and_return(client1)
|
51
|
+
expect(client2).to receive(:get).and_return(result)
|
52
|
+
end
|
53
|
+
|
54
|
+
context "when setting delivery_method to :sendgrid" do
|
55
|
+
before { allow(TestMailer).to receive(:delivery_method).and_return(:sendgrid) }
|
56
|
+
|
57
|
+
context "with valid API key" do
|
58
|
+
before { allow_any_instance_of(SendGridMailer::Deliverer).to receive(:api_key).and_return("X") }
|
59
|
+
|
60
|
+
context "with unsuccessful response" do
|
61
|
+
let(:deliver) { described_class.body_email.deliver_now! }
|
62
|
+
|
63
|
+
it "doesn't send mail" do
|
64
|
+
request_body = {
|
65
|
+
"from" =>
|
66
|
+
{
|
67
|
+
"email" => "default-sender@platan.us"
|
68
|
+
},
|
69
|
+
"personalizations" => [
|
70
|
+
{
|
71
|
+
"subject" => "Body email"
|
72
|
+
}
|
73
|
+
],
|
74
|
+
"content" => [
|
75
|
+
{
|
76
|
+
"type" => "text/plain",
|
77
|
+
"value" => "Body"
|
78
|
+
}
|
79
|
+
]
|
80
|
+
}
|
36
81
|
|
37
|
-
|
38
|
-
request_body = {
|
39
|
-
"from" =>
|
82
|
+
errors = [
|
40
83
|
{
|
41
|
-
|
84
|
+
'field' => 'from.email',
|
85
|
+
'message' => 'The from email does not...'
|
42
86
|
},
|
43
|
-
"personalizations" => [
|
44
|
-
{
|
45
|
-
"subject" => "Body email"
|
46
|
-
}
|
47
|
-
],
|
48
|
-
"content" => [
|
49
87
|
{
|
50
|
-
|
51
|
-
|
88
|
+
'field' => 'personalizations.0.to',
|
89
|
+
'message' => 'The to array is required...'
|
52
90
|
}
|
53
91
|
]
|
54
|
-
}
|
55
92
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
93
|
+
expect_invalid_sg_api_send_mail_request(request_body, errors)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
context "when setting body" do
|
98
|
+
let(:deliver) { described_class.body_email.deliver_now! }
|
99
|
+
|
100
|
+
it "sends mail with valid body" do
|
101
|
+
request_body = {
|
102
|
+
"from" =>
|
103
|
+
{
|
104
|
+
"email" => "default-sender@platan.us"
|
105
|
+
},
|
106
|
+
"personalizations" => [
|
107
|
+
{
|
108
|
+
"subject" => "Body email"
|
109
|
+
}
|
110
|
+
],
|
111
|
+
"content" => [
|
112
|
+
{
|
113
|
+
"type" => "text/plain",
|
114
|
+
"value" => "Body"
|
115
|
+
}
|
116
|
+
]
|
64
117
|
}
|
65
|
-
]
|
66
118
|
|
67
|
-
|
119
|
+
expect_valid_sg_api_send_mail_request(request_body)
|
120
|
+
end
|
68
121
|
end
|
69
|
-
end
|
70
|
-
|
71
|
-
context "setting body" do
|
72
|
-
let(:deliver) { described_class.body_email.deliver_now! }
|
73
122
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
123
|
+
context "when setting body from params" do
|
124
|
+
let(:deliver) { described_class.body_params_email.deliver_now! }
|
125
|
+
|
126
|
+
it "sends mail with valid body" do
|
127
|
+
request_body = {
|
128
|
+
"from" =>
|
129
|
+
{
|
130
|
+
"email" => "default-sender@platan.us"
|
131
|
+
},
|
132
|
+
"personalizations" => [
|
133
|
+
{
|
134
|
+
"subject" => "Body params email"
|
135
|
+
}
|
136
|
+
],
|
137
|
+
"content" => [
|
138
|
+
{
|
139
|
+
"type" => "text/html",
|
140
|
+
"value" => "<h1>Body Params</h1>"
|
141
|
+
}
|
142
|
+
]
|
143
|
+
}
|
92
144
|
|
93
|
-
|
145
|
+
expect_valid_sg_api_send_mail_request(request_body)
|
146
|
+
end
|
94
147
|
end
|
95
|
-
end
|
96
|
-
|
97
|
-
context "setting body from params" do
|
98
|
-
let(:deliver) { described_class.body_params_email.deliver_now! }
|
99
148
|
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
149
|
+
context "when setting body from rails template" do
|
150
|
+
let(:deliver) { described_class.rails_tpl_email.deliver_now! }
|
151
|
+
|
152
|
+
it "sends mail with valid body" do
|
153
|
+
request_body = {
|
154
|
+
"from" =>
|
155
|
+
{
|
156
|
+
"email" => "default-sender@platan.us"
|
157
|
+
},
|
158
|
+
"personalizations" => [
|
159
|
+
{
|
160
|
+
"subject" => "Rails tpl email"
|
161
|
+
}
|
162
|
+
],
|
163
|
+
"content" => [
|
164
|
+
{
|
165
|
+
"type" => "text/html",
|
166
|
+
"value" => "<html>\n <body>\n Rails Template!\n\n </body>\n</html>\n"
|
167
|
+
}
|
168
|
+
]
|
169
|
+
}
|
118
170
|
|
119
|
-
|
171
|
+
expect_valid_sg_api_send_mail_request(request_body)
|
172
|
+
end
|
120
173
|
end
|
121
|
-
end
|
122
174
|
|
123
|
-
|
124
|
-
|
175
|
+
context "when overriding default from" do
|
176
|
+
let(:request_body) do
|
177
|
+
{
|
178
|
+
"from" =>
|
179
|
+
{
|
180
|
+
"email" => "override@platan.us"
|
181
|
+
},
|
182
|
+
"personalizations" => [
|
183
|
+
{
|
184
|
+
"subject" => subject
|
185
|
+
}
|
186
|
+
],
|
187
|
+
"content" => [
|
188
|
+
{
|
189
|
+
"type" => "text/plain",
|
190
|
+
"value" => "X"
|
191
|
+
}
|
192
|
+
]
|
193
|
+
}
|
194
|
+
end
|
125
195
|
|
126
|
-
|
127
|
-
|
128
|
-
"
|
129
|
-
{
|
130
|
-
"email" => "default-sender@platan.us"
|
131
|
-
},
|
132
|
-
"personalizations" => [
|
133
|
-
{
|
134
|
-
"subject" => "Rails tpl email"
|
135
|
-
}
|
136
|
-
],
|
137
|
-
"content" => [
|
138
|
-
{
|
139
|
-
"type" => "text/html",
|
140
|
-
"value" => "<html>\n <body>\n Rails Template!\n\n </body>\n</html>\n"
|
141
|
-
}
|
142
|
-
]
|
143
|
-
}
|
196
|
+
context "when using params" do
|
197
|
+
let(:deliver) { described_class.from_params_email.deliver_now! }
|
198
|
+
let(:subject) { "From params email" }
|
144
199
|
|
145
|
-
|
146
|
-
|
147
|
-
|
200
|
+
it "sends mail with valid sender" do
|
201
|
+
expect_valid_sg_api_send_mail_request(request_body)
|
202
|
+
end
|
203
|
+
end
|
148
204
|
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
{
|
158
|
-
"subject" => subject
|
159
|
-
}
|
160
|
-
],
|
161
|
-
"content" => [
|
162
|
-
{
|
163
|
-
"type" => "text/plain",
|
164
|
-
"value" => "X"
|
165
|
-
}
|
166
|
-
]
|
167
|
-
}
|
205
|
+
context "when using methods" do
|
206
|
+
let(:deliver) { described_class.from_email.deliver_now! }
|
207
|
+
let(:subject) { "From email" }
|
208
|
+
|
209
|
+
it "sends mail with valid sender" do
|
210
|
+
expect_valid_sg_api_send_mail_request(request_body)
|
211
|
+
end
|
212
|
+
end
|
168
213
|
end
|
169
214
|
|
170
|
-
context "
|
171
|
-
let(:
|
172
|
-
|
215
|
+
context "with setting recipients" do
|
216
|
+
let(:request_body) do
|
217
|
+
{
|
218
|
+
"from" =>
|
219
|
+
{
|
220
|
+
"email" => "default-sender@platan.us"
|
221
|
+
},
|
222
|
+
"personalizations" => [
|
223
|
+
{
|
224
|
+
"to" => [
|
225
|
+
{
|
226
|
+
"email" => "r1@platan.us"
|
227
|
+
},
|
228
|
+
{
|
229
|
+
"email" => "r2@platan.us"
|
230
|
+
}
|
231
|
+
],
|
232
|
+
"cc" => [
|
233
|
+
{
|
234
|
+
"email" => "r4@platan.us"
|
235
|
+
}
|
236
|
+
],
|
237
|
+
"bcc" => [
|
238
|
+
{
|
239
|
+
"email" => "r5@platan.us"
|
240
|
+
}
|
241
|
+
],
|
242
|
+
"subject" => subject
|
243
|
+
}
|
244
|
+
],
|
245
|
+
"content" => [
|
246
|
+
{
|
247
|
+
"type" => "text/plain",
|
248
|
+
"value" => "X"
|
249
|
+
}
|
250
|
+
]
|
251
|
+
}
|
252
|
+
end
|
173
253
|
|
174
|
-
|
175
|
-
|
254
|
+
context "when using params" do
|
255
|
+
let(:deliver) { described_class.recipients_params_email.deliver_now! }
|
256
|
+
let(:subject) { "Recipients params email" }
|
257
|
+
|
258
|
+
it "sends mail with valid recipients" do
|
259
|
+
expect_valid_sg_api_send_mail_request(request_body)
|
260
|
+
end
|
176
261
|
end
|
177
|
-
end
|
178
262
|
|
179
|
-
|
180
|
-
|
181
|
-
|
263
|
+
context "when using methods" do
|
264
|
+
let(:deliver) { described_class.recipients_email.deliver_now! }
|
265
|
+
let(:subject) { "Recipients email" }
|
182
266
|
|
183
|
-
|
184
|
-
|
267
|
+
it "sends mail with valid recipients" do
|
268
|
+
expect_valid_sg_api_send_mail_request(request_body)
|
269
|
+
end
|
185
270
|
end
|
186
271
|
end
|
187
|
-
end
|
188
272
|
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
}
|
205
|
-
],
|
206
|
-
"cc" => [
|
207
|
-
{
|
208
|
-
"email" => "r4@platan.us"
|
209
|
-
}
|
210
|
-
],
|
211
|
-
"bcc" => [
|
212
|
-
{
|
213
|
-
"email" => "r5@platan.us"
|
214
|
-
}
|
215
|
-
],
|
216
|
-
"subject" => subject
|
217
|
-
}
|
218
|
-
],
|
219
|
-
"content" => [
|
220
|
-
{
|
221
|
-
"type" => "text/plain",
|
222
|
-
"value" => "X"
|
223
|
-
}
|
224
|
-
]
|
225
|
-
}
|
226
|
-
end
|
273
|
+
context "when setting template id" do
|
274
|
+
let(:request_body) do
|
275
|
+
{
|
276
|
+
"from" =>
|
277
|
+
{
|
278
|
+
"email" => "default-sender@platan.us"
|
279
|
+
},
|
280
|
+
"personalizations" => [
|
281
|
+
{
|
282
|
+
"subject" => subject
|
283
|
+
}
|
284
|
+
],
|
285
|
+
"template_id" => "XXX"
|
286
|
+
}
|
287
|
+
end
|
227
288
|
|
228
|
-
|
229
|
-
|
230
|
-
|
289
|
+
context "when using params" do
|
290
|
+
let(:deliver) { described_class.template_id_params_email.deliver_now! }
|
291
|
+
let(:subject) { "Template id params email" }
|
231
292
|
|
232
|
-
|
233
|
-
|
293
|
+
it "sends mail with valid template" do
|
294
|
+
expect_valid_sg_api_send_mail_request(request_body)
|
295
|
+
end
|
234
296
|
end
|
235
|
-
end
|
236
297
|
|
237
|
-
|
238
|
-
|
239
|
-
|
298
|
+
context "when using methods" do
|
299
|
+
let(:deliver) { described_class.template_id_email.deliver_now! }
|
300
|
+
let(:subject) { "Template id email" }
|
240
301
|
|
241
|
-
|
242
|
-
|
302
|
+
it "sends mail with valid template id" do
|
303
|
+
expect_valid_sg_api_send_mail_request(request_body)
|
304
|
+
end
|
243
305
|
end
|
244
306
|
end
|
245
|
-
end
|
246
307
|
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
308
|
+
context "when setting subject" do
|
309
|
+
let(:request_body) do
|
310
|
+
{
|
311
|
+
"from" =>
|
312
|
+
{
|
313
|
+
"email" => "default-sender@platan.us"
|
314
|
+
},
|
315
|
+
"personalizations" => [
|
316
|
+
{
|
317
|
+
"subject" => "My Subject"
|
318
|
+
}
|
319
|
+
],
|
320
|
+
"content" => [
|
321
|
+
{
|
322
|
+
"type" => "text/plain",
|
323
|
+
"value" => "X"
|
324
|
+
}
|
325
|
+
]
|
326
|
+
}
|
327
|
+
end
|
262
328
|
|
263
|
-
|
264
|
-
|
265
|
-
let(:subject) { "Template id params email" }
|
329
|
+
context "when using params" do
|
330
|
+
let(:deliver) { described_class.subject_params_email.deliver_now! }
|
266
331
|
|
267
|
-
|
268
|
-
|
332
|
+
it "sends mail with valid subject" do
|
333
|
+
expect_valid_sg_api_send_mail_request(request_body)
|
334
|
+
end
|
269
335
|
end
|
270
|
-
end
|
271
336
|
|
272
|
-
|
273
|
-
|
274
|
-
let(:subject) { "Template id email" }
|
337
|
+
context "when using methods" do
|
338
|
+
let(:deliver) { described_class.subject_email.deliver_now! }
|
275
339
|
|
276
|
-
|
277
|
-
|
340
|
+
it "sends mail with valid subject" do
|
341
|
+
expect_valid_sg_api_send_mail_request(request_body)
|
342
|
+
end
|
278
343
|
end
|
279
344
|
end
|
280
|
-
end
|
281
345
|
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
346
|
+
context "when setting headers" do
|
347
|
+
let(:request_body) do
|
348
|
+
{
|
349
|
+
"from" =>
|
350
|
+
{
|
351
|
+
"email" => "default-sender@platan.us"
|
352
|
+
},
|
353
|
+
"personalizations" => [
|
354
|
+
{
|
355
|
+
"subject" => subject,
|
356
|
+
"headers" =>
|
357
|
+
{
|
358
|
+
"HEADER-1" => "VALUE-1",
|
359
|
+
"HEADER-2" => "VALUE-2"
|
360
|
+
}
|
361
|
+
}
|
362
|
+
],
|
363
|
+
"content" => [
|
364
|
+
{
|
365
|
+
"type" => "text/plain",
|
366
|
+
"value" => "X"
|
367
|
+
}
|
368
|
+
]
|
369
|
+
}
|
370
|
+
end
|
302
371
|
|
303
|
-
|
304
|
-
|
372
|
+
context "when using params" do
|
373
|
+
let(:deliver) { described_class.headers_params_email.deliver_now! }
|
374
|
+
let(:subject) { "Headers params email" }
|
305
375
|
|
306
|
-
|
307
|
-
|
376
|
+
it "sends mail with valid headers" do
|
377
|
+
expect_valid_sg_api_send_mail_request(request_body)
|
378
|
+
end
|
308
379
|
end
|
309
|
-
end
|
310
380
|
|
311
|
-
|
312
|
-
|
381
|
+
context "when using methods" do
|
382
|
+
let(:deliver) { described_class.headers_email.deliver_now! }
|
383
|
+
let(:subject) { "Headers email" }
|
313
384
|
|
314
|
-
|
315
|
-
|
385
|
+
it "sends mail with valid headers" do
|
386
|
+
expect_valid_sg_api_send_mail_request(request_body)
|
387
|
+
end
|
316
388
|
end
|
317
389
|
end
|
318
|
-
end
|
319
390
|
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
391
|
+
context "when adding attachments" do
|
392
|
+
let(:deliver) { described_class.add_attachments_email.deliver_now! }
|
393
|
+
|
394
|
+
it "sends mail with valid body" do
|
395
|
+
expect_any_instance_of(SendGrid::Attachment).to receive(:content).and_return("X")
|
396
|
+
expect_any_instance_of(SendGrid::Attachment).to receive(:content_id).and_return("A")
|
397
|
+
|
398
|
+
request_body = {
|
399
|
+
"from" =>
|
400
|
+
{
|
401
|
+
"email" => "default-sender@platan.us"
|
402
|
+
},
|
403
|
+
"personalizations" => [
|
404
|
+
{
|
405
|
+
"subject" => "Add attachments email"
|
406
|
+
}
|
407
|
+
],
|
408
|
+
"content" => [
|
409
|
+
{
|
410
|
+
"type" => "text/plain",
|
411
|
+
"value" => "X"
|
412
|
+
}
|
413
|
+
],
|
414
|
+
"attachments" => [
|
415
|
+
{
|
416
|
+
"content" => "X",
|
417
|
+
"type" => "image/png",
|
418
|
+
"filename" => "nana.png",
|
419
|
+
"disposition" => "attachment",
|
420
|
+
"content_id" => "A"
|
421
|
+
}
|
422
|
+
]
|
423
|
+
}
|
424
|
+
|
425
|
+
expect_valid_sg_api_send_mail_request(request_body)
|
426
|
+
end
|
344
427
|
end
|
345
428
|
|
346
|
-
context "
|
347
|
-
let(:deliver) { described_class.
|
348
|
-
|
429
|
+
context "when adding substitutions" do
|
430
|
+
let(:deliver) { described_class.substitutions_email.deliver_now! }
|
431
|
+
|
432
|
+
it "sends mail with valid body" do
|
433
|
+
request_body = {
|
434
|
+
"from" =>
|
435
|
+
{
|
436
|
+
"email" => "default-sender@platan.us"
|
437
|
+
},
|
438
|
+
"personalizations" => [
|
439
|
+
{
|
440
|
+
"subject" => "Substitutions email",
|
441
|
+
"substitutions" =>
|
442
|
+
{
|
443
|
+
"%key1%" => "value1",
|
444
|
+
"%key2%" => "value2"
|
445
|
+
}
|
446
|
+
}
|
447
|
+
],
|
448
|
+
"content" => [
|
449
|
+
{
|
450
|
+
"type" => "text/plain",
|
451
|
+
"value" => "X"
|
452
|
+
}
|
453
|
+
]
|
454
|
+
}
|
349
455
|
|
350
|
-
|
351
|
-
expect_valid_sg_api_request(request_body)
|
456
|
+
expect_valid_sg_api_send_mail_request(request_body)
|
352
457
|
end
|
353
458
|
end
|
354
459
|
|
355
|
-
context "
|
356
|
-
let(:
|
357
|
-
let(:
|
460
|
+
context "when working with recipient interceptor" do
|
461
|
+
let(:interceptor) { double(:interceptor, class: "RecipientInterceptor") }
|
462
|
+
let(:deliver) { described_class.recipients_email.deliver_now! }
|
463
|
+
let(:request_body) do
|
464
|
+
{
|
465
|
+
"from" =>
|
466
|
+
{
|
467
|
+
"email" => "default-sender@platan.us"
|
468
|
+
},
|
469
|
+
"personalizations" => [
|
470
|
+
{
|
471
|
+
"to" => [
|
472
|
+
{ "email" => "interceptor1@platan.us" },
|
473
|
+
{ "email" => "interceptor2@platan.us" }
|
474
|
+
],
|
475
|
+
"subject" => "[STAGING] Recipients email",
|
476
|
+
"headers" =>
|
477
|
+
{
|
478
|
+
"X-Intercepted-To" => "r1@platan.us, r2@platan.us",
|
479
|
+
"X-Intercepted-Cc" => "r4@platan.us",
|
480
|
+
"X-Intercepted-Bcc" => "r5@platan.us"
|
481
|
+
}
|
482
|
+
}
|
483
|
+
],
|
484
|
+
"content" => [
|
485
|
+
{
|
486
|
+
"type" => "text/plain",
|
487
|
+
"value" => "X"
|
488
|
+
}
|
489
|
+
]
|
490
|
+
}
|
491
|
+
end
|
492
|
+
|
493
|
+
before do
|
494
|
+
allow(interceptor).to receive(:instance_variable_get)
|
495
|
+
.with(:@recipients).and_return(["interceptor1@platan.us", "interceptor2@platan.us"])
|
496
|
+
allow(interceptor).to receive(:instance_variable_get)
|
497
|
+
.with(:@subject_prefix).and_return("[STAGING]")
|
498
|
+
allow(Mail).to receive(:class_variable_get)
|
499
|
+
.with(:@@delivery_interceptors).and_return([interceptor])
|
500
|
+
end
|
358
501
|
|
359
|
-
it "sends mail with valid
|
360
|
-
|
502
|
+
it "sends mail with valid recipients" do
|
503
|
+
expect_valid_sg_api_send_mail_request(request_body)
|
361
504
|
end
|
362
505
|
end
|
363
506
|
end
|
364
507
|
|
365
|
-
context "
|
366
|
-
let(:deliver) { described_class.
|
508
|
+
context "with invalid API key" do
|
509
|
+
let(:deliver) { described_class.body_email.deliver_now! }
|
367
510
|
|
368
|
-
|
369
|
-
|
370
|
-
|
511
|
+
before do
|
512
|
+
allow_any_instance_of(SendGridMailer::Deliverer).to receive(:api_key).and_return(nil)
|
513
|
+
end
|
371
514
|
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
{
|
379
|
-
"subject" => "Add attachments email"
|
380
|
-
}
|
381
|
-
],
|
382
|
-
"content" => [
|
383
|
-
{
|
384
|
-
"type" => "text/plain",
|
385
|
-
"value" => "X"
|
386
|
-
}
|
387
|
-
],
|
388
|
-
"attachments" => [
|
389
|
-
{
|
390
|
-
"content" => "X",
|
391
|
-
"type" => "image/png",
|
392
|
-
"filename" => "nana.png",
|
393
|
-
"disposition" => "attachment",
|
394
|
-
"content_id" => "A"
|
395
|
-
}
|
396
|
-
]
|
397
|
-
}
|
515
|
+
it { expect { deliver }.to raise_error(SendGridMailer::InvalidApiKey) }
|
516
|
+
end
|
517
|
+
end
|
518
|
+
|
519
|
+
context "when setting delivery_method to :sendgrid_dev" do
|
520
|
+
before { allow(TestMailer).to receive(:delivery_method).and_return(:sendgrid_dev) }
|
398
521
|
|
399
|
-
|
522
|
+
context "with valid API key" do
|
523
|
+
before do
|
524
|
+
allow_any_instance_of(SendGridMailer::DevDeliverer).to receive(:api_key).and_return("X")
|
400
525
|
end
|
401
|
-
end
|
402
526
|
|
403
|
-
|
404
|
-
|
527
|
+
context "with unsuccessful response" do
|
528
|
+
let(:sub) { 'value' }
|
529
|
+
let(:deliver) { described_class.template_with_substitutions_email(sub).deliver_now! }
|
405
530
|
|
406
|
-
|
407
|
-
|
408
|
-
"from" =>
|
531
|
+
it "raises sendgrid mailer error" do
|
532
|
+
errors = [
|
409
533
|
{
|
410
|
-
|
534
|
+
'field' => 'from.email',
|
535
|
+
'message' => 'The from email does not...'
|
411
536
|
},
|
412
|
-
"personalizations" => [
|
413
|
-
{
|
414
|
-
"subject" => "Substitutions email",
|
415
|
-
"substitutions" =>
|
416
|
-
{
|
417
|
-
"%key1%" => "value1",
|
418
|
-
"%key2%" => "value2"
|
419
|
-
}
|
420
|
-
}
|
421
|
-
],
|
422
|
-
"content" => [
|
423
537
|
{
|
424
|
-
|
425
|
-
|
538
|
+
'field' => 'personalizations.0.to',
|
539
|
+
'message' => 'The to array is required...'
|
426
540
|
}
|
427
541
|
]
|
428
|
-
|
429
|
-
|
430
|
-
expect_valid_sg_api_request(request_body)
|
542
|
+
expect_invalid_sg_api_get_template_request(errors)
|
543
|
+
end
|
431
544
|
end
|
432
|
-
end
|
433
545
|
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
546
|
+
context "with succesful response" do
|
547
|
+
let(:lo) { double(deliver!: nil) }
|
548
|
+
|
549
|
+
before { allow(LetterOpener::DeliveryMethod).to receive(:new).and_return(lo) }
|
550
|
+
|
551
|
+
context 'when there are no versions but there is a rails template' do
|
552
|
+
let(:response) { {}.to_json }
|
553
|
+
let(:deliver) { described_class.rails_tpl_email.deliver_now! }
|
554
|
+
let(:content) do
|
555
|
+
"<html>\r\n <body>\r\n Rails Template!\r\n\r\n </body>\r\n</html>\r\n"
|
556
|
+
end
|
557
|
+
let(:request_body) do
|
444
558
|
{
|
445
|
-
"
|
446
|
-
{
|
447
|
-
|
559
|
+
"from" =>
|
560
|
+
{
|
561
|
+
"email" => "default-sender@platan.us"
|
562
|
+
},
|
563
|
+
"personalizations" => [
|
564
|
+
{
|
565
|
+
"subject" => "Rails tpl email"
|
566
|
+
}
|
448
567
|
],
|
449
|
-
"
|
450
|
-
"headers" =>
|
568
|
+
"content" => [
|
451
569
|
{
|
452
|
-
"
|
453
|
-
"
|
454
|
-
"X-Intercepted-Bcc" => "r5@platan.us"
|
570
|
+
"type" => "text/html",
|
571
|
+
"value" => content
|
455
572
|
}
|
573
|
+
]
|
456
574
|
}
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
before do
|
468
|
-
allow(interceptor).to receive(:instance_variable_get)
|
469
|
-
.with(:@recipients).and_return(["interceptor1@platan.us", "interceptor2@platan.us"])
|
470
|
-
allow(interceptor).to receive(:instance_variable_get)
|
471
|
-
.with(:@subject_prefix).and_return("[STAGING]")
|
472
|
-
allow(Mail).to receive(:class_variable_get)
|
473
|
-
.with(:@@delivery_interceptors).and_return([interceptor])
|
474
|
-
end
|
575
|
+
end
|
576
|
+
|
577
|
+
it "calls letter_opener with template content as html_part" do
|
578
|
+
expect_valid_sg_api_get_template_request(response)
|
579
|
+
expect(lo).to have_received(:deliver!) do |arg|
|
580
|
+
expect(arg.html_part.to_s).to include(content)
|
581
|
+
end
|
582
|
+
end
|
583
|
+
end
|
475
584
|
|
476
|
-
|
477
|
-
|
585
|
+
context 'when there are template versions' do
|
586
|
+
context "when using substitutions" do
|
587
|
+
let(:sub) { 'value' }
|
588
|
+
let(:deliver) { described_class.template_with_substitutions_email(sub).deliver_now! }
|
589
|
+
let(:response) do
|
590
|
+
{
|
591
|
+
versions: [
|
592
|
+
{
|
593
|
+
active: 1,
|
594
|
+
html_content: active_template
|
595
|
+
},
|
596
|
+
{
|
597
|
+
active: 0,
|
598
|
+
html_content: ''
|
599
|
+
}
|
600
|
+
]
|
601
|
+
}.to_json
|
602
|
+
end
|
603
|
+
|
604
|
+
def active_template(sub = "%key%")
|
605
|
+
"<h1>Active version</h1>"\
|
606
|
+
"<span>This should be replaced: #{sub}</span>"\
|
607
|
+
"<span>This should not be replaced: %key2%</span>"
|
608
|
+
end
|
609
|
+
|
610
|
+
it "gets templates from sendgrid api, applies substitutions to active one and "\
|
611
|
+
"uses LetterOpener to deliver it" do
|
612
|
+
expect_valid_sg_api_get_template_request(response)
|
613
|
+
expect(lo).to have_received(:deliver!) do |arg|
|
614
|
+
expect(arg.html_part.to_s).to include(active_template(sub))
|
615
|
+
end
|
616
|
+
end
|
617
|
+
end
|
618
|
+
|
619
|
+
context "when using dynamic templates" do
|
620
|
+
let(:data) { 'value' }
|
621
|
+
let(:deliver) { described_class.dynamic_template_email(data).deliver_now! }
|
622
|
+
let(:response) do
|
623
|
+
{
|
624
|
+
versions: [
|
625
|
+
{
|
626
|
+
active: 1,
|
627
|
+
html_content: active_dynamic_template
|
628
|
+
},
|
629
|
+
{
|
630
|
+
active: 0,
|
631
|
+
html_content: ''
|
632
|
+
}
|
633
|
+
]
|
634
|
+
}.to_json
|
635
|
+
end
|
636
|
+
|
637
|
+
def active_dynamic_template(data = "{{key}}")
|
638
|
+
"<h1>Active dynamic version</h1>"\
|
639
|
+
"<span>This should be replaced: #{data}</span>"
|
640
|
+
end
|
641
|
+
|
642
|
+
it "gets templates from sendgrid api, applies dynamic data to active one and "\
|
643
|
+
"uses LetterOpener to deliver it" do
|
644
|
+
expect_valid_sg_api_get_template_request(response)
|
645
|
+
expect(lo).to have_received(:deliver!) do |arg|
|
646
|
+
expect(arg.html_part.to_s).to include(active_dynamic_template(data))
|
647
|
+
end
|
648
|
+
end
|
649
|
+
end
|
650
|
+
end
|
478
651
|
end
|
479
652
|
end
|
480
653
|
end
|
481
|
-
|
482
|
-
context "with invalid API key" do
|
483
|
-
let(:deliver) { described_class.body_email.deliver_now! }
|
484
|
-
|
485
|
-
before do
|
486
|
-
allow_any_instance_of(SendGridMailer::Deliverer).to receive(:api_key).and_return(nil)
|
487
|
-
end
|
488
|
-
|
489
|
-
it { expect { deliver }.to raise_error(SendGridMailer::InvalidApiKey) }
|
490
|
-
end
|
491
654
|
end
|