mailgun-ruby 1.2.12 → 1.2.14

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.
@@ -0,0 +1,84 @@
1
+ require 'mailgun/exceptions/exceptions'
2
+
3
+ module Mailgun
4
+
5
+ # A Mailgun::Subaccounts object is a simple CRUD interface to Mailgun Subaccounts.
6
+ # Uses Mailgun
7
+ class Subaccounts
8
+ attr_reader :client
9
+
10
+ # Public: creates a new Mailgun::Subaccounts instance.
11
+ # Defaults to Mailgun::Client
12
+ def initialize(client = Mailgun::Client.new(Mailgun.api_key, Mailgun.api_host || 'api.mailgun.net', 'v5'))
13
+ @client = client
14
+ end
15
+
16
+ # Public: Get subaccounts
17
+ # options - [Hash] of
18
+ # limit - [Integer] Maximum number of records to return. (10 by default)
19
+ # skip [Integer] Number of records to skip
20
+ # sort [Array] “asc” or “desc”
21
+ # enabled [boolean] (Optional) Returns all enabled/disabled subaccounts, defaults to all if omitted
22
+ # headers - [String] (Optional) Key Value json dictionary of headers to be stored with the subaccount.
23
+ # ex.('{"Subject": "{{subject}}"}')
24
+ #
25
+ # Returns [Array] A list of subaccounts (hash)
26
+ def list(options = {})
27
+ client.get("accounts/subaccounts", options).to_h!
28
+ end
29
+ alias_method :get_subaccounts, :list
30
+
31
+ # Public: Get subaccount information
32
+ #
33
+ # subaccount_id - [String] subaccount name to lookup for
34
+ # options - [Hash] of
35
+ # headers - [String] (Optional) Key Value json dictionary of headers to be stored with the subaccount.
36
+ # ex.('{"Subject": "{{subject}}"}')
37
+ #
38
+ # Returns [Hash] Information on the requested subaccount.
39
+ def info(subaccount_id, options = {})
40
+ fail(ParameterError, 'No Id of subaccount specified', caller) unless subaccount_id
41
+ client.get("accounts/subaccounts/#{subaccount_id}", options).to_h!
42
+ end
43
+
44
+ # Public: Add Subaccount
45
+ #
46
+ # name - [String] Name of the subaccount being created
47
+ # options - [Hash] of
48
+ # name - [String] Name of the subaccount being created.
49
+ # headers - [String] (Optional) Key Value json dictionary of headers to be stored with the subaccount.
50
+ # ex.('{"Subject": "{{subject}}"}')
51
+ #
52
+ # Returns [Hash] of created subaccount
53
+ def create(name, options = {})
54
+ fail(ParameterError, 'No name given to create subaccount', caller) unless name
55
+ client.post("accounts/subaccounts", options.merge!(name: name)).to_h!
56
+ end
57
+
58
+ # Public: Disable a subaccount
59
+ #
60
+ # subaccount_id - [String] subaccount name to disable
61
+ # options - [Hash] of
62
+ # headers - [String] (Optional) Key Value json dictionary of headers to be stored with the subaccount.
63
+ # ex.('{"Subject": "{{subject}}"}')
64
+ #
65
+ # Returns [Hash] Information on the requested subaccount.
66
+ def disable(subaccount_id, options = {})
67
+ fail(ParameterError, 'No Id of subaccount specified', caller) unless subaccount_id
68
+ client.post("accounts/subaccounts/#{subaccount_id}/disable", options).to_h!
69
+ end
70
+
71
+ # Public: Enable a subaccount
72
+ #
73
+ # subaccount_id - [String] subaccount name to enable
74
+ # options - [Hash] of
75
+ # headers - [String] (Optional) Key Value json dictionary of headers to be stored with the subaccount.
76
+ # ex.('{"Subject": "{{subject}}"}')
77
+ #
78
+ # Returns [Hash] Information on the requested subaccount.
79
+ def enable(subaccount_id, options = {})
80
+ fail(ParameterError, 'No Id of subaccount specified', caller) unless subaccount_id
81
+ client.post("accounts/subaccounts/#{subaccount_id}/enable", options).to_h!
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,120 @@
1
+ require 'mailgun/exceptions/exceptions'
2
+
3
+ module Mailgun
4
+
5
+ # A Mailgun::Tags object is a simple CRUD interface to Mailgun Tags.
6
+ # Uses Mailgun
7
+ class Tags
8
+
9
+ # Public: creates a new Mailgun::Tags instance.
10
+ # Defaults to Mailgun::Client
11
+ def initialize(client = Mailgun::Client.new)
12
+ @client = client
13
+ end
14
+
15
+ # Public: Get Tags
16
+ #
17
+ # domain - [String] Domain name where tag is stored
18
+ # options - [Hash] of
19
+ # limit - [Integer] Number of entries to return. Default: 100.
20
+ #
21
+ # Returns [Array] A list of tags (hash)
22
+ def get_tags(domain, options = {})
23
+ fail(ParameterError, 'No domain given to store template on', caller) unless domain
24
+ @client.get("#{domain}/tags", options).to_h['items']
25
+ end
26
+
27
+ # Public: Get tag information
28
+ #
29
+ # domain - [String] Domain name where tag is stored
30
+ # tag - [String] Tag name to lookup for
31
+ #
32
+ # Returns [Hash] Information on the requested tag.
33
+ def get_tag(domain, tag)
34
+ fail(ParameterError, 'No domain given to find on Mailgun', caller) unless domain
35
+ fail(ParameterError, 'No tag name given to find on provided domain', caller) unless tag
36
+ @client.get("#{domain}/tags/#{tag}").to_h!
37
+ end
38
+
39
+ # Public: Updates a given tag with the information provided
40
+ #
41
+ # domain - [String] Domain name where tag is stored
42
+ # tag - [String] Tag name to lookup for
43
+ # options - [Hash] of
44
+ # description - [String] Updated description of the tag
45
+ #
46
+ # Returns [Boolean] if successful or not
47
+ def update(domain, tag, options = {})
48
+ fail(ParameterError, 'No domain given to find on Mailgun', caller) unless domain
49
+ fail(ParameterError, 'No tag name given to find on provided domain', caller) unless tag
50
+ @client.put("#{domain}/tags/#{tag}", options).to_h['message'] == 'Tag updated'
51
+ end
52
+
53
+ # Public: Get statistics for a given tag
54
+ #
55
+ # domain - [String] Domain name where tag is stored
56
+ # tag - [String] Tag name to lookup for
57
+ # options - [Hash] of
58
+ # event - [String] The type of the event. Required. (ex. accepted, delivered, failed, opened)
59
+ # start - [String] The starting time. Should be in RFC 282 or unix epoch format. Default: 7 days from the current time.
60
+ # end - [String] The ending date. Should be in RFC 2822 or unix epoch time in seconds. Default: current time.
61
+ # resolution - [String] Can be either hour, day or month. Default: day
62
+ # duration - [String] Period of time with resolution encoded. If provided, overwrites the start date and resolution.
63
+ #
64
+ # Returns [Hash] of tag stats info
65
+ def get_tag_stats(domain, tag, options = {})
66
+ fail(ParameterError, 'No domain given to find on Mailgun', caller) unless domain
67
+ fail(ParameterError, 'No tag name given to find on provided domain', caller) unless tag
68
+ @client.get("#{domain}/tags/#{tag}/stats", options).to_h
69
+ end
70
+
71
+ # Public: Delete Tag
72
+ # NOTE: Deletes the tag. Note: The statistics for the tag are not destroyed.
73
+ #
74
+ # domain - [String] Domain name where tag is stored
75
+ # tag - [String] Tag name to lookup for
76
+ #
77
+ # Returns [Boolean] if successful or not
78
+ def remove(domain, tag)
79
+ fail(ParameterError, 'No domain given to remove on Mailgun', caller) unless domain
80
+ fail(ParameterError, 'No template name given to find on provided domain', caller) unless tag
81
+ @client.delete("#{domain}/tags/#{tag}").to_h['message'] == 'Tag deleted'
82
+ end
83
+
84
+ # Public: Get a list of countries of origin for a given domain for different event types.
85
+ #
86
+ # domain - [String] Domain name where tag is stored
87
+ # tag - [String] Tag name to lookup for
88
+ #
89
+ # Returns [Hash] of countries of origin for a given domain
90
+ def get_countries_aggregated_stats(domain, tag)
91
+ fail(ParameterError, 'No domain given to find on Mailgun', caller) unless domain
92
+ fail(ParameterError, 'No tag name given to find on provided domain', caller) unless tag
93
+ @client.get("#{domain}/tags/#{tag}/stats/aggregates/countries").to_h
94
+ end
95
+
96
+ # Public: Get a list of email providers for a given domain for different event types
97
+ #
98
+ # domain - [String] Domain name where tag is stored
99
+ # tag - [String] Tag name to lookup for
100
+ #
101
+ # Returns [Hash] of email providers for a given domain
102
+ def get_providers_aggregated_stats(domain, tag)
103
+ fail(ParameterError, 'No domain given to find on Mailgun', caller) unless domain
104
+ fail(ParameterError, 'No tag name given to find on provided domain', caller) unless tag
105
+ @client.get("#{domain}/tags/#{tag}/stats/aggregates/providers").to_h
106
+ end
107
+
108
+ # Public: Get a list of devices for a given domain that have triggered event types.
109
+ #
110
+ # domain - [String] Domain name where tag is stored
111
+ # tag - [String] Tag name to lookup for
112
+ #
113
+ # Returns [Hash] of devices for a given domain
114
+ def get_devices_aggregated_stats(domain, tag)
115
+ fail(ParameterError, 'No domain given to find on Mailgun', caller) unless domain
116
+ fail(ParameterError, 'No tag name given to find on provided domain', caller) unless tag
117
+ @client.get("#{domain}/tags/#{tag}/stats/aggregates/devices").to_h
118
+ end
119
+ end
120
+ end
@@ -1,4 +1,4 @@
1
1
  # It's the version. Yeay!
2
2
  module Mailgun
3
- VERSION = '1.2.12'
3
+ VERSION = '1.2.14'
4
4
  end
@@ -3,6 +3,7 @@ module Mailgun
3
3
  # A Mailgun::Webhooks object is a simple CRUD interface to Mailgun Webhooks.
4
4
  # Uses Mailgun
5
5
  class Webhooks
6
+ ACTIONS = %w(accepted clicked complained delivered opened permanent_fail temporary_fail unsubscribed).freeze
6
7
 
7
8
  # Public creates a new Mailgun::Webhooks instance.
8
9
  # Defaults to Mailgun::Client
@@ -31,7 +32,7 @@ module Mailgun
31
32
  # empty String if one is not set
32
33
  def info(domain, action)
33
34
  res = @client.get("domains/#{domain}/webhooks/#{action}")
34
- res.to_h['webhook']['url'] || ''
35
+ res.to_h['webhook']['urls'] || ''
35
36
  rescue NoMethodError
36
37
  ''
37
38
  end
@@ -46,7 +47,7 @@ module Mailgun
46
47
  # Returns a Boolean of whether the webhook was created
47
48
  def create(domain, action, url = '')
48
49
  res = @client.post("domains/#{domain}/webhooks", id: action, url: url)
49
- res.to_h['webhook']['url'] == url && res.to_h['message'] == 'Webhook has been created'
50
+ res.to_h['webhook']['urls'].include?(url) && res.to_h['message'] == 'Webhook has been created'
50
51
  end
51
52
  alias_method :add, :create
52
53
  alias_method :add_webhook, :create
@@ -58,7 +59,7 @@ module Mailgun
58
59
  #
59
60
  # Returns true or false
60
61
  def create_all(domain, url = '')
61
- %w(accepted clicked complained delivered opened permanent_fail temporary_fail unsubscribed).each do |action|
62
+ ACTIONS.each do |action|
62
63
  add_webhook domain, action, url
63
64
  end
64
65
  true
@@ -67,6 +68,21 @@ module Mailgun
67
68
  end
68
69
  alias_method :add_all_webhooks, :create_all
69
70
 
71
+ # Public: Update webhook
72
+ #
73
+ # domain - A String of the domain name (ex. domain.com)
74
+ # action - A String of the action to create a webhook for
75
+ # url - A String of the url of the webhook
76
+ #
77
+ # Returns a Boolean of whether the webhook was updated
78
+ def update(domain, action, url = '')
79
+ fail Mailgun::ParameterError('Domain not provided to update webhooks') unless domain
80
+ fail Mailgun::ParameterError('Action not provided to identify webhook to update') unless action
81
+ res = @client.put("domains/#{domain}/webhooks/#{action}", id: action, url: url)
82
+ res.to_h['webhook']['urls'] == url && res.to_h['message'] == 'Webhook has been updated'
83
+ end
84
+ alias_method :update_webhook, :update
85
+
70
86
  # Public: Delete a specific webhook
71
87
  #
72
88
  # domain - The required String of domain name
@@ -90,7 +106,7 @@ module Mailgun
90
106
  # Returns a Boolean on the success
91
107
  def remove_all(domain)
92
108
  fail Mailgun::ParameterError('Domain not provided to remove webhooks from') unless domain
93
- %w(accepted clicked complained delivered opened permanent_fail temporary_fail unsubscribed).each do |action|
109
+ ACTIONS.each do |action|
94
110
  delete_webhook domain, action
95
111
  end
96
112
  end
data/lib/mailgun.rb CHANGED
@@ -16,6 +16,8 @@ require 'mailgun/exceptions/exceptions'
16
16
  require 'mailgun/domains/domains'
17
17
  require 'mailgun/webhooks/webhooks'
18
18
  require 'mailgun/templates/templates'
19
+ require 'mailgun/subaccounts/subaccounts'
20
+ require 'mailgun/tags/tags'
19
21
 
20
22
  # Module for interacting with the sweet Mailgun API.
21
23
  #
@@ -49,9 +49,11 @@ module Railgun
49
49
  def deliver!(mail)
50
50
  @mg_domain = set_mg_domain(mail)
51
51
  @mg_client.set_api_key(mail[:api_key].value) if mail[:api_key].present?
52
+ @mg_client.set_subaccount(mail[:subaccount_id].value) if mail[:subaccount_id].present?
52
53
 
53
54
  mail[:domain] = nil if mail[:domain].present?
54
55
  mail[:api_key] = nil if mail[:api_key].present?
56
+ mail[:subaccount_id] = nil if mail[:subaccount_id].present?
55
57
 
56
58
  mg_message = Railgun.transform_for_mailgun(mail)
57
59
  response = @mg_client.send_message(@mg_domain, mg_message)
@@ -171,6 +173,7 @@ module Railgun
171
173
  mb.subject mail.subject
172
174
  mb.body_html extract_body_html(mail)
173
175
  mb.body_text extract_body_text(mail)
176
+ mb.amp_html extract_amp_html(mail)
174
177
 
175
178
  [:to, :cc, :bcc].each do |rcpt_type|
176
179
  addrs = mail[rcpt_type] || nil
@@ -230,6 +233,20 @@ module Railgun
230
233
  end
231
234
  end
232
235
 
236
+ # Returns the decoded AMP HTML from the Mail::Message object if it is available,
237
+ # otherwise nil.
238
+ #
239
+ # @param [Mail::Message] mail message to transform
240
+ #
241
+ # @return [String]
242
+ def extract_amp_html(mail)
243
+ begin
244
+ retrieve_amp_part(mail).body.decoded || nil
245
+ rescue
246
+ nil
247
+ end
248
+ end
249
+
233
250
  # Returns the mail object from the Mail::Message object if text part exists,
234
251
  # (decomposing multipart into individual format if necessary)
235
252
  # otherwise nil.
@@ -253,4 +270,18 @@ module Railgun
253
270
  return mail.html_part if mail.multipart?
254
271
  (mail.mime_type =~ /^text\/html$/i) && mail
255
272
  end
273
+
274
+ # Returns the mail object from the Mail::Message object if AMP part exists,
275
+ # (decomposing multipart into individual format if necessary)
276
+ # otherwise nil.
277
+ #
278
+ # @param [Mail::Message] mail message to transform
279
+ #
280
+ # @return [Mail::Message] mail message with its content-type = text/x-amp-html
281
+ def retrieve_amp_part(mail)
282
+ # AMP emails should always be multipart, with an HTML fallback.
283
+ return unless mail.multipart?
284
+
285
+ mail.find_first_mime_type('text/x-amp-html')
286
+ end
256
287
  end
data/mailgun.gemspec CHANGED
@@ -30,7 +30,7 @@ Gem::Specification.new do |spec|
30
30
  spec.add_development_dependency 'bundler', '>= 1.16.2'
31
31
  spec.add_development_dependency 'rspec', '~> 3.8.0'
32
32
  spec.add_development_dependency 'rake', '~> 12.3.2'
33
- spec.add_development_dependency 'webmock', '~> 3.4.2'
33
+ spec.add_development_dependency 'webmock', '~> 3.7'
34
34
  spec.add_development_dependency 'pry', '~> 0.11.3'
35
35
  spec.add_development_dependency 'vcr', '~> 3.0.3'
36
36
  spec.add_development_dependency 'simplecov', '~> 0.16.1'
@@ -44,4 +44,248 @@ describe 'For the domains endpoint', vcr: vcr_opts do
44
44
  expect(result['domain']["web_scheme"]).to eq('https')
45
45
  expect(result['domain']["wildcard"]).to eq(true)
46
46
  end
47
+
48
+ describe '#create_smtp_credentials' do
49
+ it 'creates smtp credentials for domain' do
50
+ result = @mg_obj.create_smtp_credentials(
51
+ @domain,
52
+ {
53
+ login: 'test_login',
54
+ password: 'test_password'
55
+ }
56
+ )
57
+
58
+ expect(result['message']).to eq("Created 1 credentials pair(s)")
59
+ end
60
+ end
61
+
62
+ describe '#update_smtp_credentials' do
63
+ it 'updates smtp credentials for domain' do
64
+ result = @mg_obj.update_smtp_credentials(
65
+ @domain,
66
+ 'test_login',
67
+ {
68
+ password: 'test_password2'
69
+ }
70
+ )
71
+
72
+ expect(result['message']).to eq('Password changed')
73
+ end
74
+ end
75
+
76
+ describe '#delete_smtp_credentials' do
77
+ it 'deletes smtp credentials for domain' do
78
+ result = @mg_obj.delete_smtp_credentials(
79
+ @domain,
80
+ 'test_login'
81
+ )
82
+
83
+ expect(result['message']).to eq('Credentials have been deleted')
84
+ end
85
+ end
86
+
87
+ describe '#delete_smtp_credentials' do
88
+ it 'deletes smtp credentials for domain' do
89
+ result = @mg_obj.delete_smtp_credentials(
90
+ @domain,
91
+ 'test_login'
92
+ )
93
+
94
+ expect(result['message']).to eq('Credentials have been deleted')
95
+ end
96
+ end
97
+
98
+ describe '#get_domain_connection_settings' do
99
+ it 'returns delivery connection settings for the defined domain' do
100
+ result = @mg_obj.get_domain_connection_settings(
101
+ @domain
102
+ )
103
+
104
+ expect(result).to include(
105
+ 'require_tls' => false,
106
+ 'skip_verification' => false
107
+ )
108
+ end
109
+ end
110
+
111
+ describe '#update_domain_connection_settings' do
112
+ it 'updates the specified delivery connection settings' do
113
+ result = @mg_obj.update_domain_connection_settings(
114
+ @domain,
115
+ {
116
+ require_tls: true,
117
+ skip_verification: true
118
+ }
119
+ )
120
+
121
+ expect(result).to include(
122
+ 'require_tls' => true,
123
+ 'skip_verification' => true
124
+ )
125
+ end
126
+ end
127
+
128
+ describe '#get_domain_tracking_settings' do
129
+ it 'returns tracking settings for the defined domain' do
130
+ result = @mg_obj.get_domain_tracking_settings(
131
+ @domain
132
+ )
133
+
134
+ expect(result).to include('tracking')
135
+ expect(result['tracking']['click']['active']).to eq(true)
136
+ end
137
+ end
138
+
139
+ describe '#update_domain_tracking_open_settings' do
140
+ it 'updates the specified tracking open settings' do
141
+ result = @mg_obj.update_domain_tracking_open_settings(
142
+ @domain,
143
+ {
144
+ active: false
145
+ }
146
+ )
147
+
148
+ expect(result['open']['active']).to eq(false)
149
+ end
150
+ end
151
+
152
+ describe '#update_domain_tracking_click_settings' do
153
+ it 'updates the specified tracking click settings' do
154
+ result = @mg_obj.update_domain_tracking_click_settings(
155
+ @domain,
156
+ {
157
+ active: false
158
+ }
159
+ )
160
+
161
+ expect(result['click']['active']).to eq(false)
162
+ end
163
+ end
164
+
165
+ describe '#update_domain_tracking_unsubscribe_settings' do
166
+ it 'updates the specified tracking unsubscribe settings' do
167
+ result = @mg_obj.update_domain_tracking_unsubscribe_settings(
168
+ @domain,
169
+ {
170
+ active: false
171
+ }
172
+ )
173
+
174
+ expect(result['unsubscribe']['active']).to eq(false)
175
+ end
176
+ end
177
+
178
+ describe '#update_domain_dkim_authority' do
179
+ it 'updates the DKIM authority for a domain' do
180
+ result = @mg_obj.update_domain_dkim_authority(
181
+ @domain,
182
+ {
183
+ active: false
184
+ }
185
+ )
186
+
187
+ expect(result['message']).to eq('Domain DKIM authority has been changed')
188
+ end
189
+ end
190
+
191
+ describe '#update_domain_dkim_selector' do
192
+ it 'updates the DKIM selector for a domain' do
193
+ result = @mg_obj.update_domain_dkim_selector(
194
+ @domain,
195
+ {
196
+ dkim_selector: 'mailo1'
197
+ }
198
+ )
199
+
200
+ expect(result['message']).to eq('Domain DKIM authority changed')
201
+ end
202
+ end
203
+
204
+ describe '#update_domain_web_prefix' do
205
+ it 'updates the the CNAME used for tracking opens and clicks' do
206
+ result = @mg_obj.update_domain_web_prefix(
207
+ @domain,
208
+ {
209
+ web_prefix: 'email'
210
+ }
211
+ )
212
+
213
+ expect(result['message']).to eq('Domain web prefix updated')
214
+ end
215
+ end
216
+
217
+ describe 'V4' do
218
+ before do
219
+ @mg_client = Mailgun::Client.new(APIKEY, APIHOST, 'v4', SSL)
220
+ @mg_obj = Mailgun::Domains.new(@mg_client)
221
+ end
222
+
223
+ describe '#get_domain_keys' do
224
+ it 'lists the domain keys for a specified signing domain' do
225
+ result = @mg_obj.get_domain_keys(
226
+ @domain
227
+ )
228
+
229
+ expect(result).to include('items')
230
+ expect(result['items'].first['selector']).to eq('mailo1')
231
+ end
232
+ end
233
+
234
+ describe '#activate_domain_key' do
235
+ it 'activates a domain key' do
236
+ result = @mg_obj.activate_domain_key(
237
+ @domain,
238
+ 'smtp'
239
+ )
240
+
241
+ expect(result['message']).to eq('domain key activated')
242
+ end
243
+ end
244
+
245
+ describe '#deactivate_domain_key' do
246
+ it 'deactivates a domain key' do
247
+ result = @mg_obj.deactivate_domain_key(
248
+ {
249
+ signing_domain: 'x509.zeefarmer.com',
250
+ selector: 'tetetet'
251
+ }
252
+ )
253
+
254
+ expect(result['message']).to eq('domain key deactivated')
255
+ end
256
+ end
257
+ end
258
+
259
+ describe '#delete_domain_key' do
260
+ before do
261
+ @mg_client = Mailgun::Client.new(APIKEY, APIHOST, 'v1', SSL)
262
+ @mg_obj = Mailgun::Domains.new(@mg_client)
263
+ end
264
+
265
+ it 'deletes a domain key' do
266
+ result = @mg_obj.delete_domain_key(
267
+ {
268
+ signing_domain: @domain,
269
+ selector: 'test'
270
+ }
271
+ )
272
+
273
+ expect(result['message']).to eq('success')
274
+ end
275
+ end
276
+
277
+ describe '#get_domain_stats' do
278
+ it 'returns total stats for a given domain' do
279
+ result = @mg_obj.get_domain_stats(
280
+ @domain,
281
+ {
282
+ event: 'clicked',
283
+ start: 'Sun, 23 Dec 2023 01:23:45 JST',
284
+ duration: '24h'
285
+ }
286
+ )
287
+
288
+ expect(result).to include('stats')
289
+ end
290
+ end
47
291
  end
@@ -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).to have_key('event')
26
26
  end
27
27
  end
28
28
  end
@@ -53,15 +53,9 @@ describe 'Invalid domain', vcr: vcr_opts do
53
53
  domain: domain
54
54
  }
55
55
  end
56
- let(:mail) { UnitTestMailer.plain_message("bob@#{domain}", 'sally@not-our-doma.in' 'subject', {}) }
56
+ let(:mail) { UnitTestMailer.plain_message('sally@not-our-doma.in', "bob@#{domain}", 'subject', {}) }
57
57
 
58
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
-
59
+ expect { Railgun::Mailer.new(config).deliver!(mail) }.to raise_error Mailgun::CommunicationError, /Forbidden/
66
60
  end
67
61
  end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+ require 'mailgun'
3
+
4
+ vcr_opts = { :cassette_name => "subaccounts" }
5
+
6
+ describe 'For the subaccounts endpoints', vcr: vcr_opts do
7
+ let(:name) { 'test.subaccount' }
8
+ let(:subaccount_id) { 'xxx' }
9
+
10
+ before(:all) do
11
+ mg_client = Mailgun::Client.new(APIKEY, APIHOST, 'v5')
12
+ @mg_obj = Mailgun::Subaccounts.new mg_client
13
+ end
14
+
15
+ describe '#list' do
16
+ it 'returns a list of templates' do
17
+ result = @mg_obj.list
18
+
19
+ expect(result).to eq({"subaccounts"=>[{"id"=>"xxx", "name"=>"test-ruby-lib", "status"=>"open"}], "total"=>1})
20
+ end
21
+ end
22
+
23
+ describe '#create' do
24
+ it 'creates the subaccount' do
25
+ result = @mg_obj.create(name)
26
+
27
+ expect(result).to eq({"subaccount"=>{"id"=>"xxx", "name"=>"test.subaccount", "status"=>"open"}})
28
+ end
29
+ end
30
+
31
+
32
+ describe '#info' do
33
+ it 'gets the templates info' do
34
+ result = @mg_obj.info(subaccount_id)
35
+
36
+ expect(result).to eq({"subaccount"=>{"id"=>"xxx", "name"=>"test-ruby-lib", "status"=>"open"}})
37
+ end
38
+ end
39
+
40
+
41
+
42
+ describe '#enable' do
43
+ it 'enables the subaccount' do
44
+ result = @mg_obj.enable(subaccount_id)
45
+
46
+ expect(result).to eq({"subaccount"=>{"id"=>"xxx", "name"=>"test-ruby-lib", "status"=>"open"}})
47
+ end
48
+ end
49
+
50
+ describe '#disable' do
51
+ it 'disables the subaccount' do
52
+ result = @mg_obj.disable(subaccount_id)
53
+
54
+ expect(result).to eq({"subaccount"=>{"id"=>"xxx", "name"=>"test-ruby-lib", "status"=>"disabled"}})
55
+ end
56
+ end
57
+
58
+ end