This repository was archived by the owner on Feb 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtests.py
496 lines (408 loc) · 18.8 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
"""
To run the test cases below make sure you have following environment variables set:
TEST_API_KEY -- API Key for your organization
TEST_LINK_ID -- any link_id string
TEST_ENTITY_ID -- any entity id which you have created before using any of our APIs / libraries before under the above organization
with link_id as above (TEST_LINK_ID)
TEST_ACCOUNT_ID -- an account id of the above TEST_ENTITY_ID entity having some transactions
Make sure the statement used for testing has at least one salary, lender, credit, debit transaction under the above account id
"""
import unittest
import os
import datetime
import finbox_bankconnect as fbc
from finbox_bankconnect.custom_exceptions import EntityNotFoundError, PasswordIncorrectError
from finbox_bankconnect.custom_exceptions import UnparsablePDFError, CannotIdentityBankError
from finbox_bankconnect.utils import is_valid_uuid4
NOT_EXISTS_ENTITY_ID = "c036e96d-ccae-443c-8f64-b98ceeaa1578"
class TestUtilFunctions(unittest.TestCase):
"""
Test cases for utility functions
"""
def test_valid_uuid4(self):
self.assertEqual(is_valid_uuid4("c036e96d-ccae-443c-8f64-b98ceeaa1578"), True, "valid uuid4 detected as invalid")
def test_uuid4_without_hyphen(self):
self.assertEqual(is_valid_uuid4("c036e96dccae443c8f64b98ceeaa1578"), False, "valid uuid4 without hyphen must be invalid")
def test_string_not_uuid4(self):
self.assertEqual(is_valid_uuid4("abc"), False, "invalid uuid4 string detected as valid")
def test_integer(self):
self.assertEqual(is_valid_uuid4(123), False, "integer detected as valid uui4")
def test_list(self):
self.assertEqual(is_valid_uuid4(["sadasd", "asdasd"]), False, "list detected as valid uui4")
def test_none(self):
self.assertEqual(is_valid_uuid4(None), False, "list detected as valid uui4")
class TestGetEntityEdgeCases(unittest.TestCase):
"""
Test edge cases for Entity.get function
"""
def setUp(self):
fbc.api_key = os.environ['TEST_API_KEY']
def test_empty_entity_id(self):
exception_handled = False
try:
fbc.Entity.get(entity_id = '')
except ValueError:
exception_handled = True
except Exception as e:
print(e)
self.assertEqual(exception_handled, True, "entity_id blank string case - not handled")
def test_none_entity_id(self):
exception_handled = False
try:
fbc.Entity.get(entity_id = None)
except ValueError:
exception_handled = True
except Exception as e:
print(e)
self.assertEqual(exception_handled, True, "entity_id none case - not handled")
def test_invalid_entity_id(self):
exception_handled = False
try:
fbc.Entity.get(entity_id = 'c036e96dccae443c8f64b98ceeaa1578')
except ValueError:
exception_handled = True
except Exception as e:
print(e)
self.assertEqual(exception_handled, True, "entity_id not a valid uuid4 (with hyphen) case - not handled")
def test_non_string_entity_id(self):
list_handled = False
int_handled = False
try:
fbc.Entity.get(entity_id = ["hello"])
except ValueError:
list_handled = True
except Exception as e:
print(e)
try:
fbc.Entity.get(entity_id = 123)
except ValueError:
int_handled = True
except Exception as e:
print(e)
self.assertEqual(list_handled and int_handled, True, "entity_id non string case - not handled")
def test_detail_not_found_identity(self):
exception_handled = False
try:
entity = fbc.Entity.get(entity_id = NOT_EXISTS_ENTITY_ID)
entity.get_identity()
except EntityNotFoundError:
exception_handled = True
except Exception as e:
print(e)
self.assertEqual(exception_handled, True, "entity_id valid but not in DB case, not handled in get_identity")
def test_detail_not_found_transactions(self):
exception_handled = False
try:
entity = fbc.Entity.get(entity_id = NOT_EXISTS_ENTITY_ID)
entity.get_transactions()
except EntityNotFoundError:
exception_handled = True
except Exception as e:
print(e)
self.assertEqual(exception_handled, True, "entity_id valid but not in DB case, not handled in get_transactions")
def test_detail_not_found_fraud_info(self):
exception_handled = False
try:
entity = fbc.Entity.get(entity_id = NOT_EXISTS_ENTITY_ID)
entity.get_fraud_info()
except EntityNotFoundError:
exception_handled = True
except Exception as e:
print(e)
self.assertEqual(exception_handled, True, "entity_id valid but not in DB case, not handled in get_fraud_info")
def test_detail_not_found_accounts(self):
exception_handled = False
try:
entity = fbc.Entity.get(entity_id = NOT_EXISTS_ENTITY_ID)
entity.get_accounts()
except EntityNotFoundError:
exception_handled = True
except Exception as e:
print(e)
self.assertEqual(exception_handled, True, "entity_id valid but not in DB case, not handled in get_accounts")
class TestLinkIdFlow(unittest.TestCase):
"""
Test the flow by using the link_id
"""
def setUp(self):
fbc.api_key = os.environ['TEST_API_KEY']
def test_non_string_link_id(self):
list_handled = False
int_handled = False
try:
fbc.Entity.create(link_id = ["hello"])
except ValueError:
list_handled = True
except Exception as e:
print(e)
try:
fbc.Entity.create(link_id = 123)
except ValueError:
int_handled = True
except Exception as e:
print(e)
self.assertEqual(list_handled and int_handled, True, "link_id non string case - not handled")
def test_entity_creation(self):
entity = fbc.Entity.create(link_id = "python_link_id_test_1")
entity_id = entity.entity_id
self.assertEqual(is_valid_uuid4(entity_id), True, "entity_id couldn't be created against the link_id")
def test_link_id_fetch(self):
entity = fbc.Entity.get(entity_id=os.environ['TEST_ENTITY_ID'])
self.assertEqual(entity.link_id, os.environ['TEST_LINK_ID'], "link_id was incorrectly fetched")
class TestIdentity(unittest.TestCase):
"""
Test identity when using get_accounts function
"""
def setUp(self):
fbc.api_key = os.environ['TEST_API_KEY']
entity = fbc.Entity.get(entity_id=os.environ['TEST_ENTITY_ID'])
self.identity = entity.get_identity()
def test_name(self):
self.assertIn("name", self.identity, "name not present in identity")
def test_account_id(self):
self.assertIn("account_id", self.identity, "account_id not present in identity")
def test_account_number(self):
self.assertIn("account_number", self.identity, "account_number not present in identity")
def test_address(self):
self.assertIn("address", self.identity, "address not present in identity")
class TestAccounts(unittest.TestCase):
"""
Test accounts when using get_accounts function
"""
def setUp(self):
fbc.api_key = os.environ['TEST_API_KEY']
entity = fbc.Entity.get(entity_id=os.environ['TEST_ENTITY_ID'])
self.first_account = next(entity.get_accounts())
def test_months(self):
self.assertIn("months", self.first_account, "months not present in account")
def test_account_id(self):
self.assertIn("account_id", self.first_account, "account_id not present in account")
def test_account_number(self):
self.assertIn("account_number", self.first_account, "account_number not present in account")
class TestFraudInfo(unittest.TestCase):
"""
Test fraud info when using get_accounts function
"""
def setUp(self):
fbc.api_key = os.environ['TEST_API_KEY']
entity = fbc.Entity.get(entity_id=os.environ['TEST_ENTITY_ID'])
self.first_fraud = next(entity.get_fraud_info())
def test_fraud_type(self):
self.assertIn("fraud_type", self.first_fraud, "months not present in fraud")
def test_statement_id(self):
self.assertIn("statement_id", self.first_fraud, "statement_id not present in fraud")
class TestFetchTransactions(unittest.TestCase):
"""
Test transaction response when using get_transactions function
"""
def setUp(self):
fbc.api_key = os.environ['TEST_API_KEY']
entity = fbc.Entity.get(entity_id=os.environ['TEST_ENTITY_ID'])
self.first_transaction = next(entity.get_transactions())
def test_balance_exists(self):
self.assertIn("balance", self.first_transaction, "balance not present in transaction")
def test_transaction_type_exists(self):
self.assertIn("transaction_type", self.first_transaction, "balance not present in transaction")
def test_date_exists(self):
self.assertIn("date", self.first_transaction, "balance not present in transaction")
class TestFetchRecurring(unittest.TestCase):
"""
Test response when using get_credit_recurring and get_debit_recurring function
"""
def setUp(self):
fbc.api_key = os.environ['TEST_API_KEY']
entity = fbc.Entity.get(entity_id=os.environ['TEST_ENTITY_ID'])
self.first_r_credit = next(entity.get_credit_recurring())
self.first_r_debit = next(entity.get_debit_recurring())
def test_credit_account_id_exists(self):
self.assertIn("account_id", self.first_r_credit, "account_id not present in credit recurring")
def test_credit_transactions(self):
self.assertIn("transactions", self.first_r_credit, "transactions not present in credit recurring")
def test_debit_account_id_exists(self):
self.assertIn("account_id", self.first_r_debit, "account_id not present in debit recurring")
def test_debit_transactions(self):
self.assertIn("transactions", self.first_r_debit, "transactions not present in debit recurring")
class TestFetchSalary(unittest.TestCase):
"""
Test response when using get_salary function
"""
def setUp(self):
fbc.api_key = os.environ['TEST_API_KEY']
entity = fbc.Entity.get(entity_id=os.environ['TEST_ENTITY_ID'])
self.first_salary = next(entity.get_salary())
def test_balance_exists(self):
self.assertIn("balance", self.first_salary, "balance not present in salary")
def test_transaction_type_exists(self):
self.assertIn("transaction_type", self.first_salary, "balance not present in salary")
def test_date_exists(self):
self.assertIn("date", self.first_salary, "balance not present in salary")
class TestFetchLenderTransactions(unittest.TestCase):
"""
Test response when using get_lender_transactions function
"""
def setUp(self):
fbc.api_key = os.environ['TEST_API_KEY']
entity = fbc.Entity.get(entity_id=os.environ['TEST_ENTITY_ID'])
self.first_lender_txn = next(entity.get_lender_transactions())
def test_balance_exists(self):
self.assertIn("balance", self.first_lender_txn, "balance not present in lender transaction")
def test_transaction_type_exists(self):
self.assertIn("transaction_type", self.first_lender_txn, "balance not present in lender transaction")
def test_date_exists(self):
self.assertIn("date", self.first_lender_txn, "balance not present in lender transaction")
class TestAccountEdgeCases(unittest.TestCase):
"""
Test edge cases for invalid account_id
"""
def setUp(self):
fbc.api_key = os.environ['TEST_API_KEY']
self.entity = fbc.Entity.get(entity_id=os.environ['TEST_ENTITY_ID'])
def test_invalid_account_id(self):
array_handled = False
invalid_uuid4_handled = False
try:
next(self.entity.get_transactions(account_id=['hello']))
except ValueError:
array_handled = True
try:
next(self.entity.get_transactions(account_id="somelongstringispresenthere"))
except ValueError:
invalid_uuid4_handled = True
self.assertEqual(array_handled and invalid_uuid4_handled, True,
"array and invalid uuid4 string cases not handled for get_transactions with account_id")
class TestAccountFilteredTransactions(unittest.TestCase):
"""
Test transaction response when using get_transactions function with account_id filter
"""
def setUp(self):
fbc.api_key = os.environ['TEST_API_KEY']
entity = fbc.Entity.get(entity_id=os.environ['TEST_ENTITY_ID'])
self.first_transaction = next(entity.get_transactions(account_id = os.environ['TEST_ACCOUNT_ID']))
def test_balance_exists(self):
self.assertIn("balance", self.first_transaction, "balance not present in transaction")
def test_transaction_type_exists(self):
self.assertIn("transaction_type", self.first_transaction, "balance not present in transaction")
def test_date_exists(self):
self.assertIn("date", self.first_transaction, "balance not present in transaction")
class TestDateRangeEdgeCases(unittest.TestCase):
"""
Test edge cases for invalid date ranges
"""
def setUp(self):
fbc.api_key = os.environ['TEST_API_KEY']
self.entity = fbc.Entity.get(entity_id=os.environ['TEST_ENTITY_ID'])
def test_invalid_daterange(self):
string_handled = False
try:
next(self.entity.get_transactions(from_date="2019-10-04 00:00:00", to_date="2019-10-31 00:00:00"))
except ValueError:
string_handled = True
datetime_handled = False
try:
next(self.entity.get_transactions(from_date=datetime.datetime.today(), to_date=datetime.datetime.now()))
except ValueError:
datetime_handled = True
self.assertEqual(string_handled and datetime_handled, True,
"datetime with/without tzinfo cases not handled for get_transactions with from_date and to_date")
class TestDateRangeFilteredTransactions(unittest.TestCase):
"""
Test transaction response when using get_transactions function with from_date and to_date filter
"""
def setUp(self):
fbc.api_key = os.environ['TEST_API_KEY']
entity = fbc.Entity.get(entity_id=os.environ['TEST_ENTITY_ID'])
from_date = (datetime.datetime.today() - datetime.timedelta(days=1000)).date()
self.first_transaction = next(entity.get_transactions(from_date=from_date, to_date=datetime.datetime.today().date()))
def test_balance_exists(self):
self.assertIn("balance", self.first_transaction, "balance not present in transaction")
def test_transaction_type_exists(self):
self.assertIn("transaction_type", self.first_transaction, "balance not present in transaction")
def test_date_exists(self):
self.assertIn("date", self.first_transaction, "balance not present in transaction")
class TestUploadStatement(unittest.TestCase):
"""
Test uploading of statement pdf with edge cases
"""
def setUp(self):
fbc.api_key = os.environ['TEST_API_KEY']
def test_empty_file_path(self):
none_handled = False
blank_handled = False
entity = fbc.Entity.create()
try:
entity.upload_statement(None, bank_name='axis')
except ValueError:
none_handled = True
except Exception as e:
print(e)
try:
entity.upload_statement('', bank_name='axis')
except ValueError:
blank_handled = True
except Exception as e:
print(e)
self.assertEqual(none_handled and blank_handled, True, "None and/or blank string file path not handled for upload")
def test_non_string_file_path(self):
exception_handled = False
entity = fbc.Entity.create()
try:
entity.upload_statement(['hello.pdf'], bank_name='axis')
except ValueError:
exception_handled = True
except Exception as e:
print(e)
self.assertEqual(exception_handled, True, "Non string file path error not handled")
def test_non_pdf_file_path(self):
exception_handled = False
entity = fbc.Entity.create()
try:
entity.upload_statement('README.md', bank_name='axis')
except ValueError:
exception_handled = True
except Exception as e:
print(e)
self.assertEqual(exception_handled, True, "Non pdf path error not handled")
def test_password_incorrect_bank_name(self):
exception_handled = False
entity = fbc.Entity.create()
try:
entity.upload_statement('samples/test_statement_2.pdf', pdf_password='wrongpass', bank_name='axis')
except PasswordIncorrectError:
exception_handled = True
except Exception as e:
print(e)
self.assertEqual(exception_handled, True, "Password incorrect error not handled")
def test_unparsable_pdf_bank_name(self):
exception_handled = False
entity = fbc.Entity.create()
try:
entity.upload_statement('samples/test_statement_2.pdf', pdf_password='finbox', bank_name='axis')
except UnparsablePDFError:
exception_handled = True
except Exception as e:
print(e)
self.assertEqual(exception_handled, True, "Unparsable PDF error not handled")
def test_cannot_identify_bank(self):
exception_handled = False
entity = fbc.Entity.create()
try:
entity.upload_statement('samples/test_statement_2.pdf', pdf_password='finbox')
except CannotIdentityBankError:
exception_handled = True
except Exception as e:
print(e)
self.assertEqual(exception_handled, True, "Cannot identify bank error not handled")
def test_success_upload_file_bank_name(self):
entity = fbc.Entity.create()
is_authentic = entity.upload_statement('samples/test_statement_1.pdf', pdf_password='finbox', bank_name='axis')
self.assertEqual(is_authentic, False, "Statement file upload and fraud check failed")
def test_success_upload_file(self):
entity = fbc.Entity.create()
is_authentic = entity.upload_statement('samples/test_statement_1.pdf', pdf_password='finbox')
self.assertEqual(is_authentic, False, "Bankless Statement file upload and fraud check failed")
def test_success_upload_file_link_id(self):
entity = fbc.Entity.create(link_id='python_link_id_test_2')
is_authentic = entity.upload_statement('samples/test_statement_1.pdf', pdf_password='finbox', bank_name='axis')
self.assertEqual(is_authentic, False, "Statement file upload against link id and fraud check failed")
if __name__ == '__main__':
unittest.main()