Files
homm/backend/apps/storefront/tests.py
T
apuc 37d5d7b366 Add the customer storefront on Django templates
Pages are rendered server-side (SEO, no build step), the interactive
parts talk to the existing REST API from the browser:

- home: banners, categories, hits and new arrivals
- catalog: filters (category, brand, price, stock), search, sorting,
  pagination, filter chips with one-click removal
- product card: gallery, price with discount, stock, characteristics,
  related products
- cart: localStorage, quantity controls, live totals
- checkout: contacts, delivery, promo code, bonuses, order submission
  with Idempotency-Key, success screen
- login: OTP by phone (debug code shown in dev) and phone/password
- account: profile, orders, addresses, bonuses through the API with JWT
- static pages from the admin

Supporting pieces:
- apps/storefront app with views, urls, templates and static CSS/JS
- money/discount/attribute template filters
- seed_demo management command with demo catalog, banners and pages
- Cyrillic search fallback for SQLite: local DB does not fold case for
  non-ASCII, production PostgreSQL does

Tests: 16 storefront cases (pages, filters, search, ordering, 404s,
filters formatting); the suite is 61 passing.
2026-11-22 22:38:39 +03:00

139 lines
5.6 KiB
Python

from decimal import Decimal
from django.test import TestCase
from apps.catalog.models import Category, Product
from apps.content.models import StaticPage
from .templatetags.storefront import attr_value, discount_percent, money
class StorefrontHelperTests(TestCase):
def test_money_formats_roubles_with_spaces(self):
self.assertEqual(money(Decimal("12990.00")), "12 990")
self.assertEqual(money("490"), "490")
self.assertEqual(money(None), "—")
def test_discount_percent(self):
self.assertEqual(discount_percent("12990", "14990"), 13)
self.assertEqual(discount_percent("12990", None), 0)
self.assertEqual(discount_percent("12990", "10000"), 0)
def test_attr_value_renders_lists(self):
self.assertEqual(attr_value(["S", "M"]), "S, M")
self.assertEqual(attr_value(True), "да")
self.assertEqual(attr_value("Чёрный"), "Чёрный")
class StorefrontPagesTests(TestCase):
@classmethod
def setUpTestData(cls):
cls.outer = Category.objects.create(
external_id="t-outer", name="Верхняя одежда", slug="verhnyaya-odezhda"
)
cls.knit = Category.objects.create(
external_id="t-knit", name="Трикотаж", slug="trikotazh"
)
cls.coat = Product.objects.create(
external_id="t-coat",
sku="T-1",
name="Куртка тестовая",
slug="kurtka-testovaya",
category=cls.outer,
price=Decimal("12000.00"),
old_price=Decimal("15000.00"),
stock=5,
is_hit=True,
attributes={"Цвет": "Чёрный"},
)
cls.tshirt = Product.objects.create(
external_id="t-tshirt",
sku="T-2",
name="Футболка тестовая",
slug="futbolka-testovaya",
category=cls.knit,
brand="HOMM",
price=Decimal("2500.00"),
stock=0,
is_new=True,
)
cls.page = StaticPage.objects.create(
slug="dostavka", title="Доставка", content="Курьером", is_published=True
)
def test_home_shows_hits_and_new(self):
response = self.client.get("/")
self.assertEqual(response.status_code, 200)
self.assertContains(response, "Куртка тестовая")
self.assertContains(response, "Футболка тестовая")
def test_catalog_lists_only_active_products(self):
hidden = Product.objects.create(
external_id="t-hidden",
sku="T-3",
name="Скрытый товар",
slug="skrytyy-tovar",
price=Decimal("100.00"),
is_active=False,
)
response = self.client.get("/catalog/")
self.assertEqual(response.status_code, 200)
self.assertContains(response, "Куртка тестовая")
self.assertNotContains(response, hidden.name)
def test_catalog_filters_by_category(self):
response = self.client.get("/catalog/?category=trikotazh")
self.assertContains(response, "Футболка тестовая")
self.assertNotContains(response, "Куртка тестовая")
def test_catalog_filters_by_price_and_stock(self):
response = self.client.get("/catalog/?price_max=5000&in_stock=1")
self.assertNotContains(response, "Куртка тестовая")
self.assertNotContains(response, "Футболка тестовая")
response = self.client.get("/catalog/?price_min=10000")
self.assertContains(response, "Куртка тестовая")
def test_catalog_search(self):
response = self.client.get("/catalog/", {"search": "куртка"})
self.assertContains(response, "Куртка тестовая")
self.assertNotContains(response, "Футболка тестовая")
def test_catalog_shows_filter_chips(self):
response = self.client.get("/catalog/?brand=HOMM&in_stock=1")
self.assertContains(response, "Бренд: HOMM")
self.assertContains(response, "Только в наличии")
def test_catalog_ordering(self):
response = self.client.get("/catalog/?ordering=price")
content = response.content.decode()
self.assertLess(content.index("Футболка тестовая"), content.index("Куртка тестовая"))
def test_product_page(self):
response = self.client.get("/catalog/kurtka-testovaya/")
self.assertEqual(response.status_code, 200)
self.assertContains(response, "Куртка тестовая")
self.assertContains(response, "Цвет")
self.assertContains(response, "Чёрный")
def test_product_page_404(self):
self.assertEqual(self.client.get("/catalog/no-such-product/").status_code, 404)
def test_cart_checkout_login_account_pages(self):
for url in ("/cart/", "/checkout/", "/login/", "/account/"):
with self.subTest(url=url):
self.assertEqual(self.client.get(url).status_code, 200)
def test_static_page(self):
response = self.client.get("/pages/dostavka/")
self.assertEqual(response.status_code, 200)
self.assertContains(response, "Курьером")
def test_static_page_404(self):
self.assertEqual(self.client.get("/pages/no-such-page/").status_code, 404)
def test_api_still_works(self):
response = self.client.get("/api/v1/catalog/products/")
self.assertEqual(response.status_code, 200)
self.assertGreaterEqual(response.json()["count"], 2)