Skip to content

Latest commit

 

History

History
37 lines (29 loc) · 3.34 KB

testing.md

File metadata and controls

37 lines (29 loc) · 3.34 KB

Testing

  • Framework: pytest, with pytest-asyncio, pytest-httpx, pytest-mock, freezegun (versions in pyproject.toml dev group)
  • Location: tests/, grouped by area (test_api_client.py, test_cache.py, test_utils.py, test_act.py, test_token_store.py, test_telemetry.py), not one file per source module
  • Coverage: pytest-cov. pyproject.toml addopts always applies --cov=src --cov-report=term-missing:skip-covered --cov-report=xml. No minimum threshold is configured.

The default poetry run pytest suite needs no credentials. All Auth0 HTTP calls go through httpx_mock. Fixtures use https://auth0.local, and test_api_client.py also uses tenant1.auth0.com.

Running tests

poetry run pytest                                   # all unit tests
poetry run pytest tests/test_cache.py               # one file
poetry run pytest tests/test_api_client.py -k dpop  # filter by name

Conventions

  • Module-level test_<subject>_<condition> functions (e.g. test_verify_access_token_fail_no_iss), often with a one-line docstring, grouped under # ===== Section ===== comment banners in every file except test_act.py. No test classes.
  • Async tests are marked explicitly with @pytest.mark.asyncio. No asyncio_mode is configured, so an unmarked async def test_... will not run as intended.
  • Assert error types with pytest.raises(VerifyAccessTokenError) and similar. Where both exist, assert on get_error_code() / .code and get_status_code() rather than message text. Codes are the contract. Messages are not.
  • Test placeholders stay obviously synthetic (cid, csecret, auth0.local). Never paste a real tenant value.
  • Time-dependent tests use @freeze_time(...) from freezegun. Cache TTL tests in test_cache.py use short real sleeps.

Mocking and test utilities

  • tests/conftest.py provides:
    • api_client_confidential: an ApiClient with domain="auth0.local", placeholder client_id / client_secret, and an InMemoryTokenStore, so OBO and Token Vault calls verify the token and cache
    • api_client_confidential_no_store: the same client without a token_store, for exchanges that must not verify or cache
    • InMemoryTokenStore, InMemoryIndexedTokenStore: test-only stores (never for production), the latter for scope_matching="non_strict"
    • mock_discovery: registers the discovery response
    • mock_token_response(...), token_success(...), last_form(...), last_auth_header(...): token endpoint mocking and request inspection
    • unsigned_jwt(header, payload): a JWT-shaped string from any JSON values with a dummy signature, for malformed header and payload tests
    • assert_api_error(...), assert_http_basic_auth(...), assert_form_post(...), assert_no_requests(...): shared assertions. Use assert_api_error rather than hand-rolling .code / status checks
    • auth0_client_payload(request), assert_telemetry_headers(request, name=...): decode the Auth0-Client header of a recorded request, and assert it and User-Agent are present. Use these for any test of requests sent to Auth0
  • src/auth0_api_python/token_utils.py provides generate_token, generate_dpop_proof, generate_token_with_cnf, signed with a synthetic test key. Use these instead of hand-building JWTs.
  • Use the constants DISCOVERY_URL, JWKS_URL, TOKEN_ENDPOINT from conftest.py rather than repeating URLs.