commit 21a17b771743b23ee41d11a90ed8fdc3433468ce
Author: jableader <jacobdunk@gmail.com>
Date: Mon Oct 20 00:12:02 2025 +1100
Completed tooling improvements, fixed remaining errors
commit 7db48e222e3aa1065c326197c33ba6439720f65a
Author: jableader <jacobdunk@gmail.com>
Date: Sun Oct 19 22:05:37 2025 +1100
autoformat
commit 5705ce24b64c2aa6f0b9426730a479165fa97e2a
Author: jableader <jacobdunk@gmail.com>
Date: Sun Oct 19 22:05:29 2025 +1100
tooling changes
commit f0a6b2fd147bb86b484927afd57b9ba0ac07bf47
Author: jableader <jacobdunk@gmail.com>
Date: Sun Oct 19 21:25:49 2025 +1100
Plan
61 lines
2.1 KiB
Python
61 lines
2.1 KiB
Python
import importlib
|
|
import unittest
|
|
from fastapi.testclient import TestClient
|
|
|
|
from db import connect, create
|
|
import main
|
|
import tests.test_data as test_data
|
|
|
|
|
|
def reload_test_data():
|
|
global test_data
|
|
test_data = importlib.reload(test_data)
|
|
|
|
|
|
class TestShoppingAPI(unittest.IsolatedAsyncioTestCase):
|
|
async def asyncSetUp(self):
|
|
self.conn = await connect(":memory:")
|
|
await create(self.conn)
|
|
await test_data.create_test_data(self.conn)
|
|
reload_test_data()
|
|
|
|
async def override_get_db():
|
|
try:
|
|
yield self.conn
|
|
finally:
|
|
pass
|
|
|
|
main.app.dependency_overrides[main.get_db] = override_get_db
|
|
self.client = TestClient(main.app)
|
|
return await super().asyncSetUp()
|
|
|
|
async def asyncTearDown(self) -> None:
|
|
await self.conn.close()
|
|
main.app.dependency_overrides.clear()
|
|
return await super().asyncTearDown()
|
|
|
|
def test_purchase_unauthorized_without_cookie(self):
|
|
# Do NOT override cookie_person; no cookie provided => 401
|
|
body = {"storeName": "woolworths", "items": []}
|
|
resp = self.client.post("/api/v1/shopping", json=body)
|
|
assert resp.status_code == 401
|
|
assert "application/problem+json" in resp.headers.get("content-type", "")
|
|
prob = resp.json()
|
|
assert prob.get("status") == 401
|
|
assert prob.get("title")
|
|
|
|
def test_purchase_validation_error_returns_problem(self):
|
|
# Override cookie_person to simulate authenticated user
|
|
async def override_cookie_person():
|
|
return test_data.Persons.jacob
|
|
|
|
main.app.dependency_overrides[main.cookie_person] = override_cookie_person
|
|
|
|
# Empty items triggers domain validation error => 400 with Problem Details
|
|
body = {"storeName": "woolworths", "items": []}
|
|
resp = self.client.post("/api/v1/shopping", json=body)
|
|
assert resp.status_code == 400
|
|
assert "application/problem+json" in resp.headers.get("content-type", "")
|
|
prob = resp.json()
|
|
assert prob.get("status") == 400
|
|
assert "items" in prob.get("title", "").lower() or prob.get("title")
|