using System.Security.Claims; using Bit.Api.SecretsManager.Controllers; using Bit.Core.Exceptions; using Bit.Core.Models.Data; using Bit.Core.Repositories; using Bit.Core.SecretsManager.Entities; using Bit.Core.SecretsManager.Repositories; using Bit.Test.Common.AutoFixture; using Bit.Test.Common.AutoFixture.Attributes; using Microsoft.AspNetCore.Authorization; using NSubstitute; using Xunit; namespace Bit.Api.Test.SecretsManager.Controllers; [ControllerCustomize(typeof(SecretsManagerEventsController))] [SutProviderCustomize] [JsonDocumentCustomize] public class SecretsManagerEventsControllerTests { [Theory] [BitAutoData] public async Task GetServiceAccountEvents_NoAccess_Throws(SutProvider sutProvider, ServiceAccount data) { sutProvider.GetDependency().GetByIdAsync(default).ReturnsForAnyArgs(data); sutProvider.GetDependency() .AuthorizeAsync(Arg.Any(), data, Arg.Any>()).ReturnsForAnyArgs(AuthorizationResult.Failed()); await Assert.ThrowsAsync(() => sutProvider.Sut.GetServiceAccountEventsAsync(data.Id)); await sutProvider.GetDependency().DidNotReceiveWithAnyArgs() .GetManyByOrganizationServiceAccountAsync(Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any()); } [Theory] [BitAutoData] public async Task GetServiceAccountEvents_DateRangeOver_Throws( SutProvider sutProvider, ServiceAccount data) { sutProvider.GetDependency().GetByIdAsync(default).ReturnsForAnyArgs(data); sutProvider.GetDependency() .AuthorizeAsync(Arg.Any(), data, Arg.Any>()).ReturnsForAnyArgs(AuthorizationResult.Success()); var start = DateTime.UtcNow.AddYears(-1); var end = DateTime.UtcNow.AddYears(1); await Assert.ThrowsAsync(() => sutProvider.Sut.GetServiceAccountEventsAsync(data.Id, start, end)); await sutProvider.GetDependency().DidNotReceiveWithAnyArgs() .GetManyByOrganizationServiceAccountAsync(Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any()); } [Theory] [BitAutoData] public async Task GetServiceAccountEvents_Success(SutProvider sutProvider, ServiceAccount data) { sutProvider.GetDependency().GetByIdAsync(default).ReturnsForAnyArgs(data); sutProvider.GetDependency() .AuthorizeAsync(Arg.Any(), data, Arg.Any>()).ReturnsForAnyArgs(AuthorizationResult.Success()); sutProvider.GetDependency() .GetManyByOrganizationServiceAccountAsync(default, default, default, default, default) .ReturnsForAnyArgs(new PagedResult()); await sutProvider.Sut.GetServiceAccountEventsAsync(data.Id); await sutProvider.GetDependency().Received(1) .GetManyByOrganizationServiceAccountAsync(data.OrganizationId, data.Id, Arg.Any(), Arg.Any(), Arg.Any()); } }