mirror of
https://github.com/bitwarden/server.git
synced 2025-06-28 06:36:15 -05:00
Add validation to CreateCollectionCommand to prevent creation of DefaultUserCollection type
* Implemented a check in CreateCollectionCommand to throw a BadRequestException if a collection of type DefaultUserCollection is attempted to be created. * Added a unit test to verify that the exception is thrown with the correct message when attempting to create a collection of this type.
This commit is contained in:
parent
136200b333
commit
4ef64db486
@ -1,4 +1,5 @@
|
|||||||
using Bit.Core.Entities;
|
using Bit.Core.Entities;
|
||||||
|
using Bit.Core.Enums;
|
||||||
using Bit.Core.Exceptions;
|
using Bit.Core.Exceptions;
|
||||||
using Bit.Core.Models.Data;
|
using Bit.Core.Models.Data;
|
||||||
using Bit.Core.OrganizationFeatures.OrganizationCollections.Interfaces;
|
using Bit.Core.OrganizationFeatures.OrganizationCollections.Interfaces;
|
||||||
@ -26,6 +27,11 @@ public class CreateCollectionCommand : ICreateCollectionCommand
|
|||||||
public async Task<Collection> CreateAsync(Collection collection, IEnumerable<CollectionAccessSelection> groups = null,
|
public async Task<Collection> CreateAsync(Collection collection, IEnumerable<CollectionAccessSelection> groups = null,
|
||||||
IEnumerable<CollectionAccessSelection> users = null)
|
IEnumerable<CollectionAccessSelection> users = null)
|
||||||
{
|
{
|
||||||
|
if (collection.Type == CollectionType.DefaultUserCollection)
|
||||||
|
{
|
||||||
|
throw new BadRequestException("You cannot create a collection with the type as DefaultUserCollection.");
|
||||||
|
}
|
||||||
|
|
||||||
var org = await _organizationRepository.GetByIdAsync(collection.OrganizationId);
|
var org = await _organizationRepository.GetByIdAsync(collection.OrganizationId);
|
||||||
if (org == null)
|
if (org == null)
|
||||||
{
|
{
|
||||||
|
@ -199,4 +199,27 @@ public class CreateCollectionCommandTests
|
|||||||
.DidNotReceiveWithAnyArgs()
|
.DidNotReceiveWithAnyArgs()
|
||||||
.LogCollectionEventAsync(default, default);
|
.LogCollectionEventAsync(default, default);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Theory, BitAutoData]
|
||||||
|
public async Task CreateAsync_WithDefaultUserCollectionType_ThrowsBadRequest(
|
||||||
|
Organization organization, Collection collection, SutProvider<CreateCollectionCommand> sutProvider)
|
||||||
|
{
|
||||||
|
collection.Id = default;
|
||||||
|
collection.Type = CollectionType.DefaultUserCollection;
|
||||||
|
sutProvider.GetDependency<IOrganizationRepository>()
|
||||||
|
.GetByIdAsync(organization.Id)
|
||||||
|
.Returns(organization);
|
||||||
|
|
||||||
|
var ex = await Assert.ThrowsAsync<BadRequestException>(() => sutProvider.Sut.CreateAsync(collection));
|
||||||
|
Assert.Contains("You cannot create a collection with the type as DefaultUserCollection.", ex.Message);
|
||||||
|
await sutProvider.GetDependency<ICollectionRepository>()
|
||||||
|
.DidNotReceiveWithAnyArgs()
|
||||||
|
.CreateAsync(default);
|
||||||
|
await sutProvider.GetDependency<ICollectionRepository>()
|
||||||
|
.DidNotReceiveWithAnyArgs()
|
||||||
|
.CreateAsync(default, default, default);
|
||||||
|
await sutProvider.GetDependency<IEventService>()
|
||||||
|
.DidNotReceiveWithAnyArgs()
|
||||||
|
.LogCollectionEventAsync(default, default);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user