munch-ease-backend/tests/test_health_and_location_headers.py
2025-10-19 15:06:51 +11:00

74 lines
2.4 KiB
Python

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
# Always act as an authenticated user for tests that require auth
async def override_cookie_person():
return test_data.Persons.jacob
main.app.dependency_overrides[main.cookie_person] = override_cookie_person
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_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 an existing seeded person from test data (avoids cross-request transaction issues)
person_id = test_data.Persons.jacob.id
# 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": [{"id": person_id, "name": "Jacob"}],
"cleanup": [{"id": person_id, "name": "Jacob"}],
"consumers": [{"id": person_id, "name": "Jacob"}],
"recipes": [],
"extraIngredients": [
{
"id": -1,
"line": "1x extra",
"name": "extra",
"quantity": 1,
"unit": "each",
"preparation": "",
}
],
}
resp_meal = self.client.post("/api/v1/meals", json=meal_body)
assert resp_meal.status_code == 200
assert "Location" in resp_meal.headers