using Heimdall.Application.Common; using Heimdall.Application.Groups.Commands.AddUserToGroup; using Heimdall.Application.Tests.Common; using Heimdall.Domain.Entities; using NSubstitute; using Microsoft.EntityFrameworkCore; using Heimdall.Application.Services; namespace Heimdall.Application.Tests.Handlers.Groups; public class AddUserToGroupHandlerTests { private readonly Group _group = new("Test Group", "Description"); private readonly IdentityAccount _user = new() { Id = Guid.NewGuid(), UserName = "user@test.com", Email = "user@test.com" }; [Fact] public async Task Add_user_to_group_should_return_success() { var actorId = Guid.NewGuid(); AuditLog? auditLog = null; var context = Substitute.For(); context.Groups.Returns(MockDbSetHelper.CreateFor([_group])); context.UserGroups.Returns(MockDbSetHelper.CreateFor()); var auditLogs = Substitute.For>(); auditLogs.When(set => set.Add(Arg.Any())).Do(call => auditLog = call.Arg()); context.AuditLogs.Returns(auditLogs); var identity = Substitute.For(); identity.FindByIdAsync(_user.Id, Arg.Any()).Returns(_user); var handler = new AddUserToGroupCommandHandler(context, identity); var result = await handler.Handle( new AddUserToGroupCommand(_group.Id, _user.Id, actorId), CancellationToken.None); result.IsSuccess.ShouldBeTrue(); auditLog.ShouldNotBeNull(); auditLog.UserId.ShouldBe(actorId); auditLog.OldValue.ShouldBeNull(); auditLog.NewValue!.ShouldContain(_user.Id.ToString()); await context.Received(1).SaveChangesWithRetryAsync(Arg.Any(), Arg.Any()); } [Fact] public async Task Add_user_to_nonexistent_group_should_return_not_found() { var context = Substitute.For(); context.Groups.Returns(MockDbSetHelper.CreateFor([_group])); context.UserGroups.Returns(MockDbSetHelper.CreateFor()); var identity = Substitute.For(); identity.FindByIdAsync(_user.Id, Arg.Any()).Returns(_user); var handler = new AddUserToGroupCommandHandler(context, identity); var result = await handler.Handle( new AddUserToGroupCommand(Guid.NewGuid(), _user.Id), CancellationToken.None); result.IsSuccess.ShouldBeFalse(); result.StatusCode.ShouldBe(404); } [Fact] public async Task Add_nonexistent_user_to_group_should_return_not_found() { var context = Substitute.For(); context.Groups.Returns(MockDbSetHelper.CreateFor([_group])); context.UserGroups.Returns(MockDbSetHelper.CreateFor()); var handler = new AddUserToGroupCommandHandler(context, Substitute.For()); var result = await handler.Handle( new AddUserToGroupCommand(_group.Id, Guid.NewGuid()), CancellationToken.None); result.IsSuccess.ShouldBeFalse(); result.StatusCode.ShouldBe(404); } [Fact] public async Task Add_existing_member_should_return_conflict() { var existingMembership = new UserGroup(_user.Id, _group.Id); var context = Substitute.For(); context.Groups.Returns(MockDbSetHelper.CreateFor([_group])); context.UserGroups.Returns(MockDbSetHelper.CreateFor([existingMembership])); var identity = Substitute.For(); identity.FindByIdAsync(_user.Id, Arg.Any()).Returns(_user); var handler = new AddUserToGroupCommandHandler(context, identity); var result = await handler.Handle( new AddUserToGroupCommand(_group.Id, _user.Id), CancellationToken.None); result.IsSuccess.ShouldBeFalse(); result.StatusCode.ShouldBe(409); } }