169 lines
6.4 KiB
Python
169 lines
6.4 KiB
Python
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
|