예제 #1
0
 def test_purchase(self):
     account_obj = AccountFactory.create()
     wallet_obj = WalletFactory.create(owner_id=account_obj.id)
     WalletTrxFactory.create(
         wallet=wallet_obj,
         amount=Money(1000, 'SEK'),
         trx_status=TrxStatus.FINALIZED
     )
     product_obj1 = ProductFactory.create(
         code='1337733113370',
         name='Billys Original',
         price=Money(13, 'SEK')
     )
     product_obj2 = ProductFactory.create(
         code='7331733113370',
         name='Kebaba',
         price=Money(30, 'SEK')
     )
     products = [
         (product_obj1.id, 3),
         (product_obj2.id, 1),
     ]
     api.purchase(account_obj.id, products)
     product_obj1.refresh_from_db()
     product_obj2.refresh_from_db()
     self.assertEqual(product_obj1.qty, -3)
     self.assertEqual(product_obj2.qty, -1)
     _, balance = wallet_api.get_balance(account_obj.id)
     self.assertEqual(balance, Money(931, 'SEK'))
예제 #2
0
 def test_purchase(self):
     account_obj = AccountFactory.create()
     wallet_obj = WalletFactory.create(owner_id=account_obj.id)
     WalletTrxFactory.create(
         wallet=wallet_obj,
         amount=Money(1000, 'SEK'),
         trx_status=TrxStatus.FINALIZED
     )
     product_obj1 = ProductFactory.create(
         name='Billys Ooriginal',
         price=Money(13, 'SEK')
     )
     product_obj2 = ProductFactory.create(
         name='Kebabrulle',
         price=Money(30, 'SEK')
     )
     url = reverse('api:purchases-list')
     data = {
         'account_id': account_obj.id,
         'products': [
             {'id': product_obj1.id, 'qty': 1},
             {'id': product_obj2.id, 'qty': 3},
         ]
     }
     response = self.api_client.post(url, data, format='json')
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     _, balance = wallet_api.get_balance(wallet_obj.owner_id, 'SEK')
     self.assertEqual(balance, Money(897, 'SEK'))
예제 #3
0
 def test_purchase(self):
     account_obj = AccountFactory.create()
     wallet_obj = WalletFactory.create(owner_id=account_obj.id)
     WalletTrxFactory.create(wallet=wallet_obj, amount=Money(1000, 'SEK'))
     product_obj1 = ProductFactory.create(name='Billys Ooriginal',
                                          price=Money(13, 'SEK'))
     product_obj2 = ProductFactory.create(name='Kebabrulle',
                                          price=Money(30, 'SEK'))
     url = reverse('api:purchases-list')
     data = {
         'account_id':
         account_obj.id,
         'products': [
             {
                 'id': product_obj1.id,
                 'qty': 1
             },
             {
                 'id': product_obj2.id,
                 'qty': 3
             },
         ]
     }
     response = self.api_client.post(url, data, format='json')
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     self.assertEqual(response.data['amount'], 103)
     _, balance = wallet_api.get_balance(wallet_obj.owner_id, 'SEK')
     self.assertEqual(balance, Money(897, 'SEK'))
예제 #4
0
 def test_purchase(self):
     account_obj = AccountFactory.create()
     wallet_obj = WalletFactory.create(owner_id=account_obj.id)
     WalletTrxFactory.create(
         wallet=wallet_obj,
         amount=Money(1000, 'SEK'),
         trx_type=TrxType.FINALIZED
     )
     product_obj1 = ProductFactory.create(
         code='1337733113370',
         name='Billys Original',
         price=Money(13, 'SEK')
     )
     product_obj2 = ProductFactory.create(
         code='7331733113370',
         name='Kebaba',
         price=Money(30, 'SEK')
     )
     products = [
         (product_obj1.id, 3),
         (product_obj2.id, 1),
     ]
     purchase_obj = api.purchase(account_obj.id, products)
     self.assertEqual(purchase_obj.amount, Money(69, 'SEK'))
     product_obj1.refresh_from_db()
     product_obj2.refresh_from_db()
     self.assertEqual(product_obj1.qty, -3)
     self.assertEqual(product_obj2.qty, -1)
     _, balance = wallet_api.get_balance(account_obj.id)
     self.assertEqual(balance, Money(931, 'SEK'))
     _, balance = wallet_api.get_balance(settings.FOOBAR_MAIN_WALLET)
     self.assertEqual(balance, Money(69, 'SEK'))
예제 #5
0
 def test_cancel_card_purchase(self):
     account_obj = AccountFactory.create()
     wallet_obj = WalletFactory.create(owner_id=account_obj.id)
     WalletTrxFactory.create(wallet=wallet_obj,
                             amount=Money(1000, 'SEK'),
                             trx_status=TrxStatus.FINALIZED)
     product_obj1 = ProductFactory.create(code='1337733113370',
                                          name='Billys Original',
                                          price=Money(13, 'SEK'))
     product_obj2 = ProductFactory.create(code='7331733113370',
                                          name='Kebaba',
                                          price=Money(30, 'SEK'))
     products = [
         (product_obj1.id, 3),
         (product_obj2.id, 1),
     ]
     purchase_obj = api.purchase(account_obj.id, products)
     api.cancel_purchase(purchase_obj.id)
     purchase_obj, _ = api.get_purchase(purchase_obj.id)
     self.assertEqual(purchase_obj.status, enums.PurchaseStatus.CANCELED)
     product_obj1.refresh_from_db()
     product_obj2.refresh_from_db()
     self.assertEqual(product_obj1.qty, 0)
     self.assertEqual(product_obj2.qty, 0)
     _, balance = wallet_api.get_balance(account_obj.id)
     self.assertEqual(balance, Money(1000, 'SEK'))
     _, balance = wallet_api.get_balance(settings.FOOBAR_MAIN_WALLET)
     self.assertEqual(balance, Money(0, 'SEK'))
예제 #6
0
 def test_retrieve_existing(self):
     # retrieve an existing wallet
     wallet_obj = WalletFactory.create()
     WalletTrxFactory.create(wallet=wallet_obj,
                             amount=Money(100),
                             trx_type=TrxType.FINALIZED)
     owner_id = wallet_obj.owner_id
     url = reverse('api:wallets-detail', kwargs={'pk': owner_id})
     response = self.api_client.get(url)
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     self.assertEqual(response.data['balance'], Decimal('100'))
예제 #7
0
 def test_calculation_correctíon(self):
     wallet_obj = WalletFactory.create()
     WalletTrxFactory.create(
         wallet=wallet_obj,
         amount=Money(1000, 'SEK'),
         trx_type=TrxType.FINALIZED
     )
     user_obj = User.objects.create_superuser(
         'the_baconator', '*****@*****.**', '123'
     )
     # Test positive balance change
     correction_obj = api.calculate_correction(
         new_balance=Money(1200, 'SEK'),
         user=user_obj,
         owner_id=wallet_obj.owner_id
     )
     self.assertEqual(correction_obj.wallet.owner_id, wallet_obj.owner_id)
     self.assertEqual(correction_obj.trx_type.value, 0)
     self.assertEqual(correction_obj.pre_balance.amount, 1000)
     self.assertEqual(correction_obj.amount.amount, 200)
     _, balance_correction = wallet_api.get_balance(
         correction_obj.wallet.owner_id
     )
     self.assertEqual(balance_correction.amount, 1200)
     # Test negative balance change
     correction_obj = api.calculate_correction(
         new_balance=Money(1000, 'SEK'),
         user=user_obj,
         owner_id=wallet_obj.owner_id
     )
     self.assertEqual(correction_obj.wallet.owner_id, wallet_obj.owner_id)
     self.assertEqual(correction_obj.trx_type.value, 0)
     self.assertEqual(correction_obj.pre_balance.amount, 1200)
     self.assertEqual(correction_obj.amount.amount, -200)
     _, balance_correction = wallet_api.get_balance(
         correction_obj.wallet.owner_id
     )
     self.assertEqual(balance_correction.amount, 1000)
     # Test when balance is the same = no change
     correction_obj = api.calculate_correction(
         new_balance=Money(1000, 'SEK'),
         user=user_obj,
         owner_id=wallet_obj.owner_id
     )
     self.assertEqual(correction_obj.wallet.owner_id, wallet_obj.owner_id)
     self.assertEqual(correction_obj.trx_type.value, 0)
     self.assertEqual(correction_obj.pre_balance.amount, 1000)
     self.assertEqual(correction_obj.amount.amount, 0)
     _, balance_correction = wallet_api.get_balance(
         correction_obj.wallet.owner_id
     )
     self.assertEqual(balance_correction.amount, 1000)
예제 #8
0
 def test_list(self):
     wallet_obj = WalletFactory.create()
     WalletTrxFactory.create(wallet=wallet_obj,
                             amount=Money(100),
                             trx_type=TrxType.FINALIZED)
     owner_id = wallet_obj.owner_id
     url = reverse('api:wallet_trxs-list')
     response = self.api_client.get(url, data={'owner_id': owner_id})
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     self.assertEqual(len(response.data), 1)
     # no owner_id supplied
     response = self.api_client.get(url)
     self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
예제 #9
0
 def test_retrieve_existing(self):
     # retrieve an existing wallet
     wallet_obj = WalletFactory.create()
     WalletTrxFactory.create(
         wallet=wallet_obj,
         amount=Money(100),
         trx_type=TrxType.INCOMING,
         trx_status=TrxStatus.FINALIZED
     )
     owner_id = wallet_obj.owner_id
     url = reverse('api:wallets-detail', kwargs={'pk': owner_id})
     response = self.api_client.get(url)
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     self.assertEqual(response.data['balance'], Decimal('100'))
예제 #10
0
 def test_list_purchases(self):
     account_obj = AccountFactory.create()
     wallet_obj = WalletFactory.create(owner_id=account_obj.id)
     WalletTrxFactory.create(wallet=wallet_obj,
                             amount=Money(1000, 'SEK'),
                             trx_status=TrxStatus.FINALIZED)
     product_obj1 = ProductFactory.create(code='1337733113370',
                                          name='Billys Original',
                                          price=Money(13, 'SEK'))
     products = [
         (product_obj1.id, 3),
     ]
     api.purchase(account_obj.id, products)
     objs = api.list_purchases(account_obj.id)
     self.assertEqual(len(objs), 1)
예제 #11
0
 def test_list(self):
     wallet_obj = WalletFactory.create()
     WalletTrxFactory.create(
         wallet=wallet_obj,
         amount=Money(100),
         trx_type=TrxType.INCOMING,
         trx_status=TrxStatus.FINALIZED
     )
     owner_id = wallet_obj.owner_id
     url = reverse('api:wallet_trxs-list')
     response = self.api_client.get(url, data={'owner_id': owner_id})
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     self.assertEqual(len(response.data), 1)
     # no owner_id supplied
     response = self.api_client.get(url)
     self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
예제 #12
0
 def test_make_deposit_or_withdrawal(self):
     wallet_obj = WalletFactory.create()
     WalletTrxFactory.create(
         wallet=wallet_obj,
         amount=Money(1000, 'SEK'),
         trx_type=TrxType.FINALIZED
     )
     user_obj = User.objects.create_superuser(
         'the_baconator', '*****@*****.**', '123'
     )
     # Test a deposit
     correction_obj = api.make_deposit_or_withdrawal(
         amount=Money(100, 'SEK'),
         user=user_obj,
         owner_id=wallet_obj.owner_id
      )
     self.assertEqual(correction_obj.wallet.owner_id, wallet_obj.owner_id)
     self.assertEqual(correction_obj.trx_type, enums.TrxType.DEPOSIT)
     self.assertEqual(correction_obj.pre_balance.amount, 1000)
     self.assertEqual(correction_obj.amount.amount, 100)
     _, balance = wallet_api.get_balance(wallet_obj.owner_id)
     self.assertEqual(balance.amount, 1100)
     # Test a withdraw
     correction_obj = api.make_deposit_or_withdrawal(
         amount=Money(-50, 'SEK'),
         user=user_obj, owner_id=wallet_obj.owner_id
     )
     self.assertEqual(correction_obj.wallet.owner_id, wallet_obj.owner_id)
     self.assertEqual(correction_obj.trx_type, enums.TrxType.WITHDRAWAL)
     self.assertEqual(correction_obj.pre_balance.amount, 1100)
     self.assertEqual(correction_obj.amount.amount, -50)
     _, balance = wallet_api.get_balance(wallet_obj.owner_id)
     self.assertEqual(balance.amount, 1050)
     # Test when user tries to deposit or withdraw 0
     correction_obj = api.make_deposit_or_withdrawal(
         amount=Money(0, 'SEK'),
         user=user_obj,
         owner_id=wallet_obj.owner_id
     )
     self.assertEqual(correction_obj.wallet.owner_id, wallet_obj.owner_id)
     self.assertEqual(correction_obj.trx_type, enums.TrxType.CORRECTION)
     self.assertEqual(correction_obj.pre_balance.amount, 1050)
     self.assertEqual(correction_obj.amount.amount, 0)
     _, balance = wallet_api.get_balance(wallet_obj.owner_id)
     self.assertEqual(balance.amount, 1050)
예제 #13
0
 def test_wallet_management(self, mock_deposit_withdrawal, mock_correction):
     wallet_obj = WalletFactory.create()
     WalletTrxFactory.create(wallet=wallet_obj,
                             amount=Money(1200, 'SEK'),
                             trx_type=TrxType.FINALIZED)
     url = reverse('wallet_management',
                   kwargs={'obj_id': wallet_obj.owner_id})
     cl = self.client
     # Test that deposit or withdrawal
     # is not called if balance will get negative
     response = cl.post(
         url, {
             'deposit_or_withdrawal_1': ['SEK'],
             'save_deposit': ['Submit'],
             'comment': ['test'],
             'deposit_or_withdrawal_0': ['-3000']
         })
     mock_deposit_withdrawal.assert_not_called()
     # Test that page can be found
     response = cl.get(url)
     self.assertEqual(response.status_code, 200)
     # Test that correction form post is correct and
     # calls function with correct params
     response = cl.post(
         url, {
             'save_correction': ['Submit'],
             'balance_1': ['SEK'],
             'comment': ['test'],
             'balance_0': ['1000']
         })
     mock_correction.assert_called_with(Money(1000,
                                              'SEK'), wallet_obj.owner_id,
                                        self.user, 'test')
     # Test that deposit or withdrawal form post is correct and
     # calls fucnction with correct params
     response = cl.post(
         url, {
             'deposit_or_withdrawal_1': ['SEK'],
             'save_deposit': ['Submit'],
             'comment': ['test'],
             'deposit_or_withdrawal_0': ['100']
         })
     mock_deposit_withdrawal.assert_called_with(Money(100, 'SEK'),
                                                wallet_obj.owner_id,
                                                self.user, 'test')
예제 #14
0
 def test_get_purchase(self):
     account_obj = AccountFactory.create()
     wallet_obj = WalletFactory.create(owner_id=account_obj.id)
     WalletTrxFactory.create(
         wallet=wallet_obj,
         amount=Money(1000, 'SEK'),
         trx_type=TrxType.FINALIZED
     )
     product_obj1 = ProductFactory.create(
         code='1337733113370',
         name='Billys Original',
         price=Money(13, 'SEK')
     )
     products = [
         (product_obj1.id, 3),
     ]
     purchase_obj = api.purchase(account_obj.id, products)
     obj = api.get_purchase(purchase_obj.id)
     self.assertIsNotNone(obj)
예제 #15
0
 def test_withdraw(self):
     # make the money rain first
     wallet_obj = WalletFactory.create()
     WalletTrxFactory.create(wallet=wallet_obj,
                             amount=Money(10000),
                             trx_type=TrxType.FINALIZED)
     owner_id = wallet_obj.owner_id
     url = reverse('api:wallets-withdraw', kwargs={'pk': owner_id})
     response = self.api_client.post(url,
                                     data={
                                         'owner_id': owner_id,
                                         'amount':
                                         serialize_money(Money(5050)),
                                         'reference': '123'
                                     })
     self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
     url = reverse('api:wallets-detail', kwargs={'pk': owner_id})
     response = self.api_client.get(url)
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     self.assertEqual(response.data['balance'], Decimal('4950'))
     # test with a negative amount
     url = reverse('api:wallets-withdraw', kwargs={'pk': owner_id})
     response = self.api_client.post(url,
                                     data={
                                         'owner_id': owner_id,
                                         'amount':
                                         serialize_money(Money(-10)),
                                         'reference': '321'
                                     })
     self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
     # test withdrawing more money than what is left in the wallet
     url = reverse('api:wallets-withdraw', kwargs={'pk': owner_id})
     response = self.api_client.post(url,
                                     data={
                                         'owner_id': owner_id,
                                         'amount':
                                         serialize_money(Money(10000)),
                                         'reference': '444'
                                     })
     self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
예제 #16
0
 def test_withdraw(self):
     # make the money rain first
     wallet_obj = WalletFactory.create()
     WalletTrxFactory.create(
         wallet=wallet_obj,
         amount=Money(10000),
         trx_type=TrxType.INCOMING,
         trx_status=TrxStatus.FINALIZED
     )
     owner_id = wallet_obj.owner_id
     url = reverse('api:wallets-withdraw', kwargs={'pk': owner_id})
     response = self.api_client.post(url, data={
         'owner_id': owner_id,
         'amount': serialize_money(Money(5050)),
         'reference': '123'
     })
     self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
     url = reverse('api:wallets-detail', kwargs={'pk': owner_id})
     response = self.api_client.get(url)
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     self.assertEqual(response.data['balance'], Decimal('4950'))
     # test with a negative amount
     url = reverse('api:wallets-withdraw', kwargs={'pk': owner_id})
     response = self.api_client.post(url, data={
         'owner_id': owner_id,
         'amount': serialize_money(Money(-10)),
         'reference': '321'
     })
     self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
     # test withdrawing more money than what is left in the wallet
     url = reverse('api:wallets-withdraw', kwargs={'pk': owner_id})
     response = self.api_client.post(url, data={
         'owner_id': owner_id,
         'amount': serialize_money(Money(10000)),
         'reference': '444'
     })
     self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)