gocardless_pro 2.40.0 → 2.41.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,754 @@
1
+ require 'spec_helper'
2
+
3
+ describe GoCardlessPro::Services::SchemeIdentifiersService 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.scheme_identifiers.create(params: new_resource) }
14
+ context 'with a valid request' do
15
+ let(:new_resource) do
16
+ {
17
+
18
+ 'address_line1' => 'address_line1-input',
19
+ 'address_line2' => 'address_line2-input',
20
+ 'address_line3' => 'address_line3-input',
21
+ 'can_specify_mandate_reference' => 'can_specify_mandate_reference-input',
22
+ 'city' => 'city-input',
23
+ 'country_code' => 'country_code-input',
24
+ 'created_at' => 'created_at-input',
25
+ 'currency' => 'currency-input',
26
+ 'email' => 'email-input',
27
+ 'id' => 'id-input',
28
+ 'minimum_advance_notice' => 'minimum_advance_notice-input',
29
+ 'name' => 'name-input',
30
+ 'phone_number' => 'phone_number-input',
31
+ 'postal_code' => 'postal_code-input',
32
+ 'reference' => 'reference-input',
33
+ 'region' => 'region-input',
34
+ 'scheme' => 'scheme-input',
35
+ 'status' => 'status-input',
36
+ }
37
+ end
38
+
39
+ before do
40
+ stub_request(:post, %r{.*api.gocardless.com/scheme_identifiers}).
41
+ with(
42
+ body: {
43
+ 'scheme_identifiers' => {
44
+
45
+ 'address_line1' => 'address_line1-input',
46
+ 'address_line2' => 'address_line2-input',
47
+ 'address_line3' => 'address_line3-input',
48
+ 'can_specify_mandate_reference' => 'can_specify_mandate_reference-input',
49
+ 'city' => 'city-input',
50
+ 'country_code' => 'country_code-input',
51
+ 'created_at' => 'created_at-input',
52
+ 'currency' => 'currency-input',
53
+ 'email' => 'email-input',
54
+ 'id' => 'id-input',
55
+ 'minimum_advance_notice' => 'minimum_advance_notice-input',
56
+ 'name' => 'name-input',
57
+ 'phone_number' => 'phone_number-input',
58
+ 'postal_code' => 'postal_code-input',
59
+ 'reference' => 'reference-input',
60
+ 'region' => 'region-input',
61
+ 'scheme' => 'scheme-input',
62
+ 'status' => 'status-input',
63
+ },
64
+ }
65
+ ).
66
+ to_return(
67
+ body: {
68
+ 'scheme_identifiers' =>
69
+
70
+ {
71
+
72
+ 'address_line1' => 'address_line1-input',
73
+ 'address_line2' => 'address_line2-input',
74
+ 'address_line3' => 'address_line3-input',
75
+ 'can_specify_mandate_reference' => 'can_specify_mandate_reference-input',
76
+ 'city' => 'city-input',
77
+ 'country_code' => 'country_code-input',
78
+ 'created_at' => 'created_at-input',
79
+ 'currency' => 'currency-input',
80
+ 'email' => 'email-input',
81
+ 'id' => 'id-input',
82
+ 'minimum_advance_notice' => 'minimum_advance_notice-input',
83
+ 'name' => 'name-input',
84
+ 'phone_number' => 'phone_number-input',
85
+ 'postal_code' => 'postal_code-input',
86
+ 'reference' => 'reference-input',
87
+ 'region' => 'region-input',
88
+ 'scheme' => 'scheme-input',
89
+ 'status' => 'status-input',
90
+ },
91
+
92
+ }.to_json,
93
+ headers: response_headers
94
+ )
95
+ end
96
+
97
+ it 'creates and returns the resource' do
98
+ expect(post_create_response).to be_a(GoCardlessPro::Resources::SchemeIdentifier)
99
+ end
100
+
101
+ describe 'retry behaviour' do
102
+ before { allow_any_instance_of(GoCardlessPro::Request).to receive(:sleep) }
103
+
104
+ it 'retries timeouts' do
105
+ stub = stub_request(:post, %r{.*api.gocardless.com/scheme_identifiers}).
106
+ to_timeout.then.to_return(status: 200, headers: response_headers)
107
+
108
+ post_create_response
109
+ expect(stub).to have_been_requested.twice
110
+ end
111
+
112
+ it 'retries 5XX errors' do
113
+ stub = stub_request(:post, %r{.*api.gocardless.com/scheme_identifiers}).
114
+ to_return(status: 502,
115
+ headers: { 'Content-Type' => 'text/html' },
116
+ body: '<html><body>Response from Cloudflare</body></html>').
117
+ then.to_return(status: 200, headers: response_headers)
118
+
119
+ post_create_response
120
+ expect(stub).to have_been_requested.twice
121
+ end
122
+ end
123
+ end
124
+
125
+ context 'with a request that returns a validation error' do
126
+ let(:new_resource) { {} }
127
+
128
+ before do
129
+ stub_request(:post, %r{.*api.gocardless.com/scheme_identifiers}).to_return(
130
+ body: {
131
+ error: {
132
+ type: 'validation_failed',
133
+ code: 422,
134
+ errors: [
135
+ { message: 'test error message', field: 'test_field' },
136
+ ],
137
+ },
138
+ }.to_json,
139
+ headers: response_headers,
140
+ status: 422
141
+ )
142
+ end
143
+
144
+ it 'throws the correct error' do
145
+ expect { post_create_response }.to raise_error(GoCardlessPro::ValidationError)
146
+ end
147
+ end
148
+
149
+ context 'with a request that returns an idempotent creation conflict error' do
150
+ let(:id) { 'ID123' }
151
+
152
+ let(:new_resource) do
153
+ {
154
+
155
+ 'address_line1' => 'address_line1-input',
156
+ 'address_line2' => 'address_line2-input',
157
+ 'address_line3' => 'address_line3-input',
158
+ 'can_specify_mandate_reference' => 'can_specify_mandate_reference-input',
159
+ 'city' => 'city-input',
160
+ 'country_code' => 'country_code-input',
161
+ 'created_at' => 'created_at-input',
162
+ 'currency' => 'currency-input',
163
+ 'email' => 'email-input',
164
+ 'id' => 'id-input',
165
+ 'minimum_advance_notice' => 'minimum_advance_notice-input',
166
+ 'name' => 'name-input',
167
+ 'phone_number' => 'phone_number-input',
168
+ 'postal_code' => 'postal_code-input',
169
+ 'reference' => 'reference-input',
170
+ 'region' => 'region-input',
171
+ 'scheme' => 'scheme-input',
172
+ 'status' => 'status-input',
173
+ }
174
+ end
175
+
176
+ let!(:post_stub) do
177
+ stub_request(:post, %r{.*api.gocardless.com/scheme_identifiers}).to_return(
178
+ body: {
179
+ error: {
180
+ type: 'invalid_state',
181
+ code: 409,
182
+ errors: [
183
+ {
184
+ message: 'A resource has already been created with this idempotency key',
185
+ reason: 'idempotent_creation_conflict',
186
+ links: {
187
+ conflicting_resource_id: id,
188
+ },
189
+ },
190
+ ],
191
+ },
192
+ }.to_json,
193
+ headers: response_headers,
194
+ status: 409
195
+ )
196
+ end
197
+
198
+ let!(:get_stub) do
199
+ stub_url = "/scheme_identifiers/#{id}"
200
+ stub_request(:get, /.*api.gocardless.com#{stub_url}/).
201
+ to_return(
202
+ body: {
203
+ 'scheme_identifiers' => {
204
+
205
+ 'address_line1' => 'address_line1-input',
206
+ 'address_line2' => 'address_line2-input',
207
+ 'address_line3' => 'address_line3-input',
208
+ 'can_specify_mandate_reference' => 'can_specify_mandate_reference-input',
209
+ 'city' => 'city-input',
210
+ 'country_code' => 'country_code-input',
211
+ 'created_at' => 'created_at-input',
212
+ 'currency' => 'currency-input',
213
+ 'email' => 'email-input',
214
+ 'id' => 'id-input',
215
+ 'minimum_advance_notice' => 'minimum_advance_notice-input',
216
+ 'name' => 'name-input',
217
+ 'phone_number' => 'phone_number-input',
218
+ 'postal_code' => 'postal_code-input',
219
+ 'reference' => 'reference-input',
220
+ 'region' => 'region-input',
221
+ 'scheme' => 'scheme-input',
222
+ 'status' => 'status-input',
223
+ },
224
+ }.to_json,
225
+ headers: response_headers
226
+ )
227
+ end
228
+
229
+ context 'with default behaviour' do
230
+ it 'fetches the already-created resource' do
231
+ post_create_response
232
+ expect(post_stub).to have_been_requested
233
+ expect(get_stub).to have_been_requested
234
+ end
235
+ end
236
+
237
+ context 'with on_idempotency_conflict: :raise' do
238
+ let(:client) do
239
+ GoCardlessPro::Client.new(
240
+ access_token: 'SECRET_TOKEN',
241
+ on_idempotency_conflict: :raise
242
+ )
243
+ end
244
+
245
+ it 'raises an IdempotencyConflict error' do
246
+ expect { post_create_response }.
247
+ to raise_error(GoCardlessPro::IdempotencyConflict)
248
+ end
249
+ end
250
+ end
251
+ end
252
+
253
+ describe '#list' do
254
+ describe 'with no filters' do
255
+ subject(:get_list_response) { client.scheme_identifiers.list }
256
+
257
+ let(:body) do
258
+ {
259
+ 'scheme_identifiers' => [{
260
+
261
+ 'address_line1' => 'address_line1-input',
262
+ 'address_line2' => 'address_line2-input',
263
+ 'address_line3' => 'address_line3-input',
264
+ 'can_specify_mandate_reference' => 'can_specify_mandate_reference-input',
265
+ 'city' => 'city-input',
266
+ 'country_code' => 'country_code-input',
267
+ 'created_at' => 'created_at-input',
268
+ 'currency' => 'currency-input',
269
+ 'email' => 'email-input',
270
+ 'id' => 'id-input',
271
+ 'minimum_advance_notice' => 'minimum_advance_notice-input',
272
+ 'name' => 'name-input',
273
+ 'phone_number' => 'phone_number-input',
274
+ 'postal_code' => 'postal_code-input',
275
+ 'reference' => 'reference-input',
276
+ 'region' => 'region-input',
277
+ 'scheme' => 'scheme-input',
278
+ 'status' => 'status-input',
279
+ }],
280
+ meta: {
281
+ cursors: {
282
+ before: nil,
283
+ after: 'ABC123',
284
+ },
285
+ },
286
+ }.to_json
287
+ end
288
+
289
+ before do
290
+ stub_request(:get, %r{.*api.gocardless.com/scheme_identifiers}).to_return(
291
+ body: body,
292
+ headers: response_headers
293
+ )
294
+ end
295
+
296
+ it 'wraps each item in the resource class' do
297
+ expect(get_list_response.records.map(&:class).uniq.first).to eq(GoCardlessPro::Resources::SchemeIdentifier)
298
+
299
+ expect(get_list_response.records.first.address_line1).to eq('address_line1-input')
300
+
301
+ expect(get_list_response.records.first.address_line2).to eq('address_line2-input')
302
+
303
+ expect(get_list_response.records.first.address_line3).to eq('address_line3-input')
304
+
305
+ expect(get_list_response.records.first.can_specify_mandate_reference).to eq('can_specify_mandate_reference-input')
306
+
307
+ expect(get_list_response.records.first.city).to eq('city-input')
308
+
309
+ expect(get_list_response.records.first.country_code).to eq('country_code-input')
310
+
311
+ expect(get_list_response.records.first.created_at).to eq('created_at-input')
312
+
313
+ expect(get_list_response.records.first.currency).to eq('currency-input')
314
+
315
+ expect(get_list_response.records.first.email).to eq('email-input')
316
+
317
+ expect(get_list_response.records.first.id).to eq('id-input')
318
+
319
+ expect(get_list_response.records.first.minimum_advance_notice).to eq('minimum_advance_notice-input')
320
+
321
+ expect(get_list_response.records.first.name).to eq('name-input')
322
+
323
+ expect(get_list_response.records.first.phone_number).to eq('phone_number-input')
324
+
325
+ expect(get_list_response.records.first.postal_code).to eq('postal_code-input')
326
+
327
+ expect(get_list_response.records.first.reference).to eq('reference-input')
328
+
329
+ expect(get_list_response.records.first.region).to eq('region-input')
330
+
331
+ expect(get_list_response.records.first.scheme).to eq('scheme-input')
332
+
333
+ expect(get_list_response.records.first.status).to eq('status-input')
334
+ end
335
+
336
+ it 'exposes the cursors for before and after' do
337
+ expect(get_list_response.before).to eq(nil)
338
+ expect(get_list_response.after).to eq('ABC123')
339
+ end
340
+
341
+ specify { expect(get_list_response.api_response.headers).to eql('content-type' => 'application/json') }
342
+
343
+ describe 'retry behaviour' do
344
+ before { allow_any_instance_of(GoCardlessPro::Request).to receive(:sleep) }
345
+
346
+ it 'retries timeouts' do
347
+ stub = stub_request(:get, %r{.*api.gocardless.com/scheme_identifiers}).
348
+ to_timeout.then.to_return(status: 200, headers: response_headers, body: body)
349
+
350
+ get_list_response
351
+ expect(stub).to have_been_requested.twice
352
+ end
353
+
354
+ it 'retries 5XX errors' do
355
+ stub = stub_request(:get, %r{.*api.gocardless.com/scheme_identifiers}).
356
+ to_return(status: 502,
357
+ headers: { 'Content-Type' => 'text/html' },
358
+ body: '<html><body>Response from Cloudflare</body></html>').
359
+ then.to_return(status: 200, headers: response_headers, body: body)
360
+
361
+ get_list_response
362
+ expect(stub).to have_been_requested.twice
363
+ end
364
+ end
365
+ end
366
+ end
367
+
368
+ describe '#all' do
369
+ let!(:first_response_stub) do
370
+ stub_request(:get, %r{.*api.gocardless.com/scheme_identifiers$}).to_return(
371
+ body: {
372
+ 'scheme_identifiers' => [{
373
+
374
+ 'address_line1' => 'address_line1-input',
375
+ 'address_line2' => 'address_line2-input',
376
+ 'address_line3' => 'address_line3-input',
377
+ 'can_specify_mandate_reference' => 'can_specify_mandate_reference-input',
378
+ 'city' => 'city-input',
379
+ 'country_code' => 'country_code-input',
380
+ 'created_at' => 'created_at-input',
381
+ 'currency' => 'currency-input',
382
+ 'email' => 'email-input',
383
+ 'id' => 'id-input',
384
+ 'minimum_advance_notice' => 'minimum_advance_notice-input',
385
+ 'name' => 'name-input',
386
+ 'phone_number' => 'phone_number-input',
387
+ 'postal_code' => 'postal_code-input',
388
+ 'reference' => 'reference-input',
389
+ 'region' => 'region-input',
390
+ 'scheme' => 'scheme-input',
391
+ 'status' => 'status-input',
392
+ }],
393
+ meta: {
394
+ cursors: { after: 'AB345' },
395
+ limit: 1,
396
+ },
397
+ }.to_json,
398
+ headers: response_headers
399
+ )
400
+ end
401
+
402
+ let!(:second_response_stub) do
403
+ stub_request(:get, %r{.*api.gocardless.com/scheme_identifiers\?after=AB345}).to_return(
404
+ body: {
405
+ 'scheme_identifiers' => [{
406
+
407
+ 'address_line1' => 'address_line1-input',
408
+ 'address_line2' => 'address_line2-input',
409
+ 'address_line3' => 'address_line3-input',
410
+ 'can_specify_mandate_reference' => 'can_specify_mandate_reference-input',
411
+ 'city' => 'city-input',
412
+ 'country_code' => 'country_code-input',
413
+ 'created_at' => 'created_at-input',
414
+ 'currency' => 'currency-input',
415
+ 'email' => 'email-input',
416
+ 'id' => 'id-input',
417
+ 'minimum_advance_notice' => 'minimum_advance_notice-input',
418
+ 'name' => 'name-input',
419
+ 'phone_number' => 'phone_number-input',
420
+ 'postal_code' => 'postal_code-input',
421
+ 'reference' => 'reference-input',
422
+ 'region' => 'region-input',
423
+ 'scheme' => 'scheme-input',
424
+ 'status' => 'status-input',
425
+ }],
426
+ meta: {
427
+ limit: 2,
428
+ cursors: {},
429
+ },
430
+ }.to_json,
431
+ headers: response_headers
432
+ )
433
+ end
434
+
435
+ it 'automatically makes the extra requests' do
436
+ expect(client.scheme_identifiers.all.to_a.length).to eq(2)
437
+ expect(first_response_stub).to have_been_requested
438
+ expect(second_response_stub).to have_been_requested
439
+ end
440
+
441
+ describe 'retry behaviour' do
442
+ before { allow_any_instance_of(GoCardlessPro::Request).to receive(:sleep) }
443
+
444
+ it 'retries timeouts' do
445
+ first_response_stub = stub_request(:get, %r{.*api.gocardless.com/scheme_identifiers$}).to_return(
446
+ body: {
447
+ 'scheme_identifiers' => [{
448
+
449
+ 'address_line1' => 'address_line1-input',
450
+ 'address_line2' => 'address_line2-input',
451
+ 'address_line3' => 'address_line3-input',
452
+ 'can_specify_mandate_reference' => 'can_specify_mandate_reference-input',
453
+ 'city' => 'city-input',
454
+ 'country_code' => 'country_code-input',
455
+ 'created_at' => 'created_at-input',
456
+ 'currency' => 'currency-input',
457
+ 'email' => 'email-input',
458
+ 'id' => 'id-input',
459
+ 'minimum_advance_notice' => 'minimum_advance_notice-input',
460
+ 'name' => 'name-input',
461
+ 'phone_number' => 'phone_number-input',
462
+ 'postal_code' => 'postal_code-input',
463
+ 'reference' => 'reference-input',
464
+ 'region' => 'region-input',
465
+ 'scheme' => 'scheme-input',
466
+ 'status' => 'status-input',
467
+ }],
468
+ meta: {
469
+ cursors: { after: 'AB345' },
470
+ limit: 1,
471
+ },
472
+ }.to_json,
473
+ headers: response_headers
474
+ )
475
+
476
+ second_response_stub = stub_request(:get, %r{.*api.gocardless.com/scheme_identifiers\?after=AB345}).
477
+ to_timeout.then.
478
+ to_return(
479
+ body: {
480
+ 'scheme_identifiers' => [{
481
+
482
+ 'address_line1' => 'address_line1-input',
483
+ 'address_line2' => 'address_line2-input',
484
+ 'address_line3' => 'address_line3-input',
485
+ 'can_specify_mandate_reference' => 'can_specify_mandate_reference-input',
486
+ 'city' => 'city-input',
487
+ 'country_code' => 'country_code-input',
488
+ 'created_at' => 'created_at-input',
489
+ 'currency' => 'currency-input',
490
+ 'email' => 'email-input',
491
+ 'id' => 'id-input',
492
+ 'minimum_advance_notice' => 'minimum_advance_notice-input',
493
+ 'name' => 'name-input',
494
+ 'phone_number' => 'phone_number-input',
495
+ 'postal_code' => 'postal_code-input',
496
+ 'reference' => 'reference-input',
497
+ 'region' => 'region-input',
498
+ 'scheme' => 'scheme-input',
499
+ 'status' => 'status-input',
500
+ }],
501
+ meta: {
502
+ limit: 2,
503
+ cursors: {},
504
+ },
505
+ }.to_json,
506
+ headers: response_headers
507
+ )
508
+
509
+ client.scheme_identifiers.all.to_a
510
+
511
+ expect(first_response_stub).to have_been_requested
512
+ expect(second_response_stub).to have_been_requested.twice
513
+ end
514
+
515
+ it 'retries 5XX errors' do
516
+ first_response_stub = stub_request(:get, %r{.*api.gocardless.com/scheme_identifiers$}).to_return(
517
+ body: {
518
+ 'scheme_identifiers' => [{
519
+
520
+ 'address_line1' => 'address_line1-input',
521
+ 'address_line2' => 'address_line2-input',
522
+ 'address_line3' => 'address_line3-input',
523
+ 'can_specify_mandate_reference' => 'can_specify_mandate_reference-input',
524
+ 'city' => 'city-input',
525
+ 'country_code' => 'country_code-input',
526
+ 'created_at' => 'created_at-input',
527
+ 'currency' => 'currency-input',
528
+ 'email' => 'email-input',
529
+ 'id' => 'id-input',
530
+ 'minimum_advance_notice' => 'minimum_advance_notice-input',
531
+ 'name' => 'name-input',
532
+ 'phone_number' => 'phone_number-input',
533
+ 'postal_code' => 'postal_code-input',
534
+ 'reference' => 'reference-input',
535
+ 'region' => 'region-input',
536
+ 'scheme' => 'scheme-input',
537
+ 'status' => 'status-input',
538
+ }],
539
+ meta: {
540
+ cursors: { after: 'AB345' },
541
+ limit: 1,
542
+ },
543
+ }.to_json,
544
+ headers: response_headers
545
+ )
546
+
547
+ second_response_stub = stub_request(:get, %r{.*api.gocardless.com/scheme_identifiers\?after=AB345}).
548
+ to_return(
549
+ status: 502,
550
+ body: '<html><body>Response from Cloudflare</body></html>',
551
+ headers: { 'Content-Type' => 'text/html' }
552
+ ).then.to_return(
553
+ body: {
554
+ 'scheme_identifiers' => [{
555
+
556
+ 'address_line1' => 'address_line1-input',
557
+ 'address_line2' => 'address_line2-input',
558
+ 'address_line3' => 'address_line3-input',
559
+ 'can_specify_mandate_reference' => 'can_specify_mandate_reference-input',
560
+ 'city' => 'city-input',
561
+ 'country_code' => 'country_code-input',
562
+ 'created_at' => 'created_at-input',
563
+ 'currency' => 'currency-input',
564
+ 'email' => 'email-input',
565
+ 'id' => 'id-input',
566
+ 'minimum_advance_notice' => 'minimum_advance_notice-input',
567
+ 'name' => 'name-input',
568
+ 'phone_number' => 'phone_number-input',
569
+ 'postal_code' => 'postal_code-input',
570
+ 'reference' => 'reference-input',
571
+ 'region' => 'region-input',
572
+ 'scheme' => 'scheme-input',
573
+ 'status' => 'status-input',
574
+ }],
575
+ meta: {
576
+ limit: 2,
577
+ cursors: {},
578
+ },
579
+ }.to_json,
580
+ headers: response_headers
581
+ )
582
+
583
+ client.scheme_identifiers.all.to_a
584
+
585
+ expect(first_response_stub).to have_been_requested
586
+ expect(second_response_stub).to have_been_requested.twice
587
+ end
588
+ end
589
+ end
590
+
591
+ describe '#get' do
592
+ let(:id) { 'ID123' }
593
+
594
+ subject(:get_response) { client.scheme_identifiers.get(id) }
595
+
596
+ context 'passing in a custom header' do
597
+ let!(:stub) do
598
+ stub_url = '/scheme_identifiers/:identity'.gsub(':identity', id)
599
+ stub_request(:get, /.*api.gocardless.com#{stub_url}/).
600
+ with(headers: { 'Foo' => 'Bar' }).
601
+ to_return(
602
+ body: {
603
+ 'scheme_identifiers' => {
604
+
605
+ 'address_line1' => 'address_line1-input',
606
+ 'address_line2' => 'address_line2-input',
607
+ 'address_line3' => 'address_line3-input',
608
+ 'can_specify_mandate_reference' => 'can_specify_mandate_reference-input',
609
+ 'city' => 'city-input',
610
+ 'country_code' => 'country_code-input',
611
+ 'created_at' => 'created_at-input',
612
+ 'currency' => 'currency-input',
613
+ 'email' => 'email-input',
614
+ 'id' => 'id-input',
615
+ 'minimum_advance_notice' => 'minimum_advance_notice-input',
616
+ 'name' => 'name-input',
617
+ 'phone_number' => 'phone_number-input',
618
+ 'postal_code' => 'postal_code-input',
619
+ 'reference' => 'reference-input',
620
+ 'region' => 'region-input',
621
+ 'scheme' => 'scheme-input',
622
+ 'status' => 'status-input',
623
+ },
624
+ }.to_json,
625
+ headers: response_headers
626
+ )
627
+ end
628
+
629
+ subject(:get_response) do
630
+ client.scheme_identifiers.get(id, headers: {
631
+ 'Foo' => 'Bar',
632
+ })
633
+ end
634
+
635
+ it 'includes the header' do
636
+ get_response
637
+ expect(stub).to have_been_requested
638
+ end
639
+ end
640
+
641
+ context 'when there is a scheme_identifier to return' do
642
+ before do
643
+ stub_url = '/scheme_identifiers/:identity'.gsub(':identity', id)
644
+ stub_request(:get, /.*api.gocardless.com#{stub_url}/).to_return(
645
+ body: {
646
+ 'scheme_identifiers' => {
647
+
648
+ 'address_line1' => 'address_line1-input',
649
+ 'address_line2' => 'address_line2-input',
650
+ 'address_line3' => 'address_line3-input',
651
+ 'can_specify_mandate_reference' => 'can_specify_mandate_reference-input',
652
+ 'city' => 'city-input',
653
+ 'country_code' => 'country_code-input',
654
+ 'created_at' => 'created_at-input',
655
+ 'currency' => 'currency-input',
656
+ 'email' => 'email-input',
657
+ 'id' => 'id-input',
658
+ 'minimum_advance_notice' => 'minimum_advance_notice-input',
659
+ 'name' => 'name-input',
660
+ 'phone_number' => 'phone_number-input',
661
+ 'postal_code' => 'postal_code-input',
662
+ 'reference' => 'reference-input',
663
+ 'region' => 'region-input',
664
+ 'scheme' => 'scheme-input',
665
+ 'status' => 'status-input',
666
+ },
667
+ }.to_json,
668
+ headers: response_headers
669
+ )
670
+ end
671
+
672
+ it 'wraps the response in a resource' do
673
+ expect(get_response).to be_a(GoCardlessPro::Resources::SchemeIdentifier)
674
+ end
675
+ end
676
+
677
+ context 'when nothing is returned' do
678
+ before do
679
+ stub_url = '/scheme_identifiers/:identity'.gsub(':identity', id)
680
+ stub_request(:get, /.*api.gocardless.com#{stub_url}/).to_return(
681
+ body: '',
682
+ headers: response_headers
683
+ )
684
+ end
685
+
686
+ it 'returns nil' do
687
+ expect(get_response).to be_nil
688
+ end
689
+ end
690
+
691
+ context "when an ID is specified which can't be included in a valid URI" do
692
+ let(:id) { '`' }
693
+
694
+ it "doesn't raise an error" do
695
+ expect { get_response }.to_not raise_error(/bad URI/)
696
+ end
697
+ end
698
+
699
+ describe 'retry behaviour' do
700
+ before { allow_any_instance_of(GoCardlessPro::Request).to receive(:sleep) }
701
+
702
+ it 'retries timeouts' do
703
+ stub_url = '/scheme_identifiers/:identity'.gsub(':identity', id)
704
+
705
+ stub = stub_request(:get, /.*api.gocardless.com#{stub_url}/).
706
+ to_timeout.then.to_return(status: 200, headers: response_headers)
707
+
708
+ get_response
709
+ expect(stub).to have_been_requested.twice
710
+ end
711
+
712
+ it 'retries 5XX errors, other than 500s' do
713
+ stub_url = '/scheme_identifiers/:identity'.gsub(':identity', id)
714
+
715
+ stub = stub_request(:get, /.*api.gocardless.com#{stub_url}/).
716
+ to_return(status: 502,
717
+ headers: { 'Content-Type' => 'text/html' },
718
+ body: '<html><body>Response from Cloudflare</body></html>').
719
+ then.to_return(status: 200, headers: response_headers)
720
+
721
+ get_response
722
+ expect(stub).to have_been_requested.twice
723
+ end
724
+
725
+ it 'retries 500 errors returned by the API' do
726
+ stub_url = '/scheme_identifiers/:identity'.gsub(':identity', id)
727
+
728
+ gocardless_error = {
729
+ 'error' => {
730
+ 'message' => 'Internal server error',
731
+ 'documentation_url' => 'https://developer.gocardless.com/#gocardless',
732
+ 'errors' => [{
733
+ 'message' => 'Internal server error',
734
+ 'reason' => 'internal_server_error',
735
+ }],
736
+ 'type' => 'gocardless',
737
+ 'code' => 500,
738
+ 'request_id' => 'dummy_request_id',
739
+ 'id' => 'dummy_exception_id',
740
+ },
741
+ }
742
+
743
+ stub = stub_request(:get, /.*api.gocardless.com#{stub_url}/).
744
+ to_return(status: 500,
745
+ headers: response_headers,
746
+ body: gocardless_error.to_json).
747
+ then.to_return(status: 200, headers: response_headers)
748
+
749
+ get_response
750
+ expect(stub).to have_been_requested.twice
751
+ end
752
+ end
753
+ end
754
+ end