Files
Heimdall/tests/Heimdall.Application.Tests/Handlers/Persons/DeletePersonHandlerTests.cs
T
Arnaud Dartois 5ce13649b0 feat(app): add audit logging to person handlers
- Add AuditLog entries to CreatePerson, UpdatePerson, DeletePerson,
  RegisterFace, DeleteFace handlers
- Extend application unit tests to verify audit log creation
2026-10-10 17:49:26 +02:00

55 lines
2.1 KiB
C#

using Heimdall.Application.Common;
using Heimdall.Application.Persons.Commands.DeletePerson;
using Heimdall.Application.Tests.Common;
using Heimdall.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using NSubstitute;
namespace Heimdall.Application.Tests.Handlers.Persons;
public class DeletePersonHandlerTests
{
[Fact]
public async Task Delete_person_should_return_success()
{
var person = new Person("Test Person");
var context = Substitute.For<IApplicationDbContext>();
context.Persons.Returns(MockDbSetHelper.CreateFor<Person>([person]));
context.AuditLogs.Returns(Substitute.For<DbSet<AuditLog>>());
var handler = new DeletePersonCommandHandler(context);
var result = await handler.Handle(new DeletePersonCommand(person.Id), CancellationToken.None);
result.IsSuccess.ShouldBeTrue();
await context.Received(1).SaveChangesWithRetryAsync(Arg.Any<int>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task Delete_nonexistent_person_should_return_not_found()
{
var context = Substitute.For<IApplicationDbContext>();
context.Persons.Returns(MockDbSetHelper.CreateFor<Person>());
context.AuditLogs.Returns(Substitute.For<DbSet<AuditLog>>());
var handler = new DeletePersonCommandHandler(context);
var result = await handler.Handle(new DeletePersonCommand(Guid.NewGuid()), CancellationToken.None);
result.IsSuccess.ShouldBeFalse();
result.StatusCode.ShouldBe(404);
}
[Fact]
public async Task Delete_person_should_record_audit_log()
{
var person = new Person("Test Person");
var context = Substitute.For<IApplicationDbContext>();
context.Persons.Returns(MockDbSetHelper.CreateFor<Person>([person]));
context.AuditLogs.Returns(Substitute.For<DbSet<AuditLog>>());
var handler = new DeletePersonCommandHandler(context);
var result = await handler.Handle(new DeletePersonCommand(person.Id), CancellationToken.None);
result.IsSuccess.ShouldBeTrue();
context.AuditLogs.Received(1).Add(Arg.Any<AuditLog>());
}
}