send_grid_mailer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +8 -0
  3. data/.rspec +3 -0
  4. data/.ruby-version +1 -0
  5. data/CHANGELOG.md +7 -0
  6. data/Gemfile +15 -0
  7. data/Gemfile.lock +192 -0
  8. data/Guardfile +15 -0
  9. data/LICENSE.txt +21 -0
  10. data/README.md +209 -0
  11. data/Rakefile +10 -0
  12. data/lib/send_grid_mailer.rb +27 -0
  13. data/lib/send_grid_mailer/definition.rb +110 -0
  14. data/lib/send_grid_mailer/deliverer.rb +46 -0
  15. data/lib/send_grid_mailer/engine.rb +23 -0
  16. data/lib/send_grid_mailer/errors.rb +2 -0
  17. data/lib/send_grid_mailer/logger.rb +99 -0
  18. data/lib/send_grid_mailer/mail_message_ext.rb +7 -0
  19. data/lib/send_grid_mailer/mailer_base_ext.rb +88 -0
  20. data/lib/send_grid_mailer/version.rb +3 -0
  21. data/lib/tasks/send_grid_mailer_tasks.rake +4 -0
  22. data/send_grid_mailer.gemspec +31 -0
  23. data/spec/dummy/README.rdoc +28 -0
  24. data/spec/dummy/Rakefile +6 -0
  25. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  26. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  27. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  28. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  29. data/spec/dummy/app/mailers/application_mailer.rb +4 -0
  30. data/spec/dummy/app/mailers/test_mailer.rb +86 -0
  31. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  32. data/spec/dummy/app/views/layouts/mailer.html.erb +5 -0
  33. data/spec/dummy/app/views/layouts/mailer.text.erb +1 -0
  34. data/spec/dummy/app/views/test_mailer/rails_tpl_email.html.erb +1 -0
  35. data/spec/dummy/bin/bundle +3 -0
  36. data/spec/dummy/bin/rails +4 -0
  37. data/spec/dummy/bin/rake +4 -0
  38. data/spec/dummy/bin/setup +29 -0
  39. data/spec/dummy/config.ru +4 -0
  40. data/spec/dummy/config/application.rb +32 -0
  41. data/spec/dummy/config/boot.rb +5 -0
  42. data/spec/dummy/config/database.yml +25 -0
  43. data/spec/dummy/config/environment.rb +5 -0
  44. data/spec/dummy/config/environments/development.rb +46 -0
  45. data/spec/dummy/config/environments/production.rb +79 -0
  46. data/spec/dummy/config/environments/test.rb +42 -0
  47. data/spec/dummy/config/initializers/assets.rb +11 -0
  48. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  49. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  50. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  51. data/spec/dummy/config/initializers/inflections.rb +16 -0
  52. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  53. data/spec/dummy/config/initializers/session_store.rb +3 -0
  54. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  55. data/spec/dummy/config/locales/en.yml +23 -0
  56. data/spec/dummy/config/routes.rb +56 -0
  57. data/spec/dummy/config/secrets.yml +22 -0
  58. data/spec/dummy/db/development.sqlite3 +0 -0
  59. data/spec/dummy/db/schema.rb +16 -0
  60. data/spec/dummy/db/test.sqlite3 +0 -0
  61. data/spec/dummy/log/development.log +1984 -0
  62. data/spec/dummy/log/test.log +58252 -0
  63. data/spec/dummy/public/404.html +67 -0
  64. data/spec/dummy/public/422.html +67 -0
  65. data/spec/dummy/public/500.html +66 -0
  66. data/spec/dummy/public/favicon.ico +0 -0
  67. data/spec/dummy/spec/assets/image.png +0 -0
  68. data/spec/dummy/spec/assets/video.mp4 +0 -0
  69. data/spec/dummy/spec/lib/send_grid_mailer/definition_spec.rb +102 -0
  70. data/spec/dummy/spec/mailers/test_mailer_spec.rb +423 -0
  71. data/spec/dummy/spec/support/test_helpers.rb +5 -0
  72. data/spec/rails_helper.rb +28 -0
  73. data/spec/spec_helper.rb +9 -0
  74. metadata +313 -0
@@ -0,0 +1,67 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The page you were looking for doesn't exist (404)</title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ body {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
54
+ </style>
55
+ </head>
56
+
57
+ <body>
58
+ <!-- This file lives in public/404.html -->
59
+ <div class="dialog">
60
+ <div>
61
+ <h1>The page you were looking for doesn't exist.</h1>
62
+ <p>You may have mistyped the address or the page may have moved.</p>
63
+ </div>
64
+ <p>If you are the application owner check the logs for more information.</p>
65
+ </div>
66
+ </body>
67
+ </html>
@@ -0,0 +1,67 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The change you wanted was rejected (422)</title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ body {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
54
+ </style>
55
+ </head>
56
+
57
+ <body>
58
+ <!-- This file lives in public/422.html -->
59
+ <div class="dialog">
60
+ <div>
61
+ <h1>The change you wanted was rejected.</h1>
62
+ <p>Maybe you tried to change something you didn't have access to.</p>
63
+ </div>
64
+ <p>If you are the application owner check the logs for more information.</p>
65
+ </div>
66
+ </body>
67
+ </html>
@@ -0,0 +1,66 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>We're sorry, but something went wrong (500)</title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ body {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
54
+ </style>
55
+ </head>
56
+
57
+ <body>
58
+ <!-- This file lives in public/500.html -->
59
+ <div class="dialog">
60
+ <div>
61
+ <h1>We're sorry, but something went wrong.</h1>
62
+ </div>
63
+ <p>If you are the application owner check the logs for more information.</p>
64
+ </div>
65
+ </body>
66
+ </html>
File without changes
Binary file
Binary file
@@ -0,0 +1,102 @@
1
+ require "rails_helper"
2
+
3
+ describe SendGridMailer::Definition do
4
+ let(:personalization) { subject.send(:personalization) }
5
+ let(:mail) { subject.send(:mail) }
6
+
7
+ describe "#template_id" do
8
+ it "sets tempalte id in sengrid mail object" do
9
+ subject.set_template_id("X")
10
+ expect(mail.template_id).to eq("X")
11
+ end
12
+ end
13
+
14
+ describe "#substitute" do
15
+ before do
16
+ @substitution = subject.substitute("%subject%", "Hi!")
17
+ end
18
+
19
+ it "creates substitution with valid data" do
20
+ expect(@substitution.substitution).to eq("%subject%" => "Hi!")
21
+ end
22
+
23
+ it "adds substitution to personalization object" do
24
+ expect(personalization.substitutions.size).to eq(1)
25
+ end
26
+
27
+ it "adds substitution to collection" do
28
+ subject.substitute("%body%", "blah")
29
+ expect(personalization.substitutions.size).to eq(2)
30
+ end
31
+ end
32
+
33
+ describe "#set_sender" do
34
+ it "adds sender to mail object" do
35
+ subject.set_sender("sender@platan.us")
36
+ expect(mail.from).to eq("email" => "sender@platan.us")
37
+ end
38
+ end
39
+
40
+ describe "#set_recipients" do
41
+ let(:m1) { "leandro@platan.us" }
42
+ let(:m2) { "ldlsegovia@gmail.com" }
43
+
44
+ it "adds recipients using splat operator" do
45
+ subject.set_recipients(:to, m1, m2)
46
+ expect(personalization.tos).to eq([{ "email" => m1 }, { "email" => m2 }])
47
+ end
48
+
49
+ it "adds recipients passing emails array" do
50
+ subject.set_recipients(:to, [m1, m2])
51
+ expect(personalization.tos).to eq([{ "email" => m1 }, { "email" => m2 }])
52
+ end
53
+
54
+ it "adds bcc recipient" do
55
+ subject.set_recipients(:bcc, m1)
56
+ expect(personalization.bccs).to eq([{ "email" => m1 }])
57
+ end
58
+
59
+ it "adds cc recipient" do
60
+ subject.set_recipients(:cc, m1)
61
+ expect(personalization.ccs).to eq([{ "email" => m1 }])
62
+ end
63
+ end
64
+
65
+ describe "#set_subject" do
66
+ it "adds subject to personalization object" do
67
+ subject.set_subject("Hi!")
68
+ expect(personalization.subject).to eq("Hi!")
69
+ end
70
+ end
71
+
72
+ describe "#set_content" do
73
+ it "adds content to mail object" do
74
+ subject.set_content("X")
75
+ expect(mail.contents).to eq([{ "type" => "text/plain", "value" => "X" }])
76
+ end
77
+
78
+ it "adds content with different type" do
79
+ subject.set_content("X", "other/type")
80
+ expect(mail.contents).to eq([{ "type" => "other/type", "value" => "X" }])
81
+ end
82
+ end
83
+
84
+ describe "#add_header" do
85
+ it "adds headers to personalization object" do
86
+ subject.add_header("HEADER1", "VALUE1")
87
+ subject.add_header("HEADER2", "VALUE2")
88
+ expect(personalization.headers).to eq("HEADER1" => "VALUE1", "HEADER2" => "VALUE2")
89
+ end
90
+ end
91
+
92
+ describe "#add_attachment" do
93
+ it "adds file to mail object" do
94
+ file = File.read(fixture_file_upload("image.png", "image/png"))
95
+ subject.add_attachment(file, "platanus.png", "image/png")
96
+ attachment = mail.attachments.first
97
+ expect(attachment["type"]).to match("image/png")
98
+ expect(attachment["disposition"]).to match("inline")
99
+ expect(attachment["content"]).to eq(Base64.strict_encode64(file))
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,423 @@
1
+ require "rails_helper"
2
+
3
+ describe TestMailer do
4
+ before do
5
+ allow(TestMailer).to receive(:delivery_method).and_return(:sendgrid)
6
+ allow_any_instance_of(SendGridMailer::Deliverer).to receive(:api_key).and_return("XXX")
7
+ end
8
+
9
+ def expect_valid_mail_sent(request_body)
10
+ result = double(status_code: "202")
11
+ client2 = double(post: result)
12
+ client1 = double(_: client2)
13
+ expect_any_instance_of(SendGrid::Client).to receive(:_).with(:mail).and_return(client1)
14
+ expect(client2).to receive(:post).with(request_body: request_body).and_return(result)
15
+ deliver
16
+ end
17
+
18
+ context "setting body" do
19
+ let(:deliver) { described_class.body_email.deliver_now! }
20
+
21
+ it "sends mail with valid body" do
22
+ request_body = {
23
+ "from" =>
24
+ {
25
+ "email" => "default-sender@platan.us"
26
+ },
27
+ "personalizations" => [
28
+ {
29
+ "subject" => "Body email"
30
+ }
31
+ ],
32
+ "content" => [
33
+ {
34
+ "type" => "text/plain",
35
+ "value" => "Body"
36
+ }
37
+ ]
38
+ }
39
+
40
+ expect_valid_mail_sent(request_body)
41
+ end
42
+ end
43
+
44
+ context "setting body from params" do
45
+ let(:deliver) { described_class.body_params_email.deliver_now! }
46
+
47
+ it "sends mail with valid body" do
48
+ request_body = {
49
+ "from" =>
50
+ {
51
+ "email" => "default-sender@platan.us"
52
+ },
53
+ "personalizations" => [
54
+ {
55
+ "subject" => "Body params email"
56
+ }
57
+ ],
58
+ "content" => [
59
+ {
60
+ "type" => "text/html",
61
+ "value" => "<h1>Body Params</h1>"
62
+ }
63
+ ]
64
+ }
65
+
66
+ expect_valid_mail_sent(request_body)
67
+ end
68
+ end
69
+
70
+ context "setting body from rails template" do
71
+ let(:deliver) { described_class.rails_tpl_email.deliver_now! }
72
+
73
+ it "sends mail with valid body" do
74
+ request_body = {
75
+ "from" =>
76
+ {
77
+ "email" => "default-sender@platan.us"
78
+ },
79
+ "personalizations" => [
80
+ {
81
+ "subject" => "Rails tpl email"
82
+ }
83
+ ],
84
+ "content" => [
85
+ {
86
+ "type" => "text/html",
87
+ "value" => "<html>\n <body>\n Rails Template!\n\n </body>\n</html>\n"
88
+ }
89
+ ]
90
+ }
91
+
92
+ expect_valid_mail_sent(request_body)
93
+ end
94
+ end
95
+
96
+ context "overriding default from" do
97
+ let(:request_body) do
98
+ {
99
+ "from" =>
100
+ {
101
+ "email" => "override@platan.us"
102
+ },
103
+ "personalizations" => [
104
+ {
105
+ "subject" => subject
106
+ }
107
+ ],
108
+ "content" => [
109
+ {
110
+ "type" => "text/plain",
111
+ "value" => "X"
112
+ }
113
+ ]
114
+ }
115
+ end
116
+
117
+ context "using params" do
118
+ let(:deliver) { described_class.from_params_email.deliver_now! }
119
+ let(:subject) { "From params email" }
120
+
121
+ it "sends mail with valid sender" do
122
+ expect_valid_mail_sent(request_body)
123
+ end
124
+ end
125
+
126
+ context "using methods" do
127
+ let(:deliver) { described_class.from_email.deliver_now! }
128
+ let(:subject) { "From email" }
129
+
130
+ it "sends mail with valid sender" do
131
+ expect_valid_mail_sent(request_body)
132
+ end
133
+ end
134
+ end
135
+
136
+ context "setting recipients" do
137
+ let(:request_body) do
138
+ {
139
+ "from" =>
140
+ {
141
+ "email" => "default-sender@platan.us"
142
+ },
143
+ "personalizations" => [
144
+ {
145
+ "to" => [
146
+ {
147
+ "email" => "r1@platan.us"
148
+ },
149
+ {
150
+ "email" => "r2@platan.us"
151
+ }
152
+ ],
153
+ "cc" => [
154
+ {
155
+ "email" => "r4@platan.us"
156
+ }
157
+ ],
158
+ "bcc" => [
159
+ {
160
+ "email" => "r5@platan.us"
161
+ }
162
+ ],
163
+ "subject" => subject
164
+ }
165
+ ],
166
+ "content" => [
167
+ {
168
+ "type" => "text/plain",
169
+ "value" => "X"
170
+ }
171
+ ]
172
+ }
173
+ end
174
+
175
+ context "using params" do
176
+ let(:deliver) { described_class.recipients_params_email.deliver_now! }
177
+ let(:subject) { "Recipients params email" }
178
+
179
+ it "sends mail with valid recipients" do
180
+ expect_valid_mail_sent(request_body)
181
+ end
182
+ end
183
+
184
+ context "using methods" do
185
+ let(:deliver) { described_class.recipients_email.deliver_now! }
186
+ let(:subject) { "Recipients email" }
187
+
188
+ it "sends mail with valid recipients" do
189
+ expect_valid_mail_sent(request_body)
190
+ end
191
+ end
192
+ end
193
+
194
+ context "setting template id" do
195
+ let(:request_body) do
196
+ {
197
+ "from" =>
198
+ {
199
+ "email" => "default-sender@platan.us"
200
+ },
201
+ "personalizations" => [
202
+ {
203
+ "subject" => subject
204
+ }
205
+ ],
206
+ "template_id" => "XXX"
207
+ }
208
+ end
209
+
210
+ context "using params" do
211
+ let(:deliver) { described_class.template_id_params_email.deliver_now! }
212
+ let(:subject) { "Template id params email" }
213
+
214
+ it "sends mail with valid template" do
215
+ expect_valid_mail_sent(request_body)
216
+ end
217
+ end
218
+
219
+ context "using methods" do
220
+ let(:deliver) { described_class.template_id_email.deliver_now! }
221
+ let(:subject) { "Template id email" }
222
+
223
+ it "sends mail with valid template id" do
224
+ expect_valid_mail_sent(request_body)
225
+ end
226
+ end
227
+ end
228
+
229
+ context "setting subject" do
230
+ let(:request_body) do
231
+ {
232
+ "from" =>
233
+ {
234
+ "email" => "default-sender@platan.us"
235
+ },
236
+ "personalizations" => [
237
+ {
238
+ "subject" => "My Subject"
239
+ }
240
+ ],
241
+ "content" => [
242
+ {
243
+ "type" => "text/plain",
244
+ "value" => "X"
245
+ }
246
+ ]
247
+ }
248
+ end
249
+
250
+ context "using params" do
251
+ let(:deliver) { described_class.subject_params_email.deliver_now! }
252
+
253
+ it "sends mail with valid subject" do
254
+ expect_valid_mail_sent(request_body)
255
+ end
256
+ end
257
+
258
+ context "using methods" do
259
+ let(:deliver) { described_class.subject_email.deliver_now! }
260
+
261
+ it "sends mail with valid subject" do
262
+ expect_valid_mail_sent(request_body)
263
+ end
264
+ end
265
+ end
266
+
267
+ context "setting headers" do
268
+ let(:request_body) do
269
+ {
270
+ "from" =>
271
+ {
272
+ "email" => "default-sender@platan.us"
273
+ },
274
+ "personalizations" => [
275
+ {
276
+ "subject" => subject,
277
+ "headers" =>
278
+ {
279
+ "HEADER-1" => "VALUE-1",
280
+ "HEADER-2" => "VALUE-2"
281
+ }
282
+ }
283
+ ],
284
+ "content" => [
285
+ {
286
+ "type" => "text/plain",
287
+ "value" => "X"
288
+ }
289
+ ]
290
+ }
291
+ end
292
+
293
+ context "using params" do
294
+ let(:deliver) { described_class.headers_params_email.deliver_now! }
295
+ let(:subject) { "Headers params email" }
296
+
297
+ it "sends mail with valid headers" do
298
+ expect_valid_mail_sent(request_body)
299
+ end
300
+ end
301
+
302
+ context "using methods" do
303
+ let(:deliver) { described_class.headers_email.deliver_now! }
304
+ let(:subject) { "Headers email" }
305
+
306
+ it "sends mail with valid headers" do
307
+ expect_valid_mail_sent(request_body)
308
+ end
309
+ end
310
+ end
311
+
312
+ context "adding attachments" do
313
+ let(:deliver) { described_class.add_attachments_email.deliver_now! }
314
+
315
+ it "sends mail with valid body" do
316
+ expect_any_instance_of(SendGrid::Attachment).to receive(:content).and_return("X")
317
+ expect_any_instance_of(SendGrid::Attachment).to receive(:content_id).and_return("A")
318
+
319
+ request_body = {
320
+ "from" =>
321
+ {
322
+ "email" => "default-sender@platan.us"
323
+ },
324
+ "personalizations" => [
325
+ {
326
+ "subject" => "Add attachments email"
327
+ }
328
+ ],
329
+ "content" => [
330
+ {
331
+ "type" => "text/plain",
332
+ "value" => "X"
333
+ }
334
+ ],
335
+ "attachments" => [
336
+ {
337
+ "content" => "X",
338
+ "type" => "image/png",
339
+ "filename" => "nana.png",
340
+ "disposition" => "attachment",
341
+ "content_id" => "A"
342
+ }
343
+ ]
344
+ }
345
+
346
+ expect_valid_mail_sent(request_body)
347
+ end
348
+ end
349
+
350
+ context "adding substitutions" do
351
+ let(:deliver) { described_class.substitutions_email.deliver_now! }
352
+
353
+ it "sends mail with valid body" do
354
+ request_body = {
355
+ "from" =>
356
+ {
357
+ "email" => "default-sender@platan.us"
358
+ },
359
+ "personalizations" => [
360
+ {
361
+ "subject" => "Substitutions email",
362
+ "substitutions" =>
363
+ {
364
+ "%key1%" => "value1",
365
+ "%key2%" => "value2"
366
+ }
367
+ }
368
+ ],
369
+ "content" => [
370
+ {
371
+ "type" => "text/plain",
372
+ "value" => "X"
373
+ }
374
+ ]
375
+ }
376
+
377
+ expect_valid_mail_sent(request_body)
378
+ end
379
+ end
380
+
381
+ def get_templates(status_code, response = nil)
382
+ result = double(status_code: status_code, body: response)
383
+ client = double(get: result)
384
+ expect_any_instance_of(SendGrid::Client).to receive(:_).with(:templates).and_return(client)
385
+ end
386
+
387
+ context "setting template name" do
388
+ let(:deliver) { described_class.template_name_email.deliver_now! }
389
+
390
+ it "sends mail with valid template id" do
391
+ response = { templates: [{ id: "123", name: "my template name" }] }.to_json
392
+ get_templates("200", response)
393
+
394
+ request_body = {
395
+ "from" =>
396
+ {
397
+ "email" => "default-sender@platan.us"
398
+ },
399
+ "personalizations" => [
400
+ {
401
+ "subject" => "Template name email"
402
+ }
403
+ ],
404
+ "template_id" => "123"
405
+ }
406
+
407
+ expect_valid_mail_sent(request_body)
408
+ end
409
+
410
+ it "raises error when templates endpoint is down" do
411
+ get_templates("500")
412
+ expect { deliver }.to raise_error(SendGridMailer::Exception,
413
+ "Error trying to get templates. Status Code: 500")
414
+ end
415
+
416
+ it "raises error when template name does not match with server response" do
417
+ response = { templates: [{ id: "123", name: "other template" }] }.to_json
418
+ get_templates("200", response)
419
+ expect { deliver }.to raise_error(SendGridMailer::Exception,
420
+ "No template with name my template name")
421
+ end
422
+ end
423
+ end