From 7dcd68bb2b1619c194c56ac0d22590bdd9e1e515 Mon Sep 17 00:00:00 2001 From: Todd Martin Date: Sat, 14 Jun 2025 11:47:26 -0400 Subject: [PATCH] Added tests at controller level. --- .../Controllers/AccountsControllerTests.cs | 40 +++++++++++ .../Services/TwoFactorEmailServiceTests.cs | 71 ------------------- 2 files changed, 40 insertions(+), 71 deletions(-) diff --git a/test/Api.Test/Auth/Controllers/AccountsControllerTests.cs b/test/Api.Test/Auth/Controllers/AccountsControllerTests.cs index 3d21c9095b..20c21d7ff7 100644 --- a/test/Api.Test/Auth/Controllers/AccountsControllerTests.cs +++ b/test/Api.Test/Auth/Controllers/AccountsControllerTests.cs @@ -594,6 +594,46 @@ public class AccountsControllerTests : IDisposable Assert.Equal(model.VerifyDevices, user.VerifyDevices); } + [Theory] + [BitAutoData] + public async Task ResendNewDeviceVerificationEmail_WhenUserNotFound_ShouldFail( + UnauthenticatedSecretVerificationRequestModel model) + { + // Arrange + _userService.GetUserByPrincipalAsync(Arg.Any()).Returns(Task.FromResult((User)null)); + + // Act & Assert + await Assert.ThrowsAsync(() => _sut.ResendNewDeviceOtpAsync(model)); + } + + [Theory, BitAutoData] + public async Task ResendNewDeviceVerificationEmail_WHenSecretNotValid_ShouldFail( + User user, + UnauthenticatedSecretVerificationRequestModel model) + { + // Arrange + _userService.GetUserByPrincipalAsync(Arg.Any()).Returns(Task.FromResult(user)); + _userService.VerifySecretAsync(user, Arg.Any()).Returns(Task.FromResult(false)); + + // Act & Assert + await Assert.ThrowsAsync(() => _sut.ResendNewDeviceOtpAsync(model)); + } + + [Theory, BitAutoData] + public async Task ResendNewDeviceVerificationEmail_WhenTokenValid_SendsEmail(User user, + UnauthenticatedSecretVerificationRequestModel model) + { + // Arrange + _userService.GetUserByPrincipalAsync(Arg.Any()).Returns(Task.FromResult(user)); + _userService.VerifySecretAsync(user, Arg.Any()).Returns(Task.FromResult(true)); + + // Act + await _sut.ResendNewDeviceOtpAsync(model); + + // Assert + await _twoFactorEmailService.Received(1).SendNewDeviceVerificationEmailAsync(user); + } + // Below are helper functions that currently belong to this // test class, but ultimately may need to be split out into // something greater in order to share common test steps with diff --git a/test/Core.Test/Auth/Services/TwoFactorEmailServiceTests.cs b/test/Core.Test/Auth/Services/TwoFactorEmailServiceTests.cs index 4be79a5bac..2a76f4d90c 100644 --- a/test/Core.Test/Auth/Services/TwoFactorEmailServiceTests.cs +++ b/test/Core.Test/Auth/Services/TwoFactorEmailServiceTests.cs @@ -251,75 +251,4 @@ public class TwoFactorEmailServiceTests .Received(1) .SendTwoFactorEmailAsync(Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), "Unknown Browser", Arg.Any()); } - - // [Theory, BitAutoData] - // public async Task ResendNewDeviceVerificationEmail_UserNull_SendTwoFactorEmailAsyncNotCalled( - // SutProvider sutProvider, string email, string secret) - // { - // sutProvider.GetDependency() - // .GetByEmailAsync(email) - // .Returns(null as User); - - // await sutProvider.Sut.ResendNewDeviceVerificationEmail(email, secret); - - // await sutProvider.GetDependency() - // .DidNotReceive() - // .SendTwoFactorEmailAsync(Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any()); - // } - - // [Theory, BitAutoData] - // public async Task ResendNewDeviceVerificationEmail_SecretNotValid_SendTwoFactorEmailAsyncNotCalled( - // SutProvider sutProvider, string email, string secret) - // { - // sutProvider.GetDependency() - // .GetByEmailAsync(email) - // .Returns(null as User); - - // await sutProvider.Sut.ResendNewDeviceVerificationEmail(email, secret); - - // await sutProvider.GetDependency() - // .DidNotReceive() - // .SendTwoFactorEmailAsync(Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any()); - // } - - // [Theory, BitAutoData] - // public async Task ResendNewDeviceVerificationEmail_SendsToken_Success(User user) - // { - // // Arrange - // var testPassword = "test_password"; - // SetupUserAndDevice(user, true); - - // var sutProvider = new SutProvider(); - - // // Setup the fake password verification - // sutProvider - // .GetDependency>() - // .GetPasswordHashAsync(user, Arg.Any()) - // .Returns((ci) => - // { - // return Task.FromResult("hashed_test_password"); - // }); - - // sutProvider.GetDependency>() - // .VerifyHashedPassword(user, "hashed_test_password", testPassword) - // .Returns((ci) => - // { - // return PasswordVerificationResult.Success; - // }); - - // sutProvider.GetDependency() - // .GetByEmailAsync(user.Email) - // .Returns(user); - - // var context = sutProvider.GetDependency(); - // context.DeviceType = DeviceType.Android; - // context.IpAddress = "1.1.1.1"; - - // await sutProvider.Sut.ResendNewDeviceVerificationEmail(user.Email, testPassword); - - // await sutProvider.GetDependency() - // .Received(1) - // .SendTwoFactorEmailAsync(Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any()); - - // } }