2023-12-05 17:21:46 +01:00
|
|
|
|
using Bit.Core;
|
|
|
|
|
using Bit.Core.Auth.Models.Api.Request.Accounts;
|
2023-11-20 15:55:31 +01:00
|
|
|
|
using Bit.Core.Auth.Models.Business.Tokenables;
|
2023-04-14 19:25:56 +02:00
|
|
|
|
using Bit.Core.Auth.Services;
|
2024-06-19 19:54:20 +02:00
|
|
|
|
using Bit.Core.Auth.UserFeatures.Registration;
|
2023-12-14 09:35:52 +01:00
|
|
|
|
using Bit.Core.Auth.UserFeatures.WebAuthnLogin;
|
2024-06-19 19:54:20 +02:00
|
|
|
|
using Bit.Core.Context;
|
2023-04-14 19:25:56 +02:00
|
|
|
|
using Bit.Core.Entities;
|
2022-01-17 13:21:51 +01:00
|
|
|
|
using Bit.Core.Enums;
|
|
|
|
|
using Bit.Core.Exceptions;
|
|
|
|
|
using Bit.Core.Models.Data;
|
|
|
|
|
using Bit.Core.Repositories;
|
|
|
|
|
using Bit.Core.Services;
|
2023-11-20 15:55:31 +01:00
|
|
|
|
using Bit.Core.Tokens;
|
2024-06-19 19:54:20 +02:00
|
|
|
|
using Bit.Core.Tools.Enums;
|
|
|
|
|
using Bit.Core.Tools.Models.Business;
|
|
|
|
|
using Bit.Core.Tools.Services;
|
2022-01-17 13:21:51 +01:00
|
|
|
|
using Bit.Identity.Controllers;
|
2024-06-19 19:54:20 +02:00
|
|
|
|
using Bit.Test.Common.AutoFixture.Attributes;
|
2022-01-17 13:21:51 +01:00
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
2024-06-19 19:54:20 +02:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2022-01-17 13:21:51 +01:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using NSubstitute;
|
2024-06-19 19:54:20 +02:00
|
|
|
|
using NSubstitute.ReturnsExtensions;
|
2022-01-17 13:21:51 +01:00
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
|
|
namespace Bit.Identity.Test.Controllers;
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-01-17 13:21:51 +01:00
|
|
|
|
public class AccountsControllerTests : IDisposable
|
|
|
|
|
{
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-01-17 13:21:51 +01:00
|
|
|
|
private readonly AccountsController _sut;
|
2024-06-19 19:54:20 +02:00
|
|
|
|
private readonly ICurrentContext _currentContext;
|
2022-01-17 13:21:51 +01:00
|
|
|
|
private readonly ILogger<AccountsController> _logger;
|
|
|
|
|
private readonly IUserRepository _userRepository;
|
|
|
|
|
private readonly IUserService _userService;
|
2022-09-15 16:02:37 +02:00
|
|
|
|
private readonly ICaptchaValidationService _captchaValidationService;
|
2023-11-20 15:55:31 +01:00
|
|
|
|
private readonly IDataProtectorTokenFactory<WebAuthnLoginAssertionOptionsTokenable> _assertionOptionsDataProtector;
|
2023-12-14 09:35:52 +01:00
|
|
|
|
private readonly IGetWebAuthnLoginCredentialAssertionOptionsCommand _getWebAuthnLoginCredentialAssertionOptionsCommand;
|
2024-06-19 19:54:20 +02:00
|
|
|
|
private readonly ISendVerificationEmailForRegistrationCommand _sendVerificationEmailForRegistrationCommand;
|
|
|
|
|
private readonly IReferenceEventService _referenceEventService;
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-01-17 13:21:51 +01:00
|
|
|
|
public AccountsControllerTests()
|
|
|
|
|
{
|
2024-06-19 19:54:20 +02:00
|
|
|
|
_currentContext = Substitute.For<ICurrentContext>();
|
2022-01-17 13:21:51 +01:00
|
|
|
|
_logger = Substitute.For<ILogger<AccountsController>>();
|
|
|
|
|
_userRepository = Substitute.For<IUserRepository>();
|
|
|
|
|
_userService = Substitute.For<IUserService>();
|
2022-09-15 16:02:37 +02:00
|
|
|
|
_captchaValidationService = Substitute.For<ICaptchaValidationService>();
|
2023-11-20 15:55:31 +01:00
|
|
|
|
_assertionOptionsDataProtector = Substitute.For<IDataProtectorTokenFactory<WebAuthnLoginAssertionOptionsTokenable>>();
|
2023-12-14 09:35:52 +01:00
|
|
|
|
_getWebAuthnLoginCredentialAssertionOptionsCommand = Substitute.For<IGetWebAuthnLoginCredentialAssertionOptionsCommand>();
|
2024-06-19 19:54:20 +02:00
|
|
|
|
_sendVerificationEmailForRegistrationCommand = Substitute.For<ISendVerificationEmailForRegistrationCommand>();
|
|
|
|
|
_referenceEventService = Substitute.For<IReferenceEventService>();
|
2022-01-17 13:21:51 +01:00
|
|
|
|
_sut = new AccountsController(
|
2024-06-19 19:54:20 +02:00
|
|
|
|
_currentContext,
|
2022-08-29 22:06:55 +02:00
|
|
|
|
_logger,
|
2022-01-17 13:21:51 +01:00
|
|
|
|
_userRepository,
|
2022-09-15 16:02:37 +02:00
|
|
|
|
_userService,
|
2023-11-20 15:55:31 +01:00
|
|
|
|
_captchaValidationService,
|
2023-12-14 09:35:52 +01:00
|
|
|
|
_assertionOptionsDataProtector,
|
2024-06-19 19:54:20 +02:00
|
|
|
|
_getWebAuthnLoginCredentialAssertionOptionsCommand,
|
|
|
|
|
_sendVerificationEmailForRegistrationCommand,
|
|
|
|
|
_referenceEventService
|
2022-08-29 22:06:55 +02:00
|
|
|
|
);
|
|
|
|
|
}
|
2022-01-17 13:21:51 +01:00
|
|
|
|
|
|
|
|
|
public void Dispose()
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-01-17 13:21:51 +01:00
|
|
|
|
_sut?.Dispose();
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
2022-01-17 13:21:51 +01:00
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task PostPrelogin_WhenUserExists_ShouldReturnUserKdfInfo()
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-01-17 13:21:51 +01:00
|
|
|
|
var userKdfInfo = new UserKdfInformation
|
2022-08-29 21:53:48 +02:00
|
|
|
|
{
|
2022-01-17 13:21:51 +01:00
|
|
|
|
Kdf = KdfType.PBKDF2_SHA256,
|
2024-01-25 16:59:53 +01:00
|
|
|
|
KdfIterations = AuthConstants.PBKDF2_ITERATIONS.Default
|
2022-08-29 22:06:55 +02:00
|
|
|
|
};
|
2022-01-17 13:21:51 +01:00
|
|
|
|
_userRepository.GetKdfInformationByEmailAsync(Arg.Any<string>()).Returns(Task.FromResult(userKdfInfo));
|
|
|
|
|
|
|
|
|
|
var response = await _sut.PostPrelogin(new PreloginRequestModel { Email = "user@example.com" });
|
2022-08-29 20:53:16 +02:00
|
|
|
|
|
2022-01-17 13:21:51 +01:00
|
|
|
|
Assert.Equal(userKdfInfo.Kdf, response.Kdf);
|
|
|
|
|
Assert.Equal(userKdfInfo.KdfIterations, response.KdfIterations);
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
[Fact]
|
2023-12-05 17:21:46 +01:00
|
|
|
|
public async Task PostPrelogin_WhenUserDoesNotExist_ShouldDefaultToPBKDF()
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-01-17 13:21:51 +01:00
|
|
|
|
_userRepository.GetKdfInformationByEmailAsync(Arg.Any<string>()).Returns(Task.FromResult<UserKdfInformation>(null!));
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-01-17 13:21:51 +01:00
|
|
|
|
var response = await _sut.PostPrelogin(new PreloginRequestModel { Email = "user@example.com" });
|
2022-08-29 20:53:16 +02:00
|
|
|
|
|
2022-01-17 13:21:51 +01:00
|
|
|
|
Assert.Equal(KdfType.PBKDF2_SHA256, response.Kdf);
|
2023-12-05 17:21:46 +01:00
|
|
|
|
Assert.Equal(AuthConstants.PBKDF2_ITERATIONS.Default, response.KdfIterations);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-17 13:21:51 +01:00
|
|
|
|
[Fact]
|
|
|
|
|
public async Task PostRegister_ShouldRegisterUser()
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-01-17 13:21:51 +01:00
|
|
|
|
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",
|
2022-06-24 16:39:34 +02:00
|
|
|
|
Email = "user@example.com",
|
|
|
|
|
MasterPasswordHash = passwordHash,
|
|
|
|
|
MasterPasswordHint = "example",
|
|
|
|
|
Token = token,
|
|
|
|
|
OrganizationUserId = userGuid
|
|
|
|
|
};
|
2022-01-17 13:21:51 +01:00
|
|
|
|
|
|
|
|
|
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
|
2022-08-29 21:53:48 +02:00
|
|
|
|
{
|
2022-01-17 13:21:51 +01:00
|
|
|
|
Name = "Example User",
|
2022-06-24 16:39:34 +02:00
|
|
|
|
Email = "user@example.com",
|
|
|
|
|
MasterPasswordHash = passwordHash,
|
|
|
|
|
MasterPasswordHint = "example",
|
|
|
|
|
Token = token,
|
2022-01-17 13:21:51 +01:00
|
|
|
|
OrganizationUserId = userGuid
|
|
|
|
|
};
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-01-17 13:21:51 +01:00
|
|
|
|
await Assert.ThrowsAsync<BadRequestException>(() => _sut.PostRegister(request));
|
|
|
|
|
}
|
2024-06-19 19:54:20 +02:00
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public async Task PostRegisterSendEmailVerification_WhenTokenReturnedFromCommand_Returns200WithToken(string email, string name, bool receiveMarketingEmails)
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
var model = new RegisterSendVerificationEmailRequestModel
|
|
|
|
|
{
|
|
|
|
|
Email = email,
|
|
|
|
|
Name = name,
|
|
|
|
|
ReceiveMarketingEmails = receiveMarketingEmails
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var token = "fakeToken";
|
|
|
|
|
|
|
|
|
|
_sendVerificationEmailForRegistrationCommand.Run(email, name, receiveMarketingEmails).Returns(token);
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
var result = await _sut.PostRegisterSendVerificationEmail(model);
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
var okResult = Assert.IsType<OkObjectResult>(result);
|
|
|
|
|
Assert.Equal(200, okResult.StatusCode);
|
|
|
|
|
Assert.Equal(token, okResult.Value);
|
|
|
|
|
|
|
|
|
|
await _referenceEventService.Received(1).RaiseEventAsync(Arg.Is<ReferenceEvent>(e => e.Type == ReferenceEventType.SignupEmailSubmit));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public async Task PostRegisterSendEmailVerification_WhenNoTokenIsReturnedFromCommand_Returns204NoContent(string email, string name, bool receiveMarketingEmails)
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
var model = new RegisterSendVerificationEmailRequestModel
|
|
|
|
|
{
|
|
|
|
|
Email = email,
|
|
|
|
|
Name = name,
|
|
|
|
|
ReceiveMarketingEmails = receiveMarketingEmails
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
_sendVerificationEmailForRegistrationCommand.Run(email, name, receiveMarketingEmails).ReturnsNull();
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
var result = await _sut.PostRegisterSendVerificationEmail(model);
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
var noContentResult = Assert.IsType<NoContentResult>(result);
|
|
|
|
|
Assert.Equal(204, noContentResult.StatusCode);
|
|
|
|
|
await _referenceEventService.Received(1).RaiseEventAsync(Arg.Is<ReferenceEvent>(e => e.Type == ReferenceEventType.SignupEmailSubmit));
|
|
|
|
|
}
|
2022-01-17 13:21:51 +01:00
|
|
|
|
}
|