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 sutProvider) { sutProvider.GetDependency().GetUserByPrincipalAsync(Arg.Any()).Returns(user); sutProvider.GetDependency().GetByIdentifierAsync(Arg.Any()).ReturnsNullForAnyArgs(); await Assert.ThrowsAsync(() => sutProvider.Sut.RequestSMAccessFromAdmins(new RequestSMAccessRequestModel())); } [Theory] [BitAutoData] public async Task RequestSMAccessFromAdmins_WhenSendingValidData_ShouldSucceed( User user, RequestSMAccessRequestModel model, Core.AdminConsole.Entities.Organization org, ICollection orgUsers, SutProvider sutProvider) { sutProvider.GetDependency().GetByIdAsync(model.OrganizationId).Returns(org); sutProvider.GetDependency().GetUserByPrincipalAsync(Arg.Any()).Returns(user); sutProvider.GetDependency().GetManyDetailsByOrganizationAsync(org.Id).Returns(orgUsers); sutProvider.GetDependency().OrganizationUser(model.OrganizationId).Returns(true); await sutProvider.Sut.RequestSMAccessFromAdmins(model); //Also check that the command was called await sutProvider.GetDependency() .Received(1) .SendRequestAccessToSM(org, orgUsers, user, model.EmailContent); } [Theory] [BitAutoData] public async Task RequestSMAccessFromAdmins_WhenUserInvalid_ShouldThrowBadRequestException(RequestSMAccessRequestModel model, SutProvider sutProvider) { sutProvider.GetDependency().GetUserByPrincipalAsync(Arg.Any()).ReturnsNullForAnyArgs(); await Assert.ThrowsAsync(() => sutProvider.Sut.RequestSMAccessFromAdmins(model)); } [Theory] [BitAutoData] public async Task RequestSMAccessFromAdmins_WhenOrgInvalid_ShouldThrowNotFoundException(RequestSMAccessRequestModel model, User user, SutProvider sutProvider) { sutProvider.GetDependency().GetByIdentifierAsync(Arg.Any()).ReturnsNullForAnyArgs(); sutProvider.GetDependency().GetUserByPrincipalAsync(Arg.Any()).ReturnsForAnyArgs(user); sutProvider.GetDependency().OrganizationUser(model.OrganizationId).Returns(true); await Assert.ThrowsAsync(() => sutProvider.Sut.RequestSMAccessFromAdmins(model)); } [Theory] [BitAutoData] public async Task RequestSMAccessFromAdmins_WhenOrgUserInvalid_ShouldThrowNotFoundException(RequestSMAccessRequestModel model, User user, SutProvider sutProvider) { sutProvider.GetDependency().GetByIdentifierAsync(Arg.Any()).ReturnsNullForAnyArgs(); sutProvider.GetDependency().GetUserByPrincipalAsync(Arg.Any()).ReturnsForAnyArgs(user); sutProvider.GetDependency().OrganizationUser(model.OrganizationId).Returns(false); await Assert.ThrowsAsync(() => sutProvider.Sut.RequestSMAccessFromAdmins(model)); } }