Exemplo n.º 1
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'))
Exemplo n.º 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'))
     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'))
Exemplo n.º 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'),
         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'))
Exemplo n.º 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_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'))
Exemplo n.º 5
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'))
Exemplo n.º 6
0
 def test_cash_purchase(self):
     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':
         None,
         '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(settings.FOOBAR_CASH_WALLET, 'SEK')
     self.assertEqual(balance, Money(103, 'SEK'))
Exemplo n.º 7
0
 def test_cancel_cash_purchase(self):
     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(None, 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(settings.FOOBAR_CASH_WALLET)
     self.assertEqual(balance, Money(0, 'SEK'))
Exemplo n.º 8
0
 def test_purchase_invalid_quantity(self):
     account_obj = AccountFactory.create()
     product_obj1 = ProductFactory.create()
     product_obj2 = ProductFactory.create()
     url = reverse('api:purchases-list')
     data = {
         'account_id': account_obj.id,
         'products': [
             {'id': product_obj1.id, 'qty': -5},
             {'id': product_obj2.id, 'qty': 10},
         ]
     }
     response = self.api_client.post(url, data, format='json')
     self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
Exemplo n.º 9
0
    def test_list_products(self):
        for code in range(9):
            ProductFactory.create()
        fixed_code = '1{0:012d}'.format(7331)
        ProductFactory.create(code=fixed_code)
        url = reverse('api:products-list')
        response = self.api_client.get(url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(len(response.data), 10)

        # retrieve a single product by its code
        url = reverse('api:products-list')
        response = self.api_client.get(url, {'code': fixed_code})
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(len(response.data), 1)
Exemplo n.º 10
0
 def test_purchase_insufficient_funds(self):
     account_obj = AccountFactory.create()
     product_obj1 = ProductFactory.create(
         price=Money(13, 'SEK')
     )
     product_obj2 = ProductFactory.create(
         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_400_BAD_REQUEST)
Exemplo n.º 11
0
 def test_cash_purchase(self):
     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(None, 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(settings.FOOBAR_CASH_WALLET)
     self.assertEqual(balance, Money(69, 'SEK'))
Exemplo n.º 12
0
 def test_purchase_insufficient_funds(self):
     account_obj = AccountFactory.create()
     product_obj1 = ProductFactory.create(price=Money(13, 'SEK'))
     product_obj2 = ProductFactory.create(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_400_BAD_REQUEST)
Exemplo n.º 13
0
 def test_purchase_invalid_quantity(self):
     account_obj = AccountFactory.create()
     product_obj1 = ProductFactory.create()
     product_obj2 = ProductFactory.create()
     url = reverse('api:purchases-list')
     data = {
         'account_id':
         account_obj.id,
         'products': [
             {
                 'id': product_obj1.id,
                 'qty': -5
             },
             {
                 'id': product_obj2.id,
                 'qty': 10
             },
         ]
     }
     response = self.api_client.post(url, data, format='json')
     self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
Exemplo n.º 14
0
    def handle(self, *args, **options):
        # Create the default User
        if User.objects.count() == 0:
            user = User.objects.create_user(username='******',
                                            email='*****@*****.**',
                                            password='******')
            user.save()
        else:
            user = User.objects.get(id=1)

        if Token.objects.count() == 0:
            # Generate the token for the created user
            Token.objects.create(user=user)

        # Change the Token to a known one
        Token.objects.filter(user_id=user.id).update(
            key='b60868c38b813ea43b36036503e3f5de025dde31')

        if Product.objects.count() == 0:
            # Create a Widget and a Gizmo products on DB
            ProductFactory.create(name='Widget', collect_stamp=True)
            ProductFactory.create(name='Gizmo', collect_stamp=False)
Exemplo n.º 15
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)
Exemplo n.º 16
0
 def test_retrieve_existing(self):
     # retrieve an existing product
     product_obj = ProductFactory.create()
     ProductTrxFactory.create(
         product=product_obj,
         qty=10,
         trx_type=TrxType.INVENTORY
     )
     url = reverse('api:products-detail', kwargs={'pk': product_obj.id})
     response = self.api_client.get(url)
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     self.assertEqual(response.data['qty'], 10)
     # retrieve a non-existent product
     url = reverse('api:products-detail', kwargs={'pk': uuid.uuid4()})
     response = self.api_client.get(url)
     self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
Exemplo n.º 17
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)