class BasketProductsAPIViewDeleteTest(BasketTestMixin, APITestCase): def setUp(self): self.basket = BasketFactory(updated=False) self.product = ProductFactory() self.basket_product = BasketProductRelationFactory(basket=self.basket, product=self.product, quantity=3) self.set_up_basket_session(str(self.basket.uuid)) def test_delete_existing_product_from_basket(self): response = self.client.delete( reverse('baskets:basket-products', kwargs=dict(product_id=str(self.product.uuid))), ) self.assertEqual(response.status_code, 204) with self.assertRaises(BasketProductRelation.DoesNotExist): self.basket_product.refresh_from_db() self.basket.refresh_from_db() self.assertTrue(self.basket.updated) def test_delete_unknown_product_from_basket(self): response = self.client.delete(reverse('baskets:basket-products', kwargs=dict(product_id=str(uuid.uuid4()))),) self.assertEqual(response.status_code, 404) self.basket_product.refresh_from_db()
def test_delete_product_from_basket(self): basket = BasketFactory(updated=False) BasketProductRelationFactory.create_batch(2, basket=basket) self.assertEqual(basket.products.count(), 2) self.set_up_basket_session(str(basket.uuid)) response = self.client.delete(reverse('baskets:basket')) self.assertEqual(response.status_code, 204) basket.refresh_from_db() self.assertTrue(basket.updated) self.assertEqual(basket.products.count(), 0)
def test_send_email(self): BasketFactory(updated=False) basket = BasketFactory(updated=True) UserFactory(is_admin=False, is_active=False) UserFactory(is_admin=True, is_active=False) admin = UserFactory(is_admin=True, is_active=True) call_command('cart_changes') self.assertEqual(len(mail.outbox), 1) changes_mail = mail.outbox[0] self.assertEqual(changes_mail.subject, 'Changed baskets') self.assertIn(str(basket.uuid), changes_mail.body) self.assertEqual(changes_mail.to, [admin.email]) basket.refresh_from_db() self.assertFalse(basket.updated)
class BasketProductsAPIViewPutTest(BasketTestMixin, APITestCase): maxDiff = None def setUp(self): self.basket = BasketFactory(updated=False) self.product = ProductFactory() self.context = dict(request=MediaHttpRequest()) self.set_up_basket_session(str(self.basket.uuid)) def test_add_new_product_to_basket(self): data = {'quantity': 5} response = self.client.put( reverse('baskets:basket-products', kwargs=dict(product_id=self.product.uuid)), data=data ) self.assertEqual(response.status_code, 200) basket_product = BasketProductRelation.objects.get(product=self.product, basket=self.basket) self.assertEqual(basket_product.quantity, 5) self.assertDictEqual(response.json(), BasketProductSerializer(basket_product, context=self.context).data) self.basket.refresh_from_db() self.assertTrue(self.basket.updated) def test_update_product_quantity(self): basket_product = BasketProductRelationFactory(product=self.product, basket=self.basket, quantity=3) data = {'quantity': 2} response = self.client.put( reverse('baskets:basket-products', kwargs=dict(product_id=self.product.uuid)), data=data ) self.assertEqual(response.status_code, 200) basket_product.refresh_from_db() self.assertEqual(basket_product.quantity, 2) self.assertDictEqual(response.json(), BasketProductSerializer(basket_product, context=self.context).data) self.basket.refresh_from_db() self.assertTrue(self.basket.updated) def test_add_unknown_product(self): data = {'quantity': 2} response = self.client.put( reverse('baskets:basket-products', kwargs=dict(product_id=str(uuid.uuid4()))), data=data ) self.assertEqual(response.status_code, 404)
def test_share_basket(self): basket = BasketFactory() BasketProductRelationFactory.create_batch(2, basket=basket) self.set_up_basket_session(str(basket.uuid)) data = {'email': '*****@*****.**'} response = self.client.post(reverse('baskets:basket-share'), data=data) self.assertEqual(response.status_code, 204) self.assertEqual(len(mail.outbox), 1)
def test_preserve_basket_during_logout(self): self.client.force_login(self.user) basket = BasketFactory() self.set_up_basket_session(str(basket.uuid)) response = self.client.post(reverse('users:logout')) self.assertEqual(response.status_code, 204) self.assertEqual(self.client.session[settings.BASKET_SESSION_ID], str(basket.uuid))
def test_preserve_basket_during_login(self): basket = BasketFactory() self.set_up_basket_session(str(basket.uuid)) data = {'email': self.user.email, 'password': USER_PASSWORD} response = self.client.post(reverse('users:login'), data=data) self.assertEqual(response.status_code, 200) self.assertEqual(self.client.session[settings.BASKET_SESSION_ID], str(basket.uuid))
def test_get_existing_basket(self): basket = BasketFactory() BasketProductRelationFactory.create_batch(2, basket=basket) self.assertEqual(Basket.objects.count(), 1) self.set_up_basket_session(str(basket.uuid)) response = self.client.get(reverse('baskets:basket')) self.assertEqual(response.status_code, 200) self.assertListEqual( response.json(), BasketProductSerializer( BasketProductRelation.objects.filter(basket=basket), many=True, context=self.context ).data, ) self.assertEqual(Basket.objects.count(), 1)
def setUp(self): self.basket = BasketFactory()
def test_no_changes(self): BasketFactory(updated=False) call_command('cart_changes') self.assertEqual(len(mail.outbox), 0)
def setUp(self): self.basket = BasketFactory(updated=False) self.product = ProductFactory() self.context = dict(request=MediaHttpRequest()) self.set_up_basket_session(str(self.basket.uuid))
def setUp(self): self.basket = BasketFactory(updated=False) self.product = ProductFactory() self.basket_product = BasketProductRelationFactory(basket=self.basket, product=self.product, quantity=3) self.set_up_basket_session(str(self.basket.uuid))