ibandit 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,688 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ibandit::LocalDetailsCleaner do
4
+ subject(:cleaned) { described_class.clean(local_details) }
5
+ let(:local_details) do
6
+ {
7
+ country_code: country_code,
8
+ bank_code: bank_code,
9
+ branch_code: branch_code,
10
+ account_number: account_number
11
+ }
12
+ end
13
+ let(:country_code) { nil }
14
+ let(:bank_code) { nil }
15
+ let(:branch_code) { nil }
16
+ let(:account_number) { nil }
17
+
18
+ context 'without country code' do
19
+ it { is_expected.to eq(local_details) }
20
+ end
21
+
22
+ context 'with an unsupported country code' do
23
+ let(:country_code) { 'FU' }
24
+ it { is_expected.to eq(local_details) }
25
+ end
26
+
27
+ context 'Austria' do
28
+ let(:country_code) { 'AT' }
29
+ let(:bank_code) { '19043' }
30
+ let(:account_number) { '00234573201' }
31
+
32
+ it { is_expected.to eq(local_details) }
33
+
34
+ context 'with an account number which needs zero-padding' do
35
+ let(:account_number) { '234573201' }
36
+ its([:account_number]) { is_expected.to eq('00234573201') }
37
+ end
38
+
39
+ context 'with an account number under 4 digits' do
40
+ let(:account_number) { '123' }
41
+ its([:account_number]) { is_expected.to eq('123') }
42
+ end
43
+
44
+ context 'with a long account number' do
45
+ let(:account_number) { '234573201999' }
46
+ it { is_expected.to eq(local_details) }
47
+ end
48
+
49
+ context 'without an account number' do
50
+ let(:account_number) { nil }
51
+ it { is_expected.to eq(local_details) }
52
+ end
53
+
54
+ context 'with a short bank code' do
55
+ let(:bank_code) { '1904' }
56
+ it { is_expected.to eq(local_details) }
57
+ end
58
+
59
+ context 'with a long bank code' do
60
+ let(:bank_code) { '190430' }
61
+ it { is_expected.to eq(local_details) }
62
+ end
63
+
64
+ context 'without a bank code' do
65
+ let(:bank_code) { nil }
66
+ it { is_expected.to eq(local_details) }
67
+ end
68
+ end
69
+
70
+ context 'Belgium' do
71
+ let(:country_code) { 'BE' }
72
+ let(:account_number) { '510007547061' }
73
+
74
+ its([:country_code]) { is_expected.to eq(country_code) }
75
+ its([:account_number]) { is_expected.to eq(account_number) }
76
+ its([:bank_code]) { is_expected.to eq('510') }
77
+
78
+ context 'with dashes' do
79
+ let(:account_number) { '510-0075470-61' }
80
+ its([:bank_code]) { is_expected.to eq('510') }
81
+ its([:account_number]) { is_expected.to eq('510007547061') }
82
+ end
83
+
84
+ context 'without an account number' do
85
+ let(:account_number) { nil }
86
+ it { is_expected.to eq(local_details) }
87
+ end
88
+ end
89
+
90
+ context 'Cyprus' do
91
+ let(:country_code) { 'CY' }
92
+ let(:account_number) { '0000001200527600' }
93
+ let(:bank_code) { '002' }
94
+ let(:branch_code) { '00128' }
95
+
96
+ it { is_expected.to eq(local_details) }
97
+
98
+ context 'with a short account number' do
99
+ let(:account_number) { '1200527600' }
100
+ its([:account_number]) { is_expected.to eq('0000001200527600') }
101
+ end
102
+
103
+ context 'with a too-short account number' do
104
+ let(:account_number) { '123456' }
105
+ its([:account_number]) { is_expected.to eq(account_number) }
106
+ end
107
+
108
+ context 'with a long account number' do
109
+ let(:account_number) { '00000001200527600' }
110
+ it { is_expected.to eq(local_details) }
111
+ end
112
+
113
+ context 'without an account number' do
114
+ let(:account_number) { nil }
115
+ it { is_expected.to eq(local_details) }
116
+ end
117
+
118
+ context 'with the branch code in the bank code field' do
119
+ let(:bank_code) { '002 00128' }
120
+ let(:branch_code) { nil }
121
+ its([:bank_code]) { is_expected.to eq('002') }
122
+ its([:branch_code]) { is_expected.to eq('00128') }
123
+ end
124
+
125
+ context 'without a bank code' do
126
+ let(:bank_code) { nil }
127
+ it { is_expected.to eq(local_details) }
128
+ end
129
+ end
130
+
131
+ context 'Germany' do
132
+ let(:country_code) { 'DE' }
133
+ let(:bank_code) { '37040044' }
134
+ let(:account_number) { '0532013000' }
135
+
136
+ it { is_expected.to eq(local_details) }
137
+
138
+ context 'without a bank code' do
139
+ let(:bank_code) { nil }
140
+ it { is_expected.to eq(local_details) }
141
+ end
142
+
143
+ context 'without an account number' do
144
+ let(:account_number) { nil }
145
+ it { is_expected.to eq(local_details) }
146
+ end
147
+
148
+ context 'with an excessively short account number' do
149
+ let(:account_number) { '666' }
150
+ its([:account_number]) { is_expected.to eq(account_number) }
151
+ end
152
+
153
+ context 'with a pseudo account number' do
154
+ let(:bank_code) { '37080040' }
155
+ let(:account_number) { '111' }
156
+ its([:bank_code]) { is_expected.to eq(bank_code) }
157
+ its([:account_number]) { is_expected.to eq('0215022000') }
158
+ end
159
+ end
160
+
161
+ context 'Estonia' do
162
+ let(:country_code) { 'EE' }
163
+ let(:account_number) { '0221020145685' }
164
+
165
+ its([:bank_code]) { is_expected.to eq('22') }
166
+ its([:account_number]) { is_expected.to eq('00221020145685') }
167
+
168
+ context 'with an account number that needs translating' do
169
+ let(:account_number) { '111020145685' }
170
+ its([:bank_code]) { is_expected.to eq('22') }
171
+ its([:account_number]) { is_expected.to eq('00111020145685') }
172
+ end
173
+
174
+ context 'without an account number' do
175
+ let(:account_number) { nil }
176
+ it { is_expected.to eq(local_details) }
177
+ end
178
+ end
179
+
180
+ context 'Spain' do
181
+ let(:country_code) { 'ES' }
182
+ let(:bank_code) { '2310' }
183
+ let(:branch_code) { '0001' }
184
+ let(:account_number) { '180000012345' }
185
+
186
+ it { is_expected.to eq(local_details) }
187
+
188
+ context 'with bank and branch codes in the account number' do
189
+ let(:bank_code) { nil }
190
+ let(:branch_code) { nil }
191
+ let(:account_number) { '2310-0001-18-0000012345' }
192
+
193
+ its([:bank_code]) { is_expected.to eq('2310') }
194
+ its([:branch_code]) { is_expected.to eq('0001') }
195
+ its([:account_number]) { is_expected.to eq('180000012345') }
196
+ end
197
+
198
+ context 'without an account number' do
199
+ let(:account_number) { nil }
200
+ it { is_expected.to eq(local_details) }
201
+ end
202
+ end
203
+
204
+ context 'Finland' do
205
+ let(:country_code) { 'FI' }
206
+ let(:bank_code) { '123456' }
207
+ let(:account_number) { '00000785' }
208
+
209
+ it { is_expected.to eq(local_details) }
210
+
211
+ context 'with a shorter account number' do
212
+ let(:account_number) { '785' }
213
+ its([:account_number]) { is_expected.to eq('00000785') }
214
+ end
215
+
216
+ context 'with a savings bank account_number in traditional format' do
217
+ let(:account_number) { '78510' }
218
+ let(:bank_code) { '423456' }
219
+
220
+ its([:bank_code]) { is_expected.to eq(bank_code) }
221
+ its([:account_number]) { is_expected.to eq('70008510') }
222
+ end
223
+
224
+ context 'without an account number' do
225
+ let(:account_number) { nil }
226
+ it { is_expected.to eq(local_details) }
227
+ end
228
+
229
+ context 'without a bank code' do
230
+ let(:bank_code) { nil }
231
+ it { is_expected.to eq(local_details) }
232
+ end
233
+ end
234
+
235
+ context 'France' do
236
+ let(:country_code) { 'FR' }
237
+ let(:bank_code) { '20041' }
238
+ let(:branch_code) { '01005' }
239
+ let(:account_number) { '0500013M02606' }
240
+
241
+ it { is_expected.to eq(local_details) }
242
+
243
+ context 'with the RIB key spaced in the account number' do
244
+ let(:account_number) { '0500013M026 06' }
245
+ its([:account_number]) { is_expected.to eq('0500013M02606') }
246
+ end
247
+
248
+ context 'with the RIB key hyphenated in the account number' do
249
+ let(:account_number) { '0500013M026-06' }
250
+ its([:account_number]) { is_expected.to eq('0500013M02606') }
251
+ end
252
+
253
+ context 'with the RIB key missing' do
254
+ let(:account_number) { '0500013M026' }
255
+ it { is_expected.to eq(local_details) }
256
+ end
257
+
258
+ context 'without a bank code' do
259
+ let(:bank_code) { nil }
260
+ it { is_expected.to eq(local_details) }
261
+ end
262
+
263
+ context 'without a branch code' do
264
+ let(:branch_code) { nil }
265
+ it { is_expected.to eq(local_details) }
266
+ end
267
+
268
+ context 'without an account number' do
269
+ let(:account_number) { nil }
270
+ it { is_expected.to eq(local_details) }
271
+ end
272
+ end
273
+
274
+ context 'United Kingdom' do
275
+ let(:country_code) { 'GB' }
276
+ let(:bank_code) { 'BARC' }
277
+ let(:branch_code) { '200000' }
278
+ let(:account_number) { '55779911' }
279
+
280
+ it { is_expected.to eq(local_details) }
281
+
282
+ context 'with the sort code is hyphenated' do
283
+ let(:branch_code) { '20-00-00' }
284
+ its([:branch_code]) { is_expected.to eq('200000') }
285
+ end
286
+
287
+ context 'with the sort code spaced' do
288
+ let(:branch_code) { '20 00 00' }
289
+ its([:branch_code]) { is_expected.to eq('200000') }
290
+ end
291
+
292
+ context 'with a short account number' do
293
+ let(:account_number) { '579135' }
294
+ its([:account_number]) { is_expected.to eq('00579135') }
295
+ end
296
+
297
+ context 'with a too-short account number' do
298
+ let(:account_number) { '5678' }
299
+ its([:account_number]) { is_expected.to eq(account_number) }
300
+ end
301
+
302
+ context 'with the account number spaced' do
303
+ let(:account_number) { '5577 9911' }
304
+ its([:account_number]) { is_expected.to eq('55779911') }
305
+ end
306
+
307
+ context 'with the account number hyphenated' do
308
+ let(:account_number) { '5577-9911' }
309
+ its([:account_number]) { is_expected.to eq('55779911') }
310
+ end
311
+
312
+ context 'without a branch code' do
313
+ let(:branch_code) { nil }
314
+ it { is_expected.to eq(local_details) }
315
+ end
316
+
317
+ context 'without a bank code' do
318
+ let(:bank_code) { nil }
319
+ it { is_expected.to eq(local_details) }
320
+
321
+ context 'with a BIC finder set' do
322
+ let(:bic_finder) { double }
323
+ before do
324
+ allow(bic_finder).to receive(:call).with('GB', '200000').
325
+ and_return('BARCGB22XXX')
326
+ Ibandit.bic_finder = bic_finder
327
+ end
328
+ after { Ibandit.bic_finder = nil }
329
+
330
+ its([:bank_code]) { is_expected.to eq('BARC') }
331
+ end
332
+ end
333
+
334
+ context 'with a bank code and BIC finder set' do
335
+ let(:bank_code) { 'OVERRIDE' }
336
+ let(:bic_finder) { double }
337
+ before do
338
+ allow(bic_finder).to receive(:call).with('GB', '200000').
339
+ and_return('BARCGB22XXX')
340
+ Ibandit.bic_finder = bic_finder
341
+ end
342
+ after { Ibandit.bic_finder = nil }
343
+
344
+ its([:bank_code]) { is_expected.to eq('OVERRIDE') }
345
+ end
346
+ end
347
+
348
+ context 'Ireland' do
349
+ let(:country_code) { 'IE' }
350
+ let(:bank_code) { 'AIBK' }
351
+ let(:branch_code) { '931152' }
352
+ let(:account_number) { '12345678' }
353
+
354
+ it { is_expected.to eq(local_details) }
355
+
356
+ context 'with the sort code is hyphenated' do
357
+ let(:branch_code) { '93-11-52' }
358
+ its([:branch_code]) { is_expected.to eq('931152') }
359
+ end
360
+
361
+ context 'with the sort code spaced' do
362
+ let(:branch_code) { '93 11 52' }
363
+ its([:branch_code]) { is_expected.to eq('931152') }
364
+ end
365
+
366
+ context 'with a short account number' do
367
+ let(:account_number) { '579135' }
368
+ its([:account_number]) { is_expected.to eq('00579135') }
369
+ end
370
+
371
+ context 'with a too-short account number' do
372
+ let(:account_number) { '5678' }
373
+ its([:account_number]) { is_expected.to eq(account_number) }
374
+ end
375
+
376
+ context 'with the account number spaced' do
377
+ let(:account_number) { '5577 9911' }
378
+ its([:account_number]) { is_expected.to eq('55779911') }
379
+ end
380
+
381
+ context 'with the account number hyphenated' do
382
+ let(:account_number) { '5577-9911' }
383
+ its([:account_number]) { is_expected.to eq('55779911') }
384
+ end
385
+
386
+ context 'without a branch code' do
387
+ let(:branch_code) { nil }
388
+ it { is_expected.to eq(local_details) }
389
+ end
390
+
391
+ context 'without a bank code' do
392
+ let(:bank_code) { nil }
393
+ it { is_expected.to eq(local_details) }
394
+
395
+ context 'with a BIC finder set' do
396
+ let(:bic_finder) { double }
397
+ before do
398
+ allow(bic_finder).to receive(:call).with('IE', '931152').
399
+ and_return('AIBKIE22XXX')
400
+ Ibandit.bic_finder = bic_finder
401
+ end
402
+ after { Ibandit.bic_finder = nil }
403
+
404
+ its([:bank_code]) { is_expected.to eq('AIBK') }
405
+ end
406
+ end
407
+
408
+ context 'with a bank code and BIC finder set' do
409
+ let(:bank_code) { 'OVERRIDE' }
410
+ let(:bic_finder) { double }
411
+ before do
412
+ allow(bic_finder).to receive(:call).with('IE', '931152').
413
+ and_return('AIBKIE22XXX')
414
+ Ibandit.bic_finder = bic_finder
415
+ end
416
+ after { Ibandit.bic_finder = nil }
417
+
418
+ its([:bank_code]) { is_expected.to eq('OVERRIDE') }
419
+ end
420
+ end
421
+
422
+ context 'Italy' do
423
+ let(:country_code) { 'IT' }
424
+ let(:bank_code) { '05428' }
425
+ let(:branch_code) { '11101' }
426
+ let(:account_number) { '000000123456' }
427
+
428
+ it { is_expected.to eq(local_details) }
429
+
430
+ context 'with an explicit check digit' do
431
+ before { local_details.merge!(check_digit: 'Y') }
432
+ it { is_expected.to eq(local_details) }
433
+ end
434
+
435
+ context 'with the account number not zero-padded' do
436
+ let(:account_number) { '123456' }
437
+ its([:account_number]) { is_expected.to eq('000000123456') }
438
+ end
439
+
440
+ context 'without a bank code' do
441
+ let(:bank_code) { nil }
442
+ it { is_expected.to eq(local_details) }
443
+ end
444
+
445
+ context 'without a branch code' do
446
+ let(:branch_code) { nil }
447
+ it { is_expected.to eq(local_details) }
448
+ end
449
+
450
+ context 'without an account number' do
451
+ let(:account_number) { nil }
452
+ it { is_expected.to eq(local_details) }
453
+ end
454
+ end
455
+
456
+ context 'Lithuania' do
457
+ let(:country_code) { 'LT' }
458
+ let(:bank_code) { '10000' }
459
+ let(:account_number) { '11101001000' }
460
+
461
+ it { is_expected.to eq(local_details) }
462
+
463
+ context 'without an account number' do
464
+ let(:account_number) { nil }
465
+ it { is_expected.to eq(local_details) }
466
+ end
467
+
468
+ context 'without a bank code' do
469
+ let(:bank_code) { nil }
470
+ it { is_expected.to eq(local_details) }
471
+ end
472
+ end
473
+
474
+ context 'Luxembourg' do
475
+ let(:country_code) { 'LU' }
476
+ let(:bank_code) { '001' }
477
+ let(:account_number) { '9400644750000' }
478
+
479
+ it { is_expected.to eq(local_details) }
480
+
481
+ context 'without an account number' do
482
+ let(:account_number) { nil }
483
+ it { is_expected.to eq(local_details) }
484
+ end
485
+
486
+ context 'without a bank code' do
487
+ let(:bank_code) { nil }
488
+ it { is_expected.to eq(local_details) }
489
+ end
490
+ end
491
+
492
+ context 'Latvia' do
493
+ let(:country_code) { 'LV' }
494
+ let(:bank_code) { 'BANK' }
495
+ let(:account_number) { '1234567890123' }
496
+
497
+ it { is_expected.to eq(local_details) }
498
+
499
+ context 'without an account number' do
500
+ let(:account_number) { nil }
501
+ it { is_expected.to eq(local_details) }
502
+ end
503
+
504
+ context 'without a bank code' do
505
+ let(:bank_code) { nil }
506
+ it { is_expected.to eq(local_details) }
507
+ end
508
+ end
509
+
510
+ context 'Monaco' do
511
+ let(:country_code) { 'MC' }
512
+ let(:bank_code) { '20041' }
513
+ let(:branch_code) { '01005' }
514
+ let(:account_number) { '0500013M02606' }
515
+
516
+ it { is_expected.to eq(local_details) }
517
+
518
+ context 'with the RIB key spaced in the account number' do
519
+ let(:account_number) { '0500013M026 06' }
520
+ its([:account_number]) { is_expected.to eq('0500013M02606') }
521
+ end
522
+
523
+ context 'with the RIB key hyphenated in the account number' do
524
+ let(:account_number) { '0500013M026-06' }
525
+ its([:account_number]) { is_expected.to eq('0500013M02606') }
526
+ end
527
+
528
+ context 'with the RIB key missing' do
529
+ let(:account_number) { '0500013M026' }
530
+ it { is_expected.to eq(local_details) }
531
+ end
532
+
533
+ context 'without a bank code' do
534
+ let(:bank_code) { nil }
535
+ it { is_expected.to eq(local_details) }
536
+ end
537
+
538
+ context 'without a branch code' do
539
+ let(:branch_code) { nil }
540
+ it { is_expected.to eq(local_details) }
541
+ end
542
+
543
+ context 'without an account number' do
544
+ let(:account_number) { nil }
545
+ it { is_expected.to eq(local_details) }
546
+ end
547
+ end
548
+
549
+ context 'Netherlands' do
550
+ let(:country_code) { 'NL' }
551
+ let(:bank_code) { 'ABNA' }
552
+ let(:account_number) { '0417164300' }
553
+
554
+ it { is_expected.to eq(local_details) }
555
+
556
+ context 'without a bank code' do
557
+ let(:bank_code) { nil }
558
+ it { is_expected.to eq(local_details) }
559
+ end
560
+
561
+ context 'without a branch code' do
562
+ let(:branch_code) { nil }
563
+ it { is_expected.to eq(local_details) }
564
+ end
565
+
566
+ context 'without an account number' do
567
+ let(:account_number) { nil }
568
+ it { is_expected.to eq(local_details) }
569
+ end
570
+
571
+ context 'with an account number that needs zero-padding' do
572
+ let(:account_number) { '417164300' }
573
+ its([:account_number]) { is_expected.to eq('0417164300') }
574
+ end
575
+ end
576
+
577
+ context 'Portugal' do
578
+ let(:country_code) { 'PT' }
579
+ let(:bank_code) { '0002' }
580
+ let(:branch_code) { '0023' }
581
+ let(:account_number) { '0023843000578' }
582
+
583
+ it { is_expected.to eq(local_details) }
584
+
585
+ context 'without a bank code' do
586
+ let(:bank_code) { nil }
587
+ it { is_expected.to eq(local_details) }
588
+ end
589
+
590
+ context 'without a branch code' do
591
+ let(:branch_code) { nil }
592
+ it { is_expected.to eq(local_details) }
593
+ end
594
+
595
+ context 'without an account number' do
596
+ let(:account_number) { nil }
597
+ it { is_expected.to eq(local_details) }
598
+ end
599
+ end
600
+
601
+ context 'Slovenia' do
602
+ let(:country_code) { 'SI' }
603
+ let(:bank_code) { '19100' }
604
+ let(:account_number) { '0000123438' }
605
+
606
+ it { is_expected.to eq(local_details) }
607
+
608
+ context 'with an account number which needs zero-padding' do
609
+ let(:account_number) { '123438' }
610
+ its([:account_number]) { is_expected.to eq('0000123438') }
611
+ end
612
+
613
+ context 'without a bank code' do
614
+ let(:bank_code) { nil }
615
+ it { is_expected.to eq(local_details) }
616
+ end
617
+
618
+ context 'without an account number' do
619
+ let(:account_number) { nil }
620
+ it { is_expected.to eq(local_details) }
621
+ end
622
+ end
623
+
624
+ context 'Slovak Republic' do
625
+ let(:country_code) { 'SK' }
626
+ let(:bank_code) { '1200' }
627
+ let(:account_number) { '0000198742637541' }
628
+
629
+ it { is_expected.to eq(local_details) }
630
+
631
+ context 'with an account number prefix' do
632
+ let(:prefix) { '000019' }
633
+ let(:account_number) { '8742637541' }
634
+ before { local_details.merge!(account_number_prefix: prefix) }
635
+
636
+ its([:account_number]) { is_expected.to eq('0000198742637541') }
637
+
638
+ context 'which needs zero-padding' do
639
+ let(:prefix) { '19' }
640
+ its([:account_number]) { is_expected.to eq('0000198742637541') }
641
+ end
642
+ end
643
+
644
+ context 'without a bank code' do
645
+ let(:bank_code) { nil }
646
+ it { is_expected.to eq(local_details) }
647
+ end
648
+
649
+ context 'without an account number' do
650
+ let(:account_number) { nil }
651
+ it { is_expected.to eq(local_details) }
652
+ end
653
+ end
654
+
655
+ context 'San Marino' do
656
+ let(:country_code) { 'SM' }
657
+ let(:bank_code) { '05428' }
658
+ let(:branch_code) { '11101' }
659
+ let(:account_number) { '000000123456' }
660
+
661
+ it { is_expected.to eq(local_details) }
662
+
663
+ context 'with an explicit check digit' do
664
+ before { local_details.merge!(check_digit: 'Y') }
665
+ it { is_expected.to eq(local_details) }
666
+ end
667
+
668
+ context 'with the account number not zero-padded' do
669
+ let(:account_number) { '123456' }
670
+ its([:account_number]) { is_expected.to eq('000000123456') }
671
+ end
672
+
673
+ context 'without a bank code' do
674
+ let(:bank_code) { nil }
675
+ it { is_expected.to eq(local_details) }
676
+ end
677
+
678
+ context 'without a branch code' do
679
+ let(:branch_code) { nil }
680
+ it { is_expected.to eq(local_details) }
681
+ end
682
+
683
+ context 'without an account number' do
684
+ let(:account_number) { nil }
685
+ it { is_expected.to eq(local_details) }
686
+ end
687
+ end
688
+ end