2023-10-18 17:27:56 +02:00
|
|
|
|
using Bit.Api.AdminConsole.Public.Controllers;
|
|
|
|
|
using Bit.Api.AdminConsole.Public.Models.Request;
|
|
|
|
|
using Bit.Api.AdminConsole.Public.Models.Response;
|
2023-10-19 22:37:46 +02:00
|
|
|
|
using Bit.Core.AdminConsole.Entities;
|
|
|
|
|
using Bit.Core.AdminConsole.OrganizationFeatures.Groups.Interfaces;
|
|
|
|
|
using Bit.Core.AdminConsole.Repositories;
|
2022-12-12 10:59:48 +01:00
|
|
|
|
using Bit.Core.Context;
|
|
|
|
|
using Bit.Core.Models.Data;
|
|
|
|
|
using Bit.Core.Repositories;
|
|
|
|
|
using Bit.Test.Common.AutoFixture;
|
|
|
|
|
using Bit.Test.Common.AutoFixture.Attributes;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using NSubstitute;
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
2023-10-18 17:27:56 +02:00
|
|
|
|
namespace Bit.Api.Test.AdminConsole.Public.Controllers;
|
2022-12-12 10:59:48 +01:00
|
|
|
|
|
|
|
|
|
[ControllerCustomize(typeof(GroupsController))]
|
|
|
|
|
[SutProviderCustomize]
|
|
|
|
|
public class GroupsControllerTests
|
|
|
|
|
{
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
2024-06-04 00:58:44 +02:00
|
|
|
|
public async Task Post_Success(Organization organization, GroupCreateUpdateRequestModel groupRequestModel, SutProvider<GroupsController> sutProvider)
|
2024-02-08 14:44:36 +01:00
|
|
|
|
{
|
|
|
|
|
// Contains at least one can manage
|
|
|
|
|
groupRequestModel.Collections.First().Manage = true;
|
|
|
|
|
|
2022-12-12 10:59:48 +01:00
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().OrganizationId.Returns(organization.Id);
|
|
|
|
|
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(organization.Id).Returns(organization);
|
|
|
|
|
|
|
|
|
|
var response = await sutProvider.Sut.Post(groupRequestModel) as JsonResult;
|
|
|
|
|
var responseValue = response.Value as GroupResponseModel;
|
|
|
|
|
|
|
|
|
|
await sutProvider.GetDependency<ICreateGroupCommand>().Received(1).CreateGroupAsync(
|
|
|
|
|
Arg.Is<Group>(g =>
|
|
|
|
|
g.OrganizationId == organization.Id && g.Name == groupRequestModel.Name &&
|
2024-06-04 00:58:44 +02:00
|
|
|
|
g.ExternalId == groupRequestModel.ExternalId),
|
2022-12-12 10:59:48 +01:00
|
|
|
|
organization,
|
2024-02-08 14:44:36 +01:00
|
|
|
|
Arg.Any<ICollection<CollectionAccessSelection>>());
|
2022-12-12 10:59:48 +01:00
|
|
|
|
|
|
|
|
|
Assert.Equal(groupRequestModel.Name, responseValue.Name);
|
|
|
|
|
Assert.Equal(groupRequestModel.ExternalId, responseValue.ExternalId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
2024-06-04 00:58:44 +02:00
|
|
|
|
public async Task Put_Success(Organization organization, Group group, GroupCreateUpdateRequestModel groupRequestModel, SutProvider<GroupsController> sutProvider)
|
2022-12-12 10:59:48 +01:00
|
|
|
|
{
|
2024-02-08 14:44:36 +01:00
|
|
|
|
// Contains at least one can manage
|
|
|
|
|
groupRequestModel.Collections.First().Manage = true;
|
|
|
|
|
|
2022-12-12 10:59:48 +01:00
|
|
|
|
group.OrganizationId = organization.Id;
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(organization.Id).Returns(organization);
|
|
|
|
|
sutProvider.GetDependency<IGroupRepository>().GetByIdAsync(group.Id).Returns(group);
|
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().OrganizationId.Returns(organization.Id);
|
|
|
|
|
|
|
|
|
|
var response = await sutProvider.Sut.Put(group.Id, groupRequestModel) as JsonResult;
|
|
|
|
|
var responseValue = response.Value as GroupResponseModel;
|
|
|
|
|
|
|
|
|
|
await sutProvider.GetDependency<IUpdateGroupCommand>().Received(1).UpdateGroupAsync(
|
|
|
|
|
Arg.Is<Group>(g =>
|
|
|
|
|
g.OrganizationId == organization.Id && g.Name == groupRequestModel.Name &&
|
2024-06-04 00:58:44 +02:00
|
|
|
|
g.ExternalId == groupRequestModel.ExternalId),
|
2022-12-12 10:59:48 +01:00
|
|
|
|
Arg.Is<Organization>(o => o.Id == organization.Id),
|
2024-02-08 14:44:36 +01:00
|
|
|
|
Arg.Any<ICollection<CollectionAccessSelection>>());
|
2022-12-12 10:59:48 +01:00
|
|
|
|
|
|
|
|
|
Assert.Equal(groupRequestModel.Name, responseValue.Name);
|
|
|
|
|
Assert.Equal(groupRequestModel.ExternalId, responseValue.ExternalId);
|
|
|
|
|
}
|
|
|
|
|
}
|