2022-12-12 10:59:48 +01:00
|
|
|
|
using Bit.Api.Controllers;
|
|
|
|
|
using Bit.Api.Models.Request;
|
|
|
|
|
using Bit.Core.Context;
|
|
|
|
|
using Bit.Core.Entities;
|
|
|
|
|
using Bit.Core.Models.Data;
|
|
|
|
|
using Bit.Core.OrganizationFeatures.Groups.Interfaces;
|
|
|
|
|
using Bit.Core.Repositories;
|
|
|
|
|
using Bit.Test.Common.AutoFixture;
|
|
|
|
|
using Bit.Test.Common.AutoFixture.Attributes;
|
|
|
|
|
using NSubstitute;
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
|
|
namespace Bit.Api.Test.Controllers;
|
|
|
|
|
|
|
|
|
|
[ControllerCustomize(typeof(GroupsController))]
|
|
|
|
|
[SutProviderCustomize]
|
|
|
|
|
public class GroupsControllerTests
|
|
|
|
|
{
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public async Task Post_Success(Organization organization, GroupRequestModel groupRequestModel, SutProvider<GroupsController> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(organization.Id).Returns(organization);
|
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().ManageGroups(organization.Id).Returns(true);
|
|
|
|
|
|
|
|
|
|
var response = await sutProvider.Sut.Post(organization.Id.ToString(), groupRequestModel);
|
|
|
|
|
|
|
|
|
|
await sutProvider.GetDependency<ICurrentContext>().Received(1).ManageGroups(organization.Id);
|
|
|
|
|
await sutProvider.GetDependency<ICreateGroupCommand>().Received(1).CreateGroupAsync(
|
|
|
|
|
Arg.Is<Group>(g =>
|
|
|
|
|
g.OrganizationId == organization.Id && g.Name == groupRequestModel.Name &&
|
2023-02-06 10:27:40 +01:00
|
|
|
|
g.AccessAll == groupRequestModel.AccessAll),
|
2022-12-12 10:59:48 +01:00
|
|
|
|
organization,
|
2023-01-19 17:00:54 +01:00
|
|
|
|
Arg.Any<IEnumerable<CollectionAccessSelection>>(),
|
|
|
|
|
Arg.Any<IEnumerable<Guid>>());
|
2022-12-12 10:59:48 +01:00
|
|
|
|
Assert.Equal(groupRequestModel.Name, response.Name);
|
2023-07-14 17:18:26 +02:00
|
|
|
|
Assert.Equal(organization.Id, response.OrganizationId);
|
2022-12-12 10:59:48 +01:00
|
|
|
|
Assert.Equal(groupRequestModel.AccessAll, response.AccessAll);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public async Task Put_Success(Organization organization, Group group, GroupRequestModel groupRequestModel, SutProvider<GroupsController> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
group.OrganizationId = organization.Id;
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(organization.Id).Returns(organization);
|
|
|
|
|
sutProvider.GetDependency<IGroupRepository>().GetByIdAsync(group.Id).Returns(group);
|
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().ManageGroups(organization.Id).Returns(true);
|
|
|
|
|
|
|
|
|
|
var response = await sutProvider.Sut.Put(organization.Id.ToString(), group.Id.ToString(), groupRequestModel);
|
|
|
|
|
|
|
|
|
|
await sutProvider.GetDependency<ICurrentContext>().Received(1).ManageGroups(organization.Id);
|
|
|
|
|
await sutProvider.GetDependency<IUpdateGroupCommand>().Received(1).UpdateGroupAsync(
|
|
|
|
|
Arg.Is<Group>(g =>
|
|
|
|
|
g.OrganizationId == organization.Id && g.Name == groupRequestModel.Name &&
|
2023-02-06 10:27:40 +01:00
|
|
|
|
g.AccessAll == groupRequestModel.AccessAll),
|
2022-12-12 10:59:48 +01:00
|
|
|
|
Arg.Is<Organization>(o => o.Id == organization.Id),
|
2023-01-19 17:00:54 +01:00
|
|
|
|
Arg.Any<IEnumerable<CollectionAccessSelection>>(),
|
|
|
|
|
Arg.Any<IEnumerable<Guid>>());
|
2022-12-12 10:59:48 +01:00
|
|
|
|
Assert.Equal(groupRequestModel.Name, response.Name);
|
2023-07-14 17:18:26 +02:00
|
|
|
|
Assert.Equal(organization.Id, response.OrganizationId);
|
2022-12-12 10:59:48 +01:00
|
|
|
|
Assert.Equal(groupRequestModel.AccessAll, response.AccessAll);
|
|
|
|
|
}
|
|
|
|
|
}
|