using System.Security.Claims; using Bit.Core.Context; using Bit.Core.Exceptions; using Bit.Core.Test.Vault.AutoFixture; using Bit.Core.Vault.Authorization.SecurityTasks; using Bit.Core.Vault.Commands; using Bit.Core.Vault.Entities; using Bit.Core.Vault.Models.Api; using Bit.Core.Vault.Repositories; using Bit.Test.Common.AutoFixture; using Bit.Test.Common.AutoFixture.Attributes; using Microsoft.AspNetCore.Authorization; using NSubstitute; using Xunit; namespace Bit.Core.Test.Vault.Commands; [SutProviderCustomize] [SecurityTaskCustomize] public class CreateManyTasksCommandTest { private static void Setup(SutProvider sutProvider, Guid? userId, bool authorizedCreate = false) { sutProvider.GetDependency().UserId.Returns(userId); sutProvider.GetDependency() .AuthorizeAsync(Arg.Any(), Arg.Any(), Arg.Is>(reqs => reqs.Contains(SecurityTaskOperations.Create))) .Returns(authorizedCreate ? AuthorizationResult.Success() : AuthorizationResult.Failed()); } [Theory] [BitAutoData] public async Task CreateAsync_NotLoggedIn_NotFoundException( SutProvider sutProvider, Guid organizationId, IEnumerable tasks) { Setup(sutProvider, null, true); await Assert.ThrowsAsync(() => sutProvider.Sut.CreateAsync(organizationId, tasks)); } [Theory] [BitAutoData] public async Task CreateAsync_NoTasksProvided_BadRequestException( SutProvider sutProvider, Guid organizationId) { Setup(sutProvider, Guid.NewGuid()); await Assert.ThrowsAsync(() => sutProvider.Sut.CreateAsync(organizationId, null)); } [Theory] [BitAutoData] public async Task CreateAsync_AuthorizationFailed_NotFoundException( SutProvider sutProvider, Guid organizationId, IEnumerable tasks) { Setup(sutProvider, Guid.NewGuid()); await Assert.ThrowsAsync(() => sutProvider.Sut.CreateAsync(organizationId, tasks)); } [Theory] [BitAutoData] public async Task CreateAsync_AuthorizationSucceeded_ReturnsSecurityTasks( SutProvider sutProvider, Guid organizationId, IEnumerable tasks, ICollection securityTasks) { Setup(sutProvider, Guid.NewGuid(), true); sutProvider.GetDependency() .CreateManyAsync(Arg.Any>()) .Returns(securityTasks); var result = await sutProvider.Sut.CreateAsync(organizationId, tasks); Assert.Equal(securityTasks, result); } }