test: add unit tests for UserManagementHandler - #146
Merged
mpaulosky merged 2 commits intoApr 24, 2026
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
🏗️ PR Added to Squad Triage QueueThis PR has been labeled with Next steps:
|
Contributor
There was a problem hiding this comment.
Pull request overview
Adds initial unit test coverage for UserManagementHandler by verifying behavior when required Auth0 configuration is missing, plus minor test-assembly access and fixture visibility adjustments.
Changes:
- Added 4 unit tests asserting each
UserManagementHandler.Handle(...)overload returnsResult.FailwhenAuth0:ManagementApiDomainis not configured. - Adjusted
RedisFixture.CreateCacheService()visibility tointernalin integration tests. - Expanded
InternalsVisibleTodeclarations in the Web assembly for additional test projects.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| tests/Web.Tests.Integration/Infrastructure/RedisFixture.cs | Narrows fixture helper method visibility to internal. |
| tests/Unit.Tests/Handlers/UserManagementHandlerTests.cs | Adds config-missing error-path unit tests for UserManagementHandler. |
| src/Web/Properties/AssemblyInfo.cs | Adds additional friend test assemblies via InternalsVisibleTo. |
Comment on lines
+28
to
+37
| [Fact] | ||
| public async Task Handle_GetUsersWithRoles_ConfigMissing_ReturnsFailResult() | ||
| { | ||
| // Act | ||
| var result = await _handler.Handle(new GetUsersWithRolesQuery(), CancellationToken.None); | ||
|
|
||
| // Assert | ||
| result.Failure.Should().BeTrue(); | ||
| result.Error.Should().Contain("Auth0:ManagementApiDomain not configured"); | ||
| } |
| [assembly: InternalsVisibleTo("Architecture.Tests")] | ||
| [assembly: InternalsVisibleTo("Web.Tests")] | ||
| [assembly: InternalsVisibleTo("Web.Tests.Bunit")] | ||
| [assembly: InternalsVisibleTo("Web.Tests.Integration")] |
Comment on lines
+18
to
+73
| private readonly IConfiguration _config = Substitute.For<IConfiguration>(); | ||
| private readonly IHttpClientFactory _httpFactory = Substitute.For<IHttpClientFactory>(); | ||
| private readonly UserManagementHandler _handler; | ||
|
|
||
| public UserManagementHandlerTests() | ||
| { | ||
| _config["Auth0:ManagementApiDomain"].Returns((string?)null); | ||
| _handler = new UserManagementHandler(_config, _httpFactory); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Handle_GetUsersWithRoles_ConfigMissing_ReturnsFailResult() | ||
| { | ||
| // Act | ||
| var result = await _handler.Handle(new GetUsersWithRolesQuery(), CancellationToken.None); | ||
|
|
||
| // Assert | ||
| result.Failure.Should().BeTrue(); | ||
| result.Error.Should().Contain("Auth0:ManagementApiDomain not configured"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Handle_AssignRole_ConfigMissing_ReturnsFailResult() | ||
| { | ||
| // Act | ||
| var result = await _handler.Handle( | ||
| new AssignRoleCommand("user-1", "role-1"), CancellationToken.None); | ||
|
|
||
| // Assert | ||
| result.Failure.Should().BeTrue(); | ||
| result.Error.Should().Contain("Auth0:ManagementApiDomain not configured"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Handle_RemoveRole_ConfigMissing_ReturnsFailResult() | ||
| { | ||
| // Act | ||
| var result = await _handler.Handle( | ||
| new RemoveRoleCommand("user-1", "role-1"), CancellationToken.None); | ||
|
|
||
| // Assert | ||
| result.Failure.Should().BeTrue(); | ||
| result.Error.Should().Contain("Auth0:ManagementApiDomain not configured"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Handle_GetAvailableRoles_ConfigMissing_ReturnsFailResult() | ||
| { | ||
| // Act | ||
| var result = await _handler.Handle(new GetAvailableRolesQuery(), CancellationToken.None); | ||
|
|
||
| // Assert | ||
| result.Failure.Should().BeTrue(); | ||
| result.Error.Should().Contain("Auth0:ManagementApiDomain not configured"); | ||
| } | ||
| } |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## sprint/6-code-quality #146 +/- ##
======================================================
Coverage 76.93% 76.93%
======================================================
Files 43 43
Lines 672 672
Branches 111 111
======================================================
Hits 517 517
Misses 105 105
Partials 50 50 🚀 New features to boost your workflow:
|
- Fix indentation: all class members now use consistent tab indentation - Expand test coverage: add 12 new tests covering ClientId missing, ClientSecret missing, and HTTP token-endpoint failure (500) paths for all four handler operations (GetUsersWithRoles, AssignRole, RemoveRole, GetAvailableRoles) - Rename existing 4 Domain-missing tests from ConfigMissing to DomainMissing for clarity - Add StubHttpHandler inner class and helper factory methods for clean, isolated test setup Total: 16 tests (was 4) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
4 tasks
mpaulosky
added a commit
that referenced
this pull request
Apr 25, 2026
## Sprint 6: Code Quality **Goal:** Eliminate analyzer warnings, enforce internal visibility on Web feature types, expand test coverage, and migrate Unit.Tests into Web.Tests. --- ## Merged Feature PRs - Closes #137 — PR #144 — fix(quality): rename `ct` → `cancellationToken` in all MediatR handlers - Closes #138 — PR #145 — test: add unit tests for BlogPost, Result, and ValidationBehavior (42 tests) - Closes #139 — PR #146 — test: add unit tests for UserManagementHandler (16 tests) - Closes #140 — PR #147 — fix(quality): make Web feature types `internal` (CA1515, ~28 types) - Closes #141 — PR #148 — test: add unit tests for BlogPostMappings (22 tests) - Closes #142 — PR #149 — fix(quality): add ConfigureAwait(false) and specific exception catches - Closes #143 — PR #150 — fix(quality): address Domain and ServiceDefaults analyzer warnings - Closes #151 — PR #152 — feat(tests): migrate Unit.Tests → Web.Tests and remove Unit.Tests project --- ## Test Summary - **80+ new tests** added across all PRs - **105 Web.Tests passing** (0 failures) - `DynamicProxyGenAssembly2` added to `InternalsVisibleTo` in Web.csproj for NSubstitute support ## Notable Changes - `Unit.Tests` project removed; all tests now live in `Web.Tests` - ~28 Web feature types changed from `public` to `internal` (CA1515 compliance) - All MediatR handler parameters renamed from `ct` to `cancellationToken` for clarity - `ConfigureAwait(false)` applied to all async calls in service layer - Domain and ServiceDefaults analyzer warnings resolved --- ## Checklist - [x] All sprint issues closed (#137–#143, #151) - [x] CI green (0 build errors) - [x] 105 Web.Tests passing - [x] Milestone at 100% --------- Co-authored-by: Boromir <boromir@squad.dev> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #142
Working as Gimli, the Tester (QA Engineer)
Changes
Adds 4 unit tests in
tests/Unit.Tests/Handlers/UserManagementHandlerTests.cs:Handle_GetUsersWithRoles_ConfigMissing_ReturnsFailResultIConfigurationreturns null forAuth0:ManagementApiDomainHandle_AssignRole_ConfigMissing_ReturnsFailResultHandle_RemoveRole_ConfigMissing_ReturnsFailResultHandle_GetAvailableRoles_ConfigMissing_ReturnsFailResultStrategy: mocking
IConfiguration["Auth0:ManagementApiDomain"]to returnnullcausesGetManagementClientAsyncto throwInvalidOperationException, which eachHandleoverload catches and wraps asResult.Fail.Verification