feat: improved coverage

This commit is contained in:
2025-01-16 10:34:47 +01:00
parent b86584a702
commit ebb6d5a5a5
10 changed files with 196 additions and 7 deletions
+2
View File
@@ -12,6 +12,7 @@ class CreateProductsMixin:
name="Producto 1",
description="Descripción",
price=Decimal("10.00"),
is_shipping=False,
) -> Product:
tax, created = Tax.objects.get_or_create(
code="IVA",
@@ -21,6 +22,7 @@ class CreateProductsMixin:
sku=sku,
name=name,
description=description,
is_shipping_method=is_shipping,
)
ProductPrice.objects.create(
price=price,
+1
View File
@@ -10,6 +10,7 @@ class RegisterForm(BaseUserCreationForm, SetPasswordMixin):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
for field_name in self.fields.keys():
self.fields[field_name].required = True
self.fields[field_name].widget.attrs.update(
{
"class": "my-2 bg-gray-50 border border-gray-300 text-gray-900 sm:text-sm rounded-lg "
+1 -2
View File
@@ -8,13 +8,12 @@ class CartItemForm(forms.Form):
<label for="quantity">{% translate 'Cantidad' %}</label>
<input id="quantity" type="number" name="quantity" value="1">
"""
product = forms.IntegerField(widget=widgets.HiddenInput)
quantity = forms.IntegerField()
class CreateOrderForm(forms.Form):
email = forms.CharField(max_length=254)
email = forms.CharField(max_length=254, required=True)
shipping_address_full_name = forms.CharField(max_length=128, required=True)
shipping_address = forms.CharField(max_length=128, required=True)
Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB

+15
View File
@@ -96,3 +96,18 @@ class TestLogin(APITestCase):
response = self.client.get(reverse("web:logout"))
assert response.status_code == status.HTTP_302_FOUND
assert response.url == reverse("web:index")
def test_login_then_get_my_account(self):
response = self.client.post(
reverse("web:login"),
{
"username": self.user.username,
"password": self.password,
},
)
assert response.status_code == status.HTTP_302_FOUND
assert response.url == reverse("web:index")
response = self.client.get(reverse("web:my_account"))
assert response.status_code == status.HTTP_200_OK
+13 -2
View File
@@ -1,13 +1,24 @@
import os
from django.test import TestCase
from django.urls import reverse
from django.conf import settings
from web.models import WebSettings
from django.core.files.uploadedfile import SimpleUploadedFile
class TestManifest(TestCase):
def setUp(self):
WebSettings.objects.create()
self.settings = WebSettings.load()
def test_index_view(self):
with open(os.path.join(settings.BASE_DIR, 'web', 'tests', 'images', 'trolley.png'), 'rb') as f:
file_128 = SimpleUploadedFile('logo_128.png', f.read())
self.settings.logo_128 = file_128
file_240 = SimpleUploadedFile('logo_240.png', f.read())
self.settings.logo_240 = file_240
self.settings.logo = file_240
self.settings.save()
def test_manifest_view(self):
response = self.client.get(reverse("web:manifest_json"))
assert response.status_code == 200
+119
View File
@@ -0,0 +1,119 @@
from decimal import Decimal
from django.contrib.auth.models import User
from django.test import TestCase
from django.urls import reverse
from shop.models import Cart, CartItem, Order, CustomerAddress, ShippingMethod, ShopSettings
from shop.tests.mixins import CreateProductsMixin
class TestOrders(TestCase, CreateProductsMixin):
def setUp(self):
self.product = self.create_product()
self.user = User.objects.create_user("anakin", "anakin@skywalker.com", "ihatesand")
self.cart_for_user = self.create_cart(user=self.user)
self.anonymous_cart = self.create_cart()
self.address = CustomerAddress.objects.create(
vat_id="11111111H",
full_name="Vader",
address="Tatooine",
address_town="Mos Eisley",
address_zip="00001",
address_state="Tatooine",
address_country="Mos Eisley",
address_phone="00000000",
address_type=CustomerAddress.Types.BILLING,
email=self.user.email,
default=True,
)
self.shipping_method = self.create_shipping_method()
self.settings = ShopSettings.load()
self.settings.shared_secret = "sq7HjrUOBfKmC576ILgskD5srU870gJ7" # Debug secret
self.settings.save()
def create_cart(self, user=None):
cart = Cart.objects.create(
user=user,
)
CartItem.objects.create(
cart=cart,
product=self.product,
quantity=1,
)
return cart
def create_shipping_method(self):
product = self.create_product(sku="SM1", name="SM1", price=Decimal("1.00"))
method = ShippingMethod.objects.create(name="SM 1", shipping_product=product)
return method
def test_logged_user_orders_page(self):
self.client.force_login(self.user)
response = self.client.get(reverse("web:orders"))
assert response.status_code == 200
def test_logged_user_create_get_order_page(self):
self.client.force_login(self.user)
response = self.client.get(reverse("web:cart_detail"))
assert response.status_code == 200
# Crear pedido
data = {
"email": self.user.email,
"shipping_address_full_name": self.address.full_name,
"shipping_address": self.address.address,
"shipping_address_town": self.address.address_town,
"shipping_address_zip": self.address.address_zip,
"shipping_address_state": self.address.address_state,
"shipping_address_country": self.address.address_country,
"shipping_address_phone": self.address.address_phone,
"same_as_shipping": True,
"billing_address_full_name": self.address.full_name,
"billing_address": self.address.address,
"billing_address_town": self.address.address_town,
"billing_address_zip": self.address.address_zip,
"billing_address_state": self.address.address_state,
"billing_address_country": self.address.address_country,
"billing_address_phone": self.address.address_phone,
"shipping_method": self.shipping_method.pk,
}
response = self.client.post(reverse("web:cart_detail"), data)
assert response.status_code == 302
order = Order.objects.filter(user=self.user).first()
assert order is not None
response = self.client.get(response.url)
assert response.status_code == 200
def test_logged_user_create_get_order_page_invalid_data(self):
self.client.force_login(self.user)
response = self.client.get(reverse("web:cart_detail"))
assert response.status_code == 200
# Sin e-mail
data = {
"shipping_address_full_name": self.address.full_name,
"shipping_address": self.address.address,
"shipping_address_town": self.address.address_town,
"shipping_address_zip": self.address.address_zip,
"shipping_address_state": self.address.address_state,
"shipping_address_country": self.address.address_country,
"shipping_address_phone": self.address.address_phone,
"same_as_shipping": True,
"billing_address_full_name": self.address.full_name,
"billing_address": self.address.address,
"billing_address_town": self.address.address_town,
"billing_address_zip": self.address.address_zip,
"billing_address_state": self.address.address_state,
"billing_address_country": self.address.address_country,
"billing_address_phone": self.address.address_phone,
"shipping_method": self.shipping_method.pk,
}
response = self.client.post(reverse("web:cart_detail"), data)
assert response.status_code == 200
assert not Order.objects.filter(user=self.user).exists()
+33 -3
View File
@@ -1,17 +1,21 @@
from django.contrib.auth import get_user_model
from django.shortcuts import reverse
from django.test import TestCase
from django.test import TransactionTestCase
User = get_user_model()
class TestRegister(TestCase):
class TestRegister(TransactionTestCase):
def setUp(self) -> None:
self.password = "theonering"
self.email = "sauron@mordor.middleearth"
self.first_name = "Sauron"
self.last_name = "The Lord of the Rings"
def test_get_register_page(self):
response = self.client.get(reverse("web:register"))
assert response.status_code == 200
def test_create_user_account(self):
response = self.client.post(
reverse("web:register"),
@@ -84,7 +88,7 @@ class TestRegister(TestCase):
{
"email": self.email,
"password1": self.password,
"password2": "otherpassword",
"password2": self.password,
"first_name": self.first_name,
"last_name": "",
},
@@ -98,3 +102,29 @@ class TestRegister(TestCase):
).exists()
assert response.status_code == 400
def test_create_user_account_already_existing(self):
assert User.objects.all().count() == 0
User.objects.create_user(
email=self.email,
username=self.email,
)
response = self.client.post(
reverse("web:register"),
{
"email": self.email,
"password1": self.password,
"password2": self.password,
"first_name": self.first_name,
"last_name": self.last_name,
},
)
assert not User.objects.filter(
username=self.email,
email=self.email,
first_name=self.first_name,
last_name="",
).exists()
assert response.status_code == 400
+10
View File
@@ -44,6 +44,16 @@ class TestWishlist(TestCase, CreateProductsMixin):
user=self.user, product=self.product
).exists()
def test_not_logged_user_create_wishlisted_product(self):
assert WishlistedProduct.objects.count() == 0
response = self.client.post(
reverse("web:add_to_wishlist"),
{
"product": self.product.pk,
},
)
assert response.status_code == 400
def test_logged_user_delete_wishlisted_product(self):
wishlisted = WishlistedProduct.objects.create(
user=self.user, product=self.product
+2
View File
@@ -209,6 +209,8 @@ class OrdersView(TemplateView, FilteredQuerysetMixin, PaginatedQuerysetMixin):
page = self.get_paginated_queryset(qs)
return {
"title": settings.web_title,
"web_title": settings.web_title,
"page": page,
"has_next_page": page.has_next(),
"has_previous_page": page.has_previous(),