User service
FastAPI service acting as both a Pact consumer and a Pact provider.
This module is the centrepiece of the example. user-service sits in the middle
of a two-hop request path:
This means it plays two Pact roles simultaneously:
-
Provider of the
POST /accountsendpoint consumed byfrontend-web. The provider verification test lives intest_provider. -
Consumer of
auth-service'sPOST /auth/validateendpoint. The consumer contract test lives intest_consumer_auth.
Testability design¶
Provider verification requires the service to be started as a real HTTP server.
To avoid needing a real auth-service during those tests, this module uses the
CredentialsVerifier
protocol as a seam. In production the seam is filled by
AuthClient;
in tests it is replaced with a
StubAuthVerifier
via
set_auth_verifier.
This avoids mocking at the HTTP level. The real FastAPI application runs, and
only the collaborator that calls auth-service is swapped out.
Module-level state¶
SERVICE_STATE
and
ACCOUNT_STORE
are intentional module-level globals. Because provider state handlers in Pact
run in the same process as the application, these globals are the simplest way
for the test harness to reconfigure the service between interactions without an
additional HTTP endpoint.
Attributes¶
ACCOUNT_STORE = InMemoryAccountStore()
module-attribute
¶
SERVICE_STATE = _ServiceState()
module-attribute
¶
app = FastAPI()
module-attribute
¶
Classes¶
CreateAccountRequest
pydantic-model
¶
CreateAccountResponse
pydantic-model
¶
CredentialsVerifier
¶
Bases: Protocol
flowchart TD
examples.http.service_consumer_provider.user_service.CredentialsVerifier[CredentialsVerifier]
click examples.http.service_consumer_provider.user_service.CredentialsVerifier href "" "examples.http.service_consumer_provider.user_service.CredentialsVerifier"
Behaviour required for credential verification.
InMemoryAccountStore()
¶
Small in-memory store for example purposes.
Source code in examples/http/service_consumer_provider/user_service.py
Functions¶
create(username: str) -> UserAccount
¶
Create and store a new account.
| PARAMETER | DESCRIPTION |
|---|---|
username
|
Username for the new account.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
UserAccount
|
The created account. |
Source code in examples/http/service_consumer_provider/user_service.py
UserAccount(id: int, username: str)
dataclass
¶
Functions¶
create_account(payload: CreateAccountRequest) -> CreateAccountResponse
async
¶
Create an account after validating credentials with auth-service.
| PARAMETER | DESCRIPTION |
|---|---|
payload
|
Account request payload.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
CreateAccountResponse
|
Created account response. |
| RAISES | DESCRIPTION |
|---|---|
HTTPException
|
If credentials are invalid. |
Source code in examples/http/service_consumer_provider/user_service.py
reset_state() -> None
¶
set_auth_verifier(verifier: CredentialsVerifier) -> None
¶
Replace the auth verifier implementation.
| PARAMETER | DESCRIPTION |
|---|---|
verifier
|
New verifier implementation.
TYPE:
|