mirror of
https://github.com/bitwarden/server.git
synced 2025-06-30 15:42:48 -05:00
[PM-14381] Add POST /tasks/bulk-create endpoint (#5188)
* [PM-14378] Introduce GetCipherPermissionsForOrganization query for Dapper CipherRepository * [PM-14378] Introduce GetCipherPermissionsForOrganization method for Entity Framework * [PM-14378] Add integration tests for new repository method * [PM-14378] Introduce IGetCipherPermissionsForUserQuery CQRS query * [PM-14378] Introduce SecurityTaskOperationRequirement * [PM-14378] Introduce SecurityTaskAuthorizationHandler.cs * [PM-14378] Introduce SecurityTaskOrganizationAuthorizationHandler.cs * [PM-14378] Register new authorization handlers * [PM-14378] Formatting * [PM-14378] Add unit tests for GetCipherPermissionsForUserQuery * [PM-15378] Cleanup SecurityTaskAuthorizationHandler and add tests * [PM-14378] Add tests for SecurityTaskOrganizationAuthorizationHandler * [PM-14378] Formatting * [PM-14378] Update date in migration file * [PM-14378] Add missing awaits * Added bulk create request model * Created sproc to create bulk security tasks * Renamed tasks to SecurityTasksInput * Added create many implementation for sqlserver and ef core * removed trailing comma * created ef implementatin for create many and added integration test * Refactored request model * Refactored request model * created create many tasks command interface and class * added security authorization handler work temp * Added the implementation for the create manys tasks command * Added comment * Changed return to return list of created security tasks * Registered command * Completed bulk create action * Added unit tests for the command * removed hard coded table name * Fixed lint issue * Added JsonConverter attribute to allow enum value to be passed as string * Removed makshift security task operations * Fixed references * Removed old migration * Rebased * [PM-14378] Introduce GetCipherPermissionsForOrganization query for Dapper CipherRepository * [PM-14378] Introduce GetCipherPermissionsForOrganization method for Entity Framework * [PM-14378] Add unit tests for GetCipherPermissionsForUserQuery * Completed bulk create action * bumped migration version * Fixed lint issue * Removed complex sql data type in favour of json string * Register IGetTasksForOrganizationQuery * Fixed lint issue * Removed tasks grouping * Fixed linting * Removed unused code * Removed unused code * Aligned with client change * Fixed linting --------- Co-authored-by: Shane Melton <smelton@bitwarden.com>
This commit is contained in:
85
test/Core.Test/Vault/Commands/CreateManyTasksCommandTest.cs
Normal file
85
test/Core.Test/Vault/Commands/CreateManyTasksCommandTest.cs
Normal file
@ -0,0 +1,85 @@
|
||||
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<CreateManyTasksCommand> sutProvider, Guid? userId,
|
||||
bool authorizedCreate = false)
|
||||
{
|
||||
sutProvider.GetDependency<ICurrentContext>().UserId.Returns(userId);
|
||||
sutProvider.GetDependency<IAuthorizationService>()
|
||||
.AuthorizeAsync(Arg.Any<ClaimsPrincipal>(), Arg.Any<object>(),
|
||||
Arg.Is<IEnumerable<IAuthorizationRequirement>>(reqs =>
|
||||
reqs.Contains(SecurityTaskOperations.Create)))
|
||||
.Returns(authorizedCreate ? AuthorizationResult.Success() : AuthorizationResult.Failed());
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[BitAutoData]
|
||||
public async Task CreateAsync_NotLoggedIn_NotFoundException(
|
||||
SutProvider<CreateManyTasksCommand> sutProvider,
|
||||
Guid organizationId,
|
||||
IEnumerable<SecurityTaskCreateRequest> tasks)
|
||||
{
|
||||
Setup(sutProvider, null, true);
|
||||
|
||||
await Assert.ThrowsAsync<NotFoundException>(() => sutProvider.Sut.CreateAsync(organizationId, tasks));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[BitAutoData]
|
||||
public async Task CreateAsync_NoTasksProvided_BadRequestException(
|
||||
SutProvider<CreateManyTasksCommand> sutProvider,
|
||||
Guid organizationId)
|
||||
{
|
||||
Setup(sutProvider, Guid.NewGuid());
|
||||
|
||||
await Assert.ThrowsAsync<BadRequestException>(() => sutProvider.Sut.CreateAsync(organizationId, null));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[BitAutoData]
|
||||
public async Task CreateAsync_AuthorizationFailed_NotFoundException(
|
||||
SutProvider<CreateManyTasksCommand> sutProvider,
|
||||
Guid organizationId,
|
||||
IEnumerable<SecurityTaskCreateRequest> tasks)
|
||||
{
|
||||
Setup(sutProvider, Guid.NewGuid());
|
||||
|
||||
await Assert.ThrowsAsync<NotFoundException>(() => sutProvider.Sut.CreateAsync(organizationId, tasks));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[BitAutoData]
|
||||
public async Task CreateAsync_AuthorizationSucceeded_ReturnsSecurityTasks(
|
||||
SutProvider<CreateManyTasksCommand> sutProvider,
|
||||
Guid organizationId,
|
||||
IEnumerable<SecurityTaskCreateRequest> tasks,
|
||||
ICollection<SecurityTask> securityTasks)
|
||||
{
|
||||
Setup(sutProvider, Guid.NewGuid(), true);
|
||||
sutProvider.GetDependency<ISecurityTaskRepository>()
|
||||
.CreateManyAsync(Arg.Any<ICollection<SecurityTask>>())
|
||||
.Returns(securityTasks);
|
||||
|
||||
var result = await sutProvider.Sut.CreateAsync(organizationId, tasks);
|
||||
|
||||
Assert.Equal(securityTasks, result);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user