2026-08-11 21:35:52 +02:00
|
|
|
using Heimdall.Application.Auth.Commands.Enable2fa;
|
|
|
|
|
using Heimdall.Application.Auth.Services;
|
|
|
|
|
using Heimdall.Application.Common;
|
|
|
|
|
using Heimdall.Application.Tests.Common;
|
|
|
|
|
using Heimdall.Domain.Entities;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using NSubstitute;
|
2026-11-04 13:33:30 +01:00
|
|
|
using Heimdall.Application.Services;
|
2026-08-11 21:35:52 +02:00
|
|
|
|
|
|
|
|
namespace Heimdall.Application.Tests.Handlers.Auth;
|
|
|
|
|
|
|
|
|
|
public class Enable2faHandlerTests
|
|
|
|
|
{
|
2026-11-04 13:33:30 +01:00
|
|
|
private readonly IIdentityAccountService _userManager;
|
2026-08-11 21:35:52 +02:00
|
|
|
private readonly IApplicationDbContext _db;
|
|
|
|
|
private readonly ILogger<Enable2faHandler> _logger;
|
|
|
|
|
private readonly Enable2faHandler _handler;
|
|
|
|
|
private readonly InMemoryDbSet<UserRecoveryCode> _recoveryCodes;
|
|
|
|
|
|
|
|
|
|
public Enable2faHandlerTests()
|
|
|
|
|
{
|
2026-11-04 13:33:30 +01:00
|
|
|
_userManager = Substitute.For<IIdentityAccountService>();
|
2026-08-11 21:35:52 +02:00
|
|
|
_db = Substitute.For<IApplicationDbContext>();
|
|
|
|
|
_logger = Substitute.For<ILogger<Enable2faHandler>>();
|
|
|
|
|
_recoveryCodes = (InMemoryDbSet<UserRecoveryCode>)MockDbSetHelper.CreateFor<UserRecoveryCode>();
|
|
|
|
|
_db.UserRecoveryCodes.Returns(_recoveryCodes);
|
|
|
|
|
_handler = new Enable2faHandler(_userManager, _db, _logger);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
2026-11-04 13:33:30 +01:00
|
|
|
public async Task Enable2fa_should_initialize_key_without_activating_or_issuing_recovery_codes()
|
2026-08-11 21:35:52 +02:00
|
|
|
{
|
2026-11-04 13:33:30 +01:00
|
|
|
var user = new IdentityAccount { Id = Guid.NewGuid(), UserName = "test@test.com", Email = "test@test.com" };
|
|
|
|
|
_userManager.FindByIdAsync(user.Id, Arg.Any<CancellationToken>()).Returns(user);
|
|
|
|
|
_userManager.CheckPasswordAsync(user, "Current-password-123!").Returns(true);
|
|
|
|
|
_userManager.ResetAuthenticatorKeyAsync(user).Returns(IdentityOperationResult.Success);
|
2026-08-11 21:35:52 +02:00
|
|
|
_userManager.GetAuthenticatorKeyAsync(user).Returns("ABCDEF123456");
|
|
|
|
|
|
2026-11-04 13:33:30 +01:00
|
|
|
var result = await _handler.Handle(new Enable2faCommand(user.Id, "Current-password-123!"), CancellationToken.None);
|
2026-08-11 21:35:52 +02:00
|
|
|
|
|
|
|
|
result.IsSuccess.ShouldBeTrue();
|
|
|
|
|
result.Data.ShouldNotBeNull();
|
2026-11-04 13:33:30 +01:00
|
|
|
result.Data!.SharedKey.ShouldBe("ABCDEF123456");
|
|
|
|
|
_recoveryCodes.Count().ShouldBe(0);
|
|
|
|
|
await _userManager.DidNotReceive().SetTwoFactorEnabledAsync(user, true);
|
2026-08-11 21:35:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Enable2fa_with_unknown_user_should_return_not_found()
|
|
|
|
|
{
|
2026-11-04 13:33:30 +01:00
|
|
|
_userManager.FindByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>()).Returns((IdentityAccount?)null);
|
2026-08-11 21:35:52 +02:00
|
|
|
|
2026-11-04 13:33:30 +01:00
|
|
|
var result = await _handler.Handle(new Enable2faCommand(Guid.NewGuid(), "Current-password-123!"), CancellationToken.None);
|
2026-08-11 21:35:52 +02:00
|
|
|
|
|
|
|
|
result.IsSuccess.ShouldBeFalse();
|
|
|
|
|
result.StatusCode.ShouldBe(404);
|
|
|
|
|
_recoveryCodes.Count().ShouldBe(0);
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-04 13:33:30 +01:00
|
|
|
[Fact]
|
|
|
|
|
public async Task Enable2fa_with_wrong_current_password_should_not_rotate_authenticator_key()
|
|
|
|
|
{
|
|
|
|
|
var user = new IdentityAccount { Id = Guid.NewGuid(), UserName = "test@test.com", Email = "test@test.com" };
|
|
|
|
|
_userManager.FindByIdAsync(user.Id, Arg.Any<CancellationToken>()).Returns(user);
|
|
|
|
|
_userManager.CheckPasswordAsync(user, "wrong").Returns(false);
|
|
|
|
|
|
|
|
|
|
var result = await _handler.Handle(new Enable2faCommand(user.Id, "wrong"), CancellationToken.None);
|
|
|
|
|
|
|
|
|
|
result.IsSuccess.ShouldBeFalse();
|
|
|
|
|
result.StatusCode.ShouldBe(400);
|
|
|
|
|
await _userManager.DidNotReceive().ResetAuthenticatorKeyAsync(user);
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-11 21:35:52 +02:00
|
|
|
[Fact]
|
|
|
|
|
public async Task Enable2fa_when_identity_fails_should_return_failure()
|
|
|
|
|
{
|
2026-11-04 13:33:30 +01:00
|
|
|
var user = new IdentityAccount { Id = Guid.NewGuid(), UserName = "test@test.com", Email = "test@test.com" };
|
|
|
|
|
_userManager.FindByIdAsync(user.Id, Arg.Any<CancellationToken>()).Returns(user);
|
|
|
|
|
_userManager.CheckPasswordAsync(user, "Current-password-123!").Returns(true);
|
|
|
|
|
_userManager.ResetAuthenticatorKeyAsync(user)
|
|
|
|
|
.Returns(new IdentityOperationResult(false, ["Failure"]));
|
2026-08-11 21:35:52 +02:00
|
|
|
|
2026-11-04 13:33:30 +01:00
|
|
|
var result = await _handler.Handle(new Enable2faCommand(user.Id, "Current-password-123!"), CancellationToken.None);
|
2026-08-11 21:35:52 +02:00
|
|
|
|
|
|
|
|
result.IsSuccess.ShouldBeFalse();
|
|
|
|
|
_recoveryCodes.Count().ShouldBe(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|