def test_user_cannot_exceed_balance(self): """ Ensures user cannot exceed its balance """ user1 = AccountFactory(balance=50) self.client.force_authenticate(user1) user2 = AccountFactory() transfer = { 'amount': 60, 'account': user1.pk, 'target_account': user2.pk } response = self.client.post('/transfers/', transfer) self.assert400(response)
def test_user_can_buy_something_from_other_user(self): """ Ensures user can buy something from other user """ seller = AccountFactory(is_staff=True, balance=500) product = ProductFactory(name='P_S', value=50, is_approved=True, seller=seller) user = AccountFactory(balance=100) self.client.force_authenticate(user) transfer = {'product': product.pk, 'account': user.pk} response = self.client.post('/transfers/', transfer) self.assert201(response) self.assertFieldEqual(response, 'amount', product.value) self.assertFieldEqual(response, 'target_account', seller.pk)
def test_guest_cannot_transfer_tokens(self): """ Ensures guest cannot transfer tokens to another account """ user = AccountFactory() transfer = {'amount': 50, 'target_account': user.pk} response = self.client.post('/transfers/', transfer) self.assert403(response)
def test_user_can_add_a_product_to_sell(self): """ Ensures user can add a product to sell """ user = AccountFactory() self.client.force_authenticate(user) data = { 'value': 15, 'name': 'UserProduct', } response = self.client.post('/products/', data) self.assertFieldEqual(response, 'seller', user.pk)
def test_user_cannot_retrieve_products_not_approved(self): """ Ensures user cannot retrieve products not approved """ user = AccountFactory() self.client.force_authenticate(user) product = ProductFactory(value=5, is_approved=False) response = self.client.get('/products/') self.assert200(response) response = self.client.get('/products/{0}/'.format(product.pk)) self.assert404(response)
def test_user_can_suggest_a_product(self): """ Ensures user can suggest a product """ user = AccountFactory() self.client.force_authenticate(user) data = { 'value': 5, 'name': 'Product' } response = self.client.post('/products/', data) self.assert201(response) self.assertFieldEqual(response, 'is_approved', False)
def test_user_can_take_a_offer_from_market(self): """ Ensures user can take a offer from market """ system = get_system() product = ProductFactory(name='P_X', value=50, is_approved=True, seller=system, is_reward=True) user = AccountFactory(balance=100) self.client.force_authenticate(user) transfer = {'product': product.pk, 'account': user.pk} response = self.client.post('/transfers/', transfer) self.assert201(response) self.assertFieldEqual(response, 'amount', product.value) self.assertFieldEqual(response, 'target_account', user.pk)