munch-ease-backend/tests/test_health_and_location_headers.py

84 lines
2.6 KiB
Python
Raw Permalink Normal View History

2025-10-19 04:06:51 +00:00
import unittest
import importlib
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 TestHealthAndLocationHeaders(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
2025-10-19 04:06:51 +00:00
self.client = TestClient(main.app)
# Register user and create a household
r = self.client.post(
"/api/v1/auth/register",
json={"email": "loc@test.com", "password": "pw", "displayName": "Loc"},
)
assert r.status_code == 200, r.text
self.headers = {"Authorization": f"Bearer {r.json()['accessToken']}"}
r2 = self.client.post("/api/v1/households", headers=self.headers, json={"name": "Locals"})
assert r2.status_code == 200, r2.text
self.slug = r2.json()["slug"]
2025-10-19 04:06:51 +00:00
return await super().asyncSetUp()
async def asyncTearDown(self) -> None:
await self.conn.close()
main.app.dependency_overrides.clear()
return await super().asyncTearDown()
def test_healthz(self):
resp = self.client.get("/healthz")
assert resp.status_code == 200
assert resp.json() == {"status": "ok"}
def test_location_headers_on_create(self):
# Use the registered user id placeholder for v2 meal participants
person = {"id": 1, "displayName": "Loc"}
2025-10-19 04:06:51 +00:00
# Skip recipe endpoint complexity here; covered by other tests
# create meal and expect Location header
meal_body = {
"id": -1,
"suggestedDate": "2024-06-01T18:00:00+00:00",
"chefs": [person],
"cleanup": [person],
"consumers": [person],
2025-10-19 04:06:51 +00:00
"recipes": [],
"extraIngredients": [
{
"id": -1,
"line": "1x extra",
"name": "extra",
"quantity": 1,
"unit": "each",
"preparation": "",
}
],
}
resp_meal = self.client.post(
f"/api/v1/households/{self.slug}/meals", headers=self.headers, json=meal_body
)
2025-10-19 04:06:51 +00:00
assert resp_meal.status_code == 200
assert "Location" in resp_meal.headers