1
0
mirror of https://github.com/bitwarden/server.git synced 2025-06-15 23:40:48 -05:00

Added tests at controller level.

This commit is contained in:
Todd Martin 2025-06-14 11:47:26 -04:00
parent 019acbf859
commit 7dcd68bb2b
No known key found for this signature in database
GPG Key ID: 663E7AF5C839BC8F
2 changed files with 40 additions and 71 deletions

View File

@ -594,6 +594,46 @@ public class AccountsControllerTests : IDisposable
Assert.Equal(model.VerifyDevices, user.VerifyDevices); Assert.Equal(model.VerifyDevices, user.VerifyDevices);
} }
[Theory]
[BitAutoData]
public async Task ResendNewDeviceVerificationEmail_WhenUserNotFound_ShouldFail(
UnauthenticatedSecretVerificationRequestModel model)
{
// Arrange
_userService.GetUserByPrincipalAsync(Arg.Any<ClaimsPrincipal>()).Returns(Task.FromResult((User)null));
// Act & Assert
await Assert.ThrowsAsync<UnauthorizedAccessException>(() => _sut.ResendNewDeviceOtpAsync(model));
}
[Theory, BitAutoData]
public async Task ResendNewDeviceVerificationEmail_WHenSecretNotValid_ShouldFail(
User user,
UnauthenticatedSecretVerificationRequestModel model)
{
// Arrange
_userService.GetUserByPrincipalAsync(Arg.Any<ClaimsPrincipal>()).Returns(Task.FromResult(user));
_userService.VerifySecretAsync(user, Arg.Any<string>()).Returns(Task.FromResult(false));
// Act & Assert
await Assert.ThrowsAsync<BadRequestException>(() => _sut.ResendNewDeviceOtpAsync(model));
}
[Theory, BitAutoData]
public async Task ResendNewDeviceVerificationEmail_WhenTokenValid_SendsEmail(User user,
UnauthenticatedSecretVerificationRequestModel model)
{
// Arrange
_userService.GetUserByPrincipalAsync(Arg.Any<ClaimsPrincipal>()).Returns(Task.FromResult(user));
_userService.VerifySecretAsync(user, Arg.Any<string>()).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 // Below are helper functions that currently belong to this
// test class, but ultimately may need to be split out into // test class, but ultimately may need to be split out into
// something greater in order to share common test steps with // something greater in order to share common test steps with

View File

@ -251,75 +251,4 @@ public class TwoFactorEmailServiceTests
.Received(1) .Received(1)
.SendTwoFactorEmailAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(), "Unknown Browser", Arg.Any<TwoFactorEmailPurpose>()); .SendTwoFactorEmailAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(), "Unknown Browser", Arg.Any<TwoFactorEmailPurpose>());
} }
// [Theory, BitAutoData]
// public async Task ResendNewDeviceVerificationEmail_UserNull_SendTwoFactorEmailAsyncNotCalled(
// SutProvider<UserService> sutProvider, string email, string secret)
// {
// sutProvider.GetDependency<IUserRepository>()
// .GetByEmailAsync(email)
// .Returns(null as User);
// await sutProvider.Sut.ResendNewDeviceVerificationEmail(email, secret);
// await sutProvider.GetDependency<IMailService>()
// .DidNotReceive()
// .SendTwoFactorEmailAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<bool>());
// }
// [Theory, BitAutoData]
// public async Task ResendNewDeviceVerificationEmail_SecretNotValid_SendTwoFactorEmailAsyncNotCalled(
// SutProvider<UserService> sutProvider, string email, string secret)
// {
// sutProvider.GetDependency<IUserRepository>()
// .GetByEmailAsync(email)
// .Returns(null as User);
// await sutProvider.Sut.ResendNewDeviceVerificationEmail(email, secret);
// await sutProvider.GetDependency<IMailService>()
// .DidNotReceive()
// .SendTwoFactorEmailAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<bool>());
// }
// [Theory, BitAutoData]
// public async Task ResendNewDeviceVerificationEmail_SendsToken_Success(User user)
// {
// // Arrange
// var testPassword = "test_password";
// SetupUserAndDevice(user, true);
// var sutProvider = new SutProvider<TwoFactorEmailService>();
// // Setup the fake password verification
// sutProvider
// .GetDependency<IUserPasswordStore<User>>()
// .GetPasswordHashAsync(user, Arg.Any<CancellationToken>())
// .Returns((ci) =>
// {
// return Task.FromResult("hashed_test_password");
// });
// sutProvider.GetDependency<IPasswordHasher<User>>()
// .VerifyHashedPassword(user, "hashed_test_password", testPassword)
// .Returns((ci) =>
// {
// return PasswordVerificationResult.Success;
// });
// sutProvider.GetDependency<IUserRepository>()
// .GetByEmailAsync(user.Email)
// .Returns(user);
// var context = sutProvider.GetDependency<ICurrentContext>();
// context.DeviceType = DeviceType.Android;
// context.IpAddress = "1.1.1.1";
// await sutProvider.Sut.ResendNewDeviceVerificationEmail(user.Email, testPassword);
// await sutProvider.GetDependency<IMailService>()
// .Received(1)
// .SendTwoFactorEmailAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<bool>());
// }
} }