From e0f9d99b4934f0778462e01101d26c99d12f2584 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rui=20Tom=C3=A9?= <108268980+r-tome@users.noreply.github.com> Date: Wed, 31 Aug 2022 16:03:13 +0100 Subject: [PATCH] [EC-495] Updated GroupService.SaveAsync to check if collections are supplied and prevent deleting Group-to-Collection associations. Added unit test. (#2234) --- src/Core/Services/Implementations/GroupService.cs | 11 ++++++++++- test/Core.Test/Services/GroupServiceTests.cs | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/Core/Services/Implementations/GroupService.cs b/src/Core/Services/Implementations/GroupService.cs index c637fd0ce..d5f0ebb94 100644 --- a/src/Core/Services/Implementations/GroupService.cs +++ b/src/Core/Services/Implementations/GroupService.cs @@ -61,7 +61,16 @@ public class GroupService : IGroupService else { group.RevisionDate = DateTime.UtcNow; - await _groupRepository.ReplaceAsync(group, collections ?? new List()); + + if (collections == null) + { + await _groupRepository.ReplaceAsync(group); + } + else + { + await _groupRepository.ReplaceAsync(group, collections); + } + await _eventService.LogGroupEventAsync(group, Enums.EventType.Group_Updated); } } diff --git a/test/Core.Test/Services/GroupServiceTests.cs b/test/Core.Test/Services/GroupServiceTests.cs index 80660af17..45cb01ff7 100644 --- a/test/Core.Test/Services/GroupServiceTests.cs +++ b/test/Core.Test/Services/GroupServiceTests.cs @@ -64,6 +64,20 @@ public class GroupServiceTests Assert.True(group.RevisionDate - DateTime.UtcNow < TimeSpan.FromSeconds(1)); } + [Theory, BitAutoData] + public async Task SaveAsync_NonDefaultGroupId_ReplaceGroupInRepository_NoCollections(Group group, Organization organization, SutProvider sutProvider) + { + organization.UseGroups = true; + sutProvider.GetDependency().GetByIdAsync(organization.Id).Returns(organization); + + await sutProvider.Sut.SaveAsync(group, null); + + await sutProvider.GetDependency().Received().ReplaceAsync(group); + await sutProvider.GetDependency().Received() + .LogGroupEventAsync(group, EventType.Group_Updated); + Assert.True(group.RevisionDate - DateTime.UtcNow < TimeSpan.FromSeconds(1)); + } + [Theory, BitAutoData] public async Task SaveAsync_NonExistingOrganizationId_ThrowsBadRequest(Group group, SutProvider sutProvider) {