from django.contrib.auth.models import User from django.test import TestCase from django.urls import reverse from shop.models import Cart, CartItem from shop.tests.mixins import CreateProductsMixin from web.settings import ANONYMOUS_CART_ID_COOKIE_NAME class TestCart(TestCase, CreateProductsMixin): def setUp(self): self.product = self.create_product() self.user = User.objects.create_user('vader', 'darth@vader.com', 'ihatesand') def test_anonymous_cart(self): assert Cart.objects.count() == 0 response = self.client.get(reverse('web:cart_dropdown')) assert response.status_code == 200 cart = Cart.objects.all().first() assert cart is not None assert response.cookies.get(ANONYMOUS_CART_ID_COOKIE_NAME).value == str(cart.uuid) def test_logged_user_without_cart(self): assert Cart.objects.count() == 0 self.client.force_login(self.user) response = self.client.get(reverse('web:cart_dropdown')) assert response.status_code == 200 assert Cart.objects.count() == 1 assert not response.cookies.get(ANONYMOUS_CART_ID_COOKIE_NAME) def test_logged_user_with_cart(self): Cart.objects.create(user=self.user) self.client.force_login(self.user) response = self.client.get(reverse('web:cart_dropdown')) assert response.status_code == 200 assert not response.cookies.get(ANONYMOUS_CART_ID_COOKIE_NAME) def test_add_product_cart_item_to_anonymous_user_cart(self): quantity = 2 assert Cart.objects.count() == 0 response = self.client.post(reverse('web:add_cart_item'), {'product': self.product.pk, 'quantity': quantity}) assert response.status_code == 201 cart = Cart.objects.all().first() assert cart is not None assert response.cookies.get(ANONYMOUS_CART_ID_COOKIE_NAME).value == str(cart.uuid) cart_item = CartItem.objects.filter(cart=cart).first() assert cart_item is not None assert cart_item.quantity == quantity def test_add_product_cart_item_to_logged_user_cart(self): quantity = 2 assert Cart.objects.count() == 0 self.client.force_login(self.user) response = self.client.post(reverse('web:add_cart_item'), {'product': self.product.pk, 'quantity': quantity}) assert response.status_code == 201 cart = Cart.objects.all().first() assert cart is not None assert not response.cookies.get(ANONYMOUS_CART_ID_COOKIE_NAME) cart_item = CartItem.objects.filter(cart=cart).first() assert cart_item is not None assert cart_item.quantity == quantity def test_add_product_cart_item_to_logged_user_with_existing_cart(self): quantity = 2 Cart.objects.create(user=self.user) self.client.force_login(self.user) response = self.client.post(reverse('web:add_cart_item'), {'product': self.product.pk, 'quantity': quantity}) assert response.status_code == 201 cart = Cart.objects.all().first() assert cart is not None assert not response.cookies.get(ANONYMOUS_CART_ID_COOKIE_NAME) cart_item = CartItem.objects.filter(cart=cart).first() assert cart_item is not None assert cart_item.quantity == quantity def test_add_same_product_cart_item_multiple_times(self): quantity = 2 Cart.objects.create(user=self.user) self.client.force_login(self.user) response = self.client.post(reverse('web:add_cart_item'), {'product': self.product.pk, 'quantity': quantity}) assert response.status_code == 201 response = self.client.post(reverse('web:add_cart_item'), {'product': self.product.pk, 'quantity': quantity}) assert response.status_code == 201 cart = Cart.objects.all().first() assert cart is not None assert not response.cookies.get(ANONYMOUS_CART_ID_COOKIE_NAME) cart_item = CartItem.objects.filter(cart=cart).first() assert cart_item is not None assert cart_item.quantity == quantity * 2 assert CartItem.objects.filter(cart=cart).count() == 1 def test_create_delete_cart_item_anonymous_cart(self): quantity = 2 assert Cart.objects.count() == 0 response = self.client.post(reverse('web:add_cart_item'), {'product': self.product.pk, 'quantity': quantity}) assert response.status_code == 201 cart = Cart.objects.all().first() assert cart is not None assert response.cookies.get(ANONYMOUS_CART_ID_COOKIE_NAME).value == str(cart.uuid) cart_item = CartItem.objects.filter(cart=cart).first() assert cart_item is not None assert cart_item.quantity == quantity response = self.client.post( reverse('web:delete_cart_item', kwargs={'pk': cart_item.pk}), {'product': self.product.pk, 'quantity': quantity}, ) assert response.status_code == 204 assert CartItem.objects.filter(cart=cart).count() == 0 def test_create_delete_cart_item_logged_user_cart(self): quantity = 2 assert Cart.objects.count() == 0 self.client.force_login(self.user) response = self.client.post(reverse('web:add_cart_item'), {'product': self.product.pk, 'quantity': quantity}) assert response.status_code == 201 cart = Cart.objects.all().first() assert cart is not None assert not response.cookies.get(ANONYMOUS_CART_ID_COOKIE_NAME) cart_item = CartItem.objects.filter(cart=cart).first() assert cart_item is not None assert cart_item.quantity == quantity response = self.client.post( reverse('web:delete_cart_item', kwargs={'pk': cart_item.pk}), {'product': self.product.pk, 'quantity': quantity}, ) assert response.status_code == 204 assert CartItem.objects.filter(cart=cart).count() == 0 def test_anonymous_cart_detail_component(self): assert Cart.objects.count() == 0 response = self.client.get(reverse('web:cart')) assert response.status_code == 200 assert Cart.objects.count() == 1 def test_existing_cart_detail_component(self): cart = Cart.objects.create(user=self.user) CartItem.objects.create(cart=cart, product=self.product, quantity=1) self.client.force_login(self.user) response = self.client.get(reverse('web:cart')) assert response.status_code == 200 def test_add_product_variant_to_cart(self): size_m = self.create_attribute_value('Talla', 'M') variant = self.create_product_variant(self.product, sku='V1', attribute_values=[size_m]) quantity = 2 response = self.client.post( reverse('web:add_cart_item'), {'product': self.product.pk, 'variant': variant.pk, 'quantity': quantity}, ) assert response.status_code == 201 cart_item = CartItem.objects.filter(cart__uuid=response.cookies.get(ANONYMOUS_CART_ID_COOKIE_NAME).value).first() assert cart_item is not None assert cart_item.variant == variant assert cart_item.quantity == quantity def test_add_product_with_variants_without_selecting_one_fails(self): size_m = self.create_attribute_value('Talla', 'M') self.create_product_variant(self.product, sku='V1', attribute_values=[size_m]) response = self.client.post(reverse('web:add_cart_item'), {'product': self.product.pk, 'quantity': 1}) assert response.status_code == 404 assert CartItem.objects.count() == 0 def test_add_same_product_different_variants_creates_separate_lines(self): size_m = self.create_attribute_value('Talla', 'M') size_l = self.create_attribute_value('Talla', 'L') variant_m = self.create_product_variant(self.product, sku='V1', attribute_values=[size_m]) variant_l = self.create_product_variant(self.product, sku='V2', attribute_values=[size_l]) self.client.force_login(self.user) response = self.client.post( reverse('web:add_cart_item'), {'product': self.product.pk, 'variant': variant_m.pk, 'quantity': 1} ) assert response.status_code == 201 response = self.client.post( reverse('web:add_cart_item'), {'product': self.product.pk, 'variant': variant_l.pk, 'quantity': 1} ) assert response.status_code == 201 cart = Cart.objects.get(user=self.user) assert CartItem.objects.filter(cart=cart).count() == 2 def test_add_same_product_and_variant_multiple_times_sums_quantity(self): size_m = self.create_attribute_value('Talla', 'M') variant = self.create_product_variant(self.product, sku='V1', attribute_values=[size_m]) self.client.force_login(self.user) self.client.post( reverse('web:add_cart_item'), {'product': self.product.pk, 'variant': variant.pk, 'quantity': 1} ) self.client.post( reverse('web:add_cart_item'), {'product': self.product.pk, 'variant': variant.pk, 'quantity': 1} ) cart = Cart.objects.get(user=self.user) assert CartItem.objects.filter(cart=cart).count() == 1 assert CartItem.objects.get(cart=cart).quantity == 2