def test04(self, dwolla): """ Test initiate transfer. """ funding_source = dwolla['funding_source'] e = Emulator(num_debit_accounts=1) e.init() e.make_transfer_prepare_condition() Emulator.run_transfer_prepare() Emulator.run_transfer_donkies_prepare() # Exchange dwolla_id for mock debit account with # real funding source dwolla_id account = e.debit_accounts[0] fs = account.funding_source fs.dwolla_id = funding_source['id'] fs.save() tds = TransferDonkies.objects.first() tds.save() TransferDonkies.objects.initiate_dwolla_transfer(tds.id) tds.refresh_from_db() assert tds.is_initiated is True assert tds.initiated_at is not None assert tds.updated_at is not None assert tds.dwolla_id is not None
def test_02(self): """ The amount (total roundup) in TransferDonkies row should be equal to total sum in TransferPrepare rows. Test for 2 users. """ e1 = Emulator() e1.init() e2 = Emulator() e2.init() Emulator.run_transfer_prepare() sum1 = TransferPrepare.objects.filter( is_processed=False, account__item__user=e1.user)\ .aggregate(Sum('roundup'))['roundup__sum'] sum2 = TransferPrepare.objects.filter( is_processed=False, account__item__user=e2.user)\ .aggregate(Sum('roundup'))['roundup__sum'] Emulator.run_transfer_donkies_prepare() assert TransferDonkies.objects.count() == 2 qs = TransferDonkies.objects.filter(account__item__user=e1.user) assert qs.first().amount == sum1 qs = TransferDonkies.objects.filter(account__item__user=e2.user) assert qs.first().amount == sum2
def test_04(self): """ Test moving TransferDonkies item to TransferDonkiesFailed. TransferDonkiesFailed should have the copy of the item. Original item should be deleted from TransferDonkies. """ e = Emulator(num_debit_accounts=1) e.init() e.make_transfer_prepare_condition() Emulator.run_transfer_prepare() Emulator.run_transfer_donkies_prepare() tds = TransferDonkies.objects.first() tds.failure_code = 'R99' tds.is_failed = True tds.save() TransferDonkies.objects.move_failed(tds.id) tdf = TransferDonkiesFailed.objects.first() assert tds.dwolla_id == tdf.dwolla_id assert tds.amount == tdf.amount assert tds.status == tdf.status assert tds.created_at == tdf.created_at assert tds.initiated_at == tdf.initiated_at assert tds.updated_at == tdf.updated_at assert tds.failure_code == tdf.failure_code assert tds.is_initiated == tdf.is_initiated assert tds.is_failed == tdf.is_failed assert tds.account == tdf.account assert TransferDonkies.objects.filter(id=tds.id).exists() is False
def test_process09(self): """ Test 2 debts accounts with share: 100% and 0% TransferUser model should get only 1 item instead of 2. """ e = Emulator() e.init() e.create_dwolla_transfers(60) a = e.debt_accounts[0] a.transfer_share = 100 a.save() a = e.debt_accounts[1] a.transfer_share = 0 a.save() TransferUser.objects.process() tu = TransferUser.objects.first() qs = TransferDebt.objects.filter(tu=tu) assert qs.count() == 1 sum = qs.aggregate(Sum('amount'))['amount__sum'] assert tu.amount == sum
def test_process07(self): """ Test 3 debt accounts with share: 33%, 33% and 34% """ e = Emulator(num_debt_accounts=3) e.init() e.create_dwolla_transfers(60) a = e.debt_accounts[0] a.transfer_share = 33 a.save() a = e.debt_accounts[1] a.transfer_share = 33 a.save() a = e.debt_accounts[2] a.transfer_share = 34 a.save() assert TransferUser.objects.count() == 0 TransferUser.objects.process() assert TransferUser.objects.count() > 0 tu = TransferUser.objects.first() qs = TransferDebt.objects.filter(tu=tu) assert qs.count() == 3 sum = qs.aggregate(Sum('amount'))['amount__sum'] assert tu.amount == sum
def test01(self): """ If user didn't set funding source, do not process prepare. """ e = Emulator() e.init() e.clear_funding_source() Emulator.run_transfer_prepare() assert TransferPrepare.objects.count() == 0
def test_process04(self): """ Should process. """ e = Emulator() e.init() e.create_dwolla_transfers(60) TransferUser.objects.process() assert TransferUser.objects.count() > 0
def test_process01(self): """ User doesn't have debt accounts. Should not process. """ e = Emulator(num_debt_accounts=0) e.init() e.create_dwolla_transfers(60) TransferUser.objects.process() assert TransferUser.objects.count() == 0
def test02(self): """ Test success transfer. Collected roundup should be more than settings.TRANSFER_TO_DONKIES_MIN_AMOUNT """ e = Emulator() e.init() e.make_transfer_prepare_condition() Emulator.run_transfer_prepare() assert len(e.debit_accounts) == TransferPrepare.objects.count()
def test_get_transfer_donkies(self, client): e = Emulator() e.init() e.run_transfer_prepare() e.run_transfer_donkies_prepare() e.emulate_dwolla_transfers() client = self.get_auth_client(e.user) url = '/v1/transfers_donkies' response = client.get(url) assert response.status_code == 200
def test06(self, dwolla): """ Should be last test as it changes fixture's funding_source to R01. Test initiate and update transfer with insufficient funds. 1) Should get failed status. 2) Update failure_code. 3) Failure code should be "R01" """ customer = dwolla['customer'] funding_source = get_or_create_funding_source(customer, name='R01') e = Emulator(num_debit_accounts=1) e.init() e.make_transfer_prepare_condition() Emulator.run_transfer_prepare() Emulator.run_transfer_donkies_prepare() # Exchange dwolla_id for mock debit account with # real funding source dwolla_id from API account = e.debit_accounts[0] fs = account.funding_source fs.dwolla_id = funding_source['id'] fs.save() tds = TransferDonkies.objects.first() tds.save() TransferDonkies.objects.initiate_dwolla_transfer(tds.id) for _ in range(20): TransferDonkies.objects.update_dwolla_transfer( tds.id, is_test=True) tds.refresh_from_db() print('Transfer status: ', tds.status) # Process sandbox transfers self.dw.press_sandbox_button() if tds.status and tds.status != TransferDonkies.PENDING: break time.sleep(5) assert tds.status == TransferDonkies.FAILED assert tds.is_failed is True assert tds.failure_code is None TransferDonkies.objects.update_dwolla_failure_code(tds.id) tds.refresh_from_db() assert tds.failure_code == 'R01'
def test03(self): """ Test case where collected roundup is less than settings.TRANSFER_TO_DONKIES_MIN_AMOUNT Should not send transfers to Donkies. """ e = Emulator() e.init() sum = e.user.get_not_processed_roundup_sum() settings.TRANSFER_TO_DONKIES_MIN_AMOUNT = sum + 1 Emulator.run_transfer_prepare() assert TransferPrepare.objects.count() == 0
def test_process10(self): """ All TransferDonkies that have been processed should be marked as is_processed_to_user. """ e = Emulator(num_debt_accounts=1) e.init() e.create_dwolla_transfers(60) TransferUser.objects.process() tu = TransferUser.objects.first() assert tu.items.filter(is_processed_to_user=False).count() == 0 assert tu.items.filter(is_processed_to_user=True).count() > 0
def test_process02(self): """ User is_auto_transfer set to False. Should not process. """ e = Emulator() e.init() e.create_dwolla_transfers(60) e.user.is_auto_transfer = False e.user.save() TransferUser.objects.process() assert TransferUser.objects.count() == 0
def test_process03(self): """ User's minimum amount for transfer is more than collected amount. Should not process. """ e = Emulator() e.init() e.create_dwolla_transfers(60) e.user.minimum_transfer_amount = 1000000 e.user.save() TransferUser.objects.process() assert TransferUser.objects.count() == 0
def test_03(self): """ After processing transfers from TransferPrepare to TransferFonkies, Transfer prepare shouldn't have is_processed=False, everything should be processed. """ e = Emulator() e.init() e.make_transfer_prepare_condition() Emulator.run_transfer_prepare() qs = TransferPrepare.objects.filter(is_processed=False) assert qs.count() > 0 Emulator.run_transfer_donkies_prepare() qs = TransferPrepare.objects.filter(is_processed=False) assert qs.count() == 0
def test_01(self): """ After prepare transfer from TransferPrepare to TransferDonkies, TransferDonkies should get 1 row for 1 user. The account is user's funding source. """ # User 1 e1 = Emulator() e1.init() # User 2 e2 = Emulator() e2.init() Emulator.run_transfer_prepare() Emulator.run_transfer_donkies_prepare() assert TransferDonkies.objects.count() == 2
def test04(self): """ Test that amounts are equal. """ e = Emulator() e.init() total_should_be = e.get_total_roundup(e.transactions) sum = e.user.get_not_processed_roundup_sum() assert sum == total_should_be e.make_transfer_prepare_condition() e.run_transfer_prepare() sum = TransferPrepare.objects.aggregate(Sum('roundup'))['roundup__sum'] assert total_should_be == sum qs = Transaction.objects.active().filter(is_processed=False) assert qs.count() == 0
def test_process05(self): """ Test running "process" manager's method before 15th of the month. It should process all TransferDonkies, from month before previous. """ e = Emulator() e.init() e.create_dwolla_transfers(60) dt = timezone.now().replace(day=14) dt_str = dt.strftime('%Y-%m-%d %H:%M:%S') with freeze_time(dt_str): dt = self.get_first_day_of_previous_month() TransferUser.objects.process() qs1 = TransferDonkies.objects.filter(is_processed_to_user=True) qs2 = TransferDonkies.objects.filter(sent_at__lt=dt) assert qs1.count() == qs2.count() assert TransferUser.objects.count() == 1
def test05(self, dwolla): """ Test initiate and update transfer with success status. """ funding_source = dwolla['funding_source'] e = Emulator(num_debit_accounts=1) e.init() e.make_transfer_prepare_condition() Emulator.run_transfer_prepare() Emulator.run_transfer_donkies_prepare() # Exchange dwolla_id for mock debit account with # real funding source dwolla_id from API account = e.debit_accounts[0] fs = account.funding_source fs.dwolla_id = funding_source['id'] fs.save() tds = TransferDonkies.objects.first() tds.save() TransferDonkies.objects.initiate_dwolla_transfer(tds.id) for _ in range(20): TransferDonkies.objects.update_dwolla_transfer( tds.id, is_test=True) tds.refresh_from_db() print('Transfer status: ', tds.status) # Process sandbox transfers self.dw.press_sandbox_button() if tds.status and tds.status != TransferDonkies.PENDING: break time.sleep(5) assert tds.status == TransferDonkies.PROCESSED assert tds.is_sent is True assert tds.sent_at is not None assert tds.updated_at is not None
def test_process08(self): """ Test 2 debt accounts with share 99% and 1% """ e = Emulator(num_debt_accounts=2) e.init() e.create_dwolla_transfers(60) a = e.debt_accounts[0] a.transfer_share = 99 a.save() a = e.debt_accounts[1] a.transfer_share = 1 a.save() TransferUser.objects.process() tu = TransferUser.objects.first() qs = TransferDebt.objects.filter(tu=tu) assert qs.count() == 2 sum = qs.aggregate(Sum('amount'))['amount__sum'] assert tu.amount == sum
def setup(self): e = Emulator() e.init() e.run_transfer_prepare() e.run_transfer_donkies_prepare() self.user = e.user