gocardless_pro 2.24.0 → 2.25.0

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,572 @@
1
+ require 'spec_helper'
2
+
3
+ describe GoCardlessPro::Services::PayerAuthorisationsService 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 '#get' do
13
+ let(:id) { 'ID123' }
14
+
15
+ subject(:get_response) { client.payer_authorisations.get(id) }
16
+
17
+ context 'passing in a custom header' do
18
+ let!(:stub) do
19
+ stub_url = '/payer_authorisations/:identity'.gsub(':identity', id)
20
+ stub_request(:get, /.*api.gocardless.com#{stub_url}/).
21
+ with(headers: { 'Foo' => 'Bar' }).
22
+ to_return(
23
+ body: {
24
+ 'payer_authorisations' => {
25
+
26
+ 'bank_account' => 'bank_account-input',
27
+ 'created_at' => 'created_at-input',
28
+ 'customer' => 'customer-input',
29
+ 'id' => 'id-input',
30
+ 'incomplete_fields' => 'incomplete_fields-input',
31
+ 'links' => 'links-input',
32
+ 'mandate' => 'mandate-input',
33
+ 'status' => 'status-input',
34
+ },
35
+ }.to_json,
36
+ headers: response_headers
37
+ )
38
+ end
39
+
40
+ subject(:get_response) do
41
+ client.payer_authorisations.get(id, headers: {
42
+ 'Foo' => 'Bar',
43
+ })
44
+ end
45
+
46
+ it 'includes the header' do
47
+ get_response
48
+ expect(stub).to have_been_requested
49
+ end
50
+ end
51
+
52
+ context 'when there is a payer_authorisation to return' do
53
+ before do
54
+ stub_url = '/payer_authorisations/:identity'.gsub(':identity', id)
55
+ stub_request(:get, /.*api.gocardless.com#{stub_url}/).to_return(
56
+ body: {
57
+ 'payer_authorisations' => {
58
+
59
+ 'bank_account' => 'bank_account-input',
60
+ 'created_at' => 'created_at-input',
61
+ 'customer' => 'customer-input',
62
+ 'id' => 'id-input',
63
+ 'incomplete_fields' => 'incomplete_fields-input',
64
+ 'links' => 'links-input',
65
+ 'mandate' => 'mandate-input',
66
+ 'status' => 'status-input',
67
+ },
68
+ }.to_json,
69
+ headers: response_headers
70
+ )
71
+ end
72
+
73
+ it 'wraps the response in a resource' do
74
+ expect(get_response).to be_a(GoCardlessPro::Resources::PayerAuthorisation)
75
+ end
76
+ end
77
+
78
+ context 'when nothing is returned' do
79
+ before do
80
+ stub_url = '/payer_authorisations/:identity'.gsub(':identity', id)
81
+ stub_request(:get, /.*api.gocardless.com#{stub_url}/).to_return(
82
+ body: '',
83
+ headers: response_headers
84
+ )
85
+ end
86
+
87
+ it 'returns nil' do
88
+ expect(get_response).to be_nil
89
+ end
90
+ end
91
+
92
+ context "when an ID is specified which can't be included in a valid URI" do
93
+ let(:id) { '`' }
94
+
95
+ it "doesn't raise an error" do
96
+ expect { get_response }.to_not raise_error(/bad URI/)
97
+ end
98
+ end
99
+
100
+ describe 'retry behaviour' do
101
+ before { allow_any_instance_of(GoCardlessPro::Request).to receive(:sleep) }
102
+
103
+ it 'retries timeouts' do
104
+ stub_url = '/payer_authorisations/:identity'.gsub(':identity', id)
105
+
106
+ stub = stub_request(:get, /.*api.gocardless.com#{stub_url}/).
107
+ to_timeout.then.to_return(status: 200, headers: response_headers)
108
+
109
+ get_response
110
+ expect(stub).to have_been_requested.twice
111
+ end
112
+
113
+ it 'retries 5XX errors, other than 500s' do
114
+ stub_url = '/payer_authorisations/:identity'.gsub(':identity', id)
115
+
116
+ stub = stub_request(:get, /.*api.gocardless.com#{stub_url}/).
117
+ to_return(status: 502,
118
+ headers: { 'Content-Type' => 'text/html' },
119
+ body: '<html><body>Response from Cloudflare</body></html>').
120
+ then.to_return(status: 200, headers: response_headers)
121
+
122
+ get_response
123
+ expect(stub).to have_been_requested.twice
124
+ end
125
+
126
+ it 'retries 500 errors returned by the API' do
127
+ stub_url = '/payer_authorisations/:identity'.gsub(':identity', id)
128
+
129
+ gocardless_error = {
130
+ 'error' => {
131
+ 'message' => 'Internal server error',
132
+ 'documentation_url' => 'https://developer.gocardless.com/#gocardless',
133
+ 'errors' => [{
134
+ 'message' => 'Internal server error',
135
+ 'reason' => 'internal_server_error',
136
+ }],
137
+ 'type' => 'gocardless',
138
+ 'code' => 500,
139
+ 'request_id' => 'dummy_request_id',
140
+ 'id' => 'dummy_exception_id',
141
+ },
142
+ }
143
+
144
+ stub = stub_request(:get, /.*api.gocardless.com#{stub_url}/).
145
+ to_return(status: 500,
146
+ headers: response_headers,
147
+ body: gocardless_error.to_json).
148
+ then.to_return(status: 200, headers: response_headers)
149
+
150
+ get_response
151
+ expect(stub).to have_been_requested.twice
152
+ end
153
+ end
154
+ end
155
+
156
+ describe '#create' do
157
+ subject(:post_create_response) { client.payer_authorisations.create(params: new_resource) }
158
+ context 'with a valid request' do
159
+ let(:new_resource) do
160
+ {
161
+
162
+ 'bank_account' => 'bank_account-input',
163
+ 'created_at' => 'created_at-input',
164
+ 'customer' => 'customer-input',
165
+ 'id' => 'id-input',
166
+ 'incomplete_fields' => 'incomplete_fields-input',
167
+ 'links' => 'links-input',
168
+ 'mandate' => 'mandate-input',
169
+ 'status' => 'status-input',
170
+ }
171
+ end
172
+
173
+ before do
174
+ stub_request(:post, %r{.*api.gocardless.com/payer_authorisations}).
175
+ with(
176
+ body: {
177
+ 'payer_authorisations' => {
178
+
179
+ 'bank_account' => 'bank_account-input',
180
+ 'created_at' => 'created_at-input',
181
+ 'customer' => 'customer-input',
182
+ 'id' => 'id-input',
183
+ 'incomplete_fields' => 'incomplete_fields-input',
184
+ 'links' => 'links-input',
185
+ 'mandate' => 'mandate-input',
186
+ 'status' => 'status-input',
187
+ },
188
+ }
189
+ ).
190
+ to_return(
191
+ body: {
192
+ 'payer_authorisations' =>
193
+
194
+ {
195
+
196
+ 'bank_account' => 'bank_account-input',
197
+ 'created_at' => 'created_at-input',
198
+ 'customer' => 'customer-input',
199
+ 'id' => 'id-input',
200
+ 'incomplete_fields' => 'incomplete_fields-input',
201
+ 'links' => 'links-input',
202
+ 'mandate' => 'mandate-input',
203
+ 'status' => 'status-input',
204
+ },
205
+
206
+ }.to_json,
207
+ headers: response_headers
208
+ )
209
+ end
210
+
211
+ it 'creates and returns the resource' do
212
+ expect(post_create_response).to be_a(GoCardlessPro::Resources::PayerAuthorisation)
213
+ end
214
+
215
+ describe 'retry behaviour' do
216
+ before { allow_any_instance_of(GoCardlessPro::Request).to receive(:sleep) }
217
+
218
+ it 'retries timeouts' do
219
+ stub = stub_request(:post, %r{.*api.gocardless.com/payer_authorisations}).
220
+ to_timeout.then.to_return(status: 200, headers: response_headers)
221
+
222
+ post_create_response
223
+ expect(stub).to have_been_requested.twice
224
+ end
225
+
226
+ it 'retries 5XX errors' do
227
+ stub = stub_request(:post, %r{.*api.gocardless.com/payer_authorisations}).
228
+ to_return(status: 502,
229
+ headers: { 'Content-Type' => 'text/html' },
230
+ body: '<html><body>Response from Cloudflare</body></html>').
231
+ then.to_return(status: 200, headers: response_headers)
232
+
233
+ post_create_response
234
+ expect(stub).to have_been_requested.twice
235
+ end
236
+ end
237
+ end
238
+
239
+ context 'with a request that returns a validation error' do
240
+ let(:new_resource) { {} }
241
+
242
+ before do
243
+ stub_request(:post, %r{.*api.gocardless.com/payer_authorisations}).to_return(
244
+ body: {
245
+ error: {
246
+ type: 'validation_failed',
247
+ code: 422,
248
+ errors: [
249
+ { message: 'test error message', field: 'test_field' },
250
+ ],
251
+ },
252
+ }.to_json,
253
+ headers: response_headers,
254
+ status: 422
255
+ )
256
+ end
257
+
258
+ it 'throws the correct error' do
259
+ expect { post_create_response }.to raise_error(GoCardlessPro::ValidationError)
260
+ end
261
+ end
262
+
263
+ context 'with a request that returns an idempotent creation conflict error' do
264
+ let(:id) { 'ID123' }
265
+
266
+ let(:new_resource) do
267
+ {
268
+
269
+ 'bank_account' => 'bank_account-input',
270
+ 'created_at' => 'created_at-input',
271
+ 'customer' => 'customer-input',
272
+ 'id' => 'id-input',
273
+ 'incomplete_fields' => 'incomplete_fields-input',
274
+ 'links' => 'links-input',
275
+ 'mandate' => 'mandate-input',
276
+ 'status' => 'status-input',
277
+ }
278
+ end
279
+
280
+ let!(:post_stub) do
281
+ stub_request(:post, %r{.*api.gocardless.com/payer_authorisations}).to_return(
282
+ body: {
283
+ error: {
284
+ type: 'invalid_state',
285
+ code: 409,
286
+ errors: [
287
+ {
288
+ message: 'A resource has already been created with this idempotency key',
289
+ reason: 'idempotent_creation_conflict',
290
+ links: {
291
+ conflicting_resource_id: id,
292
+ },
293
+ },
294
+ ],
295
+ },
296
+ }.to_json,
297
+ headers: response_headers,
298
+ status: 409
299
+ )
300
+ end
301
+
302
+ let!(:get_stub) do
303
+ stub_url = "/payer_authorisations/#{id}"
304
+ stub_request(:get, /.*api.gocardless.com#{stub_url}/).
305
+ to_return(
306
+ body: {
307
+ 'payer_authorisations' => {
308
+
309
+ 'bank_account' => 'bank_account-input',
310
+ 'created_at' => 'created_at-input',
311
+ 'customer' => 'customer-input',
312
+ 'id' => 'id-input',
313
+ 'incomplete_fields' => 'incomplete_fields-input',
314
+ 'links' => 'links-input',
315
+ 'mandate' => 'mandate-input',
316
+ 'status' => 'status-input',
317
+ },
318
+ }.to_json,
319
+ headers: response_headers
320
+ )
321
+ end
322
+
323
+ context 'with default behaviour' do
324
+ it 'fetches the already-created resource' do
325
+ post_create_response
326
+ expect(post_stub).to have_been_requested
327
+ expect(get_stub).to have_been_requested
328
+ end
329
+ end
330
+
331
+ context 'with on_idempotency_conflict: :raise' do
332
+ let(:client) do
333
+ GoCardlessPro::Client.new(
334
+ access_token: 'SECRET_TOKEN',
335
+ on_idempotency_conflict: :raise
336
+ )
337
+ end
338
+
339
+ it 'raises an IdempotencyConflict error' do
340
+ expect { post_create_response }.
341
+ to raise_error(GoCardlessPro::IdempotencyConflict)
342
+ end
343
+ end
344
+
345
+ context 'with on_idempotency_conflict: :unknown' do
346
+ let(:client) do
347
+ GoCardlessPro::Client.new(
348
+ access_token: 'SECRET_TOKEN',
349
+ on_idempotency_conflict: :unknown
350
+ )
351
+ end
352
+
353
+ it 'raises an ArgumentError' do
354
+ expect { post_create_response }.to raise_error(ArgumentError)
355
+ end
356
+ end
357
+ end
358
+ end
359
+
360
+ describe '#update' do
361
+ subject(:put_update_response) { client.payer_authorisations.update(id, params: update_params) }
362
+ let(:id) { 'ABC123' }
363
+
364
+ context 'with a valid request' do
365
+ let(:update_params) { { 'hello' => 'world' } }
366
+
367
+ let!(:stub) do
368
+ stub_url = '/payer_authorisations/:identity'.gsub(':identity', id)
369
+ stub_request(:put, /.*api.gocardless.com#{stub_url}/).to_return(
370
+ body: {
371
+ 'payer_authorisations' => {
372
+
373
+ 'bank_account' => 'bank_account-input',
374
+ 'created_at' => 'created_at-input',
375
+ 'customer' => 'customer-input',
376
+ 'id' => 'id-input',
377
+ 'incomplete_fields' => 'incomplete_fields-input',
378
+ 'links' => 'links-input',
379
+ 'mandate' => 'mandate-input',
380
+ 'status' => 'status-input',
381
+ },
382
+ }.to_json,
383
+ headers: response_headers
384
+ )
385
+ end
386
+
387
+ it 'updates and returns the resource' do
388
+ expect(put_update_response).to be_a(GoCardlessPro::Resources::PayerAuthorisation)
389
+ expect(stub).to have_been_requested
390
+ end
391
+
392
+ describe 'retry behaviour' do
393
+ before { allow_any_instance_of(GoCardlessPro::Request).to receive(:sleep) }
394
+
395
+ it 'retries timeouts' do
396
+ stub_url = '/payer_authorisations/:identity'.gsub(':identity', id)
397
+ stub = stub_request(:put, /.*api.gocardless.com#{stub_url}/).
398
+ to_timeout.then.to_return(status: 200, headers: response_headers)
399
+
400
+ put_update_response
401
+ expect(stub).to have_been_requested.twice
402
+ end
403
+
404
+ it 'retries 5XX errors' do
405
+ stub_url = '/payer_authorisations/:identity'.gsub(':identity', id)
406
+ stub = stub_request(:put, /.*api.gocardless.com#{stub_url}/).
407
+ to_return(status: 502,
408
+ headers: { 'Content-Type' => 'text/html' },
409
+ body: '<html><body>Response from Cloudflare</body></html>').
410
+ then.to_return(status: 200, headers: response_headers)
411
+
412
+ put_update_response
413
+ expect(stub).to have_been_requested.twice
414
+ end
415
+ end
416
+ end
417
+ end
418
+
419
+ describe '#submit' do
420
+ subject(:post_response) { client.payer_authorisations.submit(resource_id) }
421
+
422
+ let(:resource_id) { 'ABC123' }
423
+
424
+ let!(:stub) do
425
+ # /payer_authorisations/%v/actions/submit
426
+ stub_url = '/payer_authorisations/:identity/actions/submit'.gsub(':identity', resource_id)
427
+ stub_request(:post, /.*api.gocardless.com#{stub_url}/).to_return(
428
+ body: {
429
+ 'payer_authorisations' => {
430
+
431
+ 'bank_account' => 'bank_account-input',
432
+ 'created_at' => 'created_at-input',
433
+ 'customer' => 'customer-input',
434
+ 'id' => 'id-input',
435
+ 'incomplete_fields' => 'incomplete_fields-input',
436
+ 'links' => 'links-input',
437
+ 'mandate' => 'mandate-input',
438
+ 'status' => 'status-input',
439
+ },
440
+ }.to_json,
441
+ headers: response_headers
442
+ )
443
+ end
444
+
445
+ it 'wraps the response and calls the right endpoint' do
446
+ expect(post_response).to be_a(GoCardlessPro::Resources::PayerAuthorisation)
447
+
448
+ expect(stub).to have_been_requested
449
+ end
450
+
451
+ describe 'retry behaviour' do
452
+ it "doesn't retry errors" do
453
+ stub_url = '/payer_authorisations/:identity/actions/submit'.gsub(':identity', resource_id)
454
+ stub = stub_request(:post, /.*api.gocardless.com#{stub_url}/).
455
+ to_timeout
456
+
457
+ expect { post_response }.to raise_error(Faraday::ConnectionFailed)
458
+ expect(stub).to have_been_requested
459
+ end
460
+ end
461
+
462
+ context 'when the request needs a body and custom header' do
463
+ let(:body) { { foo: 'bar' } }
464
+ let(:headers) { { 'Foo' => 'Bar' } }
465
+ subject(:post_response) { client.payer_authorisations.submit(resource_id, body, headers) }
466
+
467
+ let(:resource_id) { 'ABC123' }
468
+
469
+ let!(:stub) do
470
+ # /payer_authorisations/%v/actions/submit
471
+ stub_url = '/payer_authorisations/:identity/actions/submit'.gsub(':identity', resource_id)
472
+ stub_request(:post, /.*api.gocardless.com#{stub_url}/).
473
+ with(
474
+ body: { foo: 'bar' },
475
+ headers: { 'Foo' => 'Bar' }
476
+ ).to_return(
477
+ body: {
478
+ 'payer_authorisations' => {
479
+
480
+ 'bank_account' => 'bank_account-input',
481
+ 'created_at' => 'created_at-input',
482
+ 'customer' => 'customer-input',
483
+ 'id' => 'id-input',
484
+ 'incomplete_fields' => 'incomplete_fields-input',
485
+ 'links' => 'links-input',
486
+ 'mandate' => 'mandate-input',
487
+ 'status' => 'status-input',
488
+ },
489
+ }.to_json,
490
+ headers: response_headers
491
+ )
492
+ end
493
+ end
494
+ end
495
+
496
+ describe '#confirm' do
497
+ subject(:post_response) { client.payer_authorisations.confirm(resource_id) }
498
+
499
+ let(:resource_id) { 'ABC123' }
500
+
501
+ let!(:stub) do
502
+ # /payer_authorisations/%v/actions/confirm
503
+ stub_url = '/payer_authorisations/:identity/actions/confirm'.gsub(':identity', resource_id)
504
+ stub_request(:post, /.*api.gocardless.com#{stub_url}/).to_return(
505
+ body: {
506
+ 'payer_authorisations' => {
507
+
508
+ 'bank_account' => 'bank_account-input',
509
+ 'created_at' => 'created_at-input',
510
+ 'customer' => 'customer-input',
511
+ 'id' => 'id-input',
512
+ 'incomplete_fields' => 'incomplete_fields-input',
513
+ 'links' => 'links-input',
514
+ 'mandate' => 'mandate-input',
515
+ 'status' => 'status-input',
516
+ },
517
+ }.to_json,
518
+ headers: response_headers
519
+ )
520
+ end
521
+
522
+ it 'wraps the response and calls the right endpoint' do
523
+ expect(post_response).to be_a(GoCardlessPro::Resources::PayerAuthorisation)
524
+
525
+ expect(stub).to have_been_requested
526
+ end
527
+
528
+ describe 'retry behaviour' do
529
+ it "doesn't retry errors" do
530
+ stub_url = '/payer_authorisations/:identity/actions/confirm'.gsub(':identity', resource_id)
531
+ stub = stub_request(:post, /.*api.gocardless.com#{stub_url}/).
532
+ to_timeout
533
+
534
+ expect { post_response }.to raise_error(Faraday::ConnectionFailed)
535
+ expect(stub).to have_been_requested
536
+ end
537
+ end
538
+
539
+ context 'when the request needs a body and custom header' do
540
+ let(:body) { { foo: 'bar' } }
541
+ let(:headers) { { 'Foo' => 'Bar' } }
542
+ subject(:post_response) { client.payer_authorisations.confirm(resource_id, body, headers) }
543
+
544
+ let(:resource_id) { 'ABC123' }
545
+
546
+ let!(:stub) do
547
+ # /payer_authorisations/%v/actions/confirm
548
+ stub_url = '/payer_authorisations/:identity/actions/confirm'.gsub(':identity', resource_id)
549
+ stub_request(:post, /.*api.gocardless.com#{stub_url}/).
550
+ with(
551
+ body: { foo: 'bar' },
552
+ headers: { 'Foo' => 'Bar' }
553
+ ).to_return(
554
+ body: {
555
+ 'payer_authorisations' => {
556
+
557
+ 'bank_account' => 'bank_account-input',
558
+ 'created_at' => 'created_at-input',
559
+ 'customer' => 'customer-input',
560
+ 'id' => 'id-input',
561
+ 'incomplete_fields' => 'incomplete_fields-input',
562
+ 'links' => 'links-input',
563
+ 'mandate' => 'mandate-input',
564
+ 'status' => 'status-input',
565
+ },
566
+ }.to_json,
567
+ headers: response_headers
568
+ )
569
+ end
570
+ end
571
+ end
572
+ end