mirror of
https://github.com/bitwarden/server.git
synced 2024-11-25 12:45:18 +01:00
d63c917c95
* [PM-4619] feat: scaffold new create options command * [PM-4169] feat: implement credential create options command * [PM-4619] feat: create command for credential creation * [PM-4619] feat: create assertion options command * [PM-4619] chore: clean-up unused argument * [PM-4619] feat: implement assertion command * [PM-4619] feat: migrate to commands * [PM-4619] fix: lint * [PM-4169] fix: use constant * [PM-4619] fix: lint I have no idea what this commit acutally changes, but the file seems to have some character encoding issues. This fix was generated by `dotnet format`
126 lines
4.6 KiB
C#
126 lines
4.6 KiB
C#
using Bit.Core;
|
|
using Bit.Core.Auth.Models.Api.Request.Accounts;
|
|
using Bit.Core.Auth.Models.Business.Tokenables;
|
|
using Bit.Core.Auth.Services;
|
|
using Bit.Core.Auth.UserFeatures.WebAuthnLogin;
|
|
using Bit.Core.Entities;
|
|
using Bit.Core.Enums;
|
|
using Bit.Core.Exceptions;
|
|
using Bit.Core.Models.Data;
|
|
using Bit.Core.Repositories;
|
|
using Bit.Core.Services;
|
|
using Bit.Core.Tokens;
|
|
using Bit.Identity.Controllers;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.Extensions.Logging;
|
|
using NSubstitute;
|
|
using Xunit;
|
|
|
|
namespace Bit.Identity.Test.Controllers;
|
|
|
|
public class AccountsControllerTests : IDisposable
|
|
{
|
|
|
|
private readonly AccountsController _sut;
|
|
private readonly ILogger<AccountsController> _logger;
|
|
private readonly IUserRepository _userRepository;
|
|
private readonly IUserService _userService;
|
|
private readonly ICaptchaValidationService _captchaValidationService;
|
|
private readonly IDataProtectorTokenFactory<WebAuthnLoginAssertionOptionsTokenable> _assertionOptionsDataProtector;
|
|
private readonly IGetWebAuthnLoginCredentialAssertionOptionsCommand _getWebAuthnLoginCredentialAssertionOptionsCommand;
|
|
|
|
public AccountsControllerTests()
|
|
{
|
|
_logger = Substitute.For<ILogger<AccountsController>>();
|
|
_userRepository = Substitute.For<IUserRepository>();
|
|
_userService = Substitute.For<IUserService>();
|
|
_captchaValidationService = Substitute.For<ICaptchaValidationService>();
|
|
_assertionOptionsDataProtector = Substitute.For<IDataProtectorTokenFactory<WebAuthnLoginAssertionOptionsTokenable>>();
|
|
_getWebAuthnLoginCredentialAssertionOptionsCommand = Substitute.For<IGetWebAuthnLoginCredentialAssertionOptionsCommand>();
|
|
_sut = new AccountsController(
|
|
_logger,
|
|
_userRepository,
|
|
_userService,
|
|
_captchaValidationService,
|
|
_assertionOptionsDataProtector,
|
|
_getWebAuthnLoginCredentialAssertionOptionsCommand
|
|
);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_sut?.Dispose();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task PostPrelogin_WhenUserExists_ShouldReturnUserKdfInfo()
|
|
{
|
|
var userKdfInfo = new UserKdfInformation
|
|
{
|
|
Kdf = KdfType.PBKDF2_SHA256,
|
|
KdfIterations = 5000
|
|
};
|
|
_userRepository.GetKdfInformationByEmailAsync(Arg.Any<string>()).Returns(Task.FromResult(userKdfInfo));
|
|
|
|
var response = await _sut.PostPrelogin(new PreloginRequestModel { Email = "user@example.com" });
|
|
|
|
Assert.Equal(userKdfInfo.Kdf, response.Kdf);
|
|
Assert.Equal(userKdfInfo.KdfIterations, response.KdfIterations);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task PostPrelogin_WhenUserDoesNotExist_ShouldDefaultToPBKDF()
|
|
{
|
|
_userRepository.GetKdfInformationByEmailAsync(Arg.Any<string>()).Returns(Task.FromResult<UserKdfInformation>(null!));
|
|
|
|
var response = await _sut.PostPrelogin(new PreloginRequestModel { Email = "user@example.com" });
|
|
|
|
Assert.Equal(KdfType.PBKDF2_SHA256, response.Kdf);
|
|
Assert.Equal(AuthConstants.PBKDF2_ITERATIONS.Default, response.KdfIterations);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task PostRegister_ShouldRegisterUser()
|
|
{
|
|
var passwordHash = "abcdef";
|
|
var token = "123456";
|
|
var userGuid = new Guid();
|
|
_userService.RegisterUserAsync(Arg.Any<User>(), passwordHash, token, userGuid)
|
|
.Returns(Task.FromResult(IdentityResult.Success));
|
|
var request = new RegisterRequestModel
|
|
{
|
|
Name = "Example User",
|
|
Email = "user@example.com",
|
|
MasterPasswordHash = passwordHash,
|
|
MasterPasswordHint = "example",
|
|
Token = token,
|
|
OrganizationUserId = userGuid
|
|
};
|
|
|
|
await _sut.PostRegister(request);
|
|
|
|
await _userService.Received(1).RegisterUserAsync(Arg.Any<User>(), passwordHash, token, userGuid);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task PostRegister_WhenUserServiceFails_ShouldThrowBadRequestException()
|
|
{
|
|
var passwordHash = "abcdef";
|
|
var token = "123456";
|
|
var userGuid = new Guid();
|
|
_userService.RegisterUserAsync(Arg.Any<User>(), passwordHash, token, userGuid)
|
|
.Returns(Task.FromResult(IdentityResult.Failed()));
|
|
var request = new RegisterRequestModel
|
|
{
|
|
Name = "Example User",
|
|
Email = "user@example.com",
|
|
MasterPasswordHash = passwordHash,
|
|
MasterPasswordHint = "example",
|
|
Token = token,
|
|
OrganizationUserId = userGuid
|
|
};
|
|
|
|
await Assert.ThrowsAsync<BadRequestException>(() => _sut.PostRegister(request));
|
|
}
|
|
}
|