mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 04:53:11 +00:00
test(auth): move the password-policy reset case onto hashed, attempt-counted codes
This commit is contained in:
parent
9bd22e9a74
commit
ea1370847f
1 changed files with 18 additions and 5 deletions
|
|
@ -9,6 +9,7 @@ using Moq;
|
||||||
using SeaHaven.DataServices.Interfaces;
|
using SeaHaven.DataServices.Interfaces;
|
||||||
using SeaHaven.Services.Configuration;
|
using SeaHaven.Services.Configuration;
|
||||||
using SeaHaven.Services.DTOs;
|
using SeaHaven.Services.DTOs;
|
||||||
|
using SeaHaven.Services.Helpers;
|
||||||
using SeaHaven.Services.Implementation;
|
using SeaHaven.Services.Implementation;
|
||||||
using SeaHaven.Services.Interfaces;
|
using SeaHaven.Services.Interfaces;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
@ -132,7 +133,8 @@ public sealed class PasswordPolicyTests : IAsyncDisposable
|
||||||
Microsoft.Extensions.Options.Options.Create(new JwtOptions { Secret = new string('x', 64) }),
|
Microsoft.Extensions.Options.Options.Create(new JwtOptions { Secret = new string('x', 64) }),
|
||||||
Mock.Of<IUserDataService>(),
|
Mock.Of<IUserDataService>(),
|
||||||
Mock.Of<IForgetPasswordDataService>(),
|
Mock.Of<IForgetPasswordDataService>(),
|
||||||
Mock.Of<IEmailSender>());
|
Mock.Of<IEmailSender>(),
|
||||||
|
TimeProvider.System);
|
||||||
|
|
||||||
var result = await service.ChangePasswordAsync(user.Id, CurrentPassword, "Next2@x", CancellationToken.None);
|
var result = await service.ChangePasswordAsync(user.Id, CurrentPassword, "Next2@x", CancellationToken.None);
|
||||||
|
|
||||||
|
|
@ -169,10 +171,19 @@ public sealed class PasswordPolicyTests : IAsyncDisposable
|
||||||
{
|
{
|
||||||
var user = await CreateUserAsync();
|
var user = await CreateUserAsync();
|
||||||
var forget = new Mock<IForgetPasswordDataService>();
|
var forget = new Mock<IForgetPasswordDataService>();
|
||||||
forget.Setup(f => f.ExistsByEmailAndCodeAsync(user.Email!, "123456", It.IsAny<CancellationToken>()))
|
var salt = PasswordResetCodeSecrets.NewSalt();
|
||||||
.ReturnsAsync(true);
|
|
||||||
forget.Setup(f => f.GetByEmailAsync(user.Email!, It.IsAny<CancellationToken>()))
|
forget.Setup(f => f.GetByEmailAsync(user.Email!, It.IsAny<CancellationToken>()))
|
||||||
.ReturnsAsync(new ForgetPasswordCode { Email = user.Email!, UserId = user.Id, Code = "123456" });
|
.ReturnsAsync(new ForgetPasswordCode
|
||||||
|
{
|
||||||
|
Id = 7,
|
||||||
|
Email = user.Email!,
|
||||||
|
UserId = user.Id,
|
||||||
|
CodeSalt = salt,
|
||||||
|
CodeHash = PasswordResetCodeSecrets.Hash(salt, "123456"),
|
||||||
|
ExpiresAtUtc = DateTime.UtcNow.AddMinutes(10)
|
||||||
|
});
|
||||||
|
forget.Setup(f => f.TryConsumeAttemptAsync(7, It.IsAny<int>(), It.IsAny<DateTime>(), It.IsAny<CancellationToken>()))
|
||||||
|
.ReturnsAsync(true);
|
||||||
var service = NewAuthenticationService(forget);
|
var service = NewAuthenticationService(forget);
|
||||||
|
|
||||||
var reset = await service.ResetPasswordAsync(user.Email!, "123456", "abcdef", CancellationToken.None);
|
var reset = await service.ResetPasswordAsync(user.Email!, "123456", "abcdef", CancellationToken.None);
|
||||||
|
|
@ -180,6 +191,7 @@ public sealed class PasswordPolicyTests : IAsyncDisposable
|
||||||
reset.Should().BeFalse();
|
reset.Should().BeFalse();
|
||||||
(await UserManager.CheckPasswordAsync(user, CurrentPassword)).Should().BeTrue();
|
(await UserManager.CheckPasswordAsync(user, CurrentPassword)).Should().BeTrue();
|
||||||
forget.Verify(f => f.RemoveByEmailAsync(It.IsAny<string>(), It.IsAny<CancellationToken>()), Times.Never);
|
forget.Verify(f => f.RemoveByEmailAsync(It.IsAny<string>(), It.IsAny<CancellationToken>()), Times.Never);
|
||||||
|
forget.Verify(f => f.RefundAttemptAsync(7, It.IsAny<CancellationToken>()), Times.Once);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<IReadOnlyList<IdentityError>> ValidateAsync(ApplicationUser user, string password)
|
private async Task<IReadOnlyList<IdentityError>> ValidateAsync(ApplicationUser user, string password)
|
||||||
|
|
@ -208,7 +220,8 @@ public sealed class PasswordPolicyTests : IAsyncDisposable
|
||||||
Microsoft.Extensions.Options.Options.Create(new JwtOptions { Secret = new string('x', 64) }),
|
Microsoft.Extensions.Options.Options.Create(new JwtOptions { Secret = new string('x', 64) }),
|
||||||
Mock.Of<IUserDataService>(),
|
Mock.Of<IUserDataService>(),
|
||||||
(forget ?? new Mock<IForgetPasswordDataService>()).Object,
|
(forget ?? new Mock<IForgetPasswordDataService>()).Object,
|
||||||
Mock.Of<IEmailSender>());
|
Mock.Of<IEmailSender>(),
|
||||||
|
TimeProvider.System);
|
||||||
|
|
||||||
public async ValueTask DisposeAsync()
|
public async ValueTask DisposeAsync()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue