|
| 1 | +from decimal import Decimal |
| 2 | + |
| 3 | +from fastapi.testclient import TestClient |
| 4 | + |
| 5 | +from backend.app.main import main |
| 6 | +from backend.app.repositories.order_repository import OrderRepository |
| 7 | +from backend.tests.conftest import REAL_DATA |
| 8 | + |
| 9 | +FRAPPUCCINO = "F5hn9s3VD9qzKsLV4z8CW" # Starbucks item in test-data/restaurants.json, 5.95 |
| 10 | + |
| 11 | + |
| 12 | +def login(client, email): |
| 13 | + token = client.post("/api/v1/login", json={"email": email, "password": "password123"}).json()["access_token"] |
| 14 | + return {"Authorization": f"Bearer {token}"} |
| 15 | + |
| 16 | + |
| 17 | +def set_frappuccino_price(client, owner, restaurant_id, price): |
| 18 | + offers = {"price": price, "priceCurrency": "USD", "availability": "https://schema.org/InStock"} |
| 19 | + response = client.patch(f"/api/v1/restaurants/{restaurant_id}/menu-items/{FRAPPUCCINO}", json={"offers": offers}, headers=owner) |
| 20 | + assert response.status_code == 200 |
| 21 | + |
| 22 | + |
| 23 | +def test_customer_to_order_workflow(client): |
| 24 | + # Customer: register and sign in |
| 25 | + registered = client.post("/api/v1/customers", json={ |
| 26 | + "name": "Jordan Smith", "email": "jordan@example.com", "password": "password123", "address": "1 Main St, Kelowna", |
| 27 | + }) |
| 28 | + assert registered.status_code == 201 |
| 29 | + customer = login(client, "jordan@example.com") |
| 30 | + |
| 31 | + # Restaurant and menu: find the restaurant that sells the item |
| 32 | + restaurants = client.get("/api/v1/restaurants?name=Starbucks").json() |
| 33 | + restaurant_id = restaurants[0]["id"] |
| 34 | + assert client.get(f"/api/v1/restaurants/{restaurant_id}").status_code == 200 |
| 35 | + |
| 36 | + # Cart: add the item, backend works out the total |
| 37 | + cart = client.post("/api/v1/cart/items", json={"menuItemId": FRAPPUCCINO, "quantity": 2}, headers=customer).json() |
| 38 | + assert cart["total"] == "11.90" |
| 39 | + |
| 40 | + # The owner changes the price before checkout, checkout has to use the new one |
| 41 | + owner = login(client, "owner@example.com") |
| 42 | + set_frappuccino_price(client, owner, restaurant_id, 6.50) |
| 43 | + |
| 44 | + # Checkout: creates the order and closes the cart |
| 45 | + response = client.post("/api/v1/checkout", headers=customer) |
| 46 | + assert response.status_code == 201 |
| 47 | + order = response.json() |
| 48 | + assert order["customerId"] == registered.json()["id"] |
| 49 | + assert order["restaurantId"] == restaurant_id |
| 50 | + assert order["deliveryAddress"] == "1 Main St, Kelowna" |
| 51 | + assert order["items"][0]["unitPrice"] == "6.5" |
| 52 | + assert order["total"] == "13.00" |
| 53 | + assert client.get("/api/v1/cart", headers=customer).json()["items"] == [] |
| 54 | + |
| 55 | + # Order: still there after a restart, and a later price change doesn't touch it |
| 56 | + set_frappuccino_price(client, owner, restaurant_id, 9.99) |
| 57 | + restarted = TestClient(main()) |
| 58 | + login(restarted, "jordan@example.com") |
| 59 | + |
| 60 | + saved = OrderRepository(REAL_DATA / "orders.json").get_all_orders() |
| 61 | + assert [o.id for o in saved] == [order["id"]] |
| 62 | + assert saved[0].items[0].name.startswith("Caramel Ribbon Crunch") |
| 63 | + assert saved[0].items[0].unitPrice == Decimal("6.5") |
| 64 | + assert saved[0].total == Decimal("13.00") |
0 commit comments