View order

This commit is contained in:
2025-12-13 12:25:00 +01:00
parent fd544fcebc
commit 5d2dfe37c1
5 changed files with 101 additions and 34 deletions

View File

@@ -1,5 +1,7 @@
from decimal import Decimal
from unittest import mock
import pytest
from fake_data import fake
from starlette.testclient import TestClient
@@ -29,14 +31,33 @@ def create_user_with_account(test_db, username: str, balance: float | None = Non
return user
def test_add_item_to_cart(client: TestClient, test_db):
def add_finalized_order_to_user(test_db, user, product) -> Order:
order = Order(user=user)
total_amount = product.price
order.items.append(
OrderItem(product=product, quantity=1, total_amount=total_amount)
)
order.transaction = Transaction(total_amount=total_amount, type="order")
order.transaction.account = user.accounts[0]
test_db.add(order)
test_db.flush()
return order
@pytest.fixture
def product(test_db):
area = Area(**fake.area())
test_db.add(area)
product = Product(**fake.product())
product.area = area
test_db.add(product)
test_db.flush()
form_data = {"product_id": product.id, "quantity": 2, "area_id": area.id}
return product
def test_add_item_to_cart(client: TestClient, test_db, product):
form_data = {"product_id": product.id, "quantity": 2, "area_id": product.area.id}
response = client.post(
"/shop/cart/add",
@@ -47,13 +68,7 @@ def test_add_item_to_cart(client: TestClient, test_db):
assert response.status_code == 302
def test_finalize_order(client: TestClient, test_db):
area = Area(**fake.area())
test_db.add(area)
product = Product(**fake.product())
product.area = area
test_db.add(product)
test_db.flush()
def test_finalize_order(client: TestClient, test_db, product):
user = create_user_with_account(test_db, "test", balance=100.0)
@@ -69,3 +84,24 @@ def test_finalize_order(client: TestClient, test_db):
assert len(user.orders) == 2 # shopping cart + finalized order
assert user.accounts[0].balance == Decimal(100.0) - (product.price * 2)
def test_view_order(client: TestClient, test_db, product):
user = create_user_with_account(test_db, "test")
order = add_finalized_order_to_user(test_db, user, product)
response = client.get(f"/shop/order/{order.id}")
assert response.status_code == 200
assert f"Einkauf #{order.id}" in response.text
assert product.name in response.text
def test_view_order_wrong_user(client: TestClient, test_db, product):
user = create_user_with_account(test_db, "test")
order = add_finalized_order_to_user(test_db, user, product)
with mock.patch.dict("os.environ", {"APS_username": "other_user"}):
response = client.get(f"/shop/order/{order.id}")
assert response.status_code == 403