Backend (Django 4.2 + DRF): - accounts: phone OTP login, JWT, profile, addresses - catalog: categories, products, images, filters, search, hits/new/related - orders: creation with Idempotency-Key, stock reservation, status history, cancel - loyalty: bonus accounts, promo codes, gift certificates - payments: payment records and YooKassa webhook handling - content: static pages, banners, site settings - integration: 1C exchange (catalog sync, order push via outbox, statuses, bonuses) - common: error envelope, pagination, request-id, integration auth, admin dashboard Infrastructure: Dockerfile, docker-compose (Postgres, Redis, Celery worker/beat, Nginx), requirements split by environment, OpenAPI contracts for site and 1C, architecture doc. Tests: 22 pytest cases covering catalog, orders, idempotency, OTP, integration.
77 lines
2.6 KiB
Python
77 lines
2.6 KiB
Python
from decimal import Decimal
|
|
|
|
from django.test import TestCase
|
|
from rest_framework.test import APIClient
|
|
|
|
from apps.catalog.models import Category, Product
|
|
|
|
from .models import Order
|
|
|
|
|
|
class OrderApiTests(TestCase):
|
|
def setUp(self):
|
|
self.client = APIClient()
|
|
category = Category.objects.create(
|
|
external_id="cat-1", name="Диваны", slug="divany"
|
|
)
|
|
self.product = Product.objects.create(
|
|
external_id="prod-1",
|
|
sku="SKU-1",
|
|
name="Диван Осло",
|
|
slug="divan-oslo",
|
|
category=category,
|
|
price=Decimal("49990.00"),
|
|
stock=5,
|
|
stock_status=Product.StockStatus.IN_STOCK,
|
|
)
|
|
self.payload = {
|
|
"items": [{"product_id": str(self.product.id), "quantity": 2}],
|
|
"customer_name": "Иван Петров",
|
|
"phone": "+79991234567",
|
|
"email": "ivan@example.com",
|
|
"delivery_method": "courier",
|
|
"delivery_address": "Москва, ул. Тверская, 1",
|
|
"delivery_cost": "500.00",
|
|
}
|
|
|
|
def test_create_order_reserves_stock(self):
|
|
response = self.client.post(
|
|
"/api/v1/orders/",
|
|
self.payload,
|
|
format="json",
|
|
HTTP_IDEMPOTENCY_KEY="order-test-1",
|
|
)
|
|
self.assertEqual(response.status_code, 201, response.data)
|
|
self.assertEqual(Decimal(response.data["total"]), Decimal("100480.00"))
|
|
|
|
self.product.refresh_from_db()
|
|
self.assertEqual(self.product.stock, 3)
|
|
self.assertEqual(Order.objects.count(), 1)
|
|
|
|
def test_idempotency_key_returns_same_order(self):
|
|
first = self.client.post(
|
|
"/api/v1/orders/",
|
|
self.payload,
|
|
format="json",
|
|
HTTP_IDEMPOTENCY_KEY="order-test-2",
|
|
)
|
|
second = self.client.post(
|
|
"/api/v1/orders/",
|
|
self.payload,
|
|
format="json",
|
|
HTTP_IDEMPOTENCY_KEY="order-test-2",
|
|
)
|
|
self.assertEqual(first.status_code, 201)
|
|
self.assertEqual(second.status_code, 200)
|
|
self.assertEqual(first.data["id"], second.data["id"])
|
|
self.assertEqual(Order.objects.count(), 1)
|
|
|
|
def test_cannot_order_more_than_stock(self):
|
|
payload = {
|
|
**self.payload,
|
|
"items": [{"product_id": str(self.product.id), "quantity": 99}],
|
|
}
|
|
response = self.client.post("/api/v1/orders/", payload, format="json")
|
|
self.assertEqual(response.status_code, 400)
|
|
self.assertEqual(Order.objects.count(), 0)
|