Coverage for tests/test_meta.py: 98%
47 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-13 19:28 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-13 19:28 +0000
1"""API discovery metadata without dataset or upstream dependencies."""
3import httpx
5from data_api import __version__
6from data_api.api.api import app
7from data_api.data.manager import nthudata
10async def test_metadata_available_without_datasets_or_upstream(monkeypatch):
11 monkeypatch.setattr(nthudata, "states", {})
13 async def unexpected_request(self, request):
14 raise AssertionError("Metadata must not make upstream requests")
16 monkeypatch.setattr(httpx.AsyncHTTPTransport, "handle_async_request", unexpected_request)
17 async with httpx.AsyncClient(
18 transport=httpx.ASGITransport(app=app), base_url="http://test", follow_redirects=False
19 ) as client:
20 response = await client.get("/", headers={"Origin": "https://example.com"})
22 assert response.status_code == 200
23 assert response.json() == {
24 "name": "NTHU Data API",
25 "version": __version__,
26 "docs": "/docs",
27 "openapi": "/openapi.json",
28 "mcp": "/mcp",
29 }
30 assert "location" not in response.headers
31 assert response.headers["Content-Type"] == "application/json"
32 assert response.headers["Access-Control-Allow-Origin"] == "*"
33 assert float(response.headers["X-Process-Time"]) >= 0
34 assert nthudata.states == {}
37async def test_metadata_matches_application_configuration(monkeypatch):
38 monkeypatch.setattr(app, "title", "Test API")
39 monkeypatch.setattr(app, "version", "9.8.7")
40 monkeypatch.setattr(app, "openapi_schema", None)
41 async with httpx.AsyncClient(
42 transport=httpx.ASGITransport(app=app), base_url="http://test"
43 ) as client:
44 response = await client.get("/")
45 metadata = response.json()
46 docs = await client.get(metadata["docs"])
47 openapi = await client.get(metadata["openapi"])
49 assert response.status_code == 200
50 assert metadata["name"] == app.title
51 assert metadata["version"] == app.version
52 assert metadata["docs"] == app.docs_url
53 assert metadata["openapi"] == app.openapi_url
54 assert any(route.path == metadata["mcp"] for route in app.routes)
55 assert docs.status_code == openapi.status_code == 200
56 assert metadata["name"] == openapi.json()["info"]["title"]
57 assert metadata["version"] == openapi.json()["info"]["version"]
60async def test_metadata_hidden_from_openapi_and_swagger():
61 async with httpx.AsyncClient(
62 transport=httpx.ASGITransport(app=app), base_url="http://test"
63 ) as client:
64 schema = await client.get("/openapi.json")
65 assert schema.status_code == 200
66 assert "/" not in schema.json()["paths"]
67 assert "/courses" in schema.json()["paths"]
68 assert "MetaResponse" not in schema.json()["components"]["schemas"]
69 docs = await client.get("/docs")
70 assert docs.status_code == 200
71 assert "/openapi.json" in docs.text
72 assert (await client.get("/")).status_code == 200