Test consumer auth
Consumer contract tests for user-service → auth-service.
This module defines the contract that
user_service (acting
as a consumer) expects auth-service (the provider) to honour. When these
tests run, Pact starts a mock server in place of auth-service and verifies
that
AuthClient
makes exactly the requests specified here and can handle the responses.
The generated Pact file (written to the pacts/ directory) would normally be
published to a Pact Broker so that the auth-service team can run provider
verification against it. In this self-contained example the file is consumed
locally by the provider verification tests.
For background on consumer testing, see the Pact consumer test guide.
Classes¶
Functions¶
pact(pacts_path: Path) -> Generator[Pact, None, None]
¶
Pact fixture for user-service as consumer of auth-service.
Creates a V4 Pact between user-service (consumer) and auth-service
(provider). Each test in this module can define one or more expected
interactions on the returned Pact object; the mock provider will validate
that the consumer code sends exactly those requests and handles the
responses correctly. After the test, the contract is written to pacts_path
for use in provider verification.
| PARAMETER | DESCRIPTION |
|---|---|
pacts_path
|
Directory where the generated Pact file is written.
TYPE:
|
| YIELDS | DESCRIPTION |
|---|---|
Pact
|
Pact configured for |
Source code in examples/http/service_consumer_provider/test_consumer_auth.py
test_validate_credentials(pact: Pact, password: str, *, expected_valid: bool) -> None
¶
Verify the AuthClient contract for both valid and invalid credentials.
This parametrised test covers two interactions in a single contract:
- Valid credentials:
auth-serviceresponds{"valid": true}, andAuthClient.validate_credentialsreturnsTrue. - Invalid credentials:
auth-serviceresponds{"valid": false}, andAuthClient.validate_credentialsreturnsFalse.
Both cases map to the same endpoint (POST /auth/validate) but are modelled
as separate Pact interactions with different provider states. This ensures
that auth-service must support both outcomes, not just the happy path.
| PARAMETER | DESCRIPTION |
|---|---|
pact
|
Pact fixture for
TYPE:
|
password
|
Password sent to
TYPE:
|
expected_valid
|
The validation result the consumer expects to receive and act on.
TYPE:
|