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.
Files changed (67) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +102 -0
  3. data/.circleci/setup-rubygems.sh +3 -0
  4. data/.rubocop.yml +31 -591
  5. data/.ruby-version +1 -1
  6. data/CHANGELOG.md +28 -0
  7. data/README.md +46 -2
  8. data/lib/send_grid_mailer/api.rb +14 -9
  9. data/lib/send_grid_mailer/definition.rb +32 -14
  10. data/lib/send_grid_mailer/dev_deliverer.rb +72 -0
  11. data/lib/send_grid_mailer/engine.rb +4 -2
  12. data/lib/send_grid_mailer/logger.rb +10 -5
  13. data/lib/send_grid_mailer/mailer_base_ext.rb +13 -2
  14. data/lib/send_grid_mailer/version.rb +1 -1
  15. data/send_grid_mailer.gemspec +12 -6
  16. data/spec/dummy/Rakefile +1 -1
  17. data/spec/dummy/app/assets/config/manifest.js +3 -0
  18. data/spec/dummy/app/assets/stylesheets/application.css +3 -3
  19. data/spec/dummy/app/channels/application_cable/channel.rb +4 -0
  20. data/spec/dummy/app/channels/application_cable/connection.rb +4 -0
  21. data/spec/dummy/app/controllers/application_controller.rb +0 -3
  22. data/spec/dummy/app/{assets/javascripts → javascript/packs}/application.js +3 -1
  23. data/spec/dummy/app/jobs/application_job.rb +7 -0
  24. data/spec/dummy/app/mailers/application_mailer.rb +1 -1
  25. data/spec/dummy/app/mailers/test_mailer.rb +12 -0
  26. data/spec/dummy/app/models/application_record.rb +3 -0
  27. data/spec/dummy/app/views/layouts/application.html.erb +10 -9
  28. data/spec/dummy/bin/rails +3 -3
  29. data/spec/dummy/bin/rake +2 -2
  30. data/spec/dummy/bin/setup +18 -14
  31. data/spec/dummy/config/application.rb +12 -22
  32. data/spec/dummy/config/boot.rb +3 -3
  33. data/spec/dummy/config/cable.yml +10 -0
  34. data/spec/dummy/config/database.yml +2 -2
  35. data/spec/dummy/config/environment.rb +1 -1
  36. data/spec/dummy/config/environments/development.rb +48 -18
  37. data/spec/dummy/config/environments/production.rb +63 -22
  38. data/spec/dummy/config/environments/test.rb +29 -12
  39. data/spec/dummy/config/initializers/application_controller_renderer.rb +8 -0
  40. data/spec/dummy/config/initializers/assets.rb +4 -3
  41. data/spec/dummy/config/initializers/backtrace_silencers.rb +4 -3
  42. data/spec/dummy/config/initializers/content_security_policy.rb +28 -0
  43. data/spec/dummy/config/initializers/cookies_serializer.rb +2 -0
  44. data/spec/dummy/config/initializers/filter_parameter_logging.rb +3 -1
  45. data/spec/dummy/config/initializers/permissions_policy.rb +11 -0
  46. data/spec/dummy/config/initializers/wrap_parameters.rb +2 -2
  47. data/spec/dummy/config/locales/en.yml +11 -1
  48. data/spec/dummy/config/puma.rb +43 -0
  49. data/spec/dummy/config/routes.rb +1 -54
  50. data/spec/dummy/config/storage.yml +34 -0
  51. data/spec/dummy/config.ru +3 -1
  52. data/spec/dummy/public/404.html +6 -6
  53. data/spec/dummy/public/422.html +6 -6
  54. data/spec/dummy/public/500.html +6 -6
  55. data/spec/dummy/public/apple-touch-icon-precomposed.png +0 -0
  56. data/spec/dummy/public/apple-touch-icon.png +0 -0
  57. data/spec/dummy/spec/lib/send_grid_mailer/definition_spec.rb +52 -23
  58. data/spec/dummy/spec/mailers/test_mailer_spec.rb +547 -384
  59. data/spec/rails_helper.rb +5 -7
  60. metadata +124 -38
  61. data/.hound.yml +0 -4
  62. data/.travis.yml +0 -15
  63. data/spec/dummy/bin/bundle +0 -3
  64. data/spec/dummy/config/initializers/session_store.rb +0 -3
  65. data/spec/dummy/config/secrets.yml +0 -22
  66. data/spec/dummy/db/schema.rb +0 -16
  67. 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
- def expect_valid_sg_api_request(request_body)
5
- expect_sg_api_request(SendGridMailer::Api::SUCCESS_CODE, request_body)
6
- deliver
7
- end
4
+ let(:error_code) { "404" }
5
+ let(:success_code) { "200" }
8
6
 
9
- def expect_invalid_sg_api_request(request_body, errors)
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 expect_sg_api_request(status_code, request_body, result = nil)
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
- before { allow(TestMailer).to receive(:delivery_method).and_return(:sendgrid) }
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
- context "with valid API key" do
32
- before { allow_any_instance_of(SendGridMailer::Deliverer).to receive(:api_key).and_return("X") }
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
- context "with unsuccessful response" do
35
- let(:deliver) { described_class.body_email.deliver_now! }
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
- it "sends mail with valid body" do
38
- request_body = {
39
- "from" =>
82
+ errors = [
40
83
  {
41
- "email" => "default-sender@platan.us"
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
- "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
- errors = [
57
- {
58
- 'field' => 'from.email',
59
- 'message' => 'The from email does not...'
60
- },
61
- {
62
- 'field' => 'personalizations.0.to',
63
- 'message' => 'The to array is required...'
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
- expect_invalid_sg_api_request(request_body, errors)
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
- it "sends mail with valid body" do
75
- request_body = {
76
- "from" =>
77
- {
78
- "email" => "default-sender@platan.us"
79
- },
80
- "personalizations" => [
81
- {
82
- "subject" => "Body email"
83
- }
84
- ],
85
- "content" => [
86
- {
87
- "type" => "text/plain",
88
- "value" => "Body"
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
- expect_valid_sg_api_request(request_body)
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
- 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 params email"
109
- }
110
- ],
111
- "content" => [
112
- {
113
- "type" => "text/html",
114
- "value" => "<h1>Body Params</h1>"
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
- expect_valid_sg_api_request(request_body)
171
+ expect_valid_sg_api_send_mail_request(request_body)
172
+ end
120
173
  end
121
- end
122
174
 
123
- context "setting body from rails template" do
124
- let(:deliver) { described_class.rails_tpl_email.deliver_now! }
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
- 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" => "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
- expect_valid_sg_api_request(request_body)
146
- end
147
- end
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
- context "overriding default from" do
150
- let(:request_body) do
151
- {
152
- "from" =>
153
- {
154
- "email" => "override@platan.us"
155
- },
156
- "personalizations" => [
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 "using params" do
171
- let(:deliver) { described_class.from_params_email.deliver_now! }
172
- let(:subject) { "From params email" }
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
- it "sends mail with valid sender" do
175
- expect_valid_sg_api_request(request_body)
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
- context "using methods" do
180
- let(:deliver) { described_class.from_email.deliver_now! }
181
- let(:subject) { "From email" }
263
+ context "when using methods" do
264
+ let(:deliver) { described_class.recipients_email.deliver_now! }
265
+ let(:subject) { "Recipients email" }
182
266
 
183
- it "sends mail with valid sender" do
184
- expect_valid_sg_api_request(request_body)
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
- context "setting recipients" do
190
- let(:request_body) do
191
- {
192
- "from" =>
193
- {
194
- "email" => "default-sender@platan.us"
195
- },
196
- "personalizations" => [
197
- {
198
- "to" => [
199
- {
200
- "email" => "r1@platan.us"
201
- },
202
- {
203
- "email" => "r2@platan.us"
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
- context "using params" do
229
- let(:deliver) { described_class.recipients_params_email.deliver_now! }
230
- let(:subject) { "Recipients params email" }
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
- it "sends mail with valid recipients" do
233
- expect_valid_sg_api_request(request_body)
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
- context "using methods" do
238
- let(:deliver) { described_class.recipients_email.deliver_now! }
239
- let(:subject) { "Recipients email" }
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
- it "sends mail with valid recipients" do
242
- expect_valid_sg_api_request(request_body)
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
- context "setting template id" do
248
- let(:request_body) do
249
- {
250
- "from" =>
251
- {
252
- "email" => "default-sender@platan.us"
253
- },
254
- "personalizations" => [
255
- {
256
- "subject" => subject
257
- }
258
- ],
259
- "template_id" => "XXX"
260
- }
261
- end
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
- context "using params" do
264
- let(:deliver) { described_class.template_id_params_email.deliver_now! }
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
- it "sends mail with valid template" do
268
- expect_valid_sg_api_request(request_body)
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
- context "using methods" do
273
- let(:deliver) { described_class.template_id_email.deliver_now! }
274
- let(:subject) { "Template id email" }
337
+ context "when using methods" do
338
+ let(:deliver) { described_class.subject_email.deliver_now! }
275
339
 
276
- it "sends mail with valid template id" do
277
- expect_valid_sg_api_request(request_body)
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
- context "setting subject" do
283
- let(:request_body) do
284
- {
285
- "from" =>
286
- {
287
- "email" => "default-sender@platan.us"
288
- },
289
- "personalizations" => [
290
- {
291
- "subject" => "My Subject"
292
- }
293
- ],
294
- "content" => [
295
- {
296
- "type" => "text/plain",
297
- "value" => "X"
298
- }
299
- ]
300
- }
301
- end
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
- context "using params" do
304
- let(:deliver) { described_class.subject_params_email.deliver_now! }
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
- it "sends mail with valid subject" do
307
- expect_valid_sg_api_request(request_body)
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
- context "using methods" do
312
- let(:deliver) { described_class.subject_email.deliver_now! }
381
+ context "when using methods" do
382
+ let(:deliver) { described_class.headers_email.deliver_now! }
383
+ let(:subject) { "Headers email" }
313
384
 
314
- it "sends mail with valid subject" do
315
- expect_valid_sg_api_request(request_body)
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
- context "setting headers" do
321
- let(:request_body) do
322
- {
323
- "from" =>
324
- {
325
- "email" => "default-sender@platan.us"
326
- },
327
- "personalizations" => [
328
- {
329
- "subject" => subject,
330
- "headers" =>
331
- {
332
- "HEADER-1" => "VALUE-1",
333
- "HEADER-2" => "VALUE-2"
334
- }
335
- }
336
- ],
337
- "content" => [
338
- {
339
- "type" => "text/plain",
340
- "value" => "X"
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 "using params" do
347
- let(:deliver) { described_class.headers_params_email.deliver_now! }
348
- let(:subject) { "Headers params email" }
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
- it "sends mail with valid headers" do
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 "using methods" do
356
- let(:deliver) { described_class.headers_email.deliver_now! }
357
- let(:subject) { "Headers email" }
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 headers" do
360
- expect_valid_sg_api_request(request_body)
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 "adding attachments" do
366
- let(:deliver) { described_class.add_attachments_email.deliver_now! }
508
+ context "with invalid API key" do
509
+ let(:deliver) { described_class.body_email.deliver_now! }
367
510
 
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")
511
+ before do
512
+ allow_any_instance_of(SendGridMailer::Deliverer).to receive(:api_key).and_return(nil)
513
+ end
371
514
 
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
- }
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
- expect_valid_sg_api_request(request_body)
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
- context "adding substitutions" do
404
- let(:deliver) { described_class.substitutions_email.deliver_now! }
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
- it "sends mail with valid body" do
407
- request_body = {
408
- "from" =>
531
+ it "raises sendgrid mailer error" do
532
+ errors = [
409
533
  {
410
- "email" => "default-sender@platan.us"
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
- "type" => "text/plain",
425
- "value" => "X"
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
- context "working with recipient interceptor" do
435
- let(:interceptor) { double(:interceptor, class: "RecipientInterceptor") }
436
- let(:deliver) { described_class.recipients_email.deliver_now! }
437
- let(:request_body) do
438
- {
439
- "from" =>
440
- {
441
- "email" => "default-sender@platan.us"
442
- },
443
- "personalizations" => [
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
- "to" => [
446
- { "email" => "interceptor1@platan.us" },
447
- { "email" => "interceptor2@platan.us" }
559
+ "from" =>
560
+ {
561
+ "email" => "default-sender@platan.us"
562
+ },
563
+ "personalizations" => [
564
+ {
565
+ "subject" => "Rails tpl email"
566
+ }
448
567
  ],
449
- "subject" => "[STAGING] Recipients email",
450
- "headers" =>
568
+ "content" => [
451
569
  {
452
- "X-Intercepted-To" => "r1@platan.us, r2@platan.us",
453
- "X-Intercepted-Cc" => "r4@platan.us",
454
- "X-Intercepted-Bcc" => "r5@platan.us"
570
+ "type" => "text/html",
571
+ "value" => content
455
572
  }
573
+ ]
456
574
  }
457
- ],
458
- "content" => [
459
- {
460
- "type" => "text/plain",
461
- "value" => "X"
462
- }
463
- ]
464
- }
465
- end
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
- it "sends mail with valid recipients" do
477
- expect_valid_sg_api_request(request_body)
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