45 lines
1.7 KiB
C#
45 lines
1.7 KiB
C#
using Heimdall.Application.Common;
|
|||
|
|
using Heimdall.Application.Groups.Commands.UpdateGroup;
|
||
|
|
using Heimdall.Application.Tests.Common;
|
||
|
|
using Heimdall.Domain.Entities;
|
||
|
|
using NSubstitute;
|
||
|
|
using Microsoft.EntityFrameworkCore;
|
||
|
|
|
||
|
|
namespace Heimdall.Application.Tests.Handlers.Groups;
|
||
|
|
|
||
|
|
public class UpdateGroupHandlerTests
|
||
|
|
{
|
||
|
|
private readonly Group _group = new("Original Name", "Original description");
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task Update_group_should_return_success()
|
||
|
|
{
|
||
|
|
var context = Substitute.For<IApplicationDbContext>();
|
||
|
|
context.Groups.Returns(MockDbSetHelper.CreateFor<Group>([_group]));
|
||
|
|
context.AuditLogs.Returns(MockDbSetHelper.CreateFor<AuditLog>());
|
||
|
|
context.UserGroups.Returns(MockDbSetHelper.CreateFor<UserGroup>());
|
||
|
|
var handler = new UpdateGroupCommandHandler(context);
|
||
|
|
|
||
|
|
var result = await handler.Handle(
|
||
|
|
new UpdateGroupCommand(_group.Id, "Updated Name", "Updated description"), CancellationToken.None);
|
||
|
|
|
||
|
|
result.IsSuccess.ShouldBeTrue();
|
||
|
|
result.Data.ShouldNotBeNull();
|
||
|
|
result.Data.Name.ShouldBe("Updated Name");
|
||
|
|
await context.Received(1).SaveChangesWithRetryAsync(Arg.Any<int>(), Arg.Any<CancellationToken>());
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task Update_nonexistent_group_should_return_not_found()
|
||
|
|
{
|
||
|
|
var context = Substitute.For<IApplicationDbContext>();
|
||
|
|
context.Groups.Returns(MockDbSetHelper.CreateFor<Group>([_group]));
|
||
|
|
var handler = new UpdateGroupCommandHandler(context);
|
||
|
|
|
||
|
|
var result = await handler.Handle(
|
||
|
|
new UpdateGroupCommand(Guid.NewGuid(), "Name", null), CancellationToken.None);
|
||
|
|
|
||
|
|
result.IsSuccess.ShouldBeFalse();
|
||
|
|
result.StatusCode.ShouldBe(404);
|
||
|
|
}
|
||
|
|
}
|