test(oidc): fake-IdP harness — 19 flow tests, coverage gate to 80% (#27) #48

Merged
amiryahaya merged 1 commit from test/oidc-fake-idp-coverage into main 2026-11-04 16:20:56 +01:00
Owner

Closes the last #27 gap: OIDC authorize/callback/verify/exchanger/validator (~280 lines) covered end-to-end over real HTTP against an in-process fake OpenID Connect provider.

What

  • FakeIdpServer (HttpListener-based): discovery + RSA JWKS + /oauth/token issuing RS256 id_tokens with tampered/expired/wrong-audience/missing-claim variants. Deliberately not Kestrel — an in-process WebApplication host interferes with WebApplicationFactory's host construction (app under test 404s; documented in the class doc).
  • OidcFlowTests (19): authorize URL/PKCE building + redirect allowlist; callback (existing login, admin MFA/TOTP branches, invitation registration, email mismatch, bad state, provider mismatch, unknown code, tampered signature); verify (existing login, no invite, expired, wrong audience, missing email); link (happy + duplicate conflict).
  • Prod seam: Oidc provider endpoints now config-overridable (AuthorizeUrl/TokenUrl/MetadataUrl/Issuer — defaults preserved); validator honors explicitly-configured http:// metadata via FlexDocumentRetriever; validation failures fail closed as 401 (previously garbage tokens leaked 500s).

Coverage

78.7% → 82.51% Release / 81.77% Debug (746 tests); ratchet raised 78 → 80. SRS §2.3 80% exit gate satisfied.

Verification

Full suite green ×3; pre-push hook green (build + tests + coverage gate).

Closes #27

Closes the last #27 gap: OIDC authorize/callback/verify/exchanger/validator (~280 lines) covered end-to-end over real HTTP against an in-process fake OpenID Connect provider. ## What - **FakeIdpServer** (HttpListener-based): discovery + RSA JWKS + /oauth/token issuing RS256 id_tokens with tampered/expired/wrong-audience/missing-claim variants. Deliberately not Kestrel — an in-process WebApplication host interferes with WebApplicationFactory's host construction (app under test 404s; documented in the class doc). - **OidcFlowTests (19)**: authorize URL/PKCE building + redirect allowlist; callback (existing login, admin MFA/TOTP branches, invitation registration, email mismatch, bad state, provider mismatch, unknown code, tampered signature); verify (existing login, no invite, expired, wrong audience, missing email); link (happy + duplicate conflict). - **Prod seam**: Oidc provider endpoints now config-overridable (AuthorizeUrl/TokenUrl/MetadataUrl/Issuer — defaults preserved); validator honors explicitly-configured http:// metadata via FlexDocumentRetriever; validation failures fail closed as 401 (previously garbage tokens leaked 500s). ## Coverage 78.7% → **82.51% Release / 81.77% Debug** (746 tests); ratchet raised 78 → 80. SRS §2.3 80% exit gate satisfied. ## Verification Full suite green ×3; pre-push hook green (build + tests + coverage gate). Closes #27
test(oidc): fake-IdP harness — 19 flow tests, coverage gate to 80% (#27)
All checks were successful
API CI / build-and-test (pull_request) Successful in 33m26s
8a5955b752
Closes the last #27 gap: OIDC authorize/callback/verify/exchanger/validator
(~280 lines) now covered end-to-end over real HTTP.

Test harness:
- FakeIdpServer: in-process OpenID Connect provider on HttpListener —
  discovery, RSA JWKS, /oauth/token issuing RS256 id_tokens (tampered/
  expired/wrong-audience/missing-claim variants for validation tests).
  Deliberately NOT Kestrel: an in-process WebApplication host interferes
  with WebApplicationFactory's host construction (app under test 404s).
- FakeIdpOidcFactory: wires Oidc:Google at the fake IdP.
- OidcFlowTests (19): authorize URL/PKCE building + redirect allowlist,
  callback (existing login, admin MFA/TOTP branches, invitation
  registration, email mismatch, bad state, provider mismatch, unknown
  code, tampered signature), verify (existing login, no invite, expired,
  wrong audience, missing email), link (happy + duplicate conflict).

Prod seam (config-overridable, defaults preserved):
- Oidc:Google|Microsoft:AuthorizeUrl/TokenUrl/MetadataUrl/Issuer — enables
  the fake IdP and future regional/tenant-specific endpoints
- OidcTokenValidator: FlexDocumentRetriever honors explicitly-configured
  http:// metadata (IdentityModel's HttpDocumentRetriever is HTTPS-locked);
  validation failures now fail closed as 401 on ANY error (was: uncaught
  exceptions leaked 500s for garbage tokens)

Coverage: 78.7% → 82.51% Release / 81.77% Debug (746 tests, all green);
ratchet raised 78 → 80 (§2.3 exit gate satisfied). Existing EmailAuthFlow
503/500-expectation updated to fail-closed 401.

🤖 Generated with [ZCode](https://zcode.ai)

Co-Authored-By: ZCode <noreply@anthropic.com>
Author
Owner

ponytail-review (§2.4 AI first-pass) — Verdict: Ship.

What this change does: adds an in-process fake OpenID Connect provider and 19 end-to-end tests for the authorize/callback/verify/link flows, plus a small config seam (Oidc provider endpoint URLs become config-overridable, defaults preserved) and a fail-closed catch-all in the token validator.

Checked: all callers of ValidateAsync (3 — all map error results to ProblemResults, so the 401 fail-closed change is safe); catch broadening (SecurityTokenException → Exception: garbage tokens previously leaked 500s — the fail-closed 401 is the fix, covered by the updated EmailAuthFlow test); metadata URL provenance (operator config only, never user input — http:// opt-in is documented in the FlexDocumentRetriever doc); fake-IdP state (codes dict lock-guarded, consumed codes removed); ratchet headroom (82.5/81.8 vs 80 floor).

Nice to have (not blocking): catch (Exception) in ValidateAsync also swallows OperationCanceledException from an aborted request — maps to a 401 written to a disconnected client, harmless. Could filter catch (Exception ex) when (ex is not OperationCanceledException) in a future pass.

Not checked: iOS/Android clients against the renamed schemes (untouched by this PR); real Zitadel interop (Phase 2 scope).

**ponytail-review (§2.4 AI first-pass) — Verdict: Ship.** What this change does: adds an in-process fake OpenID Connect provider and 19 end-to-end tests for the authorize/callback/verify/link flows, plus a small config seam (Oidc provider endpoint URLs become config-overridable, defaults preserved) and a fail-closed catch-all in the token validator. Checked: all callers of ValidateAsync (3 — all map error results to ProblemResults, so the 401 fail-closed change is safe); catch broadening (SecurityTokenException → Exception: garbage tokens previously leaked 500s — the fail-closed 401 is the fix, covered by the updated EmailAuthFlow test); metadata URL provenance (operator config only, never user input — http:// opt-in is documented in the FlexDocumentRetriever doc); fake-IdP state (codes dict lock-guarded, consumed codes removed); ratchet headroom (82.5/81.8 vs 80 floor). Nice to have (not blocking): `catch (Exception)` in ValidateAsync also swallows OperationCanceledException from an aborted request — maps to a 401 written to a disconnected client, harmless. Could filter `catch (Exception ex) when (ex is not OperationCanceledException)` in a future pass. Not checked: iOS/Android clients against the renamed schemes (untouched by this PR); real Zitadel interop (Phase 2 scope).
amiryahaya deleted branch test/oidc-fake-idp-coverage 2026-11-04 16:20:57 +01:00
Sign in to join this conversation.
No description provided.