fix: improved coverage

This commit is contained in:
Pablo Moreno
2024-11-28 00:55:06 +01:00
parent 30e4a57177
commit f70aeac71f
3 changed files with 182 additions and 10 deletions
+181
View File
@@ -0,0 +1,181 @@
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
-9
View File
@@ -2,15 +2,6 @@ from shop.models import Cart
from web.settings import ANONYMOUS_CART_ID_COOKIE_NAME from web.settings import ANONYMOUS_CART_ID_COOKIE_NAME
def get_cart(request):
if not request.user.is_authenticated:
uid = request.COOKIES.get(ANONYMOUS_CART_ID_COOKIE_NAME)
cart = Cart.objects.get(uuid=uid)
else:
cart = Cart.objects.filter(user=request.user).first()
return cart
def get_or_create_cart(request): def get_or_create_cart(request):
created = False created = False
if not request.user.is_authenticated: if not request.user.is_authenticated:
+1 -1
View File
@@ -4,7 +4,7 @@ from django.http.response import HttpResponse
from django.views.decorators.http import require_http_methods from django.views.decorators.http import require_http_methods
from shop.models import Product, Cart, CartItem from shop.models import Product, Cart, CartItem
from web.utils import get_cart, get_or_create_cart from web.utils import get_or_create_cart
from web.settings import ANONYMOUS_CART_ID_COOKIE_NAME from web.settings import ANONYMOUS_CART_ID_COOKIE_NAME