mailgun-ruby 1.4.2 → 1.4.3

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.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ci.yml +30 -8
  3. data/.rubocop.yml +68 -0
  4. data/Gemfile +1 -1
  5. data/README.md +1 -1
  6. data/Rakefile +0 -5
  7. data/lib/mailgun/client.rb +12 -8
  8. data/lib/mailgun/domains/domains.rb +4 -2
  9. data/lib/mailgun/lists/opt_in_handler.rb +2 -4
  10. data/lib/mailgun/messages/batch_message.rb +2 -1
  11. data/lib/mailgun/messages/message_builder.rb +4 -32
  12. data/lib/mailgun/metrics/metrics.rb +6 -2
  13. data/lib/mailgun/response.rb +2 -2
  14. data/lib/mailgun/tags/analytics_tags.rb +9 -5
  15. data/lib/mailgun/tags/tags.rb +4 -2
  16. data/lib/mailgun/version.rb +1 -1
  17. data/lib/railgun/attachment.rb +4 -6
  18. data/lib/railgun/mailer.rb +2 -2
  19. data/mailgun.gemspec +4 -1
  20. data/spec/integration/analytics_tags_spec.rb +1 -1
  21. data/spec/integration/domains_spec.rb +7 -13
  22. data/spec/integration/events_spec.rb +1 -3
  23. data/spec/integration/list_members_spec.rb +1 -1
  24. data/spec/integration/logs_spec.rb +1 -1
  25. data/spec/integration/mailgun_spec.rb +3 -2
  26. data/spec/integration/metrics_spec.rb +9 -3
  27. data/spec/integration/suppressions_spec.rb +203 -26
  28. data/spec/integration/webhook_spec.rb +7 -2
  29. data/spec/spec_helper.rb +7 -0
  30. data/spec/unit/client_spec.rb +424 -0
  31. data/spec/unit/connection/test_client.rb +60 -13
  32. data/spec/unit/events/events_spec.rb +25 -9
  33. data/spec/unit/helpers/api_version_checker_spec.rb +206 -0
  34. data/spec/unit/lists/opt_in_handler_spec.rb +4 -2
  35. data/spec/unit/mailgun_spec.rb +7 -5
  36. data/spec/unit/messages/batch_message_spec.rb +25 -24
  37. data/spec/unit/messages/message_builder_spec.rb +83 -86
  38. data/spec/unit/railgun/content_type_spec.rb +7 -7
  39. data/spec/unit/railgun/mailer_spec.rb +17 -14
  40. data/spec/unit/response_spec.rb +225 -0
  41. data/vcr_cassettes/For_the_suppressions_handling_class/creates_a_single_bounce.yml +55 -0
  42. data/vcr_cassettes/suppressions.yml +1053 -170
  43. metadata +55 -5
@@ -5,19 +5,16 @@ require 'spec_helper'
5
5
  require 'mailgun'
6
6
  require 'mailgun/suppressions'
7
7
 
8
- vcr_opts = { cassette_name: 'suppressions' }
8
+ vcr_opts = { cassette_name: 'suppressions', match_requests_on: %i[uri method body] }
9
9
 
10
10
  describe 'For the suppressions handling class', order: :defined, vcr: vcr_opts do
11
- before(:all) do
12
- @mg_obj = Mailgun::Client.new(APIKEY)
13
- @suppress = Mailgun::Suppressions.new(@mg_obj, TESTDOMAIN)
14
-
15
- @addresses = ['test1@example.com', 'test2@example.org', 'test3@example.net', 'test4@example.info']
16
- end
11
+ let(:mg_obj) { Mailgun::Client.new(APIKEY, APIHOST, 'v3') }
12
+ let(:suppress) { Mailgun::Suppressions.new(mg_obj, TESTDOMAIN) }
13
+ let(:addresses) { %w[test1@example.com test2@example.org test3@example.net test4@example.info] }
17
14
 
18
15
  it 'can batch-add bounces' do
19
16
  bounces = []
20
- @addresses.each do |addr|
17
+ addresses.each do |addr|
21
18
  bounces.push({
22
19
  address: addr,
23
20
  code: 500,
@@ -25,7 +22,7 @@ describe 'For the suppressions handling class', order: :defined, vcr: vcr_opts d
25
22
  })
26
23
  end
27
24
 
28
- response, nested = @suppress.create_bounces bounces
25
+ response, nested = suppress.create_bounces bounces
29
26
  response.to_h!
30
27
 
31
28
  expect(response.code).to eq(200)
@@ -40,12 +37,40 @@ describe 'For the suppressions handling class', order: :defined, vcr: vcr_opts d
40
37
  error: 'integration testing'
41
38
  })
42
39
 
43
- expect { @suppress.create_bounces bounces }.to raise_error(Mailgun::ParameterError)
40
+ expect { suppress.create_bounces bounces }.to raise_error(Mailgun::ParameterError)
41
+ end
42
+
43
+ it 'returns nil if no bounces passed' do
44
+ response = suppress.create_bounces({})
45
+
46
+ expect(response[0]).to be_nil
47
+ end
48
+
49
+ it 'creates a single bounce' do
50
+ bounce = {
51
+ address: 'test777@example.info',
52
+ code: 777,
53
+ error: 'integration testing123'
54
+ }
55
+
56
+ response = suppress.create_bounce bounce
57
+ response.to_h!
58
+
59
+ expect(response.code).to eq(200)
60
+ expect(response.body['message']).to eq('Address has been added to the bounces table')
61
+ end
62
+
63
+ it 'fetches a single bounce' do
64
+ response = suppress.get_bounce('test777@example.info')
65
+ response.to_h!
66
+
67
+ expect(response.code).to eq(200)
68
+ expect(response.body['address']).to eq('test777@example.info')
44
69
  end
45
70
 
46
71
  it 'removes a single bounce address' do
47
- @addresses.each do |addr|
48
- response = @suppress.delete_bounce addr
72
+ addresses.each do |addr|
73
+ response = suppress.delete_bounce addr
49
74
  response.to_h!
50
75
 
51
76
  expect(response.code).to eq(200)
@@ -53,16 +78,24 @@ describe 'For the suppressions handling class', order: :defined, vcr: vcr_opts d
53
78
  end
54
79
  end
55
80
 
81
+ it 'removes all bounces' do
82
+ response = suppress.delete_all_bounces
83
+ response.to_h!
84
+
85
+ expect(response.code).to eq(200)
86
+ expect(response.body['message']).to eq('Bounced addresses for this domain have been removed')
87
+ end
88
+
56
89
  it 'can batch-add unsubscribes with tags as string' do
57
90
  unsubscribes = []
58
- @addresses.each do |addr|
91
+ addresses.each do |addr|
59
92
  unsubscribes.push({
60
93
  address: addr,
61
- tag: 'integration'
94
+ tag: '123'
62
95
  })
63
96
  end
64
97
 
65
- response, nested = @suppress.create_unsubscribes unsubscribes
98
+ response, nested = suppress.create_unsubscribes unsubscribes
66
99
  response.to_h!
67
100
 
68
101
  expect(response.code).to eq(200)
@@ -72,14 +105,31 @@ describe 'For the suppressions handling class', order: :defined, vcr: vcr_opts d
72
105
 
73
106
  it 'can batch-add unsubscribes with tags as array' do
74
107
  unsubscribes = []
75
- @addresses.each do |addr|
108
+ addresses.each do |addr|
76
109
  unsubscribes.push({
77
110
  address: addr,
78
111
  tags: ['integration']
79
112
  })
80
113
  end
81
114
 
82
- response, nested = @suppress.create_unsubscribes unsubscribes
115
+ response, nested = suppress.create_unsubscribes unsubscribes
116
+ response.to_h!
117
+
118
+ expect(response.code).to eq(200)
119
+ expect(response.body['message']).to eq('4 addresses have been added to the unsubscribes table')
120
+ expect(nested.length).to eq(0)
121
+ end
122
+
123
+ it 'can batch-add unsubscribes with tags as digits' do
124
+ unsubscribes = []
125
+ addresses.each do |addr|
126
+ unsubscribes.push({
127
+ address: addr,
128
+ tag: 123
129
+ })
130
+ end
131
+
132
+ response, nested = suppress.create_unsubscribes unsubscribes
83
133
  response.to_h!
84
134
 
85
135
  expect(response.code).to eq(200)
@@ -93,12 +143,18 @@ describe 'For the suppressions handling class', order: :defined, vcr: vcr_opts d
93
143
  tag: 'integration'
94
144
  })
95
145
 
96
- expect { @suppress.create_unsubscribes unsubscribes }.to raise_error(Mailgun::ParameterError)
146
+ expect { suppress.create_unsubscribes unsubscribes }.to raise_error(Mailgun::ParameterError)
147
+ end
148
+
149
+ it 'returns nil if no unsubscribes passed' do
150
+ response = suppress.create_unsubscribes({})
151
+
152
+ expect(response[0]).to be_nil
97
153
  end
98
154
 
99
155
  it 'removes a single unsubscribe address' do
100
- @addresses.each do |addr|
101
- response = @suppress.delete_unsubscribe addr
156
+ addresses.each do |addr|
157
+ response = suppress.delete_unsubscribe addr
102
158
  response.to_h!
103
159
 
104
160
  expect(response.code).to eq(200)
@@ -106,13 +162,40 @@ describe 'For the suppressions handling class', order: :defined, vcr: vcr_opts d
106
162
  end
107
163
  end
108
164
 
165
+ it 'creates a single unsubscribe' do
166
+ response = suppress.create_unsubscribe({
167
+ address: 'test777@example.com',
168
+ tags: ['integration']
169
+ })
170
+ response.to_h!
171
+
172
+ expect(response.code).to eq(200)
173
+ expect(response.body['message']).to eq('Address has been added to the unsubscribes table')
174
+ end
175
+
176
+ it 'returns a single unsubscribe' do
177
+ response = suppress.get_unsubscribe('test777@example.com')
178
+ response.to_h!
179
+
180
+ expect(response.code).to eq(200)
181
+ expect(response.body['address']).to eq('test777@example.com')
182
+ end
183
+
184
+ it 'returns all unsubscribers' do
185
+ response = suppress.list_unsubscribes
186
+ response.to_h!
187
+
188
+ expect(response.code).to eq(200)
189
+ expect(response.body['items'][0]['address']).to eq('test777@example.com')
190
+ end
191
+
109
192
  it 'can batch-add complaints' do
110
193
  complaints = []
111
- @addresses.each do |addr|
194
+ addresses.each do |addr|
112
195
  complaints.push address: addr
113
196
  end
114
197
 
115
- response, nested = @suppress.create_complaints complaints
198
+ response, nested = suppress.create_complaints complaints
116
199
  response.to_h!
117
200
 
118
201
  expect(response.code).to eq(200)
@@ -126,12 +209,42 @@ describe 'For the suppressions handling class', order: :defined, vcr: vcr_opts d
126
209
  tag: 'integration'
127
210
  })
128
211
 
129
- expect { @suppress.create_complaints complaints }.to raise_error(Mailgun::ParameterError)
212
+ expect { suppress.create_complaints complaints }.to raise_error(Mailgun::ParameterError)
213
+ end
214
+
215
+ it 'returns nil if no complaints passed' do
216
+ response = suppress.create_complaints({})
217
+
218
+ expect(response[0]).to be_nil
219
+ end
220
+
221
+ it 'creates a single complaint' do
222
+ response = suppress.create_complaint({ address: 'test777@example.com' })
223
+ response.to_h!
224
+
225
+ expect(response.code).to eq(200)
226
+ expect(response.body['message']).to eq('Address has been added to the complaints table')
227
+ end
228
+
229
+ it 'returns a single complaint' do
230
+ response = suppress.get_complaint('test777@example.com')
231
+ response.to_h!
232
+
233
+ expect(response.code).to eq(200)
234
+ expect(response.body['address']).to eq('test777@example.com')
235
+ end
236
+
237
+ it 'returns all complaints' do
238
+ response = suppress.list_complaints
239
+ response.to_h!
240
+
241
+ expect(response.code).to eq(200)
242
+ expect(response.body['items'][0]['address']).to eq('test1@example.com')
130
243
  end
131
244
 
132
245
  it 'removes a single complaint address' do
133
- @addresses.each do |addr|
134
- response = @suppress.delete_complaint addr
246
+ addresses.each do |addr|
247
+ response = suppress.delete_complaint addr
135
248
  response.to_h!
136
249
 
137
250
  expect(response.code).to eq(200)
@@ -139,5 +252,69 @@ describe 'For the suppressions handling class', order: :defined, vcr: vcr_opts d
139
252
  end
140
253
  end
141
254
 
142
- # TODO: Add tests for pagination support.
255
+ describe 'paging' do
256
+ before do
257
+ suppress.list_bounces
258
+ end
259
+
260
+ describe '#next' do
261
+ it 'returns the response' do
262
+ resp = suppress.next
263
+ expect(resp.status).to eq(200)
264
+ expect(JSON.parse(resp.body)).to include('items')
265
+ end
266
+ end
267
+
268
+ describe '#prev' do
269
+ it 'returns the response' do
270
+ resp = suppress.prev
271
+ expect(resp.status).to eq(200)
272
+ expect(JSON.parse(resp.body)).to include('items')
273
+ end
274
+ end
275
+ end
276
+
277
+ context 'with 1000 or more entries' do
278
+ subject(:suppressions) { Mailgun::Suppressions.new(client, domain) }
279
+
280
+ let(:large_list) { Array.new(1000) { |i| { address: "user#{i}@example.com" } } }
281
+ let(:client) { double(:client) }
282
+ let(:domain) { 'example.com' }
283
+
284
+ def stub_response(body = {})
285
+ double(:response).tap do |r|
286
+ allow(r).to receive_messages(to_h: body, to_h!: body, code: 200, body: body)
287
+ end
288
+ end
289
+
290
+ it 'splits bounces list and makes two POST requests' do
291
+ expect(client).to receive(:post)
292
+ .with('example.com/bounces', anything, { 'Content-Type' => 'application/json' })
293
+ .twice
294
+ .and_return(stub_response)
295
+
296
+ _resp, split = suppressions.create_bounces(large_list)
297
+ expect(split).not_to be_empty
298
+ end
299
+
300
+ it 'splits unsubscribes list and makes two POST requests' do
301
+ expect(client).to receive(:post)
302
+ .with('example.com/unsubscribes', anything, { 'Content-Type' => 'application/json' })
303
+ .twice
304
+ .and_return(stub_response)
305
+
306
+ _resp, split = suppressions.create_unsubscribes(large_list)
307
+ expect(split).not_to be_empty
308
+ end
309
+
310
+ it 'splits complaints list and makes two POST requests' do
311
+ expect(client).to receive(:post)
312
+ .with('example.com/complaints', anything, { 'Content-Type' => 'application/json' })
313
+ .twice
314
+ .and_return(stub_response)
315
+
316
+ _resp, split = suppressions.create_complaints(large_list)
317
+ expect(split).not_to be_empty
318
+ end
319
+ end
143
320
  end
@@ -37,8 +37,13 @@ describe 'For the webhooks endpoint', order: :defined, vcr: vcr_opts do
37
37
  end
38
38
 
39
39
  it 'updates a webhook.' do
40
- result = @mg_obj.put("domains/#{@domain}/webhooks/#{@testhook}", { id: @testhook,
41
- url: "http://example.com/mailgun/events/#{@testhookup}" })
40
+ result = @mg_obj.put(
41
+ "domains/#{@domain}/webhooks/#{@testhook}",
42
+ {
43
+ id: @testhook,
44
+ url: "http://example.com/mailgun/events/#{@testhookup}"
45
+ }
46
+ )
42
47
 
43
48
  result.to_h!
44
49
  expect(result.body['message']).to eq('Webhook has been updated')
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'simplecov'
4
+ require 'simplecov-json'
5
+ SimpleCov.formatters = SimpleCov::Formatter::MultiFormatter.new(
6
+ [
7
+ SimpleCov::Formatter::HTMLFormatter,
8
+ SimpleCov::Formatter::JSONFormatter
9
+ ]
10
+ )
4
11
 
5
12
  SimpleCov.start do
6
13
  add_filter '/spec/'