duendesoftware/duende-skills/identity-testing-patterns
Claude Code and agent skill by duendesoftware/duende-skills
- community
Checking that the SKILL.md file is still in the repository.
Testing patterns for IdentityServer-based systems including integration testing with WebApplicationFactory, mock token issuance, test authority configuration, protocol response validation, and end-to-end authentication flow testing.
Install
npx vskill@latest install duendesoftware/duende-skills/identity-testing-patternsSKILL.md
The same SKILL.md was published earlier as duendesoftware/duende-skills/identity-testing-patterns.
Identity Testing Patterns
When to Use This Skill
Use this skill when:
- Writing integration tests for applications that issue or validate tokens using Duende IdentityServer
- Hosting IdentityServer in-memory with
WebApplicationFactory<T>to test grant flows end-to-end - Creating mock JWT tokens for testing protected APIs without a live authority
- Testing custom
IProfileServiceimplementations or claim transformation logic - Verifying
IAuthorizationHandlerand policy-based authorization against specific claim sets - Testing BFF endpoints that rely on cookie-based sessions and proxied API calls
- Scaffolding an integration-test project from the
duende-is-inmemtemplate to test IdentityServer itself - Running a post-deployment login-flow smoke test without a headless browser
Core Principles
- Integration over unit — Test token issuance, claim mapping, and policy enforcement against a real (in-process) IdentityServer instance. Avoid mocking the token pipeline itself; mock only external I/O (databases, downstream services).
- In-process authority — Use
WebApplicationFactory<T>to host IdentityServer inside the test process. This avoids network round-trips, eliminates certificate trust issues, and makes tests deterministic. - Predictable signing keys — Override key management in tests with a static development signing key so token signatures are verifiable without key rotation logic.
- Minimal test clients — Register only the clients, scopes, and resources each test needs. Over-broad test configurations mask permission bugs.
- Test auth handler for API tests — When testing protected APIs in isolation (without a live token endpoint), replace JWT Bearer authentication with a
TestAuthHandlerthat accepts a fake scheme. Never disable authorization wholesale. - Builder pattern for test data — Use fluent builders for
Client,ApiScope,ApiResource, and test users to keep test setup readable and reduce duplication.
Related Skills
identityserver-configuration— Production client and resource registration patternsaspnetcore-authentication— OIDC and JWT Bearer handler configurationaspnetcore-authorization— Policy definitions and requirement handlersclaims-authorization—IProfileServiceand claim pipeline internalsduende-bff— BFF session and proxy architecture being tested
Docs: https://docs.duendesoftware.com/identityserver/fundamentals
Sub-Documents
| Document | Description | When to Load | |
|---|---|---|---|
| docs/bff-testing.md | BFF endpoint testing with cookie simulation, antiforgery headers, and OIDC redirect bypass | BFF testing, CookieContainer, x-csrf header, BffFactory, session simulation | |
| docs/aspire-testing.md | Full-stack Aspire testing with identity server health checks and token endpoint wiring | Aspire testing, DistributedApplicationTestingBuilder, WaitForResourceHealthyAsync, end-to-end |
Testing Strategy Overview
| What to test | Recommended approach | |
|---|---|---|
| Token issuance (client credentials, code flow) | In-process WebApplicationFactory hitting /connect/token | |
Claim mapping / IProfileService | Unit test with DefaultProfileService + mock context, or integration test | |
| Authorization policy requirements | IAuthorizationService + TestAuthHandler in integration test | |
IAuthorizationHandler logic | Direct unit test with AuthorizationHandlerContext | |
| Protected API access control | WebApplicationFactory with TestAuthHandler and constructed ClaimsPrincipal | |
| BFF endpoints (login/logout/user) | WebApplicationFactory with cookie simulation | |
| EF Core store implementations | In-memory EF provider or isolated SQL container | |
| Deployed login flow (smoke test) | Cookie-aware HttpClient + AngleSharp HTML parsing (Pattern 10) |
Pattern 1: WebApplicationFactory for IdentityServer
Host a complete IdentityServer in-memory. Override configuration to inject test clients, resources, and a static signing key.
Required NuGet Packages
IdentityServer WebApplicationFactory
Requesting a Token in a Test
Testing IdentityServer Itself from the duende-is-inmem Template
The fastest way to get a real, in-process IdentityServer under test is to scaffold from the in-memory template duende-is-inmem (from the `Duende.Templates` NuGet package) and add a test project that references the host. Unlike the TestAuthHandler/TestTokenFactory patterns below — which mock auth in a downstream API — this exercises IdentityServer's real endpoints and token issuance (no auth mocking).
Test project setup:
- The test project must use the Web SDK so ASP.NET Core testing APIs resolve. Change the top of the
.csproj:
- Add packages to the test project:
- Reference the IdentityServer host project (
<ProjectReference Include="..\IdentityServerHost\IdentityServerHost.csproj" />).
- Make the template's static
Configcollections mutable so tests can add/clear clients and scopes:
Caution: With xUnit's parallel test execution, mutating shared staticConfigcollections causes cross-test interference. Prefer per-test collections (or a fresh factory per test) over mutating shared statics.
Test class using the primary-constructor `IClassFixture`:
The discovery/token helpers (GetDiscoveryDocumentAsync,RequestClientCredentialsTokenAsync) come from `Duende.IdentityModel`. Accessingfactory.Serviceslets you assert on real in-process services such asIProfileService, stores, or options.
Pattern 2: Test Configuration Builders
Use static builders — not scattered inline literals — so every test builds from a consistent baseline.
This is the start of the file. Read the full SKILL.md on GitHub.
Security
- Security scanPassed, 95 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/identity-testing-patterns/SKILL.md
Popularity
- 7-day trend score
- 0
Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/duendesoftware/duende-skills/identity-testing-patterns)