feat: add app factory with health endpoint and test infrastructure
This commit is contained in:
parent
fd8c8cbf39
commit
6a8b41cd38
3 changed files with 58 additions and 0 deletions
23
src/fastapi_oidc_op/app.py
Normal file
23
src/fastapi_oidc_op/app.py
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
from fastapi import FastAPI
|
||||||
|
|
||||||
|
from fastapi_oidc_op.config import Settings
|
||||||
|
|
||||||
|
|
||||||
|
def create_app(settings: Settings | None = None) -> FastAPI:
|
||||||
|
if settings is None:
|
||||||
|
settings = Settings() # type: ignore[call-arg]
|
||||||
|
|
||||||
|
app = FastAPI(
|
||||||
|
title="FastAPI OIDC OP",
|
||||||
|
version="0.1.0",
|
||||||
|
docs_url="/docs" if settings.debug else None,
|
||||||
|
redoc_url=None,
|
||||||
|
)
|
||||||
|
|
||||||
|
app.state.settings = settings
|
||||||
|
|
||||||
|
@app.get("/health")
|
||||||
|
async def health() -> dict[str, str]:
|
||||||
|
return {"status": "ok"}
|
||||||
|
|
||||||
|
return app
|
||||||
20
tests/conftest.py
Normal file
20
tests/conftest.py
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
from collections.abc import AsyncIterator
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from httpx import ASGITransport, AsyncClient
|
||||||
|
|
||||||
|
from fastapi_oidc_op.app import create_app
|
||||||
|
from fastapi_oidc_op.config import Settings
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def settings() -> Settings:
|
||||||
|
return Settings(issuer="http://localhost:8000")
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
async def client(settings: Settings) -> AsyncIterator[AsyncClient]:
|
||||||
|
app = create_app(settings)
|
||||||
|
transport = ASGITransport(app=app)
|
||||||
|
async with AsyncClient(transport=transport, base_url=settings.issuer) as ac:
|
||||||
|
yield ac
|
||||||
15
tests/test_app.py
Normal file
15
tests/test_app.py
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
from httpx import AsyncClient
|
||||||
|
|
||||||
|
|
||||||
|
async def test_health_endpoint(client: AsyncClient) -> None:
|
||||||
|
response = await client.get("/health")
|
||||||
|
assert response.status_code == 200
|
||||||
|
data = response.json()
|
||||||
|
assert data["status"] == "ok"
|
||||||
|
|
||||||
|
|
||||||
|
async def test_app_has_title(client: AsyncClient) -> None:
|
||||||
|
response = await client.get("/openapi.json")
|
||||||
|
assert response.status_code == 200
|
||||||
|
data = response.json()
|
||||||
|
assert data["info"]["title"] == "FastAPI OIDC OP"
|
||||||
Loading…
Add table
Add a link
Reference in a new issue