Path: redb-identity/tests/redb.Identity.Tests/Routes/MfaSetupProcessorTests.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/MfaSetupProcessorTests.cs ---- using AwesomeAssertions; using Microsoft.AspNetCore.DataProtection; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging.Abstractions; using NSubstitute; using OtpNet; using redb.Core; using redb.Core.Models.Entities; using redb.Identity.Contracts.Common; using redb.Identity.Contracts.Mfa; 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 : /// All 5 MFA management operations (status, setup, confirm, disable, regenerate-recovery). /// public sealed class MfaSetupProcessorTests { private readonly IRedbService _redb = Substitute.For(); private readonly MfaSecretProtector _secretProtector; private readonly MfaService _mfaService; private readonly MfaSetupProcessor _sut; public MfaSetupProcessorTests() { var dpProvider = DataProtectionProvider.Create("redb-mfa-processor-tests"); _secretProtector = new MfaSecretProtector(dpProvider); var 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); // Build a minimal IServiceProvider that returns MfaService from scopes var sp = BuildServiceProvider(_mfaService); _sut = new MfaSetupProcessor(sp); } private static IServiceProvider BuildServiceProvider(MfaService mfaService) { var scopedSp = Substitute.For(); scopedSp.GetService(typeof(MfaService)).Returns(mfaService); 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 TestExchange CreateExchange(string operation, IDictionary? body = null) { var exchange = new TestExchange { Pattern = redb.Route.Abstractions.ExchangePattern.InOut }; exchange.In.Headers["operation"] = operation; exchange.In.Body = body; return exchange; } // ── Status ── [Fact] public async Task Status_MissingUserId_ReturnsError() { MockRedbQuery.Setup(_redb, new List>()); var exchange = CreateExchange("status", new Dictionary { }); await _sut.Process(exchange); exchange.Out!.Body.Should().BeOfType().Subject.Error.Should().Be("invalid_request"); } [Fact] public async Task Status_ValidUserId_ReturnsStatus() { var obj = new RedbObject(new MfaProps { Enabled = true, TotpConfirmed = true, RecoveryCodes = new List { "a", "b" } }) { Id = 100 }; obj.key = 42; MockRedbQuery.Setup(_redb, new List> { obj }); var exchange = CreateExchange("status", new Dictionary { ["userId"] = 42L }); await _sut.Process(exchange); var result = exchange.Out!.Body.Should().BeOfType().Subject; result.Enabled.Should().BeTrue(); result.Methods.Should().Contain("totp"); result.RecoveryCodesRemaining.Should().Be(2); } // ── Setup ── [Fact] public async Task Setup_ValidRequest_ReturnsSecretAndQrUri() { MockRedbQuery.Setup(_redb, new List>()); var exchange = CreateExchange("setup", new Dictionary { ["userId"] = 10L, ["username"] = "bob" }); await _sut.Process(exchange); var result = exchange.Out!.Body.Should().BeOfType().Subject; result.Method.Should().Be("totp"); result.SecretBase32.Should().NotBeNull(); result.QrUri.Should().Contain("bob"); } [Fact] public async Task Setup_MissingUserId_ReturnsError() { var exchange = CreateExchange("setup", new Dictionary { ["username"] = "bob" }); await _sut.Process(exchange); exchange.Out!.Body.Should().BeOfType().Subject.Error.Should().Be("invalid_request"); } // ── Confirm ── [Fact] public async Task Confirm_ValidCode_ReturnsConfirmedWithRecoveryCodes() { // First setup — issues setup_token, no DB write. MockRedbQuery.Setup(_redb, new List>()); var setupExchange = CreateExchange("setup", new Dictionary { ["userId"] = 10L, ["username"] = "alice" }); await _sut.Process(setupExchange); var setupResult = setupExchange.Out!.Body.Should().BeOfType().Subject; var secret = setupResult.SecretBase32!; var setupToken = setupResult.SetupToken!; setupToken.Should().NotBeNullOrEmpty(); // Confirm path will create the props row on demand. MockRedbQuery.Setup(_redb, new List>()); var code = GenerateValidCode(secret); var confirmExchange = CreateExchange("confirm", new Dictionary { ["userId"] = 10L, ["code"] = code, ["setup_token"] = setupToken }); await _sut.Process(confirmExchange); var result = confirmExchange.Out!.Body.Should().BeOfType().Subject; result.Confirmed.Should().BeTrue(); result.RecoveryCodes.Should().HaveCount(10); } [Fact] public async Task Confirm_InvalidCode_ReturnsError() { MockRedbQuery.Setup(_redb, new List>()); var setupExchange = CreateExchange("setup", new Dictionary { ["userId"] = 10L, ["username"] = "alice" }); await _sut.Process(setupExchange); var setupToken = setupExchange.Out!.Body.Should().BeOfType().Subject.SetupToken!; MockRedbQuery.Setup(_redb, new List>()); var exchange = CreateExchange("confirm", new Dictionary { ["userId"] = 10L, ["code"] = "000000", ["setup_token"] = setupToken }); await _sut.Process(exchange); exchange.Out!.Body.Should().BeOfType().Subject.Error.Should().Be("invalid_code"); } [Fact] public async Task Confirm_MissingSetupToken_ReturnsError() { var exchange = CreateExchange("confirm", new Dictionary { ["userId"] = 10L, ["code"] = "123456" // setup_token deliberately missing }); await _sut.Process(exchange); exchange.Out!.Body.Should().BeOfType().Subject.Error.Should().Be("invalid_request"); } [Fact] public async Task Confirm_MissingCode_ReturnsError() { var exchange = CreateExchange("confirm", new Dictionary { ["userId"] = 10L }); await _sut.Process(exchange); exchange.Out!.Body.Should().BeOfType().Subject.Error.Should().Be("invalid_request"); } // ── Disable ── [Fact] public async Task Disable_ValidRequest_ReturnsDisabled() { var obj = new RedbObject(new MfaProps { Enabled = true, TotpConfirmed = true, TotpSecret = "enc" }) { Id = 100 }; obj.key = 10; MockRedbQuery.Setup(_redb, new List> { obj }); var exchange = CreateExchange("disable", new Dictionary { ["userId"] = 10L }); await _sut.Process(exchange); exchange.Out!.Body.Should().BeOfType().Subject.Disabled.Should().BeTrue(); } // ── Regenerate Recovery ── [Fact] public async Task RegenerateRecovery_MfaEnabled_ReturnsCodes() { var obj = new RedbObject(new MfaProps { Enabled = true, TotpConfirmed = true, RecoveryCodes = new List { "old" } }) { Id = 100 }; obj.key = 10; MockRedbQuery.Setup(_redb, new List> { obj }); var exchange = CreateExchange("regenerate-recovery", new Dictionary { ["userId"] = 10L }); await _sut.Process(exchange); exchange.Out!.Body.Should().BeOfType().Subject.RecoveryCodes.Should().HaveCount(10); } [Fact] public async Task RegenerateRecovery_MfaNotEnabled_ReturnsError() { var obj = new RedbObject(new MfaProps { Enabled = false }) { Id = 100 }; obj.key = 10; MockRedbQuery.Setup(_redb, new List> { obj }); var exchange = CreateExchange("regenerate-recovery", new Dictionary { ["userId"] = 10L }); await _sut.Process(exchange); exchange.Out!.Body.Should().BeOfType().Subject.Error.Should().Be("mfa_not_enabled"); } // ── Invalid operation ── [Fact] public async Task UnknownOperation_ReturnsError() { var exchange = CreateExchange("nonexistent"); await _sut.Process(exchange); exchange.Out!.Body.Should().BeOfType().Subject.Error.Should().Be("invalid_operation"); } [Fact] public async Task MissingOperationHeader_Throws() { var exchange = new TestExchange(); // no operation header var act = () => _sut.Process(exchange); await act.Should().ThrowAsync(); } // ── GetUserId type handling ── [Theory] [InlineData(42L)] // long [InlineData(42)] // int public async Task GetUserId_HandlesNumericTypes(object userId) { MockRedbQuery.Setup(_redb, new List>()); var exchange = CreateExchange("status", new Dictionary { ["userId"] = userId }); await _sut.Process(exchange); // Not an error: the userId was parsed. exchange.Out!.Body.Should().BeOfType(); } [Fact] public async Task GetUserId_HandlesStringType() { MockRedbQuery.Setup(_redb, new List>()); var exchange = CreateExchange("status", new Dictionary { ["userId"] = "42" }); await _sut.Process(exchange); exchange.Out!.Body.Should().BeOfType(); } private static string GenerateValidCode(string base32Secret) { var key = Base32Encoding.ToBytes(base32Secret); var totp = new Totp(key, step: 30, totpSize: 6); return totp.ComputeTotp(); } }