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
This commit is contained in:
1 parent
d2a2d6c0c1
commit
5ce13649b0
10 files changed
+117
-4
No files matched your search
@@ -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<CreatePersonCommand, S
|
||||
person.Update(request.DisplayName, request.Notes ?? "", request.Category, request.IsWatchlisted);
|
||||
|
||||
_db.Persons.Add(person);
|
||||
_db.AuditLogs.Add(new AuditLog(null, "Person.Created", "Person", person.Id.ToString(),
|
||||
newValue: JsonSerializer.Serialize(new { person.DisplayName, person.Category })));
|
||||
|
||||
await _db.SaveChangesWithRetryAsync(ct: ct).ConfigureAwait(false);
|
||||
|
||||
return ServiceResult<PersonResponse>.Success(Map(person));
|
||||
|
||||
@@ -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<DeleteFaceCommand, Servi
|
||||
if (face is null)
|
||||
return ServiceResult.NotFound("Face not found");
|
||||
|
||||
_db.AuditLogs.Add(new AuditLog(null, "Person.FaceDeleted", "KnownFace", request.FaceId.ToString(),
|
||||
oldValue: face.ThumbnailUrl));
|
||||
_db.KnownFaces.Remove(face);
|
||||
await _db.SaveChangesWithRetryAsync(ct: ct).ConfigureAwait(false);
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Heimdall.Application.Common;
|
||||
using Heimdall.Domain.Entities;
|
||||
using MediatR;
|
||||
|
||||
namespace Heimdall.Application.Persons.Commands.DeletePerson;
|
||||
@@ -17,6 +18,8 @@ public class DeletePersonCommandHandler : IRequestHandler<DeletePersonCommand, S
|
||||
if (person is null)
|
||||
return ServiceResult.NotFound("Person not found");
|
||||
|
||||
_db.AuditLogs.Add(new AuditLog(null, "Person.Deleted", "Person", request.Id.ToString(),
|
||||
oldValue: person.DisplayName));
|
||||
_db.Persons.Remove(person);
|
||||
await _db.SaveChangesWithRetryAsync(ct: ct).ConfigureAwait(false);
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using System.Text.Json;
|
||||
using Heimdall.Application.Common;
|
||||
using Heimdall.Application.DTOs;
|
||||
using Heimdall.Domain.Entities;
|
||||
@@ -27,6 +28,8 @@ public class RegisterFaceCommandHandler : IRequestHandler<RegisterFaceCommand, S
|
||||
var face = new KnownFace(request.PersonId, request.Embedding,
|
||||
request.ThumbnailUrl, request.SourceEventId);
|
||||
person.AddFace(face);
|
||||
_db.AuditLogs.Add(new AuditLog(null, "Person.FaceRegistered", "KnownFace", face.Id.ToString(),
|
||||
newValue: JsonSerializer.Serialize(new { PersonId = request.PersonId, face.ThumbnailUrl })));
|
||||
await _db.SaveChangesWithRetryAsync(ct: ct).ConfigureAwait(false);
|
||||
|
||||
return ServiceResult<KnownFaceResponse>.Success(Map(face));
|
||||
|
||||
@@ -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<UpdatePersonCommand, S
|
||||
return ServiceResult<PersonResponse>.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<PersonResponse>.Success(Map(person));
|
||||
|
||||
@@ -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<IApplicationDbContext>();
|
||||
context.Persons.Returns(MockDbSetHelper.CreateFor<Person>());
|
||||
context.AuditLogs.Returns(Substitute.For<DbSet<AuditLog>>());
|
||||
var handler = new CreatePersonCommandHandler(context);
|
||||
|
||||
var result = await handler.Handle(
|
||||
@@ -30,6 +33,7 @@ public class CreatePersonHandlerTests
|
||||
{
|
||||
var context = Substitute.For<IApplicationDbContext>();
|
||||
context.Persons.Returns(MockDbSetHelper.CreateFor<Person>());
|
||||
context.AuditLogs.Returns(Substitute.For<DbSet<AuditLog>>());
|
||||
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<IApplicationDbContext>();
|
||||
context.Persons.Returns(MockDbSetHelper.CreateFor<Person>());
|
||||
context.AuditLogs.Returns(Substitute.For<DbSet<AuditLog>>());
|
||||
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<AuditLog>());
|
||||
}
|
||||
}
|
||||
@@ -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<IApplicationDbContext>();
|
||||
context.KnownFaces.Returns(MockDbSetHelper.CreateFor<KnownFace>([face]));
|
||||
context.AuditLogs.Returns(Substitute.For<DbSet<AuditLog>>());
|
||||
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<IApplicationDbContext>();
|
||||
context.KnownFaces.Returns(MockDbSetHelper.CreateFor<KnownFace>());
|
||||
context.AuditLogs.Returns(Substitute.For<DbSet<AuditLog>>());
|
||||
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<IApplicationDbContext>();
|
||||
context.KnownFaces.Returns(MockDbSetHelper.CreateFor<KnownFace>([face]));
|
||||
context.AuditLogs.Returns(Substitute.For<DbSet<AuditLog>>());
|
||||
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<AuditLog>());
|
||||
}
|
||||
}
|
||||
@@ -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<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);
|
||||
@@ -27,6 +29,7 @@ public class DeletePersonHandlerTests
|
||||
{
|
||||
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);
|
||||
@@ -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<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>());
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,8 @@ public class RegisterFaceHandlerTests
|
||||
var person = new Person("Test Person");
|
||||
var context = Substitute.For<IApplicationDbContext>();
|
||||
context.Persons.Returns(MockDbSetHelper.CreateFor<Person>([person]));
|
||||
context.KnownFaces.Returns(MockDbSetHelper.CreateFor<KnownFace>());
|
||||
context.AuditLogs.Returns(Substitute.For<DbSet<AuditLog>>());
|
||||
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<IApplicationDbContext>();
|
||||
context.Persons.Returns(MockDbSetHelper.CreateFor<Person>());
|
||||
context.KnownFaces.Returns(MockDbSetHelper.CreateFor<KnownFace>());
|
||||
context.AuditLogs.Returns(Substitute.For<DbSet<AuditLog>>());
|
||||
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<IApplicationDbContext>();
|
||||
context.Persons.Returns(MockDbSetHelper.CreateFor<Person>([person]));
|
||||
context.KnownFaces.Returns(MockDbSetHelper.CreateFor<KnownFace>());
|
||||
context.AuditLogs.Returns(Substitute.For<DbSet<AuditLog>>());
|
||||
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<AuditLog>());
|
||||
}
|
||||
}
|
||||
@@ -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<IApplicationDbContext>();
|
||||
context.Persons.Returns(MockDbSetHelper.CreateFor<Person>([person]));
|
||||
context.AuditLogs.Returns(Substitute.For<DbSet<AuditLog>>());
|
||||
var handler = new UpdatePersonCommandHandler(context);
|
||||
|
||||
var result = await handler.Handle(
|
||||
@@ -32,6 +34,7 @@ public class UpdatePersonHandlerTests
|
||||
{
|
||||
var context = Substitute.For<IApplicationDbContext>();
|
||||
context.Persons.Returns(MockDbSetHelper.CreateFor<Person>());
|
||||
context.AuditLogs.Returns(Substitute.For<DbSet<AuditLog>>());
|
||||
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<IApplicationDbContext>();
|
||||
context.Persons.Returns(MockDbSetHelper.CreateFor<Person>([person]));
|
||||
context.AuditLogs.Returns(Substitute.For<DbSet<AuditLog>>());
|
||||
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<AuditLog>());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user