using Bit.Api.SecretsManager.Controllers; using Bit.Api.SecretsManager.Models.Request; using Bit.Core.Context; using Bit.Core.Exceptions; using Bit.Core.SecretsManager.Commands.Secrets.Interfaces; using Bit.Core.SecretsManager.Entities; using Bit.Core.SecretsManager.Repositories; using Bit.Core.Test.SecretsManager.AutoFixture.SecretsFixture; using Bit.Test.Common.AutoFixture; using Bit.Test.Common.AutoFixture.Attributes; using Bit.Test.Common.Helpers; using NSubstitute; using Xunit; namespace Bit.Api.Test.SecretsManager.Controllers; [ControllerCustomize(typeof(SecretsController))] [SutProviderCustomize] [JsonDocumentCustomize] [SecretCustomize] public class SecretsControllerTests { [Theory] [BitAutoData] public async void GetSecretsByOrganization_ReturnsEmptyList(SutProvider sutProvider, Guid id) { sutProvider.GetDependency().AccessSecretsManager(id).Returns(true); var result = await sutProvider.Sut.ListByOrganizationAsync(id); await sutProvider.GetDependency().Received(1) .GetManyByOrganizationIdAsync(Arg.Is(AssertHelper.AssertPropertyEqual(id))); Assert.Empty(result.Secrets); } [Theory] [BitAutoData] public async void GetSecretsByOrganization_Success(SutProvider sutProvider, Secret resultSecret) { sutProvider.GetDependency().AccessSecretsManager(default).ReturnsForAnyArgs(true); sutProvider.GetDependency().GetManyByOrganizationIdAsync(default).ReturnsForAnyArgs(new List { resultSecret }); var result = await sutProvider.Sut.ListByOrganizationAsync(resultSecret.OrganizationId); await sutProvider.GetDependency().Received(1) .GetManyByOrganizationIdAsync(Arg.Is(AssertHelper.AssertPropertyEqual(resultSecret.OrganizationId))); } [Theory] [BitAutoData] public async void GetSecretsByOrganization_AccessDenied_Throws(SutProvider sutProvider, Secret resultSecret) { sutProvider.GetDependency().AccessSecretsManager(default).ReturnsForAnyArgs(false); await Assert.ThrowsAsync(() => sutProvider.Sut.ListByOrganizationAsync(resultSecret.OrganizationId)); } [Theory] [BitAutoData] public async void GetSecret_NotFound(SutProvider sutProvider) { await Assert.ThrowsAsync(() => sutProvider.Sut.GetAsync(Guid.NewGuid())); } [Theory] [BitAutoData] public async void GetSecret_Success(SutProvider sutProvider, Secret resultSecret) { sutProvider.GetDependency().GetByIdAsync(default).ReturnsForAnyArgs(resultSecret); var result = await sutProvider.Sut.GetAsync(resultSecret.Id); await sutProvider.GetDependency().Received(1) .GetByIdAsync(Arg.Is(AssertHelper.AssertPropertyEqual(resultSecret.Id))); } [Theory] [BitAutoData] public async void CreateSecret_Success(SutProvider sutProvider, SecretCreateRequestModel data, Guid organizationId) { var resultSecret = data.ToSecret(organizationId); sutProvider.GetDependency().AccessSecretsManager(organizationId).Returns(true); sutProvider.GetDependency().CreateAsync(default).ReturnsForAnyArgs(resultSecret); var result = await sutProvider.Sut.CreateAsync(organizationId, data); await sutProvider.GetDependency().Received(1) .CreateAsync(Arg.Any()); } [Theory] [BitAutoData] public async void UpdateSecret_Success(SutProvider sutProvider, SecretUpdateRequestModel data, Guid secretId) { var resultSecret = data.ToSecret(secretId); sutProvider.GetDependency().UpdateAsync(default).ReturnsForAnyArgs(resultSecret); var result = await sutProvider.Sut.UpdateAsync(secretId, data); await sutProvider.GetDependency().Received(1) .UpdateAsync(Arg.Any()); } [Theory] [BitAutoData] public async void BulkDeleteSecret_Success(SutProvider sutProvider, List data) { var ids = data.Select(secret => secret.Id).ToList(); var mockResult = new List>(); foreach (var secret in data) { mockResult.Add(new Tuple(secret, "")); } sutProvider.GetDependency().DeleteSecrets(ids).ReturnsForAnyArgs(mockResult); var results = await sutProvider.Sut.BulkDeleteAsync(ids); await sutProvider.GetDependency().Received(1) .DeleteSecrets(Arg.Is(ids)); Assert.Equal(data.Count, results.Data.Count()); } [Theory] [BitAutoData] public async void BulkDeleteSecret_NoGuids_ThrowsArgumentNullException(SutProvider sutProvider) { await Assert.ThrowsAsync(() => sutProvider.Sut.BulkDeleteAsync(new List())); } }