Skip to content

test: add unit tests for UserManagementHandler - #146

Merged
mpaulosky merged 2 commits into
sprint/6-code-qualityfrom
squad/142-usermanagementhandler-tests
Apr 24, 2026
Merged

test: add unit tests for UserManagementHandler#146
mpaulosky merged 2 commits into
sprint/6-code-qualityfrom
squad/142-usermanagementhandler-tests

Conversation

@mpaulosky

Copy link
Copy Markdown
Owner

Closes #142

Working as Gimli, the Tester (QA Engineer)

Changes

Adds 4 unit tests in tests/Unit.Tests/Handlers/UserManagementHandlerTests.cs:

Test Scenario
Handle_GetUsersWithRoles_ConfigMissing_ReturnsFailResult IConfiguration returns null for Auth0:ManagementApiDomain
Handle_AssignRole_ConfigMissing_ReturnsFailResult Same — config missing
Handle_RemoveRole_ConfigMissing_ReturnsFailResult Same — config missing
Handle_GetAvailableRoles_ConfigMissing_ReturnsFailResult Same — config missing

Strategy: mocking IConfiguration["Auth0:ManagementApiDomain"] to return null causes GetManagementClientAsync to throw InvalidOperationException, which each Handle overload catches and wraps as Result.Fail.

Verification

Passed! - Failed: 0, Passed: 20, Skipped: 0, Total: 20

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 24, 2026 18:48
@github-actions

Copy link
Copy Markdown
Contributor

🏗️ PR Added to Squad Triage Queue

This PR has been labeled with squad and added to the triage queue.

Next steps:

  • The squad Lead will review and assign to an appropriate team member
  • A squad:member label will be added after triage

If you know which squad member should handle this, you can add the appropriate squad:member label yourself.

@github-actions github-actions Bot added the squad Squad triage inbox — Lead will assign to a member label Apr 24, 2026
@github-actions

github-actions Bot commented Apr 24, 2026

Copy link
Copy Markdown
Contributor

Test Results Summary

206 tests  ±0   206 ✅ ±0   14s ⏱️ ±0s
  5 suites ±0     0 💤 ±0 
  5 files   ±0     0 ❌ ±0 

Results for commit 2db4278. ± Comparison against base commit 475185e.

♻️ This comment has been updated with latest results.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 returns Result.Fail when Auth0:ManagementApiDomain is not configured.
  • Adjusted RedisFixture.CreateCacheService() visibility to internal in integration tests.
  • Expanded InternalsVisibleTo declarations 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

codecov Bot commented Apr 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 76.93%. Comparing base (475185e) to head (2db4278).
⚠️ Report is 15 commits behind head on sprint/6-code-quality.

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:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

- 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>
@mpaulosky
mpaulosky merged commit 677efce into sprint/6-code-quality Apr 24, 2026
14 checks passed
@mpaulosky
mpaulosky deleted the squad/142-usermanagementhandler-tests branch April 24, 2026 19:30
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

squad Squad triage inbox — Lead will assign to a member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants