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) # Ensure v2 household schema is present from scripts.migration_to_households import run_migration await run_migration(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) # 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"] 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, "name": "Loc"} # 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], "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 ) assert resp_meal.status_code == 200 assert "Location" in resp_meal.headers