mailgun-ruby 1.1.9 → 1.2.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +0 -0
  3. data/.rubocop_todo.yml +0 -0
  4. data/.ruby-env.yml.example +0 -0
  5. data/.travis.yml +6 -5
  6. data/CHANGELOG.md +16 -0
  7. data/Gemfile +1 -1
  8. data/README.md +27 -3
  9. data/docs/Domains.md +3 -0
  10. data/docs/OptInHandler.md +1 -1
  11. data/docs/Snippets.md +54 -61
  12. data/docs/Webhooks.md +0 -0
  13. data/docs/railgun/Overview.md +11 -0
  14. data/docs/railgun/Parameters.md +83 -0
  15. data/docs/railgun/Templates.md +92 -0
  16. data/lib/mailgun/address.rb +3 -28
  17. data/lib/mailgun/chains.rb +0 -0
  18. data/lib/mailgun/client.rb +44 -9
  19. data/lib/mailgun/domains/domains.rb +20 -2
  20. data/lib/mailgun/events/events.rb +2 -2
  21. data/lib/mailgun/exceptions/exceptions.rb +28 -1
  22. data/lib/mailgun/messages/batch_message.rb +1 -0
  23. data/lib/mailgun/messages/message_builder.rb +56 -8
  24. data/lib/mailgun/response.rb +7 -0
  25. data/lib/mailgun/suppressions.rb +15 -8
  26. data/lib/mailgun/templates/templates.rb +187 -0
  27. data/lib/mailgun/version.rb +1 -1
  28. data/lib/mailgun/webhooks/webhooks.rb +2 -2
  29. data/lib/mailgun-ruby.rb +1 -1
  30. data/lib/mailgun.rb +4 -1
  31. data/lib/railgun/mailer.rb +83 -12
  32. data/lib/railgun/message.rb +2 -1
  33. data/lib/railgun/railtie.rb +3 -2
  34. data/mailgun.gemspec +15 -12
  35. data/spec/integration/bounces_spec.rb +3 -3
  36. data/spec/integration/campaign_spec.rb +0 -0
  37. data/spec/integration/complaints_spec.rb +0 -0
  38. data/spec/integration/domains_spec.rb +8 -0
  39. data/spec/integration/email_validation_spec.rb +10 -2
  40. data/spec/integration/events_spec.rb +1 -1
  41. data/spec/integration/list_members_spec.rb +0 -0
  42. data/spec/integration/list_spec.rb +0 -0
  43. data/spec/integration/mailer_spec.rb +67 -0
  44. data/spec/integration/mailgun_spec.rb +92 -1
  45. data/spec/integration/routes_spec.rb +0 -0
  46. data/spec/integration/stats_spec.rb +0 -0
  47. data/spec/integration/suppressions_spec.rb +18 -2
  48. data/spec/integration/templates_spec.rb +135 -0
  49. data/spec/integration/unsubscribes_spec.rb +0 -0
  50. data/spec/integration/webhook_spec.rb +0 -0
  51. data/spec/spec_helper.rb +3 -1
  52. data/spec/unit/connection/test_client.rb +18 -1
  53. data/spec/unit/events/events_spec.rb +19 -0
  54. data/spec/unit/mailgun_spec.rb +43 -2
  55. data/spec/unit/messages/batch_message_spec.rb +56 -40
  56. data/spec/unit/messages/message_builder_spec.rb +149 -16
  57. data/spec/unit/messages/sample_data/unknown.type +0 -0
  58. data/spec/unit/railgun/mailer_spec.rb +388 -0
  59. data/vcr_cassettes/bounces.yml +12 -12
  60. data/vcr_cassettes/complaints.yml +0 -0
  61. data/vcr_cassettes/domains.todo.yml +0 -0
  62. data/vcr_cassettes/domains.yml +51 -1
  63. data/vcr_cassettes/email_validation.yml +5 -5
  64. data/vcr_cassettes/events.yml +0 -0
  65. data/vcr_cassettes/exceptions-invalid-api-key.yml +52 -0
  66. data/vcr_cassettes/exceptions-invalid-data.yml +52 -0
  67. data/vcr_cassettes/exceptions-not-allowed.yml +54 -0
  68. data/vcr_cassettes/list_members.yml +0 -0
  69. data/vcr_cassettes/mailer_invalid_domain.yml +109 -0
  70. data/vcr_cassettes/mailing_list.todo.yml +0 -0
  71. data/vcr_cassettes/mailing_list.yml +0 -0
  72. data/vcr_cassettes/message_deliver.yml +149 -0
  73. data/vcr_cassettes/routes.yml +0 -0
  74. data/vcr_cassettes/send_message.yml +0 -0
  75. data/vcr_cassettes/stats.yml +0 -0
  76. data/vcr_cassettes/suppressions.yml +66 -15
  77. data/vcr_cassettes/templates.yml +1065 -0
  78. data/vcr_cassettes/unsubscribes.yml +0 -0
  79. data/vcr_cassettes/webhooks.yml +0 -0
  80. metadata +49 -29
  81. data/.ruby-version +0 -1
  82. /data/spec/unit/{railgun_spec.rb → railgun/content_type_spec.rb} +0 -0
@@ -34,6 +34,78 @@ describe 'Client exceptions', vcr: vcr_opts do
34
34
  end
35
35
  end
36
36
 
37
+ vcr_opts = { :cassette_name => "exceptions-invalid-api-key" }
38
+
39
+ describe 'Client exceptions', vcr: vcr_opts do
40
+ before(:all) do
41
+ @mg_obj = Mailgun::Client.new(APIKEY, APIHOST, APIVERSION, SSL)
42
+ @domain = TESTDOMAIN
43
+ end
44
+
45
+ it 'displays error information that API key is invalid' do
46
+ begin
47
+ @mg_obj.send_message(@domain, {
48
+ :from => "sally@#{@domain}",
49
+ :to => "sally@#{@domain}",
50
+ :subject => 'Exception Integration Test',
51
+ :text => 'INTEGRATION TESTING'
52
+ })
53
+ rescue Mailgun::Unauthorized => err
54
+ expect(err.message).to eq('401 Unauthorized - Invalid Domain or API key: Forbidden')
55
+ else
56
+ fail
57
+ end
58
+ end
59
+ end
60
+
61
+ vcr_opts = { :cassette_name => "exceptions-invalid-data" }
62
+
63
+ describe 'Client exceptions', vcr: vcr_opts do
64
+ before(:all) do
65
+ @mg_obj = Mailgun::Client.new(APIKEY, APIHOST, APIVERSION, SSL)
66
+ @domain = TESTDOMAIN
67
+ end
68
+
69
+ it 'display useful error information' do
70
+ begin
71
+ @mg_obj.send_message(@domain, {
72
+ :from => "sally@#{@domain}",
73
+ :to => "sally#{@domain}",
74
+ :subject => 'Exception Integration Test',
75
+ :text => 'INTEGRATION TESTING'
76
+ })
77
+ rescue Mailgun::BadRequest => err
78
+ expect(err.message).to eq('400 Bad Request: to parameter is not a valid address. please check documentation')
79
+ else
80
+ fail
81
+ end
82
+ end
83
+ end
84
+
85
+ vcr_opts = { :cassette_name => "exceptions-not-allowed" }
86
+
87
+ describe 'Client exceptions', vcr: vcr_opts do
88
+ before(:all) do
89
+ @mg_obj = Mailgun::Client.new(APIKEY, APIHOST, APIVERSION, SSL)
90
+ @domain = TESTDOMAIN
91
+ end
92
+
93
+ it 'display useful error information' do
94
+ begin
95
+ @mg_obj.send_message(@domain, {
96
+ :from => "invalid@#{@domain}",
97
+ :to => "invalid#{@domain}",
98
+ :subject => 'Exception Integration Test',
99
+ :text => 'INTEGRATION TESTING'
100
+ })
101
+ rescue Mailgun::CommunicationError => err
102
+ expect(err.message).to include('403 Forbidden')
103
+ else
104
+ fail
105
+ end
106
+ end
107
+ end
108
+
37
109
  vcr_opts = { :cassette_name => "send_message" }
38
110
 
39
111
  describe 'The method send_message()', vcr: vcr_opts do
@@ -63,6 +135,9 @@ describe 'The method send_message()', vcr: vcr_opts do
63
135
  :to => "bob@#{@domain}",
64
136
  :subject => "Test",
65
137
  :text => "Test Data" }
138
+ uuid = 'uuid'
139
+
140
+ allow(SecureRandom).to receive(:uuid).and_return(uuid)
66
141
 
67
142
  result = @mg_obj.send_message(@domain, data)
68
143
 
@@ -72,7 +147,7 @@ describe 'The method send_message()', vcr: vcr_opts do
72
147
  expect(result.body).to include("id")
73
148
 
74
149
  expect(result.code).to eq(200)
75
- expect(result.body['id']).to eq("test-mode-mail@localhost")
150
+ expect(result.body['id']).to eq("test-mode-mail-#{uuid}@localhost")
76
151
  expect(result.body['message']).to eq("Queued. Thank you.")
77
152
  end
78
153
 
@@ -117,5 +192,21 @@ Testing some Mailgun awesomness!'
117
192
  expect(result.body).to include("message")
118
193
  expect(result.body).to include("id")
119
194
  end
195
+
196
+ it 'receives success response code' do
197
+ @mg_obj.enable_test_mode!
198
+
199
+ expect(@mg_obj.test_mode?).to eq(true)
200
+
201
+ data = { :from => "joe@#{@domain}",
202
+ :to => "bob@#{@domain}",
203
+ :subject => "Test",
204
+ :text => "Test Data" }
205
+
206
+ result = @mg_obj.send_message(@domain, data)
207
+ result.to_h!
208
+
209
+ expect(result.success?).to be(true)
210
+ end
120
211
  end
121
212
 
File without changes
File without changes
@@ -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
-
@@ -0,0 +1,135 @@
1
+ require 'spec_helper'
2
+ require 'mailgun'
3
+
4
+ vcr_opts = { :cassette_name => "templates" }
5
+
6
+ describe 'For the templates endpoints', vcr: vcr_opts do
7
+ let(:template_name) { 'test.template' }
8
+ let(:domain) { "integration-test.domain.invalid" }
9
+ let(:tag) { 'v2' }
10
+
11
+ before(:all) do
12
+ @mg_client = Mailgun::Client.new(APIKEY, APIHOST, APIVERSION, SSL)
13
+ @mg_obj = Mailgun::Templates.new(@mg_client)
14
+ end
15
+
16
+ describe '#create' do
17
+ it 'creates the template' do
18
+ result = @mg_obj.create(
19
+ domain,
20
+ {
21
+ name: template_name,
22
+ description: 'Test',
23
+ template: '{{fname}} {{lname}}',
24
+ comment: 'test comment',
25
+ headers: '{"Subject": "{{subject}}"}',
26
+ tag: 'V1'
27
+ }
28
+ )
29
+
30
+ expect(result['template']["name"]).to eq('test.template')
31
+ expect(result['template']["description"]).to eq("Test")
32
+ end
33
+ end
34
+
35
+ describe '#info' do
36
+ it 'gets the templates info' do
37
+ result = @mg_obj.info(domain, 'test.template')
38
+
39
+ expect(result).to include("template")
40
+ expect(result["template"]["name"]).to eq(template_name)
41
+ end
42
+ end
43
+
44
+ describe '#update' do
45
+ it 'updates the template' do
46
+ result = @mg_obj.update(domain, template_name, { description: 'Updated Description' })
47
+
48
+ expect(result['message']).to eq('template has been updated')
49
+ end
50
+ end
51
+
52
+ describe '#list' do
53
+ it 'returns a list of templates' do
54
+ result = @mg_obj.list(domain)
55
+
56
+ expect(result.first).to have_key('name')
57
+ expect(result.first).to have_key('description')
58
+ end
59
+ end
60
+
61
+ describe '#delete' do
62
+ it 'deletes a template' do
63
+ result = @mg_obj.delete(domain, template_name)
64
+
65
+ expect(result).to be_truthy
66
+ end
67
+ end
68
+
69
+ describe '#remove_all' do
70
+ it 'deletes all templates from domain' do
71
+ result = @mg_obj.remove_all(domain)
72
+
73
+ expect(result).to be_truthy
74
+ end
75
+ end
76
+
77
+ describe '#create_version' do
78
+ it 'creates the version for the template' do
79
+ result = @mg_obj.create_version(
80
+ domain,
81
+ template_name,
82
+ {
83
+ template: '{{fname}} {{lname}}',
84
+ comment: 'test comment',
85
+ headers: '{"Subject": "{{subject}}"}',
86
+ tag: tag,
87
+ active: 'yes'
88
+ }
89
+ )
90
+
91
+ expect(result['template']["version"]['tag']).to eq(tag)
92
+ end
93
+ end
94
+
95
+ describe '#info_version' do
96
+ it "gets the template's version info" do
97
+ result = @mg_obj.info_version(domain, template_name, tag)
98
+
99
+ expect(result["template"]["version"]['tag']).to eq(tag)
100
+ end
101
+ end
102
+
103
+ describe '#update_version' do
104
+ it 'updates the template' do
105
+ result = @mg_obj.update_version(
106
+ domain,
107
+ template_name,
108
+ tag,
109
+ {
110
+ template: '{{fname}} {{lname}}',
111
+ comment: 'test comment 2',
112
+ headers: '{"Subject": "{{subject}}"}'
113
+ }
114
+ )
115
+
116
+ expect(result['message']).to eq('version has been updated')
117
+ end
118
+ end
119
+
120
+ describe '#template_versions_list' do
121
+ it "returns template's versions" do
122
+ result = @mg_obj.template_versions_list(domain, template_name)
123
+
124
+ expect(result["template"]["versions"].first).to include('tag')
125
+ end
126
+ end
127
+
128
+ describe '#delete_version' do
129
+ it "deletes template's version" do
130
+ result = @mg_obj.delete_version(domain, template_name, tag)
131
+
132
+ expect(result).to be_truthy
133
+ end
134
+ end
135
+ end
File without changes
File without changes
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)
@@ -45,7 +46,8 @@ module Mailgun
45
46
  Mailgun::Response.new(response_generator(@endpoint))
46
47
  rescue => e
47
48
  p e
48
- raise CommunicationError.new(e), e.response
49
+ raise CommunicationError.new(e), e.response if e.respond_to? :response
50
+ raise CommunicationError.new(e.message)
49
51
  end
50
52
  end
51
53
 
@@ -75,6 +77,21 @@ module Mailgun
75
77
 
76
78
  private
77
79
 
80
+ def perform_data_validation(working_domain, data)
81
+ fail ParameterError.new('Missing working domain', working_domain) unless working_domain
82
+ return true unless data.is_a?(Hash) && data.present?
83
+ message = data.respond_to?(:message) ? data.message : data
84
+
85
+ fail ParameterError.new(
86
+ 'Missing `to` recipient, message should contain at least 1 recipient',
87
+ working_domain
88
+ ) if message.fetch('to', []).empty? && message.fetch(:to, []).empty?
89
+ fail ParameterError.new(
90
+ 'Missing a `from` sender, message should contain at least 1 `from` sender',
91
+ working_domain
92
+ ) if message.fetch('from', []).empty? && message.fetch(:from, []).empty?
93
+ end
94
+
78
95
  def response_generator(resource_endpoint)
79
96
  if resource_endpoint == "messages"
80
97
  t = Time.now
@@ -11,6 +11,25 @@ describe 'The method get' do
11
11
  end
12
12
  end
13
13
 
14
+ describe 'Pagination' do
15
+ it 'should return a proper hash of log data.' do
16
+ @mg_obj = Mailgun::UnitClient.new('events')
17
+ events = Mailgun::Events.new(@mg_obj, "samples.mailgun.org")
18
+ result = events.get()
19
+
20
+ json = JSON.parse(result.body)
21
+ expect(json).to include("paging")
22
+ expect(json["paging"]).to include("next")
23
+ expect(json["paging"]).to include{"previous"}
24
+ end
25
+
26
+ it 'should calculate proper next-page url' do
27
+ events = Mailgun::Events.new(@mg_obj, "samples.mailgun.org")
28
+ output = events.send(:extract_endpoint_from, '/v3/samples.mailgun.org/events/W3siYiI6ICIyMDE3LTA1LTEwVDIwOjA2OjU0LjU3NiswMDowMCIsICJlIjogIjIwMTctMDUtMDhUMjA6MDY6NTQuNTc3KzAwOjAwIn0sIHsiYiI6ICIyMDE3LTA1LTEwVDIwOjA2OjU0LjU3NiswMDowMCIsICJlIjogIjIwMTctMDUtMDhUMjA6MDY6NTQuNTc3KzAwOjAwIn0sIFsiZiJdLCBudWxsLCBbWyJhY2NvdW50LmlkIiwgIjU4MDUyMTg2NzhmYTE2MTNjNzkwYjUwZiJdLCBbImRvbWFpbi5uYW1lIiwgInNhbmRib3gyOTcwMTUyYWYzZDM0NTU5YmZjN2U3MTcwM2E2Y2YyNC5tYWlsZ3VuLm9yZyJdXSwgMTAwLCBudWxsXQ==')
29
+
30
+ expect(output).to eq 'W3siYiI6ICIyMDE3LTA1LTEwVDIwOjA2OjU0LjU3NiswMDowMCIsICJlIjogIjIwMTctMDUtMDhUMjA6MDY6NTQuNTc3KzAwOjAwIn0sIHsiYiI6ICIyMDE3LTA1LTEwVDIwOjA2OjU0LjU3NiswMDowMCIsICJlIjogIjIwMTctMDUtMDhUMjA6MDY6NTQuNTc3KzAwOjAwIn0sIFsiZiJdLCBudWxsLCBbWyJhY2NvdW50LmlkIiwgIjU4MDUyMTg2NzhmYTE2MTNjNzkwYjUwZiJdLCBbImRvbWFpbi5uYW1lIiwgInNhbmRib3gyOTcwMTUyYWYzZDM0NTU5YmZjN2U3MTcwM2E2Y2YyNC5tYWlsZ3VuLm9yZyJdXSwgMTAwLCBudWxsXQ=='
31
+ end
32
+ end
14
33
 
15
34
  describe 'The method next' do
16
35
  it 'should return the next series of data.' do
@@ -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
@@ -65,6 +87,25 @@ describe 'The method post()' do
65
87
  expect(result.body).to include("message")
66
88
  expect(result.body).to include("id")
67
89
  end
90
+
91
+ context 'when Unknown API error is raised' do
92
+ before do
93
+ allow(Mailgun::Response).to receive(:new).and_raise(StandardError, "message")
94
+ allow(JSON).to receive(:parse).and_raise('Unknown')
95
+ end
96
+
97
+ it 'adds Unknown API error to message' do
98
+ data = {'from' => 'joe@test.com',
99
+ 'to' => 'bob@example.com',
100
+ 'subject' => 'Test',
101
+ 'text' => 'Test Data'}
102
+ @mg_obj.post("#{@domain}/messages", data)
103
+ rescue Mailgun::CommunicationError => err
104
+ expect(err.message).to eq('message: Unknown API error')
105
+ else
106
+ fail
107
+ end
108
+ end
68
109
  end
69
110
 
70
111
  describe 'The method put()' 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