mirror of
https://github.com/bitwarden/server.git
synced 2025-04-06 13:38:13 -05:00

* [EC-654] Add CreateGroupCommand and UpdateGroupCommand Added new CQRS commands CreateGroupCommand and UpdateGroupCommand Updated GroupService to use new commands Edited existing GroupServiceTests and added new tests for the new commands * [EC-654] dotnet format * [EC-654] Replace GroupService.SaveAsync with CreateGroup and UpdateGroup commands * [EC-654] Add assertions to check calls on IReferenceEventService * [EC-654] Use AssertHelper.AssertRecent for DateTime properties * [EC-654] Extracted database reads from CreateGroupCommand and UpdateGroupCommand. Added unit tests. * [EC-654] Changed CreateGroupCommand and UpdateGroupCommand Validate method to private
68 lines
3.1 KiB
C#
68 lines
3.1 KiB
C#
using Bit.Api.Models.Public.Request;
|
|
using Bit.Api.Models.Public.Response;
|
|
using Bit.Api.Public.Controllers;
|
|
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 Microsoft.AspNetCore.Mvc;
|
|
using NSubstitute;
|
|
using Xunit;
|
|
|
|
namespace Bit.Api.Test.Public.Controllers;
|
|
|
|
[ControllerCustomize(typeof(GroupsController))]
|
|
[SutProviderCustomize]
|
|
public class GroupsControllerTests
|
|
{
|
|
[Theory]
|
|
[BitAutoData]
|
|
public async Task Post_Success(Organization organization, GroupCreateUpdateRequestModel groupRequestModel, SutProvider<GroupsController> sutProvider)
|
|
{
|
|
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 &&
|
|
g.AccessAll == groupRequestModel.AccessAll && g.ExternalId == groupRequestModel.ExternalId),
|
|
organization,
|
|
Arg.Any<IEnumerable<SelectionReadOnly>>());
|
|
|
|
Assert.Equal(groupRequestModel.Name, responseValue.Name);
|
|
Assert.Equal(groupRequestModel.AccessAll, responseValue.AccessAll);
|
|
Assert.Equal(groupRequestModel.ExternalId, responseValue.ExternalId);
|
|
}
|
|
|
|
[Theory]
|
|
[BitAutoData]
|
|
public async Task Put_Success(Organization organization, Group group, GroupCreateUpdateRequestModel 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>().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 &&
|
|
g.AccessAll == groupRequestModel.AccessAll && g.ExternalId == groupRequestModel.ExternalId),
|
|
Arg.Is<Organization>(o => o.Id == organization.Id),
|
|
Arg.Any<IEnumerable<SelectionReadOnly>>());
|
|
|
|
Assert.Equal(groupRequestModel.Name, responseValue.Name);
|
|
Assert.Equal(groupRequestModel.AccessAll, responseValue.AccessAll);
|
|
Assert.Equal(groupRequestModel.ExternalId, responseValue.ExternalId);
|
|
}
|
|
}
|