feat: add lifespan integration and dependency injection
This commit is contained in:
parent
9f4914a922
commit
a45604ff2f
4 changed files with 88 additions and 3 deletions
|
|
@ -9,12 +9,12 @@ from fastapi_oidc_op.config import Settings
|
|||
|
||||
@pytest.fixture
|
||||
def settings() -> Settings:
|
||||
return Settings(issuer="http://localhost:8000")
|
||||
return Settings(issuer="http://localhost:8000", sqlite_path=":memory:")
|
||||
|
||||
|
||||
@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:
|
||||
async with app.router.lifespan_context(app), AsyncClient(transport=transport, base_url=settings.issuer) as ac:
|
||||
yield ac
|
||||
|
|
|
|||
|
|
@ -13,3 +13,35 @@ async def test_app_has_title(client: AsyncClient) -> None:
|
|||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["info"]["title"] == "FastAPI OIDC OP"
|
||||
|
||||
|
||||
async def test_app_has_repos_on_state(client: AsyncClient) -> None:
|
||||
from fastapi_oidc_op.store.protocols import (
|
||||
CredentialRepository,
|
||||
MagicLinkRepository,
|
||||
UserRepository,
|
||||
)
|
||||
|
||||
app = client._transport.app # type: ignore[union-attr]
|
||||
assert isinstance(app.state.user_repo, UserRepository)
|
||||
assert isinstance(app.state.credential_repo, CredentialRepository)
|
||||
assert isinstance(app.state.magic_link_repo, MagicLinkRepository)
|
||||
|
||||
|
||||
async def test_dependency_functions() -> None:
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from fastapi_oidc_op.dependencies import (
|
||||
get_credential_repo,
|
||||
get_magic_link_repo,
|
||||
get_user_repo,
|
||||
)
|
||||
|
||||
request = MagicMock()
|
||||
request.app.state.user_repo = "user_repo_sentinel"
|
||||
request.app.state.credential_repo = "credential_repo_sentinel"
|
||||
request.app.state.magic_link_repo = "magic_link_repo_sentinel"
|
||||
|
||||
assert get_user_repo(request) == "user_repo_sentinel"
|
||||
assert get_credential_repo(request) == "credential_repo_sentinel"
|
||||
assert get_magic_link_repo(request) == "magic_link_repo_sentinel"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue