Files
Heimdall/tests/Heimdall.Application.Tests/Handlers/Auth/Verify2faHandlerTests.cs
T
arnaud.dartois 2bf3dbc95b
Heimdall Gitea / Detect CI scope (pull_request) Successful in 5s
Heimdall Gitea / Build and tests (pull_request) Successful in 20m8s
Heimdall Gitea / Publish versioned release (pull_request) Skipped
Heimdall Gitea / Build and scan agent image (pull_request) Failing after 6m10s
Heimdall Gitea / Build and scan server image (pull_request) Failing after 8m2s
Heimdall Gitea / Publish Docker Hub latest tags (pull_request) Skipped
Heimdall Gitea / CI policy (pull_request) Failing after 2s
fix(identity): harden user lifecycle and permissions
2026-11-04 10:26:07 +01:00

50 lines
2.2 KiB
C#

using Heimdall.Application.Auth.Commands.Verify2fa;
using Heimdall.Application.Auth.Services;
using Heimdall.Application.Common;
using Heimdall.Application.Services;
using Heimdall.Application.Tests.Common;
using Heimdall.Domain.Entities;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Logging;
using NSubstitute;
namespace Heimdall.Application.Tests.Handlers.Auth;
public class Verify2faHandlerTests
{
private readonly UserManager<AppUser> _userManager;
private readonly IJwtService _jwtService;
private readonly IRolePermissionService _rolePermissionService;
private readonly IApplicationDbContext _db;
private readonly ILogger<Verify2faHandler> _logger;
private readonly Verify2faHandler _handler;
private readonly InMemoryDbSet<UserRecoveryCode> _recoveryCodes;
public Verify2faHandlerTests()
{
var userStore = Substitute.For<IUserStore<AppUser>>();
var identityOptions = Microsoft.Extensions.Options.Options.Create(new IdentityOptions());
_userManager = Substitute.For<UserManager<AppUser>>(userStore, identityOptions, null!, null!, null!, null!, null!, null!, null!);
_jwtService = Substitute.For<IJwtService>();
_rolePermissionService = Substitute.For<IRolePermissionService>();
_rolePermissionService.GetPermissionsForRolesAsync(Arg.Any<IList<string>>(), Arg.Any<CancellationToken>())
.Returns(Task.FromResult<IList<string>>([]));
_db = Substitute.For<IApplicationDbContext>();
_logger = Substitute.For<ILogger<Verify2faHandler>>();
_recoveryCodes = (InMemoryDbSet<UserRecoveryCode>)MockDbSetHelper.CreateFor<UserRecoveryCode>();
_db.UserRecoveryCodes.Returns(_recoveryCodes);
_handler = new Verify2faHandler(_userManager, _jwtService, _rolePermissionService, _db, _logger);
}
[Theory]
[InlineData("")]
[InlineData("test@test.com")]
[InlineData("123456")]
public async Task Verify2fa_without_password_challenge_is_rejected(string proof)
{
var result = await _handler.Handle(new Verify2faCommand(proof, "123456"), CancellationToken.None);
result.StatusCode.ShouldBe(401);
await _userManager.DidNotReceiveWithAnyArgs().VerifyTwoFactorTokenAsync(null!, null!, null!);
}
}