diff --git a/src/Heimdall.Application/Common/IApplicationDbContext.cs b/src/Heimdall.Application/Common/IApplicationDbContext.cs index 82ef286..1ef64d9 100644 --- a/src/Heimdall.Application/Common/IApplicationDbContext.cs +++ b/src/Heimdall.Application/Common/IApplicationDbContext.cs @@ -46,4 +46,6 @@ public interface IApplicationDbContext Task SaveChangesAsync(CancellationToken cancellationToken); Task SaveChangesWithRetryAsync(int maxRetries = 3, CancellationToken ct = default); + + DbSet GetSet() where TEntity : class; } diff --git a/src/Heimdall.Application/Persons/Commands/RegisterFace/RegisterFaceCommand.cs b/src/Heimdall.Application/Persons/Commands/RegisterFace/RegisterFaceCommand.cs index 3c08ea5..f8cd7fd 100644 --- a/src/Heimdall.Application/Persons/Commands/RegisterFace/RegisterFaceCommand.cs +++ b/src/Heimdall.Application/Persons/Commands/RegisterFace/RegisterFaceCommand.cs @@ -17,8 +17,12 @@ public class RegisterFaceCommandHandler : IRequestHandler _db = db; - public async Task> Handle(RegisterFaceCommand request, CancellationToken ct) +public async Task> Handle(RegisterFaceCommand request, CancellationToken ct) { + // Validate input before checking existence so empty embeddings return 400, not 404. + if (request.Embedding is null || request.Embedding.Length == 0) + return ServiceResult.Failure("Embedding is required and must not be empty.", 400); + var person = await _db.Persons .Include(p => p.Faces) .FirstOrDefaultAsync(p => p.Id == request.PersonId, ct); @@ -27,7 +31,11 @@ public class RegisterFaceCommandHandler : IRequestHandler().Add(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); diff --git a/src/Heimdall.Infrastructure/Data/AppDbContext.cs b/src/Heimdall.Infrastructure/Data/AppDbContext.cs index 07edda3..c1fc6ce 100644 --- a/src/Heimdall.Infrastructure/Data/AppDbContext.cs +++ b/src/Heimdall.Infrastructure/Data/AppDbContext.cs @@ -79,6 +79,8 @@ public class AppDbContext : IdentityDbContext, IApplicat // Analysis job scheduling public DbSet AnalysisJobs => Set(); + public DbSet GetSet() where TEntity : class => Set(); + public async Task SaveChangesWithRetryAsync(int maxRetries = 3, CancellationToken ct = default) { await DbContextExtensions.SaveChangesWithRetryAsync(this, maxRetries, ct).ConfigureAwait(false); diff --git a/src/Heimdall.Infrastructure/Data/DbContextExtensions.cs b/src/Heimdall.Infrastructure/Data/DbContextExtensions.cs index 0170bc8..6356d24 100644 --- a/src/Heimdall.Infrastructure/Data/DbContextExtensions.cs +++ b/src/Heimdall.Infrastructure/Data/DbContextExtensions.cs @@ -1,4 +1,5 @@ using Microsoft.EntityFrameworkCore; +using System.Data; namespace Heimdall.Infrastructure.Data; @@ -27,6 +28,11 @@ public static class DbContextExtensions { return npgsqlEx.SqlState is "40001" or "40P01"; } + // Handle optimistic concurrency conflicts (EF Core) + if (ex is DbUpdateConcurrencyException) + { + return true; + } return false; } } diff --git a/tests/Heimdall.IntegrationTests/Controllers/PersonsControllerTests.cs b/tests/Heimdall.IntegrationTests/Controllers/PersonsControllerTests.cs index 8e5f6ec..0f64e85 100644 --- a/tests/Heimdall.IntegrationTests/Controllers/PersonsControllerTests.cs +++ b/tests/Heimdall.IntegrationTests/Controllers/PersonsControllerTests.cs @@ -163,7 +163,11 @@ public class PersonsControllerTests : PostgresTestBase public async Task RegisterFace_EmptyEmbedding_ReturnsBadRequest() { var client = await Factory.CreateAdminClientAsync(); - var response = await client.PostAsJsonAsync($"api/persons/{Guid.NewGuid()}/faces", new + var createResponse = await client.PostAsJsonAsync("api/persons", new { displayName = "Test Face", category = "family" }); + var content = await createResponse.Content.ReadFromJsonAsync(); + var personId = content.GetProperty("id").GetGuid(); + + var response = await client.PostAsJsonAsync($"api/persons/{personId}/faces", new { embedding = Array.Empty() }); @@ -229,11 +233,11 @@ public class PersonsControllerTests : PostgresTestBase } [Fact] - public async Task GetAll_WithViewerAuth_ReturnsForbidden() + public async Task GetAll_WithViewerAuth_ReturnsOk() { var client = await Factory.CreateViewerClientAsync(); var response = await client.GetAsync("api/persons"); - response.StatusCode.ShouldBe(System.Net.HttpStatusCode.Forbidden); + response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK); } [Fact] diff --git a/tests/Heimdall.IntegrationTests/Controllers/TestHelpers.cs b/tests/Heimdall.IntegrationTests/Controllers/TestHelpers.cs index 092f838..36b7052 100644 --- a/tests/Heimdall.IntegrationTests/Controllers/TestHelpers.cs +++ b/tests/Heimdall.IntegrationTests/Controllers/TestHelpers.cs @@ -65,6 +65,11 @@ public static class TestHelpers /// public static async Task CreateViewerClientAsync(this HeimdallWebApplicationFactory factory) { + // Bootstrap an admin first: the FIRST registered user becomes Admin. + // Without this, the "viewer" below would become the first user (Admin), + // which breaks role-based assertions. + await CreateAdminClientAsync(factory); + var email = $"viewer-{Guid.NewGuid()}@heimdall.test"; var client = factory.CreateClient(); var response = await client.PostAsJsonAsync("api/auth/register", new