gocardless_pro 2.41.0 → 2.41.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (144) hide show
  1. checksums.yaml +4 -4
  2. data/lib/gocardless_pro/api_service.rb +4 -5
  3. data/lib/gocardless_pro/client.rb +3 -3
  4. data/lib/gocardless_pro/error/invalid_state_error.rb +1 -0
  5. data/lib/gocardless_pro/error.rb +1 -0
  6. data/lib/gocardless_pro/middlewares/raise_gocardless_errors.rb +7 -9
  7. data/lib/gocardless_pro/request.rb +5 -7
  8. data/lib/gocardless_pro/resources/bank_authorisation.rb +0 -2
  9. data/lib/gocardless_pro/resources/bank_details_lookup.rb +0 -2
  10. data/lib/gocardless_pro/resources/billing_request.rb +0 -2
  11. data/lib/gocardless_pro/resources/billing_request_flow.rb +0 -2
  12. data/lib/gocardless_pro/resources/billing_request_template.rb +0 -2
  13. data/lib/gocardless_pro/resources/block.rb +0 -2
  14. data/lib/gocardless_pro/resources/creditor.rb +0 -2
  15. data/lib/gocardless_pro/resources/creditor_bank_account.rb +0 -2
  16. data/lib/gocardless_pro/resources/currency_exchange_rate.rb +0 -2
  17. data/lib/gocardless_pro/resources/customer.rb +0 -2
  18. data/lib/gocardless_pro/resources/customer_bank_account.rb +0 -2
  19. data/lib/gocardless_pro/resources/customer_notification.rb +0 -2
  20. data/lib/gocardless_pro/resources/event.rb +0 -2
  21. data/lib/gocardless_pro/resources/instalment_schedule.rb +0 -2
  22. data/lib/gocardless_pro/resources/institution.rb +0 -2
  23. data/lib/gocardless_pro/resources/mandate.rb +0 -2
  24. data/lib/gocardless_pro/resources/mandate_import.rb +0 -2
  25. data/lib/gocardless_pro/resources/mandate_import_entry.rb +0 -2
  26. data/lib/gocardless_pro/resources/mandate_pdf.rb +0 -2
  27. data/lib/gocardless_pro/resources/payer_authorisation.rb +0 -2
  28. data/lib/gocardless_pro/resources/payment.rb +0 -2
  29. data/lib/gocardless_pro/resources/payout.rb +0 -2
  30. data/lib/gocardless_pro/resources/payout_item.rb +0 -2
  31. data/lib/gocardless_pro/resources/redirect_flow.rb +0 -2
  32. data/lib/gocardless_pro/resources/refund.rb +0 -2
  33. data/lib/gocardless_pro/resources/scenario_simulator.rb +0 -2
  34. data/lib/gocardless_pro/resources/scheme_identifier.rb +0 -2
  35. data/lib/gocardless_pro/resources/subscription.rb +0 -2
  36. data/lib/gocardless_pro/resources/tax_rate.rb +0 -2
  37. data/lib/gocardless_pro/resources/verification_detail.rb +0 -2
  38. data/lib/gocardless_pro/resources/webhook.rb +0 -2
  39. data/lib/gocardless_pro/services/bank_authorisations_service.rb +3 -1
  40. data/lib/gocardless_pro/services/billing_request_flows_service.rb +3 -1
  41. data/lib/gocardless_pro/services/billing_request_templates_service.rb +6 -2
  42. data/lib/gocardless_pro/services/billing_requests_service.rb +27 -9
  43. data/lib/gocardless_pro/services/blocks_service.rb +9 -3
  44. data/lib/gocardless_pro/services/creditor_bank_accounts_service.rb +6 -2
  45. data/lib/gocardless_pro/services/creditors_service.rb +9 -3
  46. data/lib/gocardless_pro/services/customer_bank_accounts_service.rb +9 -3
  47. data/lib/gocardless_pro/services/customer_notifications_service.rb +3 -1
  48. data/lib/gocardless_pro/services/customers_service.rb +9 -3
  49. data/lib/gocardless_pro/services/events_service.rb +3 -1
  50. data/lib/gocardless_pro/services/instalment_schedules_service.rb +9 -3
  51. data/lib/gocardless_pro/services/mandate_imports_service.rb +9 -3
  52. data/lib/gocardless_pro/services/mandates_service.rb +12 -4
  53. data/lib/gocardless_pro/services/payer_authorisations_service.rb +12 -4
  54. data/lib/gocardless_pro/services/payments_service.rb +12 -4
  55. data/lib/gocardless_pro/services/payouts_service.rb +6 -2
  56. data/lib/gocardless_pro/services/redirect_flows_service.rb +6 -2
  57. data/lib/gocardless_pro/services/refunds_service.rb +6 -2
  58. data/lib/gocardless_pro/services/scenario_simulators_service.rb +3 -1
  59. data/lib/gocardless_pro/services/scheme_identifiers_service.rb +3 -1
  60. data/lib/gocardless_pro/services/subscriptions_service.rb +15 -5
  61. data/lib/gocardless_pro/services/tax_rates_service.rb +3 -1
  62. data/lib/gocardless_pro/services/webhooks_service.rb +6 -2
  63. data/lib/gocardless_pro/version.rb +1 -2
  64. data/lib/gocardless_pro/webhook.rb +5 -4
  65. data/lib/gocardless_pro.rb +2 -4
  66. metadata +5 -195
  67. data/.github/workflows/tests.yml +0 -40
  68. data/.gitignore +0 -1
  69. data/.rspec +0 -2
  70. data/.rubocop.yml +0 -5
  71. data/.travis.yml +0 -10
  72. data/Gemfile +0 -10
  73. data/demo.rb +0 -9
  74. data/gocardless_pro.gemspec +0 -39
  75. data/spec/api_response_spec.rb +0 -28
  76. data/spec/api_service_spec.rb +0 -188
  77. data/spec/client_spec.rb +0 -19
  78. data/spec/error_spec.rb +0 -53
  79. data/spec/middlewares/raise_gocardless_errors_spec.rb +0 -127
  80. data/spec/resources/bank_authorisation_spec.rb +0 -259
  81. data/spec/resources/bank_details_lookup_spec.rb +0 -121
  82. data/spec/resources/billing_request_flow_spec.rb +0 -257
  83. data/spec/resources/billing_request_spec.rb +0 -962
  84. data/spec/resources/billing_request_template_spec.rb +0 -515
  85. data/spec/resources/block_spec.rb +0 -577
  86. data/spec/resources/creditor_bank_account_spec.rb +0 -472
  87. data/spec/resources/creditor_spec.rb +0 -659
  88. data/spec/resources/currency_exchange_rate_spec.rb +0 -103
  89. data/spec/resources/customer_bank_account_spec.rb +0 -509
  90. data/spec/resources/customer_notification_spec.rb +0 -75
  91. data/spec/resources/customer_spec.rb +0 -616
  92. data/spec/resources/event_spec.rb +0 -210
  93. data/spec/resources/instalment_schedule_spec.rb +0 -636
  94. data/spec/resources/institution_spec.rb +0 -113
  95. data/spec/resources/mandate_import_entry_spec.rb +0 -206
  96. data/spec/resources/mandate_import_spec.rb +0 -344
  97. data/spec/resources/mandate_pdf_spec.rb +0 -117
  98. data/spec/resources/mandate_spec.rb +0 -600
  99. data/spec/resources/payer_authorisation_spec.rb +0 -422
  100. data/spec/resources/payment_spec.rb +0 -617
  101. data/spec/resources/payout_item_spec.rb +0 -101
  102. data/spec/resources/payout_spec.rb +0 -284
  103. data/spec/resources/redirect_flow_spec.rb +0 -347
  104. data/spec/resources/refund_spec.rb +0 -409
  105. data/spec/resources/scenario_simulator_spec.rb +0 -65
  106. data/spec/resources/scheme_identifier_spec.rb +0 -484
  107. data/spec/resources/subscription_spec.rb +0 -828
  108. data/spec/resources/tax_rate_spec.rb +0 -198
  109. data/spec/resources/verification_detail_spec.rb +0 -145
  110. data/spec/resources/webhook_spec.rb +0 -325
  111. data/spec/response_spec.rb +0 -34
  112. data/spec/services/bank_authorisations_service_spec.rb +0 -353
  113. data/spec/services/bank_details_lookups_service_spec.rb +0 -144
  114. data/spec/services/billing_request_flows_service_spec.rb +0 -291
  115. data/spec/services/billing_request_templates_service_spec.rb +0 -806
  116. data/spec/services/billing_requests_service_spec.rb +0 -1288
  117. data/spec/services/blocks_service_spec.rb +0 -840
  118. data/spec/services/creditor_bank_accounts_service_spec.rb +0 -725
  119. data/spec/services/creditors_service_spec.rb +0 -977
  120. data/spec/services/currency_exchange_rates_service_spec.rb +0 -223
  121. data/spec/services/customer_bank_accounts_service_spec.rb +0 -787
  122. data/spec/services/customer_notifications_service_spec.rb +0 -86
  123. data/spec/services/customers_service_spec.rb +0 -922
  124. data/spec/services/events_service_spec.rb +0 -401
  125. data/spec/services/instalment_schedules_service_spec.rb +0 -945
  126. data/spec/services/institutions_service_spec.rb +0 -241
  127. data/spec/services/mandate_import_entries_service_spec.rb +0 -345
  128. data/spec/services/mandate_imports_service_spec.rb +0 -460
  129. data/spec/services/mandate_pdfs_service_spec.rb +0 -140
  130. data/spec/services/mandates_service_spec.rb +0 -893
  131. data/spec/services/payer_authorisations_service_spec.rb +0 -563
  132. data/spec/services/payments_service_spec.rb +0 -914
  133. data/spec/services/payout_items_service_spec.rb +0 -221
  134. data/spec/services/payouts_service_spec.rb +0 -520
  135. data/spec/services/redirect_flows_service_spec.rb +0 -452
  136. data/spec/services/refunds_service_spec.rb +0 -668
  137. data/spec/services/scenario_simulators_service_spec.rb +0 -76
  138. data/spec/services/scheme_identifiers_service_spec.rb +0 -754
  139. data/spec/services/subscriptions_service_spec.rb +0 -1164
  140. data/spec/services/tax_rates_service_spec.rb +0 -381
  141. data/spec/services/verification_details_service_spec.rb +0 -168
  142. data/spec/services/webhooks_service_spec.rb +0 -547
  143. data/spec/spec_helper.rb +0 -90
  144. data/spec/webhook_spec.rb +0 -122
@@ -1,460 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe GoCardlessPro::Services::MandateImportsService do
4
- let(:client) do
5
- GoCardlessPro::Client.new(
6
- access_token: 'SECRET_TOKEN'
7
- )
8
- end
9
-
10
- let(:response_headers) { { 'Content-Type' => 'application/json' } }
11
-
12
- describe '#create' do
13
- subject(:post_create_response) { client.mandate_imports.create(params: new_resource) }
14
- context 'with a valid request' do
15
- let(:new_resource) do
16
- {
17
-
18
- 'created_at' => 'created_at-input',
19
- 'id' => 'id-input',
20
- 'scheme' => 'scheme-input',
21
- 'status' => 'status-input',
22
- }
23
- end
24
-
25
- before do
26
- stub_request(:post, %r{.*api.gocardless.com/mandate_imports}).
27
- with(
28
- body: {
29
- 'mandate_imports' => {
30
-
31
- 'created_at' => 'created_at-input',
32
- 'id' => 'id-input',
33
- 'scheme' => 'scheme-input',
34
- 'status' => 'status-input',
35
- },
36
- }
37
- ).
38
- to_return(
39
- body: {
40
- 'mandate_imports' =>
41
-
42
- {
43
-
44
- 'created_at' => 'created_at-input',
45
- 'id' => 'id-input',
46
- 'scheme' => 'scheme-input',
47
- 'status' => 'status-input',
48
- },
49
-
50
- }.to_json,
51
- headers: response_headers
52
- )
53
- end
54
-
55
- it 'creates and returns the resource' do
56
- expect(post_create_response).to be_a(GoCardlessPro::Resources::MandateImport)
57
- end
58
-
59
- describe 'retry behaviour' do
60
- before { allow_any_instance_of(GoCardlessPro::Request).to receive(:sleep) }
61
-
62
- it 'retries timeouts' do
63
- stub = stub_request(:post, %r{.*api.gocardless.com/mandate_imports}).
64
- to_timeout.then.to_return(status: 200, headers: response_headers)
65
-
66
- post_create_response
67
- expect(stub).to have_been_requested.twice
68
- end
69
-
70
- it 'retries 5XX errors' do
71
- stub = stub_request(:post, %r{.*api.gocardless.com/mandate_imports}).
72
- to_return(status: 502,
73
- headers: { 'Content-Type' => 'text/html' },
74
- body: '<html><body>Response from Cloudflare</body></html>').
75
- then.to_return(status: 200, headers: response_headers)
76
-
77
- post_create_response
78
- expect(stub).to have_been_requested.twice
79
- end
80
- end
81
- end
82
-
83
- context 'with a request that returns a validation error' do
84
- let(:new_resource) { {} }
85
-
86
- before do
87
- stub_request(:post, %r{.*api.gocardless.com/mandate_imports}).to_return(
88
- body: {
89
- error: {
90
- type: 'validation_failed',
91
- code: 422,
92
- errors: [
93
- { message: 'test error message', field: 'test_field' },
94
- ],
95
- },
96
- }.to_json,
97
- headers: response_headers,
98
- status: 422
99
- )
100
- end
101
-
102
- it 'throws the correct error' do
103
- expect { post_create_response }.to raise_error(GoCardlessPro::ValidationError)
104
- end
105
- end
106
-
107
- context 'with a request that returns an idempotent creation conflict error' do
108
- let(:id) { 'ID123' }
109
-
110
- let(:new_resource) do
111
- {
112
-
113
- 'created_at' => 'created_at-input',
114
- 'id' => 'id-input',
115
- 'scheme' => 'scheme-input',
116
- 'status' => 'status-input',
117
- }
118
- end
119
-
120
- let!(:post_stub) do
121
- stub_request(:post, %r{.*api.gocardless.com/mandate_imports}).to_return(
122
- body: {
123
- error: {
124
- type: 'invalid_state',
125
- code: 409,
126
- errors: [
127
- {
128
- message: 'A resource has already been created with this idempotency key',
129
- reason: 'idempotent_creation_conflict',
130
- links: {
131
- conflicting_resource_id: id,
132
- },
133
- },
134
- ],
135
- },
136
- }.to_json,
137
- headers: response_headers,
138
- status: 409
139
- )
140
- end
141
-
142
- let!(:get_stub) do
143
- stub_url = "/mandate_imports/#{id}"
144
- stub_request(:get, /.*api.gocardless.com#{stub_url}/).
145
- to_return(
146
- body: {
147
- 'mandate_imports' => {
148
-
149
- 'created_at' => 'created_at-input',
150
- 'id' => 'id-input',
151
- 'scheme' => 'scheme-input',
152
- 'status' => 'status-input',
153
- },
154
- }.to_json,
155
- headers: response_headers
156
- )
157
- end
158
-
159
- context 'with default behaviour' do
160
- it 'fetches the already-created resource' do
161
- post_create_response
162
- expect(post_stub).to have_been_requested
163
- expect(get_stub).to have_been_requested
164
- end
165
- end
166
-
167
- context 'with on_idempotency_conflict: :raise' do
168
- let(:client) do
169
- GoCardlessPro::Client.new(
170
- access_token: 'SECRET_TOKEN',
171
- on_idempotency_conflict: :raise
172
- )
173
- end
174
-
175
- it 'raises an IdempotencyConflict error' do
176
- expect { post_create_response }.
177
- to raise_error(GoCardlessPro::IdempotencyConflict)
178
- end
179
- end
180
- end
181
- end
182
-
183
- describe '#get' do
184
- let(:id) { 'ID123' }
185
-
186
- subject(:get_response) { client.mandate_imports.get(id) }
187
-
188
- context 'passing in a custom header' do
189
- let!(:stub) do
190
- stub_url = '/mandate_imports/:identity'.gsub(':identity', id)
191
- stub_request(:get, /.*api.gocardless.com#{stub_url}/).
192
- with(headers: { 'Foo' => 'Bar' }).
193
- to_return(
194
- body: {
195
- 'mandate_imports' => {
196
-
197
- 'created_at' => 'created_at-input',
198
- 'id' => 'id-input',
199
- 'scheme' => 'scheme-input',
200
- 'status' => 'status-input',
201
- },
202
- }.to_json,
203
- headers: response_headers
204
- )
205
- end
206
-
207
- subject(:get_response) do
208
- client.mandate_imports.get(id, headers: {
209
- 'Foo' => 'Bar',
210
- })
211
- end
212
-
213
- it 'includes the header' do
214
- get_response
215
- expect(stub).to have_been_requested
216
- end
217
- end
218
-
219
- context 'when there is a mandate_import to return' do
220
- before do
221
- stub_url = '/mandate_imports/:identity'.gsub(':identity', id)
222
- stub_request(:get, /.*api.gocardless.com#{stub_url}/).to_return(
223
- body: {
224
- 'mandate_imports' => {
225
-
226
- 'created_at' => 'created_at-input',
227
- 'id' => 'id-input',
228
- 'scheme' => 'scheme-input',
229
- 'status' => 'status-input',
230
- },
231
- }.to_json,
232
- headers: response_headers
233
- )
234
- end
235
-
236
- it 'wraps the response in a resource' do
237
- expect(get_response).to be_a(GoCardlessPro::Resources::MandateImport)
238
- end
239
- end
240
-
241
- context 'when nothing is returned' do
242
- before do
243
- stub_url = '/mandate_imports/:identity'.gsub(':identity', id)
244
- stub_request(:get, /.*api.gocardless.com#{stub_url}/).to_return(
245
- body: '',
246
- headers: response_headers
247
- )
248
- end
249
-
250
- it 'returns nil' do
251
- expect(get_response).to be_nil
252
- end
253
- end
254
-
255
- context "when an ID is specified which can't be included in a valid URI" do
256
- let(:id) { '`' }
257
-
258
- it "doesn't raise an error" do
259
- expect { get_response }.to_not raise_error(/bad URI/)
260
- end
261
- end
262
-
263
- describe 'retry behaviour' do
264
- before { allow_any_instance_of(GoCardlessPro::Request).to receive(:sleep) }
265
-
266
- it 'retries timeouts' do
267
- stub_url = '/mandate_imports/:identity'.gsub(':identity', id)
268
-
269
- stub = stub_request(:get, /.*api.gocardless.com#{stub_url}/).
270
- to_timeout.then.to_return(status: 200, headers: response_headers)
271
-
272
- get_response
273
- expect(stub).to have_been_requested.twice
274
- end
275
-
276
- it 'retries 5XX errors, other than 500s' do
277
- stub_url = '/mandate_imports/:identity'.gsub(':identity', id)
278
-
279
- stub = stub_request(:get, /.*api.gocardless.com#{stub_url}/).
280
- to_return(status: 502,
281
- headers: { 'Content-Type' => 'text/html' },
282
- body: '<html><body>Response from Cloudflare</body></html>').
283
- then.to_return(status: 200, headers: response_headers)
284
-
285
- get_response
286
- expect(stub).to have_been_requested.twice
287
- end
288
-
289
- it 'retries 500 errors returned by the API' do
290
- stub_url = '/mandate_imports/:identity'.gsub(':identity', id)
291
-
292
- gocardless_error = {
293
- 'error' => {
294
- 'message' => 'Internal server error',
295
- 'documentation_url' => 'https://developer.gocardless.com/#gocardless',
296
- 'errors' => [{
297
- 'message' => 'Internal server error',
298
- 'reason' => 'internal_server_error',
299
- }],
300
- 'type' => 'gocardless',
301
- 'code' => 500,
302
- 'request_id' => 'dummy_request_id',
303
- 'id' => 'dummy_exception_id',
304
- },
305
- }
306
-
307
- stub = stub_request(:get, /.*api.gocardless.com#{stub_url}/).
308
- to_return(status: 500,
309
- headers: response_headers,
310
- body: gocardless_error.to_json).
311
- then.to_return(status: 200, headers: response_headers)
312
-
313
- get_response
314
- expect(stub).to have_been_requested.twice
315
- end
316
- end
317
- end
318
-
319
- describe '#submit' do
320
- subject(:post_response) { client.mandate_imports.submit(resource_id) }
321
-
322
- let(:resource_id) { 'ABC123' }
323
-
324
- let!(:stub) do
325
- # /mandate_imports/%v/actions/submit
326
- stub_url = '/mandate_imports/:identity/actions/submit'.gsub(':identity', resource_id)
327
- stub_request(:post, /.*api.gocardless.com#{stub_url}/).to_return(
328
-
329
- body: {
330
- 'mandate_imports' => {
331
-
332
- 'created_at' => 'created_at-input',
333
- 'id' => 'id-input',
334
- 'scheme' => 'scheme-input',
335
- 'status' => 'status-input',
336
- },
337
- }.to_json,
338
-
339
- headers: response_headers
340
- )
341
- end
342
-
343
- it 'wraps the response and calls the right endpoint' do
344
- expect(post_response).to be_a(GoCardlessPro::Resources::MandateImport)
345
-
346
- expect(stub).to have_been_requested
347
- end
348
-
349
- describe 'retry behaviour' do
350
- it "doesn't retry errors" do
351
- stub_url = '/mandate_imports/:identity/actions/submit'.gsub(':identity', resource_id)
352
- stub = stub_request(:post, /.*api.gocardless.com#{stub_url}/).
353
- to_timeout
354
-
355
- expect { post_response }.to raise_error(Faraday::ConnectionFailed)
356
- expect(stub).to have_been_requested
357
- end
358
- end
359
-
360
- context 'when the request needs a body and custom header' do
361
- let(:body) { { foo: 'bar' } }
362
- let(:headers) { { 'Foo' => 'Bar' } }
363
- subject(:post_response) { client.mandate_imports.submit(resource_id, body, headers) }
364
-
365
- let(:resource_id) { 'ABC123' }
366
-
367
- let!(:stub) do
368
- # /mandate_imports/%v/actions/submit
369
- stub_url = '/mandate_imports/:identity/actions/submit'.gsub(':identity', resource_id)
370
- stub_request(:post, /.*api.gocardless.com#{stub_url}/).
371
- with(
372
- body: { foo: 'bar' },
373
- headers: { 'Foo' => 'Bar' }
374
- ).to_return(
375
- body: {
376
- 'mandate_imports' => {
377
-
378
- 'created_at' => 'created_at-input',
379
- 'id' => 'id-input',
380
- 'scheme' => 'scheme-input',
381
- 'status' => 'status-input',
382
- },
383
- }.to_json,
384
- headers: response_headers
385
- )
386
- end
387
- end
388
- end
389
-
390
- describe '#cancel' do
391
- subject(:post_response) { client.mandate_imports.cancel(resource_id) }
392
-
393
- let(:resource_id) { 'ABC123' }
394
-
395
- let!(:stub) do
396
- # /mandate_imports/%v/actions/cancel
397
- stub_url = '/mandate_imports/:identity/actions/cancel'.gsub(':identity', resource_id)
398
- stub_request(:post, /.*api.gocardless.com#{stub_url}/).to_return(
399
-
400
- body: {
401
- 'mandate_imports' => {
402
-
403
- 'created_at' => 'created_at-input',
404
- 'id' => 'id-input',
405
- 'scheme' => 'scheme-input',
406
- 'status' => 'status-input',
407
- },
408
- }.to_json,
409
-
410
- headers: response_headers
411
- )
412
- end
413
-
414
- it 'wraps the response and calls the right endpoint' do
415
- expect(post_response).to be_a(GoCardlessPro::Resources::MandateImport)
416
-
417
- expect(stub).to have_been_requested
418
- end
419
-
420
- describe 'retry behaviour' do
421
- it "doesn't retry errors" do
422
- stub_url = '/mandate_imports/:identity/actions/cancel'.gsub(':identity', resource_id)
423
- stub = stub_request(:post, /.*api.gocardless.com#{stub_url}/).
424
- to_timeout
425
-
426
- expect { post_response }.to raise_error(Faraday::ConnectionFailed)
427
- expect(stub).to have_been_requested
428
- end
429
- end
430
-
431
- context 'when the request needs a body and custom header' do
432
- let(:body) { { foo: 'bar' } }
433
- let(:headers) { { 'Foo' => 'Bar' } }
434
- subject(:post_response) { client.mandate_imports.cancel(resource_id, body, headers) }
435
-
436
- let(:resource_id) { 'ABC123' }
437
-
438
- let!(:stub) do
439
- # /mandate_imports/%v/actions/cancel
440
- stub_url = '/mandate_imports/:identity/actions/cancel'.gsub(':identity', resource_id)
441
- stub_request(:post, /.*api.gocardless.com#{stub_url}/).
442
- with(
443
- body: { foo: 'bar' },
444
- headers: { 'Foo' => 'Bar' }
445
- ).to_return(
446
- body: {
447
- 'mandate_imports' => {
448
-
449
- 'created_at' => 'created_at-input',
450
- 'id' => 'id-input',
451
- 'scheme' => 'scheme-input',
452
- 'status' => 'status-input',
453
- },
454
- }.to_json,
455
- headers: response_headers
456
- )
457
- end
458
- end
459
- end
460
- end
@@ -1,140 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe GoCardlessPro::Services::MandatePdfsService do
4
- let(:client) do
5
- GoCardlessPro::Client.new(
6
- access_token: 'SECRET_TOKEN'
7
- )
8
- end
9
-
10
- let(:response_headers) { { 'Content-Type' => 'application/json' } }
11
-
12
- describe '#create' do
13
- subject(:post_create_response) { client.mandate_pdfs.create(params: new_resource) }
14
- context 'with a valid request' do
15
- let(:new_resource) do
16
- {
17
-
18
- 'expires_at' => 'expires_at-input',
19
- 'url' => 'url-input',
20
- }
21
- end
22
-
23
- before do
24
- stub_request(:post, %r{.*api.gocardless.com/mandate_pdfs}).
25
- with(
26
- body: {
27
- 'mandate_pdfs' => {
28
-
29
- 'expires_at' => 'expires_at-input',
30
- 'url' => 'url-input',
31
- },
32
- }
33
- ).
34
- to_return(
35
- body: {
36
- 'mandate_pdfs' =>
37
-
38
- {
39
-
40
- 'expires_at' => 'expires_at-input',
41
- 'url' => 'url-input',
42
- },
43
-
44
- }.to_json,
45
- headers: response_headers
46
- )
47
- end
48
-
49
- it 'creates and returns the resource' do
50
- expect(post_create_response).to be_a(GoCardlessPro::Resources::MandatePdf)
51
- end
52
-
53
- describe 'retry behaviour' do
54
- before { allow_any_instance_of(GoCardlessPro::Request).to receive(:sleep) }
55
-
56
- it 'retries timeouts' do
57
- stub = stub_request(:post, %r{.*api.gocardless.com/mandate_pdfs}).
58
- to_timeout.then.to_return(status: 200, headers: response_headers)
59
-
60
- post_create_response
61
- expect(stub).to have_been_requested.twice
62
- end
63
-
64
- it 'retries 5XX errors' do
65
- stub = stub_request(:post, %r{.*api.gocardless.com/mandate_pdfs}).
66
- to_return(status: 502,
67
- headers: { 'Content-Type' => 'text/html' },
68
- body: '<html><body>Response from Cloudflare</body></html>').
69
- then.to_return(status: 200, headers: response_headers)
70
-
71
- post_create_response
72
- expect(stub).to have_been_requested.twice
73
- end
74
- end
75
- end
76
-
77
- context 'with a request that returns a validation error' do
78
- let(:new_resource) { {} }
79
-
80
- before do
81
- stub_request(:post, %r{.*api.gocardless.com/mandate_pdfs}).to_return(
82
- body: {
83
- error: {
84
- type: 'validation_failed',
85
- code: 422,
86
- errors: [
87
- { message: 'test error message', field: 'test_field' },
88
- ],
89
- },
90
- }.to_json,
91
- headers: response_headers,
92
- status: 422
93
- )
94
- end
95
-
96
- it 'throws the correct error' do
97
- expect { post_create_response }.to raise_error(GoCardlessPro::ValidationError)
98
- end
99
- end
100
-
101
- context 'with a request that returns an idempotent creation conflict error' do
102
- let(:id) { 'ID123' }
103
-
104
- let(:new_resource) do
105
- {
106
-
107
- 'expires_at' => 'expires_at-input',
108
- 'url' => 'url-input',
109
- }
110
- end
111
-
112
- let!(:post_stub) do
113
- stub_request(:post, %r{.*api.gocardless.com/mandate_pdfs}).to_return(
114
- body: {
115
- error: {
116
- type: 'invalid_state',
117
- code: 409,
118
- errors: [
119
- {
120
- message: 'A resource has already been created with this idempotency key',
121
- reason: 'idempotent_creation_conflict',
122
- links: {
123
- conflicting_resource_id: id,
124
- },
125
- },
126
- ],
127
- },
128
- }.to_json,
129
- headers: response_headers,
130
- status: 409
131
- )
132
- end
133
-
134
- it 'raises an InvalidStateError' do
135
- expect { post_create_response }.to raise_error(GoCardlessPro::InvalidStateError)
136
- expect(post_stub).to have_been_requested
137
- end
138
- end
139
- end
140
- end