Modify cart items

This commit is contained in:
2025-12-13 14:30:14 +01:00
parent 5d2dfe37c1
commit d1fc874c35
6 changed files with 137 additions and 11 deletions

View File

@@ -3,6 +3,7 @@ from unittest import mock
import pytest
from fake_data import fake
from sqlalchemy import select
from starlette.testclient import TestClient
from allmende_payment_system.database import ensure_user
@@ -67,6 +68,80 @@ def test_add_item_to_cart(client: TestClient, test_db, product):
)
assert response.status_code == 302
cart = test_db.scalar(select(Order))
assert len(cart.items) == 1
def test_edit_item_in_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",
data=form_data,
headers={"Content-Type": "application/x-www-form-urlencoded"},
follow_redirects=False,
)
assert response.status_code == 302
form_data = {"quantity": 3}
response = client.post(
f"/shop/cart/update/{test_db.scalar(select(OrderItem)).id}",
data=form_data,
headers={"Content-Type": "application/x-www-form-urlencoded"},
follow_redirects=False,
)
assert response.status_code == 302
cart = test_db.scalar(select(Order))
assert len(cart.items) == 1
assert cart.items[0].quantity == 3
assert cart.items[0].total_amount == product.price * 3
def test_remove_item_from_cart(client: TestClient, test_db, product):
user = create_user_with_account(test_db, "test")
user.shopping_cart.items.append(
OrderItem(product=product, quantity=2, total_amount=product.price * 2)
)
test_db.flush()
cart = test_db.scalar(select(Order))
assert len(cart.items) == 1
response = client.get(
f"/shop/cart/remove/{user.shopping_cart.items[0].id}", follow_redirects=False
)
assert response.status_code == 302
cart = test_db.scalar(select(Order))
assert len(cart.items) == 0
assert len(test_db.scalars(select(OrderItem)).all()) == 0
def test_remove_item_from_cart_wrong_user(client: TestClient, test_db, product):
user = create_user_with_account(test_db, "test")
user.shopping_cart.items.append(
OrderItem(product=product, quantity=2, total_amount=product.price * 2)
)
test_db.flush()
cart = test_db.scalar(select(Order))
assert len(cart.items) == 1
id_ = user.shopping_cart.items[0].id
with mock.patch.dict("os.environ", {"APS_username": "other_user"}):
response = client.get(f"/shop/cart/remove/{id_}", follow_redirects=False)
assert response.status_code == 404
cart = test_db.scalar(select(Order))
assert len(cart.items) == 1
def test_finalize_order(client: TestClient, test_db, product):