sendgrid-actionmailer 0.2.1 → 2.0.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.
@@ -4,25 +4,56 @@ require 'webmock/rspec'
4
4
  module SendGridActionMailer
5
5
  describe DeliveryMethod do
6
6
  subject(:mailer) do
7
- DeliveryMethod.new(api_user: 'user', api_key: 'key')
7
+ DeliveryMethod.new(api_key: 'key')
8
8
  end
9
9
 
10
- class TestClient < SendGrid::Client
10
+ class TestClient
11
11
  attr_reader :sent_mail
12
12
 
13
13
  def send(mail)
14
14
  @sent_mail = mail
15
15
  super(mail)
16
16
  end
17
+
18
+ def mail()
19
+ self
20
+ end
21
+
22
+ def _(param)
23
+ return self if param == 'send'
24
+ raise "Unknown param #{param.inspect}"
25
+ end
26
+
27
+ def post(request_body:)
28
+ @sent_mail = request_body
29
+ OpenStruct.new(status_code: '200')
30
+ end
17
31
  end
18
32
 
19
- describe '#initialize' do
20
- it 'configures the client API user' do
21
- expect(mailer.client.api_user).to eq('user')
33
+ describe 'settings' do
34
+ it 'has correct api_key' do
35
+ m = DeliveryMethod.new(api_key: 'ABCDEFG')
36
+ expect(m.settings[:api_key]).to eq('ABCDEFG')
37
+ end
38
+
39
+ it 'default raise_delivery_errors' do
40
+ m = DeliveryMethod.new()
41
+ expect(m.settings[:raise_delivery_errors]).to eq(false)
42
+ end
43
+
44
+ it 'sets raise_delivery_errors' do
45
+ m = DeliveryMethod.new(raise_delivery_errors: true)
46
+ expect(m.settings[:raise_delivery_errors]).to eq(true)
47
+ end
48
+
49
+ it 'default return_response' do
50
+ m = DeliveryMethod.new()
51
+ expect(mailer.settings[:return_response]).to eq(nil)
22
52
  end
23
53
 
24
- it 'configures the client API key' do
25
- expect(mailer.client.api_key).to eq('key')
54
+ it 'sets return_response' do
55
+ m = DeliveryMethod.new(return_response: true)
56
+ expect(m.settings[:return_response]).to eq(true)
26
57
  end
27
58
  end
28
59
 
@@ -32,7 +63,7 @@ module SendGridActionMailer
32
63
  Mail.new(
33
64
  to: 'test@sendgrid.com',
34
65
  from: 'taco@cat.limo',
35
- subject: 'Hello, world!'
66
+ subject: 'Hello, world!',
36
67
  )
37
68
  end
38
69
 
@@ -44,7 +75,36 @@ module SendGridActionMailer
44
75
 
45
76
  it 'sets to' do
46
77
  mailer.deliver!(mail)
47
- expect(client.sent_mail.to).to eq(%w[test@sendgrid.com])
78
+ expect(client.sent_mail['personalizations'][0]).to include({"to"=>[{"email"=>"test@sendgrid.com"}]})
79
+ end
80
+
81
+ it 'returns mailer itself' do
82
+ ret = mailer.deliver!(mail)
83
+ expect(ret).to eq(mailer)
84
+ end
85
+
86
+ it 'returns api response' do
87
+ m = DeliveryMethod.new(return_response: true, api_key: 'key')
88
+ ret = m.deliver!(mail)
89
+ expect(ret.status_code).to eq('200')
90
+ end
91
+
92
+ context 'to with a friendly name' do
93
+ before { mail.to = 'Test SendGrid <test@sendgrid.com>' }
94
+
95
+ it 'sets to' do
96
+ mailer.deliver!(mail)
97
+ expect(client.sent_mail['personalizations'][0]).to include({"to"=>[{"email"=>"test@sendgrid.com", "name"=>"Test SendGrid"}]})
98
+ end
99
+ end
100
+
101
+ context 'to with a friendly name (with quotes)' do
102
+ before { mail.to = '"Test SendGrid" <test@sendgrid.com>' }
103
+
104
+ it 'sets to' do
105
+ mailer.deliver!(mail)
106
+ expect(client.sent_mail['personalizations'][0]).to include({"to"=>[{"email"=>"test@sendgrid.com", "name"=>"Test SendGrid"}]})
107
+ end
48
108
  end
49
109
 
50
110
  context 'there are ccs' do
@@ -52,7 +112,7 @@ module SendGridActionMailer
52
112
 
53
113
  it 'sets cc' do
54
114
  mailer.deliver!(mail)
55
- expect(client.sent_mail.cc).to eq(%w[burrito@cat.limo])
115
+ expect(client.sent_mail['personalizations'][0]).to include({"to"=>[{"email"=>"test@sendgrid.com"}], "cc"=>[{"email"=>"burrito@cat.limo"}]})
56
116
  end
57
117
  end
58
118
 
@@ -61,40 +121,44 @@ module SendGridActionMailer
61
121
 
62
122
  it 'sets bcc' do
63
123
  mailer.deliver!(mail)
64
- expect(client.sent_mail.bcc).to eq(%w[nachos@cat.limo])
124
+ expect(client.sent_mail['personalizations'][0]).to include({"to"=>[{"email"=>"test@sendgrid.com"}], "bcc"=>[{"email"=>"nachos@cat.limo"}]})
65
125
  end
66
126
  end
67
127
 
68
- context 'there is a reply to' do
69
- before { mail.reply_to = 'nachos@cat.limo' }
128
+ context 'there are bccs with a friendly name' do
129
+ before { mail.bcc = 'Taco Cat <nachos@cat.limo>' }
70
130
 
71
- it 'sets reply_to' do
131
+ it 'sets bcc' do
72
132
  mailer.deliver!(mail)
73
- expect(client.sent_mail.reply_to).to eq('nachos@cat.limo')
133
+ expect(client.sent_mail['personalizations'][0]).to include({"to"=>[{"email"=>"test@sendgrid.com"}], "bcc"=>[{"email"=>"nachos@cat.limo", "name"=>"Taco Cat"}]})
74
134
  end
75
135
  end
76
136
 
77
- context 'there is a reply to with a friendly name' do
78
- before { mail.reply_to = 'Taco Cat <nachos@cat.limo>' }
137
+ context 'there are bccs with a friendly name (with quotes)' do
138
+ before { mail.bcc = '"Taco Cat" <nachos@cat.limo>' }
79
139
 
80
- it 'sets reply_to' do
140
+ it 'sets bcc' do
81
141
  mailer.deliver!(mail)
82
- expect(client.sent_mail.reply_to).to eq('nachos@cat.limo')
142
+ expect(client.sent_mail['personalizations'][0]).to include({"to"=>[{"email"=>"test@sendgrid.com"}], "bcc"=>[{"email"=>"nachos@cat.limo", "name"=>"Taco Cat"}]})
83
143
  end
84
144
  end
85
145
 
86
- context 'there is a date' do
87
- before { mail.date = Time.utc(2016) }
146
+ context 'there is a reply to' do
147
+ before { mail.reply_to = 'nachos@cat.limo' }
88
148
 
89
- it 'sets date' do
149
+ it 'sets reply_to' do
90
150
  mailer.deliver!(mail)
91
- expect(client.sent_mail.date).to eq("Fri, 01 Jan 2016 00:00:00 +0000")
151
+ expect(client.sent_mail['reply_to']).to eq({'email' => 'nachos@cat.limo'})
92
152
  end
93
153
  end
94
154
 
95
- it 'sets from' do
96
- mailer.deliver!(mail)
97
- expect(client.sent_mail.from).to eq('taco@cat.limo')
155
+ context 'there is a reply to with a friendly name' do
156
+ before { mail.reply_to = 'Taco Cat <nachos@cat.limo>' }
157
+
158
+ it 'sets reply_to' do
159
+ mailer.deliver!(mail)
160
+ expect(client.sent_mail['reply_to']).to eq('email' => 'nachos@cat.limo', 'name' => 'Taco Cat')
161
+ end
98
162
  end
99
163
 
100
164
  context 'from contains a friendly name' do
@@ -102,341 +166,293 @@ module SendGridActionMailer
102
166
 
103
167
  it 'sets from' do
104
168
  mailer.deliver!(mail)
105
- expect(client.sent_mail.from).to eq('taco@cat.limo')
169
+ expect(client.sent_mail['from']).to eq('email' => 'taco@cat.limo', 'name' => 'Taco Cat')
106
170
  end
171
+ end
172
+
173
+ context 'from contains a friendly name (with quotes)' do
174
+ before { mail.from = '"Taco Cat" <taco@cat.limo>'}
107
175
 
108
- it 'sets from_name' do
176
+ it 'sets from' do
109
177
  mailer.deliver!(mail)
110
- expect(client.sent_mail.from_name).to eq('Taco Cat')
178
+ expect(client.sent_mail['from']).to eq('email' => 'taco@cat.limo', 'name' => 'Taco Cat')
111
179
  end
112
180
  end
113
181
 
114
182
  it 'sets subject' do
115
183
  mailer.deliver!(mail)
116
- expect(client.sent_mail.subject).to eq('Hello, world!')
184
+ expect(client.sent_mail['subject']).to eq('Hello, world!')
117
185
  end
118
186
 
119
187
  it 'sets a text/plain body' do
120
188
  mail.content_type = 'text/plain'
121
189
  mail.body = 'I heard you like pineapple.'
122
190
  mailer.deliver!(mail)
123
- expect(client.sent_mail.text).to eq('I heard you like pineapple.')
191
+ expect(client.sent_mail['content']).to eq([
192
+ {
193
+ 'type' => 'text/plain',
194
+ 'value' => 'I heard you like pineapple.'
195
+ }
196
+ ])
124
197
  end
125
198
 
126
199
  it 'sets a text/html body' do
127
200
  mail.content_type = 'text/html'
128
201
  mail.body = 'I heard you like <b>pineapple</b>.'
129
202
  mailer.deliver!(mail)
130
- expect(client.sent_mail.html).to eq('I heard you like <b>pineapple</b>.')
131
- end
132
203
 
133
- context 'multipart/alternative' do
134
- before do
135
- mail.content_type 'multipart/alternative'
136
- mail.part do |part|
137
- part.text_part = Mail::Part.new do
138
- content_type 'text/plain'
139
- body 'I heard you like pineapple.'
140
- end
141
- part.html_part = Mail::Part.new do
142
- content_type 'text/html'
143
- body 'I heard you like <b>pineapple</b>.'
144
- end
145
- end
146
- end
204
+ expect(client.sent_mail['content']).to eq([
205
+ {
206
+ 'type' => 'text/html',
207
+ 'value' => 'I heard you like <b>pineapple</b>.'
208
+ }
209
+ ])
210
+ end
147
211
 
148
- it 'sets the text body' do
212
+ context 'send options' do
213
+ it 'sets a template_id' do
214
+ mail['template_id'] = '1'
149
215
  mailer.deliver!(mail)
150
- expect(client.sent_mail.text).to eq('I heard you like pineapple.')
216
+ expect(client.sent_mail['template_id']).to eq('1')
151
217
  end
152
218
 
153
- it 'sets the html body' do
219
+ it 'sets sections' do
220
+ mail['sections'] = {'%foo%' => 'bar'}
154
221
  mailer.deliver!(mail)
155
- expect(client.sent_mail.html)
156
- .to eq('I heard you like <b>pineapple</b>.')
222
+ expect(client.sent_mail['sections']).to eq({'%foo%' => 'bar'})
157
223
  end
158
- end
159
224
 
160
- context 'multipart/mixed' do
161
- before do
162
- mail.content_type 'multipart/mixed'
163
- mail.part do |part|
164
- part.text_part = Mail::Part.new do
165
- content_type 'text/plain'
166
- body 'I heard you like pineapple.'
167
- end
168
- part.html_part = Mail::Part.new do
169
- content_type 'text/html'
170
- body 'I heard you like <b>pineapple</b>.'
171
- end
172
- end
173
- mail.attachments['specs.rb'] = File.read(__FILE__)
174
- end
175
-
176
- it 'sets the text body' do
225
+ it 'sets headers' do
226
+ mail['headers'] = {'X-FOO' => 'bar'}
177
227
  mailer.deliver!(mail)
178
- expect(client.sent_mail.text).to eq('I heard you like pineapple.')
228
+ expect(client.sent_mail['headers']).to eq({'X-FOO' => 'bar'})
179
229
  end
180
230
 
181
- it 'sets the html body' do
231
+ it 'sets categories' do
232
+ mail['categories'] = ['foo', 'bar']
182
233
  mailer.deliver!(mail)
183
- expect(client.sent_mail.html)
184
- .to eq('I heard you like <b>pineapple</b>.')
234
+ expect(client.sent_mail['categories']).to eq(['foo', 'bar'])
185
235
  end
186
236
 
187
- it 'adds the attachment' do
188
- expect(mail.attachments.first.read).to eq(File.read(__FILE__))
237
+ it 'sets custom_args' do
238
+ mail['custom_args'] = {'campaign' => 'welcome'}
189
239
  mailer.deliver!(mail)
190
- attachment = client.sent_mail.attachments.first
191
- expect(attachment[:name]).to eq('specs.rb')
192
- expect(attachment[:file].content_type.to_s).to eq('application/x-ruby')
193
- end
194
- end
195
-
196
- context 'multipart/related' do
197
- before do
198
- mail.content_type 'multipart/related'
199
- mail.part do |part|
200
- part.text_part = Mail::Part.new do
201
- content_type 'text/plain'
202
- body 'I heard you like pineapple.'
203
- end
204
- part.html_part = Mail::Part.new do
205
- content_type 'text/html'
206
- body 'I heard you like <b>pineapple</b>.'
207
- end
208
- end
209
- mail.attachments.inline['specs.rb'] = File.read(__FILE__)
240
+ expect(client.sent_mail['custom_args']).to eq({'campaign' => 'welcome'})
210
241
  end
211
242
 
212
- it 'sets the text body' do
243
+ it 'sets send_at and batch_id' do
244
+ epoch = Time.now.to_i
245
+ mail['send_at'] = epoch
246
+ mail['batch_id'] = 3
213
247
  mailer.deliver!(mail)
214
- expect(client.sent_mail.text).to eq('I heard you like pineapple.')
248
+ expect(client.sent_mail['send_at']).to eq(epoch)
249
+ expect(client.sent_mail['batch_id']).to eq('3')
215
250
  end
216
251
 
217
- it 'sets the html body' do
252
+ it 'sets asm' do
253
+ asm = {'group_id' => 99, 'groups_to_display' => [4,5,6,7,8]}
254
+ mail['asm'] = asm
218
255
  mailer.deliver!(mail)
219
- expect(client.sent_mail.html)
220
- .to eq('I heard you like <b>pineapple</b>.')
256
+ expect(client.sent_mail['asm']).to eq(asm)
221
257
  end
222
258
 
223
- it 'adds the inline attachment' do
224
- expect(mail.attachments.first.read).to eq(File.read(__FILE__))
259
+ it 'sets ip_pool_name' do
260
+ mail['ip_pool_name'] = 'marketing'
225
261
  mailer.deliver!(mail)
226
- content = client.sent_mail.contents.first
227
- expect(content[:name]).to eq('specs.rb')
228
- expect(content[:file].content_type.to_s).to eq('application/x-ruby')
229
- expect(content[:cid].class).to eq(String)
262
+ expect(client.sent_mail['ip_pool_name']).to eq('marketing')
230
263
  end
231
- end
232
-
233
- context 'SMTPAPI' do
234
- context 'it is not JSON' do
235
- before { mail['X-SMTPAPI'] = '<xml>JSON sucks!</xml>' }
236
264
 
237
- it 'raises a useful error' do
238
- expect { mailer.deliver!(mail) }.to raise_error(
239
- ArgumentError,
240
- "X-SMTPAPI is not JSON: <xml>JSON sucks!</xml>"
241
- )
242
- end
243
- end
244
-
245
- context 'filters are present' do
246
- before do
247
- mail['X-SMTPAPI'] = {
248
- filters: {
249
- clicktrack: {
250
- settings: {
251
- enable: 0
252
- }
253
- },
254
- dkim: {
255
- settings: {
256
- domain: 'example.com',
257
- use_from: false
258
- }
259
- }
260
- }
261
- }.to_json
262
- end
263
-
264
- it 'gets attached' do
265
+ context 'parse object' do
266
+ it "should parse 1.8 hash" do
267
+ asm = {'group_id' => 99, 'groups_to_display' => [4,5,6,7,8]}
268
+ mail['asm'] = asm
265
269
  mailer.deliver!(mail)
266
- expect(client.sent_mail.smtpapi.filters).to eq({
267
- 'clicktrack' => {
268
- 'settings' => {
269
- 'enable' => 0
270
- }
271
- },
272
- 'dkim' => {
273
- 'settings' => {
274
- 'domain' => 'example.com',
275
- 'use_from' => false
276
- }
277
- }
278
- })
270
+ expect(client.sent_mail['asm']).to eq({"group_id" => 99, "groups_to_display" => [4,5,6,7,8]})
279
271
  end
280
- end
281
272
 
282
- context 'a category is present' do
283
- before do
284
- mail['X-SMTPAPI'] = { category: 'food_feline' }.to_json
273
+ it "should parse 1.9 hash" do
274
+ asm = { group_id: 99, groups_to_display: [4,5,6,7,8]}
275
+ mail['asm'] = asm
276
+ mailer.deliver!(mail)
277
+ expect(client.sent_mail['asm']).to eq({"group_id" => 99, "groups_to_display" => [4,5,6,7,8]})
285
278
  end
286
279
 
287
- it 'gets attached' do
280
+ it "should parse json" do
281
+ asm = {'group_id' => 99, 'groups_to_display' => [4,5,6,7,8]}
282
+ mail['asm'] = asm.to_json
288
283
  mailer.deliver!(mail)
289
- expect(client.sent_mail.smtpapi.category).to eq('food_feline')
284
+ expect(client.sent_mail['asm']).to eq({"group_id" => 99, "groups_to_display" => [4,5,6,7,8]})
290
285
  end
291
286
  end
292
287
 
293
- context 'multiple categories are present' do
294
- before do
295
- mail['X-SMTPAPI'] = {
296
- category: %w[food_feline cuisine_canine]
297
- }.to_json
298
- end
299
-
300
- it 'attaches them all' do
288
+ context 'mail_settings' do
289
+ it 'sets bcc' do
290
+ bcc = { 'bcc' => { 'enable' => true, 'email' => 'test@example.com' }}
291
+ mail['mail_settings'] = bcc
301
292
  mailer.deliver!(mail)
302
- expect(client.sent_mail.smtpapi.category).to eq([
303
- 'food_feline',
304
- 'cuisine_canine',
305
- ])
293
+ expect(client.sent_mail['mail_settings']).to eq(bcc)
306
294
  end
307
- end
308
295
 
309
- context 'send_at is present' do
310
- before do
311
- mail['X-SMTPAPI'] = {
312
- send_at: 1409348513
313
- }.to_json
296
+ it 'sets bypass_list_management' do
297
+ bypass = { 'bypass_list_management' => { 'enable' => true }}
298
+ mail['mail_settings'] = bypass
299
+ mailer.deliver!(mail)
300
+ expect(client.sent_mail['mail_settings']).to eq(bypass)
314
301
  end
315
302
 
316
- it 'gets attached' do
303
+ it 'sets footer' do
304
+ footer = {'footer' => { 'enable' => true, 'text' => 'Footer Text', 'html' => '<html><body>Footer Text</body></html>'}}
305
+ mail['mail_settings'] = footer
317
306
  mailer.deliver!(mail)
318
- expect(client.sent_mail.smtpapi.send_at).to eq(1409348513)
307
+ expect(client.sent_mail['mail_settings']).to eq(footer)
319
308
  end
320
- end
321
309
 
322
- context 'send_each_at is present' do
323
- before do
324
- mail['X-SMTPAPI'] = {
325
- send_each_at: [1409348513, 1409348514]
326
- }.to_json
310
+ it 'sets sandbox_mode' do
311
+ sandbox = {'sandbox_mode' => { 'enable' => true }}
312
+ mail['mail_settings'] = sandbox
313
+ mailer.deliver!(mail)
314
+ expect(client.sent_mail['mail_settings']).to eq(sandbox)
327
315
  end
328
316
 
329
- it 'gets attached' do
317
+ it 'sets spam_check' do
318
+ spam_check = {'spam_check' => { 'enable' => true, 'threshold' => 1, 'post_to_url' => 'https://spamcatcher.sendgrid.com'}}
319
+ mail['mail_settings'] = spam_check
330
320
  mailer.deliver!(mail)
331
- expect(client.sent_mail.smtpapi.send_each_at).to eq([1409348513, 1409348514])
321
+ expect(client.sent_mail['mail_settings']).to eq(spam_check)
332
322
  end
333
323
  end
334
324
 
335
- context 'section is present' do
336
- before do
337
- mail['X-SMTPAPI'] = {
338
- section: {
339
- ":sectionName1" => "section 1 text",
340
- ":sectionName2" => "section 2 text"
341
- }
342
- }.to_json
325
+ context 'tracking_settings' do
326
+ it 'sets click_tracking' do
327
+ tracking = { 'click_tracking' => { 'enable' => false, 'enable_text' => false }}
328
+ mail['tracking_settings'] = tracking
329
+ mailer.deliver!(mail)
330
+ expect(client.sent_mail['tracking_settings']).to eq(tracking)
343
331
  end
344
332
 
345
- it 'gets attached' do
333
+ it 'sets open_tracking' do
334
+ tracking = { 'open_tracking' => { 'enable' => true, 'substitution_tag' => 'Optional tag to replace with the open image in the body of the message' }}
335
+ mail['tracking_settings'] = tracking
346
336
  mailer.deliver!(mail)
347
- expect(client.sent_mail.smtpapi.section).to eq({
348
- ":sectionName1" => "section 1 text",
349
- ":sectionName2" => "section 2 text"
350
- })
337
+ expect(client.sent_mail['tracking_settings']).to eq(tracking)
351
338
  end
352
- end
353
339
 
354
- context 'sub is present' do
355
- before do
356
- mail['X-SMTPAPI'] = {
357
- sub: {
358
- "-name-" => [
359
- "John",
360
- "Jane"
361
- ],
362
- "-customerID-" => [
363
- "1234",
364
- "5678"
365
- ],
366
- }
367
- }.to_json
340
+ it 'sets subscription_tracking' do
341
+ tracking = { 'subscription_tracking' => { 'enable' => true, 'text' => 'text to insert into the text/plain portion of the message', 'html' => 'html to insert into the text/html portion of the message', 'substitution_tag' => 'Optional tag to replace with the open image in the body of the def message' }}
342
+ mail['tracking_settings'] = tracking
343
+ mailer.deliver!(mail)
344
+ expect(client.sent_mail['tracking_settings']).to eq(tracking)
368
345
  end
369
346
 
370
- it 'gets attached' do
347
+ it 'sets ganalytics' do
348
+ tracking = { 'ganalytics' => {'enable' => true, 'utm_source' => 'some source', 'utm_medium' => 'some medium', 'utm_term' => 'some term', 'utm_content' => 'some content', 'utm_campaign' => 'some campaign' }}
349
+ mail['tracking_settings'] = tracking
371
350
  mailer.deliver!(mail)
372
- expect(client.sent_mail.smtpapi.sub).to eq({
373
- "-name-" => [
374
- "John",
375
- "Jane"
376
- ],
377
- "-customerID-" => [
378
- "1234",
379
- "5678"
380
- ],
381
- })
351
+ expect(client.sent_mail['tracking_settings']).to eq(tracking)
382
352
  end
383
353
  end
354
+ end
384
355
 
385
- context 'asm_group_id is present' do
386
- before do
387
- mail['X-SMTPAPI'] = {
388
- asm_group_id: 1
389
- }.to_json
356
+ context 'multipart/alternative' do
357
+ before do
358
+ mail.content_type 'multipart/alternative'
359
+ mail.part do |part|
360
+ part.text_part = Mail::Part.new do
361
+ content_type 'text/plain'
362
+ body 'I heard you like pineapple.'
363
+ end
364
+ part.html_part = Mail::Part.new do
365
+ content_type 'text/html'
366
+ body 'I heard you like <b>pineapple</b>.'
367
+ end
390
368
  end
369
+ end
391
370
 
392
- it 'gets attached' do
393
- mailer.deliver!(mail)
394
- expect(client.sent_mail.smtpapi.asm_group_id).to eq(1)
395
- end
371
+ it 'sets the text and html body' do
372
+ mailer.deliver!(mail)
373
+ expect(client.sent_mail['content']).to include({
374
+ 'type' => 'text/html',
375
+ 'value' => 'I heard you like <b>pineapple</b>.'
376
+ })
377
+ expect(client.sent_mail['content']).to include({
378
+ 'type' => 'text/plain',
379
+ 'value' => 'I heard you like pineapple.'
380
+ })
396
381
  end
382
+ end
397
383
 
398
- context 'unique_args are present' do
399
- before do
400
- mail['X-SMTPAPI'] = {
401
- unique_args: {
402
- customerAccountNumber: "55555",
403
- activationAttempt: "1",
404
- }
405
- }.to_json
384
+ context 'multipart/mixed' do
385
+ before do
386
+ mail.content_type 'multipart/mixed'
387
+ mail.part do |part|
388
+ part.text_part = Mail::Part.new do
389
+ content_type 'text/plain'
390
+ body 'I heard you like pineapple.'
391
+ end
392
+ part.html_part = Mail::Part.new do
393
+ content_type 'text/html'
394
+ body 'I heard you like <b>pineapple</b>.'
395
+ end
406
396
  end
397
+ mail.attachments['specs.rb'] = File.read(__FILE__)
398
+ end
407
399
 
408
- it 'gets attached' do
409
- mailer.deliver!(mail)
410
- expect(client.sent_mail.smtpapi.unique_args).to eq({
411
- "customerAccountNumber" => "55555",
412
- "activationAttempt" => "1",
413
- })
414
- end
400
+ it 'sets the text and html body' do
401
+ mailer.deliver!(mail)
402
+ expect(client.sent_mail['content']).to include({
403
+ 'type' => 'text/html',
404
+ 'value' => 'I heard you like <b>pineapple</b>.'
405
+ })
406
+ expect(client.sent_mail['content']).to include({
407
+ 'type' => 'text/plain',
408
+ 'value' => 'I heard you like pineapple.'
409
+ })
415
410
  end
416
411
 
417
- context 'ip_pool is present' do
418
- before do
419
- mail['X-SMTPAPI'] = {
420
- ip_pool: "pool_name"
421
- }.to_json
422
- end
412
+ it 'adds the attachment' do
413
+ expect(mail.attachments.first.read).to eq(File.read(__FILE__))
414
+ mailer.deliver!(mail)
415
+ attachment = client.sent_mail['attachments'].first
416
+ expect(attachment['filename']).to eq('specs.rb')
417
+ expect(attachment['type']).to eq('application/x-ruby')
418
+ end
419
+ end
423
420
 
424
- it 'gets attached' do
425
- mailer.deliver!(mail)
426
- expect(client.sent_mail.smtpapi.ip_pool).to eq("pool_name")
421
+ context 'multipart/related' do
422
+ before do
423
+ mail.content_type 'multipart/related'
424
+ mail.part do |part|
425
+ part.text_part = Mail::Part.new do
426
+ content_type 'text/plain'
427
+ body 'I heard you like pineapple.'
428
+ end
429
+ part.html_part = Mail::Part.new do
430
+ content_type 'text/html'
431
+ body 'I heard you like <b>pineapple</b>.'
432
+ end
427
433
  end
434
+ mail.attachments.inline['specs.rb'] = File.read(__FILE__)
428
435
  end
429
436
 
430
- context 'multiple X-SMTPAPI headers are present' do
431
- before do
432
- mail['X-SMTPAPI'] = { category: 'food_canine' }.to_json
433
- mail['X-SMTPAPI'] = { category: 'food_feline' }.to_json
434
- end
437
+ it 'sets the text and html body' do
438
+ mailer.deliver!(mail)
439
+ expect(client.sent_mail['content']).to include({
440
+ 'type' => 'text/html',
441
+ 'value' => 'I heard you like <b>pineapple</b>.'
442
+ })
443
+ expect(client.sent_mail['content']).to include({
444
+ 'type' => 'text/plain',
445
+ 'value' => 'I heard you like pineapple.'
446
+ })
447
+ end
435
448
 
436
- it 'uses the last header' do
437
- mailer.deliver!(mail)
438
- expect(client.sent_mail.smtpapi.category).to eq('food_canine')
439
- end
449
+ it 'adds the inline attachment' do
450
+ expect(mail.attachments.first.read).to eq(File.read(__FILE__))
451
+ mailer.deliver!(mail)
452
+ content = client.sent_mail['attachments'].first
453
+ expect(content['filename']).to eq('specs.rb')
454
+ expect(content['type']).to eq('application/x-ruby')
455
+ expect(content['content_id'].class).to eq(String)
440
456
  end
441
457
  end
442
458
  end