Path: redb-identity/tests/redb.Identity.Tests/Routes/MfaRecoveryProcessorTests.cs Snapshot: redb-identity, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-identity Page: https://redbase.app/src/dev-2026-12-06/redb-identity/tests/redb.Identity.Tests/Routes/MfaRecoveryProcessorTests.cs ---- using AwesomeAssertions; using Microsoft.AspNetCore.DataProtection; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging.Abstractions; using NSubstitute; using redb.Core; using redb.Core.Models.Entities; using redb.Identity.Core.Models; using redb.Identity.Core.Routes.Processors; using redb.Identity.Core.Services; using redb.Identity.Tests.Infrastructure; using Xunit; namespace redb.Identity.Tests.Routes; /// /// Unit tests for : /// Recovery code verification during login — state decryption, one-time code consumption. /// public sealed class MfaRecoveryProcessorTests { private readonly IRedbService _redb = Substitute.For(); private readonly MfaStateProtector _stateProtector; private readonly MfaService _mfaService; private readonly MfaRecoveryProcessor _sut; public MfaRecoveryProcessorTests() { var dpProvider = DataProtectionProvider.Create("redb-mfa-recovery-tests"); var secretProtector = new MfaSecretProtector(dpProvider); _stateProtector = new MfaStateProtector(dpProvider); var totpMethod = new TotpMfaMethod(secretProtector); _mfaService = new MfaService( _redb, new IMfaMethod[] { totpMethod }, Array.Empty(), _stateProtector, new MfaSetupTokenProtector(dpProvider), Microsoft.Extensions.Options.Options.Create(new redb.Identity.Core.Configuration.RedbIdentityOptions()), RecoveryCodePepperProvider.ForTesting(), NullLogger.Instance); // B1-recovery: processor now wraps the verify in BeginTransactionAsync + LockForUpdateAsync. // Wire no-op tx + no-op lock on the substitute so the processor path executes to completion. var tx = Substitute.For(); tx.CommitAsync().Returns(Task.CompletedTask); var ctx = Substitute.For(); ctx.BeginTransactionAsync().Returns(Task.FromResult(tx)); _redb.Context.Returns(ctx); // LockForUpdateAsync returns Task (locked-row count) and, since the // CancellationToken waves, has a second (long[], ct) overload — stub both. _redb.LockForUpdateAsync(Arg.Any()).Returns(0); _redb.LockForUpdateAsync(Arg.Any(), Arg.Any()).Returns(0); var sp = BuildServiceProvider(_mfaService, _redb); _sut = new MfaRecoveryProcessor(sp); } private static IServiceProvider BuildServiceProvider(MfaService mfaService, IRedbService redb) { var scopedSp = Substitute.For(); scopedSp.GetService(typeof(MfaService)).Returns(mfaService); scopedSp.GetService(typeof(IRedbService)).Returns(redb); var scope = Substitute.For(); scope.ServiceProvider.Returns(scopedSp); var scopeFactory = Substitute.For(); scopeFactory.CreateScope().Returns(scope); var sp = Substitute.For(); sp.GetService(typeof(IServiceScopeFactory)).Returns(scopeFactory); return sp; } private static string HashCode(string code) { var normalized = code.Replace("-", "", StringComparison.Ordinal).ToUpperInvariant(); var bytes = System.Security.Cryptography.SHA256.HashData( System.Text.Encoding.UTF8.GetBytes(normalized)); return Convert.ToHexStringLower(bytes); } [Fact] public async Task Process_ValidRecoveryCode_ReturnsSuccessAndConsumesCode() { var plainCode = "ABCD-EF23"; var hash = HashCode(plainCode); var obj = new RedbObject(new MfaProps { Enabled = true, TotpConfirmed = true, RecoveryCodes = new List { hash, "other-hash" } }) { Id = 100 }; obj.key = 42; MockRedbQuery.Setup(_redb, new List> { obj }); var state = new MfaState { UserId = 42, Username = "alice", Methods = ["totp"], ReturnUrl = "/cb" }; var mfaToken = _stateProtector.Protect(state); var exchange = new TestExchange(); exchange.In.Body = new Dictionary { ["mfa_state"] = mfaToken, ["recovery_code"] = plainCode }; await _sut.Process(exchange); var result = exchange.Out!.Body.Should().BeAssignableTo>().Subject; result["success"].Should().Be(true); result["userId"].Should().Be(42L); result["username"].Should().Be("alice"); result["returnUrl"].Should().Be("/cb"); // Code should be consumed obj.Props.RecoveryCodes.Should().HaveCount(1); obj.Props.RecoveryCodes.Should().NotContain(hash); // Event exchange.Properties["identity-event-type"].Should().Be("UserLoggedIn"); } [Fact] public async Task Process_InvalidRecoveryCode_ReturnsErrorWithState() { var obj = new RedbObject(new MfaProps { Enabled = true, RecoveryCodes = new List { "some-hash" } }) { Id = 100 }; obj.key = 42; MockRedbQuery.Setup(_redb, new List> { obj }); var state = new MfaState { UserId = 42, Username = "alice", Methods = ["totp"] }; var mfaToken = _stateProtector.Protect(state); var exchange = new TestExchange(); exchange.In.Body = new Dictionary { ["mfa_state"] = mfaToken, ["recovery_code"] = "WRONG-CODE" }; await _sut.Process(exchange); var result = exchange.Out!.Body.Should().BeAssignableTo>().Subject; result["success"].Should().Be(false); result["error"].Should().Be("invalid_code"); result["mfa_state"].Should().Be(mfaToken); } [Fact] public async Task Process_MissingMfaState_ReturnsInvalidRequest() { var exchange = new TestExchange(); exchange.In.Body = new Dictionary { ["recovery_code"] = "ABCD-EF23" }; await _sut.Process(exchange); var result = exchange.Out!.Body.Should().BeAssignableTo>().Subject; result["error"].Should().Be("invalid_request"); } [Fact] public async Task Process_MissingRecoveryCode_ReturnsInvalidRequest() { var state = new MfaState { UserId = 42, Username = "alice" }; var mfaToken = _stateProtector.Protect(state); var exchange = new TestExchange(); exchange.In.Body = new Dictionary { ["mfa_state"] = mfaToken }; await _sut.Process(exchange); var result = exchange.Out!.Body.Should().BeAssignableTo>().Subject; result["error"].Should().Be("invalid_request"); } [Fact] public async Task Process_ExpiredState_ReturnsInvalidGrant() { var state = new MfaState { UserId = 42, Username = "alice", IssuedAt = DateTimeOffset.UtcNow.AddMinutes(-10) }; var mfaToken = _stateProtector.Protect(state); var exchange = new TestExchange(); exchange.In.Body = new Dictionary { ["mfa_state"] = mfaToken, ["recovery_code"] = "ABCD-EF23" }; await _sut.Process(exchange); var result = exchange.Out!.Body.Should().BeAssignableTo>().Subject; result["error"].Should().Be("invalid_grant"); } [Fact] public async Task Process_TamperedState_ReturnsInvalidGrant() { var exchange = new TestExchange(); exchange.In.Body = new Dictionary { ["mfa_state"] = "garbage-token", ["recovery_code"] = "ABCD-EF23" }; await _sut.Process(exchange); var result = exchange.Out!.Body.Should().BeAssignableTo>().Subject; result["error"].Should().Be("invalid_grant"); } [Fact] public async Task Process_NullBody_ReturnsInvalidRequest() { var exchange = new TestExchange(); exchange.In.Body = null; await _sut.Process(exchange); var result = exchange.Out!.Body.Should().BeAssignableTo>().Subject; result["error"].Should().Be("invalid_request"); } }