mailgun-ruby 1.1.10 → 1.2.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -11,7 +11,8 @@ module Mail
11
11
  attr_accessor :mailgun_variables,
12
12
  :mailgun_options,
13
13
  :mailgun_recipient_variables,
14
- :mailgun_headers
14
+ :mailgun_headers,
15
+ :mailgun_template_variables
15
16
 
16
17
  end
17
18
  end
@@ -2,8 +2,9 @@ require 'railgun/mailer'
2
2
 
3
3
  module Railgun
4
4
  class Railtie < ::Rails::Railtie
5
- config.before_configuration do
6
- ActionMailer::Base.add_delivery_method :mailgun, Railgun::Mailer
5
+ ActiveSupport.on_load(:action_mailer) do
6
+ add_delivery_method :mailgun, Railgun::Mailer
7
+ ActiveSupport.run_load_hooks(:mailgun_mailer, Railgun::Mailer)
7
8
  end
8
9
  end
9
10
  end
data/mailgun.gemspec CHANGED
@@ -22,16 +22,16 @@ Gem::Specification.new do |spec|
22
22
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
23
23
  spec.require_paths = ["lib"]
24
24
 
25
- spec.required_ruby_version = '>= 2.0.0'
26
-
27
- spec.add_development_dependency 'bundler', '~> 1.5'
28
- spec.add_development_dependency 'rspec', '~> 3.0'
29
- spec.add_development_dependency 'rake', '~> 10.0'
30
- spec.add_development_dependency 'webmock', '~> 1.22'
31
- spec.add_development_dependency 'pry', '~> 0.9'
32
- spec.add_development_dependency 'vcr', '~> 3.0'
33
- spec.add_development_dependency 'simplecov', '~> 0.11'
34
- spec.add_development_dependency "rails"
35
- spec.add_dependency 'rest-client', '~> 2.0'
25
+ spec.required_ruby_version = '>= 2.2.2'
26
+
27
+ spec.add_development_dependency 'bundler', '>= 1.16.2'
28
+ spec.add_development_dependency 'rspec', '~> 3.8.0'
29
+ spec.add_development_dependency 'rake', '~> 12.3.2'
30
+ spec.add_development_dependency 'webmock', '~> 3.4.2'
31
+ spec.add_development_dependency 'pry', '~> 0.11.3'
32
+ spec.add_development_dependency 'vcr', '~> 3.0.3'
33
+ spec.add_development_dependency 'simplecov', '~> 0.16.1'
34
+ spec.add_development_dependency 'rails'
35
+ spec.add_dependency 'rest-client', '>= 2.0.2'
36
36
 
37
37
  end
@@ -28,7 +28,11 @@ describe 'For the email validation endpoint', order: :defined, vcr: vcr_opts do
28
28
  expected = {
29
29
  "address" => "alice@mailgun.net",
30
30
  "did_you_mean" => nil,
31
+ "is_disposable_address" => false,
32
+ "is_role_address" => false,
31
33
  "is_valid" => true,
34
+ "mailbox_verification" => "true",
35
+ "reason" => nil,
32
36
  "parts" => {
33
37
  "display_name" => nil,
34
38
  "domain" => "mailgun.net",
@@ -50,7 +54,11 @@ describe 'For the email validation endpoint', order: :defined, vcr: vcr_opts do
50
54
  expected = {
51
55
  "address" => "example.org",
52
56
  "did_you_mean" => nil,
57
+ "is_disposable_address" => false,
58
+ "is_role_address" => false,
53
59
  "is_valid" => false,
60
+ "mailbox_verification" => "unknown",
61
+ "reason" => "Validation failed for 'example.org', reason: 'malformed address; missing @ sign'",
54
62
  "parts" => {
55
63
  "display_name" => nil,
56
64
  "domain" => nil,
@@ -22,7 +22,7 @@ describe 'For the Events endpoint', vcr: vcr_opts do
22
22
 
23
23
  it 'can iterate over all events with `each`' do
24
24
  @events.each do |e|
25
- expect(e.id).to eq("JAx9z641TuGGUyaJlD9sCQ")
25
+ expect(e["id"]).to eq("JAx9z641TuGGUyaJlD9sCQ")
26
26
  end
27
27
  end
28
28
  end
@@ -0,0 +1,67 @@
1
+ require 'spec_helper'
2
+ require 'json'
3
+ require 'logger'
4
+ require 'railgun'
5
+ require 'mailgun'
6
+ require 'mailgun/exceptions/exceptions'
7
+
8
+ ActionMailer::Base.raise_delivery_errors = true
9
+ Rails.logger = Logger.new('/dev/null')
10
+ Rails.logger.level = Logger::DEBUG
11
+
12
+ class UnitTestMailer < ActionMailer::Base
13
+ default from: 'unittest@example.org'
14
+
15
+ def plain_message(address, from, subject, headers)
16
+ headers(headers)
17
+ mail(to: address, from: from, subject: subject) do |format|
18
+ format.text { render plain: 'Test!' }
19
+ format.html { render html: '<p>Test!</p>'.html_safe }
20
+ end
21
+ end
22
+ end
23
+
24
+ vcr_opts = { :cassette_name => 'message_deliver' }
25
+
26
+ describe 'Message deliver', vcr: vcr_opts do
27
+ let(:domain) { TESTDOMAIN }
28
+ let(:config) do
29
+ {
30
+ api_key: APIKEY,
31
+ domain: domain
32
+ }
33
+ end
34
+ let(:mail) { UnitTestMailer.plain_message("bob@#{domain}", "bob@#{domain}", 'subject', {}) }
35
+
36
+ it 'successfully delivers message' do
37
+ result = Railgun::Mailer.new(config).deliver!(mail)
38
+ result.to_h!
39
+
40
+ expect(result.body['message']).to eq('Queued. Thank you.')
41
+ expect(result.body).to include('id')
42
+ expect(result.code).to eq(200)
43
+ end
44
+ end
45
+
46
+ vcr_opts = { :cassette_name => 'mailer_invalid_domain' }
47
+
48
+ describe 'Invalid domain', vcr: vcr_opts do
49
+ let(:domain) { 'not-our-doma.in' }
50
+ let(:config) do
51
+ {
52
+ api_key: APIKEY,
53
+ domain: domain
54
+ }
55
+ end
56
+ let(:mail) { UnitTestMailer.plain_message("bob@#{domain}", 'sally@not-our-doma.in' 'subject', {}) }
57
+
58
+ it 'raises expected error' do
59
+
60
+ Railgun::Mailer.new(config).deliver!(mail)
61
+ rescue Mailgun::CommunicationError => err
62
+ expect(err.message).to eq('401 Unauthorized: Forbidden - Invalid Domain or API key')
63
+ else
64
+ fail
65
+
66
+ end
67
+ end
@@ -63,6 +63,9 @@ describe 'The method send_message()', vcr: vcr_opts do
63
63
  :to => "bob@#{@domain}",
64
64
  :subject => "Test",
65
65
  :text => "Test Data" }
66
+ uuid = 'uuid'
67
+
68
+ allow(SecureRandom).to receive(:uuid).and_return(uuid)
66
69
 
67
70
  result = @mg_obj.send_message(@domain, data)
68
71
 
@@ -72,7 +75,7 @@ describe 'The method send_message()', vcr: vcr_opts do
72
75
  expect(result.body).to include("id")
73
76
 
74
77
  expect(result.code).to eq(200)
75
- expect(result.body['id']).to eq("test-mode-mail@localhost")
78
+ expect(result.body['id']).to eq("test-mode-mail-#{uuid}@localhost")
76
79
  expect(result.body['message']).to eq("Queued. Thank you.")
77
80
  end
78
81
 
@@ -52,7 +52,7 @@ describe 'For the suppressions handling class', order: :defined, vcr: vcr_opts d
52
52
  end
53
53
  end
54
54
 
55
- it 'can batch-add unsubscribes' do
55
+ it 'can batch-add unsubscribes with tags as string' do
56
56
  unsubscribes = []
57
57
  @addresses.each do |addr|
58
58
  unsubscribes.push({
@@ -69,6 +69,23 @@ describe 'For the suppressions handling class', order: :defined, vcr: vcr_opts d
69
69
  expect(nested.length).to eq(0)
70
70
  end
71
71
 
72
+ it 'can batch-add unsubscribes with tags as array' do
73
+ unsubscribes = []
74
+ @addresses.each do |addr|
75
+ unsubscribes.push({
76
+ :address => addr,
77
+ :tags => ['integration'],
78
+ })
79
+ end
80
+
81
+ response, nested = @suppress.create_unsubscribes unsubscribes
82
+ response.to_h!
83
+
84
+ expect(response.code).to eq(200)
85
+ expect(response.body['message']).to eq('4 addresses have been added to the unsubscribes table')
86
+ expect(nested.length).to eq(0)
87
+ end
88
+
72
89
  it 'raises ParameterError if no unsubscribe[:address] is present' do
73
90
  unsubscribes = []
74
91
  unsubscribes.push({
@@ -123,4 +140,3 @@ describe 'For the suppressions handling class', order: :defined, vcr: vcr_opts d
123
140
 
124
141
  # TODO: Add tests for pagination support.
125
142
  end
126
-
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'rubygems'
2
+ require 'base64'
2
3
  require 'bundler'
3
4
  require 'bundler/setup'
4
5
  Bundler.setup(:development)
@@ -37,9 +38,10 @@ TESTDOMAIN = envs['MAILGUN_TESTDOMAIN']
37
38
  VCR.configure do |c|
38
39
  c.cassette_library_dir = 'vcr_cassettes'
39
40
  c.hook_into :webmock
40
- c.configure_rspec_metadata!
41
41
  c.default_cassette_options = { record: :new_episodes }
42
42
  c.filter_sensitive_data('<APIKEY>') { APIKEY }
43
43
  c.filter_sensitive_data('DOMAIN.TEST') { TESTDOMAIN }
44
44
  c.filter_sensitive_data('<PUBKEY>') { PUB_APIKEY }
45
+
46
+ c.configure_rspec_metadata!
45
47
  end
@@ -23,6 +23,7 @@ module Mailgun
23
23
  end
24
24
 
25
25
  def send_message(working_domain, data)
26
+ perform_data_validation(working_domain, data)
26
27
  case data
27
28
  when Hash
28
29
  if data.has_key?(:message)
@@ -75,6 +76,21 @@ module Mailgun
75
76
 
76
77
  private
77
78
 
79
+ def perform_data_validation(working_domain, data)
80
+ fail ParameterError.new('Missing working domain', working_domain) unless working_domain
81
+ return true unless data.is_a?(Hash) && data.present?
82
+ message = data.respond_to?(:message) ? data.message : data
83
+
84
+ fail ParameterError.new(
85
+ 'Missing `to` recipient, message should containg at least 1 recipient',
86
+ working_domain
87
+ ) if message.fetch('to', []).empty? && message.fetch(:to, []).empty?
88
+ fail ParameterError.new(
89
+ 'Missing a `from` sender, message should containg at least 1 `from` sender',
90
+ working_domain
91
+ ) if message.fetch('from', []).empty? && message.fetch(:from, []).empty?
92
+ end
93
+
78
94
  def response_generator(resource_endpoint)
79
95
  if resource_endpoint == "messages"
80
96
  t = Time.now
@@ -37,8 +37,11 @@ describe 'The method send_message()' do
37
37
  end
38
38
 
39
39
  it 'opens the message MIME and sends the MIME message.' do
40
- data = {'to' => 'joe@test.com',
41
- 'message' => 'Sample Data/mime.txt'}
40
+ data = {
41
+ 'to' => 'joe@test.com',
42
+ 'message' => 'Sample Data/mime.txt',
43
+ 'from' => 'joe@test.com'
44
+ }
42
45
  result = @mg_obj.send_message("testdomain.com", data)
43
46
 
44
47
  result.to_h!
@@ -46,6 +49,25 @@ describe 'The method send_message()' do
46
49
  expect(result.body).to include("message")
47
50
  expect(result.body).to include("id")
48
51
  end
52
+
53
+ context 'when domain is missing' do
54
+ it 'shows failure message' do
55
+ expect(@mg_obj).to receive(:fail)
56
+ @mg_obj.send_message(nil, {})
57
+ end
58
+ end
59
+
60
+ context 'when to is missing' do
61
+ it 'shows failure message' do
62
+ data = {
63
+ 'to' => '',
64
+ 'message' => 'Sample Data/mime.txt',
65
+ 'from' => 'joe@test.com'
66
+ }
67
+ expect(@mg_obj).to receive(:fail)
68
+ @mg_obj.send_message("testdomain.com", data)
69
+ end
70
+ end
49
71
  end
50
72
 
51
73
  describe 'The method post()' do
@@ -72,60 +72,76 @@ describe 'The method add_recipient' do
72
72
  @address_3 = 'sam@example.com'
73
73
  @variables_3 = {'first' => 'Sam', 'last' => 'Doe', 'tracking' => 'GHI123'}
74
74
  end
75
-
76
- it 'adds 1,000 recipients to the message body and validates counter is incremented then reset' do
77
- recipient_type = :to
78
- 1000.times do
79
- @mb_obj.add_recipient(recipient_type, @address_1, @variables_1)
75
+ context 'when from is present' do
76
+ before(:each) do
77
+ @mb_obj.from('example@email.com')
80
78
  end
81
79
 
82
- expect(@mb_obj.counters[:recipients][recipient_type]).to eq(1000)
83
-
84
- @mb_obj.add_recipient(recipient_type, @address_1, @variables_1)
85
-
86
- expect(@mb_obj.counters[:recipients][recipient_type]).to eq(1)
87
- end
80
+ it 'adds 1,000 recipients to the message body and validates counter is incremented then reset' do
81
+ recipient_type = :to
82
+ 1000.times do
83
+ @mb_obj.add_recipient(recipient_type, @address_1, @variables_1)
84
+ end
88
85
 
89
- it 'adds recipients to the message, calls finalize, and cleans up' do
90
- recipient_type = :to
91
- 1000.times do
86
+ expect(@mb_obj.counters[:recipients][recipient_type]).to eq(1000)
87
+
92
88
  @mb_obj.add_recipient(recipient_type, @address_1, @variables_1)
89
+
90
+ expect(@mb_obj.counters[:recipients][recipient_type]).to eq(1)
93
91
  end
94
92
 
95
- expect(@mb_obj.counters[:recipients][recipient_type]).to eq(1000)
96
- @mb_obj.finalize
93
+ it 'adds recipients to the message, calls finalize, and cleans up' do
94
+ recipient_type = :to
95
+ 1000.times do
96
+ @mb_obj.add_recipient(recipient_type, @address_1, @variables_1)
97
+ end
97
98
 
98
- expect(@mb_obj.message['recipient-variables'].length).to eq(0)
99
- expect(@mb_obj.message[:to].length).to eq(0)
100
- expect(@mb_obj.counters[:recipients][recipient_type]).to eq(0)
101
- end
99
+ expect(@mb_obj.counters[:recipients][recipient_type]).to eq(1000)
100
+ @mb_obj.finalize
102
101
 
103
- it 'adds 5,005 recipients to the message body and validates we receive message_ids back' do
104
- recipient_type = :to
105
- 5005.times do
106
- @mb_obj.add_recipient(recipient_type, @address_1, @variables_1)
102
+ expect(@mb_obj.recipient_variables).to eq({})
103
+ expect(@mb_obj.message['recipient-variables'].length).to eq(0)
104
+ expect(@mb_obj.message[:to].length).to eq(0)
105
+ expect(@mb_obj.counters[:recipients][recipient_type]).to eq(0)
107
106
  end
108
- @mb_obj.finalize
109
107
 
110
- expect(@mb_obj.message_ids.length).to eq(6)
111
- end
108
+ it 'adds 5,005 recipients to the message body and validates we receive message_ids back' do
109
+ recipient_type = :to
110
+ 5005.times do
111
+ @mb_obj.add_recipient(recipient_type, @address_1, @variables_1)
112
+ end
113
+ @mb_obj.finalize
112
114
 
113
- it 'sets recipient-variables, for batch expansion' do
114
- recipient_type = :to
115
- @mb_obj.add_recipient(recipient_type, @address_1, @variables_1)
115
+ expect(@mb_obj.message_ids.length).to eq(6)
116
+ end
116
117
 
117
- expect(@mb_obj.recipient_variables[@address_1]).to eq(@variables_1)
118
+ it 'sets recipient-variables, for batch expansion' do
119
+ recipient_type = :to
120
+ @mb_obj.add_recipient(recipient_type, @address_1, @variables_1)
121
+
122
+ expect(@mb_obj.recipient_variables[@address_1]).to eq(@variables_1)
123
+ end
124
+
125
+ it 'sets multiple recipient-variables, for batch expansion' do
126
+ recipient_type = :to
127
+ @mb_obj.add_recipient(recipient_type, @address_1, @variables_1)
128
+ @mb_obj.add_recipient(recipient_type, @address_2, @variables_2)
129
+ @mb_obj.add_recipient(recipient_type, @address_3, @variables_3)
130
+
131
+ expect(@mb_obj.recipient_variables[@address_1]).to eq(@variables_1)
132
+ expect(@mb_obj.recipient_variables[@address_2]).to eq(@variables_2)
133
+ expect(@mb_obj.recipient_variables[@address_3]).to eq(@variables_3)
134
+ end
118
135
  end
119
136
 
120
- it 'sets multiple recipient-variables, for batch expansion' do
121
- recipient_type = :to
122
- @mb_obj.add_recipient(recipient_type, @address_1, @variables_1)
123
- @mb_obj.add_recipient(recipient_type, @address_2, @variables_2)
124
- @mb_obj.add_recipient(recipient_type, @address_3, @variables_3)
125
-
126
- expect(@mb_obj.recipient_variables[@address_1]).to eq(@variables_1)
127
- expect(@mb_obj.recipient_variables[@address_2]).to eq(@variables_2)
128
- expect(@mb_obj.recipient_variables[@address_3]).to eq(@variables_3)
137
+ context 'when from is empty' do
138
+ it 'shows error message' do
139
+ recipient_type = :to
140
+ @mb_obj.add_recipient(recipient_type, @address_1, @variables_1)
141
+ @mb_obj.add_recipient(recipient_type, @address_2, @variables_2)
142
+ expect(@mb_client).to receive(:fail)
143
+ @mb_obj.finalize
144
+ end
129
145
  end
130
146
 
131
147
  end
@@ -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)
@@ -246,6 +256,16 @@ describe 'The method add_attachment' do
246
256
  expect(@mb_obj.message[:attachment].length).to eq(1)
247
257
  expect(@mb_obj.message[:attachment].first.original_filename).to eq 'mailgun_icon.png'
248
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
249
269
  end
250
270
 
251
271
  describe 'The method add_inline_image' do
@@ -293,7 +313,7 @@ describe 'The method set_test_mode' do
293
313
  it 'warns of set_test_mode deprecation' do
294
314
  @mb_obj = Mailgun::MessageBuilder.new
295
315
  expect(@mb_obj).to receive :warn
296
- @mb_obj.set_test_mode 'warn on set_test_mode'
316
+ @mb_obj.set_test_mode 'Yes'
297
317
  end
298
318
  end
299
319
 
@@ -335,7 +355,7 @@ describe 'The method set_dkim' do
335
355
  it 'warns of set_dkim deprecation' do
336
356
  @mb_obj = Mailgun::MessageBuilder.new
337
357
  expect(@mb_obj).to receive :warn
338
- @mb_obj.set_dkim 'warn on set_dkim'
358
+ @mb_obj.set_dkim 'Yes'
339
359
  end
340
360
  end
341
361
 
@@ -429,7 +449,7 @@ describe 'The method set_open_tracking' do
429
449
  it 'warns of set_open_tracking deprecation' do
430
450
  @mb_obj = Mailgun::MessageBuilder.new
431
451
  expect(@mb_obj).to receive :warn
432
- @mb_obj.set_open_tracking 'warn on set_open_tracking'
452
+ @mb_obj.set_open_tracking 'Yes'
433
453
  end
434
454
  end
435
455
 
@@ -440,19 +460,20 @@ describe 'The method track_opens' do
440
460
  it 'enables/disables open tracking on a per message basis.' do
441
461
  @mb_obj.track_opens('Yes')
442
462
 
443
- 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"])
444
465
 
445
466
  @mb_obj.track_opens('No')
446
467
 
447
- expect(@mb_obj.message["o:tracking-opens"][0]).to eq("no")
468
+ expect(@mb_obj.message["o:tracking-opens"]).to eq("no")
448
469
 
449
470
  @mb_obj.track_opens(true)
450
471
 
451
- expect(@mb_obj.message["o:tracking-opens"][0]).to eq("yes")
472
+ expect(@mb_obj.message["o:tracking-opens"]).to eq("yes")
452
473
 
453
474
  @mb_obj.track_opens(false)
454
475
 
455
- expect(@mb_obj.message["o:tracking-opens"][0]).to eq("no")
476
+ expect(@mb_obj.message["o:tracking-opens"]).to eq("no")
456
477
  end
457
478
  end
458
479
 
@@ -460,7 +481,7 @@ describe 'The method set_click_tracking' do
460
481
  it 'warns of set_click_tracking deprecation' do
461
482
  @mb_obj = Mailgun::MessageBuilder.new
462
483
  expect(@mb_obj).to receive :warn
463
- @mb_obj.set_click_tracking 'warn on set_click_tracking'
484
+ @mb_obj.set_click_tracking 'Yes'
464
485
  end
465
486
  end
466
487
 
@@ -471,23 +492,31 @@ describe 'The method track_clicks' do
471
492
  it 'enables/disables click tracking on a per message basis.' do
472
493
  @mb_obj.track_clicks('Yes')
473
494
 
474
- 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"])
475
497
 
476
498
  @mb_obj.track_clicks('No')
477
499
 
478
- expect(@mb_obj.message["o:tracking-clicks"][0]).to eq("no")
500
+ expect(@mb_obj.message["o:tracking-clicks"]).to eq("no")
479
501
 
480
502
  @mb_obj.track_clicks(true)
481
503
 
482
- expect(@mb_obj.message["o:tracking-clicks"][0]).to eq("yes")
504
+ expect(@mb_obj.message["o:tracking-clicks"]).to eq("yes")
483
505
 
484
506
  @mb_obj.track_clicks(false)
485
507
 
486
- expect(@mb_obj.message["o:tracking-clicks"][0]).to eq("no")
508
+ expect(@mb_obj.message["o:tracking-clicks"]).to eq("no")
487
509
 
488
510
  @mb_obj.track_clicks('html')
489
511
 
490
- 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
491
520
  end
492
521
  end
493
522
 
@@ -547,9 +576,13 @@ describe 'The method variable' do
547
576
  expect(@mb_obj.message["v:my-data"]).to be_kind_of(String)
548
577
  expect(@mb_obj.message["v:my-data"].to_s).to eq('{"key":"value"}')
549
578
  end
550
- it 'throws an exception on broken JSON.' do
551
- data = 'This is some crappy JSON.'
552
- 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.')
553
586
  end
554
587
  end
555
588
 
@@ -597,3 +630,103 @@ describe 'The method message_id' do
597
630
  expect(@mb_obj.message.has_key?('h:Message-Id')).to eq(false)
598
631
  end
599
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