mailgun-ruby 1.1.8 → 1.2.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +5 -5
  2. data/.travis.yml +6 -5
  3. data/Gemfile +1 -1
  4. data/README.md +24 -2
  5. data/docs/Domains.md +0 -0
  6. data/docs/Webhooks.md +1 -1
  7. data/docs/railgun/Overview.md +11 -0
  8. data/docs/railgun/Parameters.md +83 -0
  9. data/lib/mailgun/client.rb +27 -6
  10. data/lib/mailgun/events/events.rb +1 -1
  11. data/lib/mailgun/exceptions/exceptions.rb +2 -0
  12. data/lib/mailgun/messages/batch_message.rb +1 -0
  13. data/lib/mailgun/messages/message_builder.rb +61 -6
  14. data/lib/mailgun/suppressions.rb +4 -1
  15. data/lib/mailgun/version.rb +1 -1
  16. data/lib/mailgun/webhooks/webhooks.rb +1 -1
  17. data/lib/railgun/mailer.rb +105 -13
  18. data/lib/railgun/message.rb +2 -1
  19. data/lib/railgun/railtie.rb +3 -2
  20. data/mailgun.gemspec +11 -11
  21. data/spec/integration/email_validation_spec.rb +8 -0
  22. data/spec/integration/events_spec.rb +1 -1
  23. data/spec/integration/mailer_spec.rb +67 -0
  24. data/spec/integration/mailgun_spec.rb +4 -1
  25. data/spec/integration/suppressions_spec.rb +18 -2
  26. data/spec/spec_helper.rb +3 -1
  27. data/spec/unit/connection/test_client.rb +16 -0
  28. data/spec/unit/events/events_spec.rb +19 -0
  29. data/spec/unit/mailgun_spec.rb +24 -2
  30. data/spec/unit/messages/batch_message_spec.rb +56 -40
  31. data/spec/unit/messages/message_builder_spec.rb +165 -17
  32. data/spec/unit/messages/sample_data/unknown.type +0 -0
  33. data/spec/unit/railgun/content_type_spec.rb +71 -0
  34. data/spec/unit/railgun/mailer_spec.rb +388 -0
  35. data/vcr_cassettes/mailer_invalid_domain.yml +109 -0
  36. data/vcr_cassettes/message_deliver.yml +149 -0
  37. data/vcr_cassettes/suppressions.yml +66 -15
  38. metadata +50 -26
  39. data/.ruby-version +0 -1
@@ -50,6 +50,16 @@ describe 'The method add_recipient' do
50
50
  expect(@mb_obj.counters[:recipients][recipient_type]).to eq(1)
51
51
  end
52
52
 
53
+ context 'when variables is empty and recepeint type - "to"' do
54
+ it 'adds email address as "to" recipient type and increments counter' do
55
+ recipient_type = :to
56
+ @mb_obj.add_recipient(recipient_type, @address, {})
57
+
58
+ expect(@mb_obj.message[recipient_type][0]).to eq("#{@address}")
59
+ expect(@mb_obj.counters[:recipients][recipient_type]).to eq(1)
60
+ end
61
+ end
62
+
53
63
  it 'adds a "cc" recipient type to the message body and counter is incremented' do
54
64
  recipient_type = :cc
55
65
  @mb_obj.add_recipient(recipient_type, @address, @variables)
@@ -196,7 +206,7 @@ describe 'The method from' do
196
206
  expect(@mb_obj.message[:from]).to eq([the_from_address])
197
207
  end
198
208
 
199
- it 'sets the from address with metadata' do
209
+ it 'sets the from address with first/last metadata' do
200
210
  the_from_address = 'test@mailgun.com'
201
211
  the_first_name = 'Magilla'
202
212
  the_last_name = 'Gorilla'
@@ -204,6 +214,21 @@ describe 'The method from' do
204
214
 
205
215
  expect(@mb_obj.message[:from]).to eq(["'#{the_first_name} #{the_last_name}' <#{the_from_address}>"])
206
216
  end
217
+
218
+ it 'sets the from address with full name metadata' do
219
+ the_from_address = 'test@mailgun.com'
220
+ full_name = 'Magilla Gorilla'
221
+ @mb_obj.from(the_from_address, {'full_name' => full_name})
222
+
223
+ expect(@mb_obj.message[:from]).to eq(["'#{full_name}' <#{the_from_address}>"])
224
+ end
225
+
226
+ it 'fails when first/last and full_name are used' do
227
+ the_from_address = 'test@mailgun.com'
228
+ full_name = 'Magilla Gorilla'
229
+ first_name = 'Magilla'
230
+ expect{@mb_obj.from(the_from_address, {'full_name' => full_name, 'first' => first_name})}.to raise_error(Mailgun::ParameterError)
231
+ end
207
232
  end
208
233
 
209
234
  describe 'The method add_attachment' do
@@ -231,6 +256,16 @@ describe 'The method add_attachment' do
231
256
  expect(@mb_obj.message[:attachment].length).to eq(1)
232
257
  expect(@mb_obj.message[:attachment].first.original_filename).to eq 'mailgun_icon.png'
233
258
  end
259
+
260
+ context 'when attachment has unknown type' do
261
+ it 'sets content type application/octet-stream for attachment' do
262
+ file = File.dirname(__FILE__) + "/sample_data/unknown.type"
263
+
264
+ @mb_obj.add_attachment(file)
265
+
266
+ expect(@mb_obj.message[:attachment][0].content_type).to eq('application/octet-stream')
267
+ end
268
+ end
234
269
  end
235
270
 
236
271
  describe 'The method add_inline_image' do
@@ -278,7 +313,7 @@ describe 'The method set_test_mode' do
278
313
  it 'warns of set_test_mode deprecation' do
279
314
  @mb_obj = Mailgun::MessageBuilder.new
280
315
  expect(@mb_obj).to receive :warn
281
- @mb_obj.set_test_mode 'warn on set_test_mode'
316
+ @mb_obj.set_test_mode 'Yes'
282
317
  end
283
318
  end
284
319
 
@@ -320,7 +355,7 @@ describe 'The method set_dkim' do
320
355
  it 'warns of set_dkim deprecation' do
321
356
  @mb_obj = Mailgun::MessageBuilder.new
322
357
  expect(@mb_obj).to receive :warn
323
- @mb_obj.set_dkim 'warn on set_dkim'
358
+ @mb_obj.set_dkim 'Yes'
324
359
  end
325
360
  end
326
361
 
@@ -414,7 +449,7 @@ describe 'The method set_open_tracking' do
414
449
  it 'warns of set_open_tracking deprecation' do
415
450
  @mb_obj = Mailgun::MessageBuilder.new
416
451
  expect(@mb_obj).to receive :warn
417
- @mb_obj.set_open_tracking 'warn on set_open_tracking'
452
+ @mb_obj.set_open_tracking 'Yes'
418
453
  end
419
454
  end
420
455
 
@@ -425,19 +460,20 @@ describe 'The method track_opens' do
425
460
  it 'enables/disables open tracking on a per message basis.' do
426
461
  @mb_obj.track_opens('Yes')
427
462
 
428
- expect(@mb_obj.message["o:tracking-opens"][0]).to eq("yes")
463
+ expect(@mb_obj.message["o:tracking-opens"]).to eq("yes")
464
+ expect(@mb_obj.message["o:tracking"]).to eq(["yes"])
429
465
 
430
466
  @mb_obj.track_opens('No')
431
467
 
432
- expect(@mb_obj.message["o:tracking-opens"][0]).to eq("no")
468
+ expect(@mb_obj.message["o:tracking-opens"]).to eq("no")
433
469
 
434
470
  @mb_obj.track_opens(true)
435
471
 
436
- expect(@mb_obj.message["o:tracking-opens"][0]).to eq("yes")
472
+ expect(@mb_obj.message["o:tracking-opens"]).to eq("yes")
437
473
 
438
474
  @mb_obj.track_opens(false)
439
475
 
440
- expect(@mb_obj.message["o:tracking-opens"][0]).to eq("no")
476
+ expect(@mb_obj.message["o:tracking-opens"]).to eq("no")
441
477
  end
442
478
  end
443
479
 
@@ -445,7 +481,7 @@ describe 'The method set_click_tracking' do
445
481
  it 'warns of set_click_tracking deprecation' do
446
482
  @mb_obj = Mailgun::MessageBuilder.new
447
483
  expect(@mb_obj).to receive :warn
448
- @mb_obj.set_click_tracking 'warn on set_click_tracking'
484
+ @mb_obj.set_click_tracking 'Yes'
449
485
  end
450
486
  end
451
487
 
@@ -456,23 +492,31 @@ describe 'The method track_clicks' do
456
492
  it 'enables/disables click tracking on a per message basis.' do
457
493
  @mb_obj.track_clicks('Yes')
458
494
 
459
- expect(@mb_obj.message["o:tracking-clicks"][0]).to eq("yes")
495
+ expect(@mb_obj.message["o:tracking-clicks"]).to eq("yes")
496
+ expect(@mb_obj.message["o:tracking"]).to eq(["yes"])
460
497
 
461
498
  @mb_obj.track_clicks('No')
462
499
 
463
- expect(@mb_obj.message["o:tracking-clicks"][0]).to eq("no")
500
+ expect(@mb_obj.message["o:tracking-clicks"]).to eq("no")
464
501
 
465
502
  @mb_obj.track_clicks(true)
466
503
 
467
- expect(@mb_obj.message["o:tracking-clicks"][0]).to eq("yes")
504
+ expect(@mb_obj.message["o:tracking-clicks"]).to eq("yes")
468
505
 
469
506
  @mb_obj.track_clicks(false)
470
507
 
471
- expect(@mb_obj.message["o:tracking-clicks"][0]).to eq("no")
508
+ expect(@mb_obj.message["o:tracking-clicks"]).to eq("no")
472
509
 
473
510
  @mb_obj.track_clicks('html')
474
511
 
475
- expect(@mb_obj.message["o:tracking-clicks"][0]).to eq("html")
512
+ expect(@mb_obj.message["o:tracking-clicks"]).to eq("html")
513
+ end
514
+
515
+ context 'when unexpected value is provided' do
516
+ it 'warns about prefered values' do
517
+ expect(@mb_obj).to receive :warn
518
+ @mb_obj.track_clicks('random')
519
+ end
476
520
  end
477
521
  end
478
522
 
@@ -532,9 +576,13 @@ describe 'The method variable' do
532
576
  expect(@mb_obj.message["v:my-data"]).to be_kind_of(String)
533
577
  expect(@mb_obj.message["v:my-data"].to_s).to eq('{"key":"value"}')
534
578
  end
535
- it 'throws an exception on broken JSON.' do
536
- data = 'This is some crappy JSON.'
537
- expect {@mb_obj.variable('my-data', data)}.to raise_error(Mailgun::ParameterError)
579
+ it 'accepts string values' do
580
+ data = 'String Value.'
581
+
582
+ @mb_obj.variable('my-data', data)
583
+
584
+ expect(@mb_obj.message["v:my-data"]).to be_kind_of(String)
585
+ expect(@mb_obj.message["v:my-data"].to_s).to eq('String Value.')
538
586
  end
539
587
  end
540
588
 
@@ -582,3 +630,103 @@ describe 'The method message_id' do
582
630
  expect(@mb_obj.message.has_key?('h:Message-Id')).to eq(false)
583
631
  end
584
632
  end
633
+
634
+ describe 'The method template' do
635
+ before(:each) do
636
+ @mb_obj = Mailgun::MessageBuilder.new
637
+ end
638
+ context 'when template name is passed' do
639
+ it 'sets `template` to the message' do
640
+ template_name = 'template.name'
641
+ @mb_obj.template(template_name)
642
+
643
+ expect(@mb_obj.message['template']).to eq(template_name)
644
+ end
645
+ end
646
+
647
+ context 'when multiple values are passed' do
648
+ it 'sets the last value as message template' do
649
+ template_name_1 = 'template.name_1'
650
+ template_name_2 = 'template.name_2'
651
+
652
+ @mb_obj.template(template_name_1)
653
+ @mb_obj.template(template_name_2)
654
+
655
+ expect(@mb_obj.message['template']).to eq(template_name_2)
656
+ end
657
+ end
658
+
659
+ context 'when template name is not passed' do
660
+ it 'it deletes `template` key from the message' do
661
+ @mb_obj.template('template.name')
662
+
663
+ expect(@mb_obj.message.has_key?('template')).to eq(true)
664
+
665
+ @mb_obj.template
666
+
667
+ expect(@mb_obj.message.has_key?('template')).to eq(false)
668
+ end
669
+ end
670
+ end
671
+
672
+ describe 'The method template_version' do
673
+ before(:each) do
674
+ @mb_obj = Mailgun::MessageBuilder.new
675
+ end
676
+ context 'when template version is passed' do
677
+ it 'adds `t:version` key value to the message' do
678
+ version = 'version_1'
679
+ @mb_obj.template_version(version)
680
+
681
+ expect(@mb_obj.message['t:version']).to eq(version)
682
+ end
683
+ end
684
+
685
+ context 'when multiple values are passed' do
686
+ it 'adds the last value as `t:version` key value to the message' do
687
+ version_1 = 'version_1'
688
+ version_2 = 'version_2'
689
+
690
+ @mb_obj.template_version(version_1)
691
+ @mb_obj.template_version(version_2)
692
+
693
+ expect(@mb_obj.message['t:version']).to eq(version_2)
694
+ end
695
+ end
696
+
697
+ context 'when version is not passed' do
698
+ it 'it deletes `t:version` key from the message' do
699
+ @mb_obj.template_version('version')
700
+
701
+ expect(@mb_obj.message.has_key?('t:version')).to eq(true)
702
+
703
+ @mb_obj.template_version
704
+
705
+ expect(@mb_obj.message.has_key?('t:version')).to eq(false)
706
+ end
707
+ end
708
+ end
709
+
710
+ describe 'The method template_text' do
711
+ before(:each) do
712
+ @mb_obj = Mailgun::MessageBuilder.new
713
+ end
714
+
715
+ it 'enables/disables rendering in the text part of the message in case of template sending' do
716
+ @mb_obj.template_text('Yes')
717
+
718
+ expect(@mb_obj.message["t:text"]).to eq("yes")
719
+
720
+ @mb_obj.template_text('No')
721
+
722
+ expect(@mb_obj.message["t:text"]).to eq("no")
723
+
724
+ @mb_obj.template_text(true)
725
+
726
+ expect(@mb_obj.message["t:text"]).to eq("yes")
727
+
728
+ @mb_obj.template_text(false)
729
+
730
+ expect(@mb_obj.message["t:text"]).to eq("no")
731
+ end
732
+ end
File without changes
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+ require 'mailgun'
3
+ require 'railgun'
4
+
5
+ describe 'extract_body' do
6
+
7
+ let(:text_mail_option) {
8
+ {
9
+ from: 'bob@example.com',
10
+ to: 'sally@example.com',
11
+ subject: 'RAILGUN TEST SAMPLE',
12
+ body: text_content,
13
+ content_type: 'text/plain',
14
+ }
15
+ }
16
+ let(:text_content) { '[TEST] Hello, world.' }
17
+
18
+ let(:html_mail_option) {
19
+ {
20
+ from: 'bob@example.com',
21
+ to: 'sally@example.com',
22
+ subject: 'RAILGUN TEST SAMPLE',
23
+ body: html_content,
24
+ content_type: 'text/html',
25
+ }
26
+ }
27
+ let(:html_content) { '<h3> [TEST] </h3> <br/> Hello, world!' }
28
+
29
+ context 'with <Content-Type: text/plain>' do
30
+ let(:sample_mail) { Mail.new(text_mail_option) }
31
+
32
+ it 'should return body text' do
33
+ expect(Railgun.extract_body_text(sample_mail)).to eq(text_content)
34
+ end
35
+
36
+ it 'should not return body html' do
37
+ expect(Railgun.extract_body_html(sample_mail)).to be_nil
38
+ end
39
+ end
40
+
41
+ context 'with <Content-Type: text/html>' do
42
+ let(:sample_mail) { Mail.new(html_mail_option) }
43
+
44
+ it 'should not return body text' do
45
+ expect(Railgun.extract_body_text(sample_mail)).to be_nil
46
+ end
47
+
48
+ it 'should return body html' do
49
+ expect(Railgun.extract_body_html(sample_mail)).to eq(html_content)
50
+ end
51
+ end
52
+
53
+ context 'with <Content-Type: multipart/alternative>' do
54
+ let(:text_mail) { Mail.new(text_mail_option) }
55
+ let(:html_mail) { Mail.new(html_mail_option) }
56
+
57
+ before do
58
+ @sample_mail = Mail::Part.new(content_type: "multipart/alternative")
59
+ @sample_mail.add_part text_mail
60
+ @sample_mail.add_part html_mail
61
+ end
62
+
63
+ it 'should return body text' do
64
+ expect(Railgun.extract_body_text(@sample_mail)).to eq(text_content)
65
+ end
66
+
67
+ it 'should return body html' do
68
+ expect(Railgun.extract_body_html(@sample_mail)).to eq(html_content)
69
+ end
70
+ end
71
+ end