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 _userManager; private readonly IJwtService _jwtService; private readonly IRolePermissionService _rolePermissionService; private readonly IApplicationDbContext _db; private readonly ILogger _logger; private readonly Verify2faHandler _handler; private readonly InMemoryDbSet _recoveryCodes; public Verify2faHandlerTests() { var userStore = Substitute.For>(); var identityOptions = Microsoft.Extensions.Options.Options.Create(new IdentityOptions()); _userManager = Substitute.For>(userStore, identityOptions, null!, null!, null!, null!, null!, null!, null!); _jwtService = Substitute.For(); _rolePermissionService = Substitute.For(); _db = Substitute.For(); _logger = Substitute.For>(); _recoveryCodes = (InMemoryDbSet)MockDbSetHelper.CreateFor(); _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!); } }