diff --git a/src/Heimdall.Application/Persons/Commands/CreatePerson/CreatePersonCommand.cs b/src/Heimdall.Application/Persons/Commands/CreatePerson/CreatePersonCommand.cs index 3a0fdce..e4d62d5 100644 --- a/src/Heimdall.Application/Persons/Commands/CreatePerson/CreatePersonCommand.cs +++ b/src/Heimdall.Application/Persons/Commands/CreatePerson/CreatePersonCommand.cs @@ -1,3 +1,4 @@ +using System.Text.Json; using Heimdall.Application.Common; using Heimdall.Application.DTOs; using Heimdall.Domain.Entities; @@ -21,6 +22,9 @@ public class CreatePersonCommandHandler : IRequestHandler.Success(Map(person)); diff --git a/src/Heimdall.Application/Persons/Commands/DeleteFace/DeleteFaceCommand.cs b/src/Heimdall.Application/Persons/Commands/DeleteFace/DeleteFaceCommand.cs index 915f4d6..c00b864 100644 --- a/src/Heimdall.Application/Persons/Commands/DeleteFace/DeleteFaceCommand.cs +++ b/src/Heimdall.Application/Persons/Commands/DeleteFace/DeleteFaceCommand.cs @@ -1,4 +1,5 @@ using Heimdall.Application.Common; +using Heimdall.Domain.Entities; using MediatR; namespace Heimdall.Application.Persons.Commands.DeleteFace; @@ -17,6 +18,8 @@ public class DeleteFaceCommandHandler : IRequestHandler.Success(Map(face)); diff --git a/src/Heimdall.Application/Persons/Commands/UpdatePerson/UpdatePersonCommand.cs b/src/Heimdall.Application/Persons/Commands/UpdatePerson/UpdatePersonCommand.cs index c118491..2cb9f87 100644 --- a/src/Heimdall.Application/Persons/Commands/UpdatePerson/UpdatePersonCommand.cs +++ b/src/Heimdall.Application/Persons/Commands/UpdatePerson/UpdatePersonCommand.cs @@ -1,3 +1,4 @@ +using System.Text.Json; using Heimdall.Application.Common; using Heimdall.Application.DTOs; using Heimdall.Domain.Entities; @@ -22,6 +23,8 @@ public class UpdatePersonCommandHandler : IRequestHandler.NotFound("Person not found"); person.Update(request.DisplayName, request.Notes, request.Category, request.IsWatchlisted); + _db.AuditLogs.Add(new AuditLog(null, "Person.Updated", "Person", person.Id.ToString(), + newValue: JsonSerializer.Serialize(new { person.DisplayName, person.Category, person.IsWatchlisted }))); await _db.SaveChangesWithRetryAsync(ct: ct).ConfigureAwait(false); return ServiceResult.Success(Map(person)); diff --git a/tests/Heimdall.Application.Tests/Handlers/Persons/CreatePersonHandlerTests.cs b/tests/Heimdall.Application.Tests/Handlers/Persons/CreatePersonHandlerTests.cs index e0e690d..f963a90 100644 --- a/tests/Heimdall.Application.Tests/Handlers/Persons/CreatePersonHandlerTests.cs +++ b/tests/Heimdall.Application.Tests/Handlers/Persons/CreatePersonHandlerTests.cs @@ -1,7 +1,9 @@ +using System.Linq; using Heimdall.Application.Common; using Heimdall.Application.Persons.Commands.CreatePerson; using Heimdall.Application.Tests.Common; using Heimdall.Domain.Entities; +using Microsoft.EntityFrameworkCore; using NSubstitute; namespace Heimdall.Application.Tests.Handlers.Persons; @@ -13,6 +15,7 @@ public class CreatePersonHandlerTests { var context = Substitute.For(); context.Persons.Returns(MockDbSetHelper.CreateFor()); + context.AuditLogs.Returns(Substitute.For>()); var handler = new CreatePersonCommandHandler(context); var result = await handler.Handle( @@ -30,6 +33,7 @@ public class CreatePersonHandlerTests { var context = Substitute.For(); context.Persons.Returns(MockDbSetHelper.CreateFor()); + context.AuditLogs.Returns(Substitute.For>()); var handler = new CreatePersonCommandHandler(context); var result = await handler.Handle( @@ -38,4 +42,19 @@ public class CreatePersonHandlerTests result.IsSuccess.ShouldBeTrue(); result.Data!.DisplayName.ShouldBe("Minimal Person"); } + + [Fact] + public async Task Create_person_should_record_audit_log() + { + var context = Substitute.For(); + context.Persons.Returns(MockDbSetHelper.CreateFor()); + context.AuditLogs.Returns(Substitute.For>()); + var handler = new CreatePersonCommandHandler(context); + + var result = await handler.Handle( + new CreatePersonCommand("Audited Person", "family", null, false), CancellationToken.None); + + result.IsSuccess.ShouldBeTrue(); + context.AuditLogs.Received(1).Add(Arg.Any()); + } } diff --git a/tests/Heimdall.Application.Tests/Handlers/Persons/DeleteFaceHandlerTests.cs b/tests/Heimdall.Application.Tests/Handlers/Persons/DeleteFaceHandlerTests.cs index b1af618..e9a85d2 100644 --- a/tests/Heimdall.Application.Tests/Handlers/Persons/DeleteFaceHandlerTests.cs +++ b/tests/Heimdall.Application.Tests/Handlers/Persons/DeleteFaceHandlerTests.cs @@ -2,6 +2,7 @@ using Heimdall.Application.Common; using Heimdall.Application.Persons.Commands.DeleteFace; using Heimdall.Application.Tests.Common; using Heimdall.Domain.Entities; +using Microsoft.EntityFrameworkCore; using NSubstitute; namespace Heimdall.Application.Tests.Handlers.Persons; @@ -14,6 +15,7 @@ public class DeleteFaceHandlerTests var face = new KnownFace(Guid.NewGuid(), [0.1f], "thumb.jpg", null); var context = Substitute.For(); context.KnownFaces.Returns(MockDbSetHelper.CreateFor([face])); + context.AuditLogs.Returns(Substitute.For>()); var handler = new DeleteFaceCommandHandler(context); var result = await handler.Handle(new DeleteFaceCommand(face.Id), CancellationToken.None); @@ -27,6 +29,7 @@ public class DeleteFaceHandlerTests { var context = Substitute.For(); context.KnownFaces.Returns(MockDbSetHelper.CreateFor()); + context.AuditLogs.Returns(Substitute.For>()); var handler = new DeleteFaceCommandHandler(context); var result = await handler.Handle(new DeleteFaceCommand(Guid.NewGuid()), CancellationToken.None); @@ -34,4 +37,19 @@ public class DeleteFaceHandlerTests result.IsSuccess.ShouldBeFalse(); result.StatusCode.ShouldBe(404); } -} + + [Fact] + public async Task Delete_face_should_record_audit_log() + { + var face = new KnownFace(Guid.NewGuid(), [0.1f], "thumb.jpg", null); + var context = Substitute.For(); + context.KnownFaces.Returns(MockDbSetHelper.CreateFor([face])); + context.AuditLogs.Returns(Substitute.For>()); + var handler = new DeleteFaceCommandHandler(context); + + var result = await handler.Handle(new DeleteFaceCommand(face.Id), CancellationToken.None); + + result.IsSuccess.ShouldBeTrue(); + context.AuditLogs.Received(1).Add(Arg.Any()); + } +} \ No newline at end of file diff --git a/tests/Heimdall.Application.Tests/Handlers/Persons/DeletePersonHandlerTests.cs b/tests/Heimdall.Application.Tests/Handlers/Persons/DeletePersonHandlerTests.cs index 2c20e06..9c8fdce 100644 --- a/tests/Heimdall.Application.Tests/Handlers/Persons/DeletePersonHandlerTests.cs +++ b/tests/Heimdall.Application.Tests/Handlers/Persons/DeletePersonHandlerTests.cs @@ -2,6 +2,7 @@ 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; @@ -14,6 +15,7 @@ public class DeletePersonHandlerTests var person = new Person("Test Person"); var context = Substitute.For(); context.Persons.Returns(MockDbSetHelper.CreateFor([person])); + context.AuditLogs.Returns(Substitute.For>()); var handler = new DeletePersonCommandHandler(context); var result = await handler.Handle(new DeletePersonCommand(person.Id), CancellationToken.None); @@ -27,6 +29,7 @@ public class DeletePersonHandlerTests { var context = Substitute.For(); context.Persons.Returns(MockDbSetHelper.CreateFor()); + context.AuditLogs.Returns(Substitute.For>()); var handler = new DeletePersonCommandHandler(context); var result = await handler.Handle(new DeletePersonCommand(Guid.NewGuid()), CancellationToken.None); @@ -34,4 +37,19 @@ public class DeletePersonHandlerTests 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(); + context.Persons.Returns(MockDbSetHelper.CreateFor([person])); + context.AuditLogs.Returns(Substitute.For>()); + 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()); + } +} \ No newline at end of file diff --git a/tests/Heimdall.Application.Tests/Handlers/Persons/RegisterFaceHandlerTests.cs b/tests/Heimdall.Application.Tests/Handlers/Persons/RegisterFaceHandlerTests.cs index 7c5df0a..b3e075c 100644 --- a/tests/Heimdall.Application.Tests/Handlers/Persons/RegisterFaceHandlerTests.cs +++ b/tests/Heimdall.Application.Tests/Handlers/Persons/RegisterFaceHandlerTests.cs @@ -15,6 +15,8 @@ public class RegisterFaceHandlerTests var person = new Person("Test Person"); var context = Substitute.For(); context.Persons.Returns(MockDbSetHelper.CreateFor([person])); + context.KnownFaces.Returns(MockDbSetHelper.CreateFor()); + context.AuditLogs.Returns(Substitute.For>()); var handler = new RegisterFaceCommandHandler(context); var embedding = new float[] { 0.1f, 0.2f, 0.3f }; @@ -33,6 +35,8 @@ public class RegisterFaceHandlerTests { var context = Substitute.For(); context.Persons.Returns(MockDbSetHelper.CreateFor()); + context.KnownFaces.Returns(MockDbSetHelper.CreateFor()); + context.AuditLogs.Returns(Substitute.For>()); var handler = new RegisterFaceCommandHandler(context); var result = await handler.Handle( @@ -41,4 +45,22 @@ public class RegisterFaceHandlerTests result.IsSuccess.ShouldBeFalse(); result.StatusCode.ShouldBe(404); } -} + + [Fact] + public async Task Register_face_should_record_audit_log() + { + var person = new Person("Test Person"); + var context = Substitute.For(); + context.Persons.Returns(MockDbSetHelper.CreateFor([person])); + context.KnownFaces.Returns(MockDbSetHelper.CreateFor()); + context.AuditLogs.Returns(Substitute.For>()); + var handler = new RegisterFaceCommandHandler(context); + var embedding = new float[] { 0.1f, 0.2f, 0.3f }; + + var result = await handler.Handle( + new RegisterFaceCommand(person.Id, embedding, "thumb.jpg", "evt-1"), CancellationToken.None); + + result.IsSuccess.ShouldBeTrue(); + context.AuditLogs.Received(1).Add(Arg.Any()); + } +} \ No newline at end of file diff --git a/tests/Heimdall.Application.Tests/Handlers/Persons/UpdatePersonHandlerTests.cs b/tests/Heimdall.Application.Tests/Handlers/Persons/UpdatePersonHandlerTests.cs index b11fd9d..559cd58 100644 --- a/tests/Heimdall.Application.Tests/Handlers/Persons/UpdatePersonHandlerTests.cs +++ b/tests/Heimdall.Application.Tests/Handlers/Persons/UpdatePersonHandlerTests.cs @@ -2,6 +2,7 @@ using Heimdall.Application.Common; using Heimdall.Application.Persons.Commands.UpdatePerson; using Heimdall.Application.Tests.Common; using Heimdall.Domain.Entities; +using Microsoft.EntityFrameworkCore; using NSubstitute; namespace Heimdall.Application.Tests.Handlers.Persons; @@ -14,6 +15,7 @@ public class UpdatePersonHandlerTests var person = new Person("Old Name", "family"); var context = Substitute.For(); context.Persons.Returns(MockDbSetHelper.CreateFor([person])); + context.AuditLogs.Returns(Substitute.For>()); var handler = new UpdatePersonCommandHandler(context); var result = await handler.Handle( @@ -32,6 +34,7 @@ public class UpdatePersonHandlerTests { var context = Substitute.For(); context.Persons.Returns(MockDbSetHelper.CreateFor()); + context.AuditLogs.Returns(Substitute.For>()); var handler = new UpdatePersonCommandHandler(context); var result = await handler.Handle( @@ -41,4 +44,21 @@ public class UpdatePersonHandlerTests result.IsSuccess.ShouldBeFalse(); result.StatusCode.ShouldBe(404); } -} + + [Fact] + public async Task Update_person_should_record_audit_log() + { + var person = new Person("Old Name", "family"); + var context = Substitute.For(); + context.Persons.Returns(MockDbSetHelper.CreateFor([person])); + context.AuditLogs.Returns(Substitute.For>()); + var handler = new UpdatePersonCommandHandler(context); + + var result = await handler.Handle( + new UpdatePersonCommand(person.Id, "New Name", "Some notes", "employee", true), + CancellationToken.None); + + result.IsSuccess.ShouldBeTrue(); + context.AuditLogs.Received(1).Add(Arg.Any()); + } +} \ No newline at end of file