send_grid_mailer 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +56 -56
- data/CHANGELOG.md +6 -0
- data/README.md +11 -1
- data/lib/send_grid_mailer/api.rb +14 -9
- data/lib/send_grid_mailer/dev_deliverer.rb +60 -0
- data/lib/send_grid_mailer/engine.rb +4 -2
- data/lib/send_grid_mailer/logger.rb +4 -4
- data/lib/send_grid_mailer/mailer_base_ext.rb +11 -2
- data/lib/send_grid_mailer/version.rb +1 -1
- data/send_grid_mailer.gemspec +3 -2
- data/spec/dummy/app/mailers/test_mailer.rb +6 -0
- data/spec/dummy/spec/mailers/test_mailer_spec.rb +476 -386
- data/spec/rails_helper.rb +4 -4
- metadata +58 -44
@@ -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,550 @@ 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
|
36
53
|
|
37
|
-
|
38
|
-
|
39
|
-
|
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
|
+
}
|
81
|
+
|
82
|
+
errors = [
|
40
83
|
{
|
41
|
-
|
84
|
+
'field' => 'from.email',
|
85
|
+
'message' => 'The from email does not...'
|
42
86
|
},
|
43
|
-
"personalizations" => [
|
44
87
|
{
|
45
|
-
|
46
|
-
|
47
|
-
],
|
48
|
-
"content" => [
|
49
|
-
{
|
50
|
-
"type" => "text/plain",
|
51
|
-
"value" => "Body"
|
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 "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
122
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
123
|
+
context "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 "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 "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 "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 "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 "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
|
253
|
+
|
254
|
+
context "using params" do
|
255
|
+
let(:deliver) { described_class.recipients_params_email.deliver_now! }
|
256
|
+
let(:subject) { "Recipients params email" }
|
173
257
|
|
174
|
-
|
175
|
-
|
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 "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 "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 "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 "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 "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 "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 "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 "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 "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 "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 "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 "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 "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.
|
367
|
-
|
368
|
-
it "sends mail with valid body" do
|
369
|
-
expect_any_instance_of(SendGrid::Attachment).to receive(:content).and_return("X")
|
370
|
-
expect_any_instance_of(SendGrid::Attachment).to receive(:content_id).and_return("A")
|
371
|
-
|
372
|
-
request_body = {
|
373
|
-
"from" =>
|
374
|
-
{
|
375
|
-
"email" => "default-sender@platan.us"
|
376
|
-
},
|
377
|
-
"personalizations" => [
|
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
|
-
}
|
508
|
+
context "with invalid API key" do
|
509
|
+
let(:deliver) { described_class.body_email.deliver_now! }
|
398
510
|
|
399
|
-
|
511
|
+
before do
|
512
|
+
allow_any_instance_of(SendGridMailer::Deliverer).to receive(:api_key).and_return(nil)
|
400
513
|
end
|
514
|
+
|
515
|
+
it { expect { deliver }.to raise_error(SendGridMailer::InvalidApiKey) }
|
401
516
|
end
|
517
|
+
end
|
402
518
|
|
403
|
-
|
404
|
-
|
519
|
+
context "when setting delivery_method to :sendgrid_dev" do
|
520
|
+
let(:sub) { 'value' }
|
521
|
+
let(:deliver) { described_class.template_with_substitutions_email(sub).deliver_now! }
|
405
522
|
|
406
|
-
|
407
|
-
request_body = {
|
408
|
-
"from" =>
|
409
|
-
{
|
410
|
-
"email" => "default-sender@platan.us"
|
411
|
-
},
|
412
|
-
"personalizations" => [
|
413
|
-
{
|
414
|
-
"subject" => "Substitutions email",
|
415
|
-
"substitutions" =>
|
416
|
-
{
|
417
|
-
"%key1%" => "value1",
|
418
|
-
"%key2%" => "value2"
|
419
|
-
}
|
420
|
-
}
|
421
|
-
],
|
422
|
-
"content" => [
|
423
|
-
{
|
424
|
-
"type" => "text/plain",
|
425
|
-
"value" => "X"
|
426
|
-
}
|
427
|
-
]
|
428
|
-
}
|
523
|
+
before { allow(TestMailer).to receive(:delivery_method).and_return(:sendgrid_dev) }
|
429
524
|
|
430
|
-
|
525
|
+
context "with valid API key" do
|
526
|
+
before do
|
527
|
+
allow_any_instance_of(SendGridMailer::DevDeliverer).to receive(:api_key).and_return("X")
|
431
528
|
end
|
432
|
-
end
|
433
529
|
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
let(:request_body) do
|
438
|
-
{
|
439
|
-
"from" =>
|
530
|
+
context "with unsuccessful response" do
|
531
|
+
it "raises sendgrid mailer error" do
|
532
|
+
errors = [
|
440
533
|
{
|
441
|
-
|
534
|
+
'field' => 'from.email',
|
535
|
+
'message' => 'The from email does not...'
|
442
536
|
},
|
443
|
-
"personalizations" => [
|
444
537
|
{
|
445
|
-
|
446
|
-
|
447
|
-
{ "email" => "interceptor2@platan.us" }
|
448
|
-
],
|
449
|
-
"subject" => "[STAGING] Recipients email",
|
450
|
-
"headers" =>
|
451
|
-
{
|
452
|
-
"X-Intercepted-To" => "r1@platan.us, r2@platan.us",
|
453
|
-
"X-Intercepted-Cc" => "r4@platan.us",
|
454
|
-
"X-Intercepted-Bcc" => "r5@platan.us"
|
455
|
-
}
|
456
|
-
}
|
457
|
-
],
|
458
|
-
"content" => [
|
459
|
-
{
|
460
|
-
"type" => "text/plain",
|
461
|
-
"value" => "X"
|
538
|
+
'field' => 'personalizations.0.to',
|
539
|
+
'message' => 'The to array is required...'
|
462
540
|
}
|
463
541
|
]
|
464
|
-
|
542
|
+
expect_invalid_sg_api_get_template_request(errors)
|
543
|
+
end
|
465
544
|
end
|
466
545
|
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
.with(:@@delivery_interceptors).and_return([interceptor])
|
474
|
-
end
|
546
|
+
context "with succesful response" do
|
547
|
+
def active_template(sub = "%key%")
|
548
|
+
"<h1>Active version</h1>"\
|
549
|
+
"<span>This should be replaced: #{sub}</span>"\
|
550
|
+
"<span>This should not be replaced: %key2%</span>"
|
551
|
+
end
|
475
552
|
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
553
|
+
let(:response) { {
|
554
|
+
versions: [
|
555
|
+
{
|
556
|
+
active: 1,
|
557
|
+
html_content: active_template
|
558
|
+
},
|
559
|
+
{
|
560
|
+
active: 0,
|
561
|
+
html_content: ''
|
562
|
+
},
|
563
|
+
]
|
564
|
+
}.to_json}
|
565
|
+
let(:lo) { double(deliver!: nil) }
|
481
566
|
|
482
|
-
|
483
|
-
|
567
|
+
before do
|
568
|
+
allow(LetterOpener::DeliveryMethod).to receive(:new).and_return(lo)
|
569
|
+
end
|
484
570
|
|
485
|
-
|
486
|
-
|
571
|
+
it "gets templates form sendgrid api, applies substitutions to active one and "\
|
572
|
+
"uses LetterOpener to deliver it" do
|
573
|
+
expect_valid_sg_api_get_template_request(response)
|
574
|
+
expect(lo).to have_received(:deliver!) do |arg|
|
575
|
+
expect(arg.html_part.to_s).to include(active_template(sub))
|
576
|
+
end
|
577
|
+
end
|
578
|
+
end
|
487
579
|
end
|
488
|
-
|
489
|
-
it { expect { deliver }.to raise_error(SendGridMailer::InvalidApiKey) }
|
490
580
|
end
|
491
581
|
end
|