mirror of
https://github.com/bitwarden/server.git
synced 2024-11-30 13:33:24 +01:00
9560a32495
* Adding API endpoint to send out Access Request for SM to Admins, adding email template * Fixing email template HTML, adding tests * fixing tests * fixing lint * Moving files to proper locations * fixing build error relating to not removing some old code * Updating namespaces and removing unused using statements * Dependency injection fix * Fixing tests and moving them to proper files * lint * format fixes * dotnet format fix * small fixes * removing using directive's that aren't needed * Update bitwarden_license/test/Commercial.Core.Test/SecretsManager/Commands/PasswordManager/RequestSMAccessCommandTests.cs Co-authored-by: Thomas Avery <43214426+Thomas-Avery@users.noreply.github.com> * Update src/Core/MailTemplates/Handlebars/SecretsManagerAccessRequest.text.hbs Co-authored-by: Thomas Avery <43214426+Thomas-Avery@users.noreply.github.com> * Update bitwarden_license/src/Commercial.Core/SecretsManager/Commands/PasswordManager/RequestSMAccessCommand.cs Co-authored-by: Thomas Avery <43214426+Thomas-Avery@users.noreply.github.com> * Changes requested by Thomas * Lint fixes * Suggested changes from Maceij * Current state of tests * Fixing tests and getting the core.csproj file from main * Reverting csproj file change * Removing usings directory * dotnet format * Fixing test * Update bitwarden_license/test/Commercial.Core.Test/SecretsManager/Commands/Requests/RequestSMAccessCommandTests.cs Co-authored-by: Thomas Avery <43214426+Thomas-Avery@users.noreply.github.com> * Update test/Api.Test/SecretsManager/Controllers/RequestSMAccessControllerTests.cs Co-authored-by: Thomas Avery <43214426+Thomas-Avery@users.noreply.github.com> * Thomas requested changes * Fixing 500 error when user name is null * Prettier error message if user sends over an whitespace string * Fixing word wrapping issue in email contents --------- Co-authored-by: Thomas Avery <43214426+Thomas-Avery@users.noreply.github.com>
87 lines
4.2 KiB
C#
87 lines
4.2 KiB
C#
using System.Security.Claims;
|
|
using Bit.Api.SecretsManager.Controllers;
|
|
using Bit.Api.SecretsManager.Models.Request;
|
|
using Bit.Core.Context;
|
|
using Bit.Core.Entities;
|
|
using Bit.Core.Exceptions;
|
|
using Bit.Core.Models.Data.Organizations.OrganizationUsers;
|
|
using Bit.Core.Repositories;
|
|
using Bit.Core.SecretsManager.Commands.Requests.Interfaces;
|
|
using Bit.Core.Services;
|
|
using Bit.Test.Common.AutoFixture;
|
|
using Bit.Test.Common.AutoFixture.Attributes;
|
|
using NSubstitute;
|
|
using NSubstitute.ReturnsExtensions;
|
|
using Xunit;
|
|
|
|
namespace Bit.Api.Test.SecretsManager.Controllers;
|
|
|
|
[ControllerCustomize(typeof(RequestSMAccessController))]
|
|
[SutProviderCustomize]
|
|
public class RequestSMAccessControllerTests
|
|
{
|
|
[Theory]
|
|
[BitAutoData]
|
|
public async Task RequestSMAccessFromAdmins_WhenSendingNoModel_ShouldThrowNotFoundException(
|
|
User user, SutProvider<RequestSMAccessController> sutProvider)
|
|
{
|
|
sutProvider.GetDependency<IUserService>().GetUserByPrincipalAsync(Arg.Any<ClaimsPrincipal>()).Returns(user);
|
|
sutProvider.GetDependency<IOrganizationRepository>().GetByIdentifierAsync(Arg.Any<string>()).ReturnsNullForAnyArgs();
|
|
|
|
await Assert.ThrowsAsync<NotFoundException>(() => sutProvider.Sut.RequestSMAccessFromAdmins(new RequestSMAccessRequestModel()));
|
|
}
|
|
|
|
[Theory]
|
|
[BitAutoData]
|
|
public async Task RequestSMAccessFromAdmins_WhenSendingValidData_ShouldSucceed(
|
|
User user,
|
|
RequestSMAccessRequestModel model,
|
|
Core.AdminConsole.Entities.Organization org,
|
|
ICollection<OrganizationUserUserDetails> orgUsers,
|
|
SutProvider<RequestSMAccessController> sutProvider)
|
|
{
|
|
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(model.OrganizationId).Returns(org);
|
|
sutProvider.GetDependency<IUserService>().GetUserByPrincipalAsync(Arg.Any<ClaimsPrincipal>()).Returns(user);
|
|
sutProvider.GetDependency<IOrganizationUserRepository>().GetManyDetailsByOrganizationAsync(org.Id).Returns(orgUsers);
|
|
sutProvider.GetDependency<ICurrentContext>().OrganizationUser(model.OrganizationId).Returns(true);
|
|
|
|
await sutProvider.Sut.RequestSMAccessFromAdmins(model);
|
|
|
|
//Also check that the command was called
|
|
await sutProvider.GetDependency<IRequestSMAccessCommand>()
|
|
.Received(1)
|
|
.SendRequestAccessToSM(org, orgUsers, user, model.EmailContent);
|
|
}
|
|
|
|
[Theory]
|
|
[BitAutoData]
|
|
public async Task RequestSMAccessFromAdmins_WhenUserInvalid_ShouldThrowBadRequestException(RequestSMAccessRequestModel model, SutProvider<RequestSMAccessController> sutProvider)
|
|
{
|
|
sutProvider.GetDependency<IUserService>().GetUserByPrincipalAsync(Arg.Any<ClaimsPrincipal>()).ReturnsNullForAnyArgs();
|
|
|
|
await Assert.ThrowsAsync<UnauthorizedAccessException>(() => sutProvider.Sut.RequestSMAccessFromAdmins(model));
|
|
}
|
|
|
|
[Theory]
|
|
[BitAutoData]
|
|
public async Task RequestSMAccessFromAdmins_WhenOrgInvalid_ShouldThrowNotFoundException(RequestSMAccessRequestModel model, User user, SutProvider<RequestSMAccessController> sutProvider)
|
|
{
|
|
sutProvider.GetDependency<IOrganizationRepository>().GetByIdentifierAsync(Arg.Any<string>()).ReturnsNullForAnyArgs();
|
|
sutProvider.GetDependency<IUserService>().GetUserByPrincipalAsync(Arg.Any<ClaimsPrincipal>()).ReturnsForAnyArgs(user);
|
|
sutProvider.GetDependency<ICurrentContext>().OrganizationUser(model.OrganizationId).Returns(true);
|
|
|
|
await Assert.ThrowsAsync<NotFoundException>(() => sutProvider.Sut.RequestSMAccessFromAdmins(model));
|
|
}
|
|
|
|
[Theory]
|
|
[BitAutoData]
|
|
public async Task RequestSMAccessFromAdmins_WhenOrgUserInvalid_ShouldThrowNotFoundException(RequestSMAccessRequestModel model, User user, SutProvider<RequestSMAccessController> sutProvider)
|
|
{
|
|
sutProvider.GetDependency<IOrganizationRepository>().GetByIdentifierAsync(Arg.Any<string>()).ReturnsNullForAnyArgs();
|
|
sutProvider.GetDependency<IUserService>().GetUserByPrincipalAsync(Arg.Any<ClaimsPrincipal>()).ReturnsForAnyArgs(user);
|
|
sutProvider.GetDependency<ICurrentContext>().OrganizationUser(model.OrganizationId).Returns(false);
|
|
|
|
await Assert.ThrowsAsync<NotFoundException>(() => sutProvider.Sut.RequestSMAccessFromAdmins(model));
|
|
}
|
|
}
|