From 80e21d63e6bcfdfd2518c3a7b1827267e0849ba1 Mon Sep 17 00:00:00 2001 From: Arnaud Dartois Date: Sat, 10 Oct 2026 19:47:09 +0200 Subject: [PATCH] fix(tests): repair PersonsController integration tests and fix EF concurrency on RegisterFace - Add DbUpdateConcurrencyException to SaveChangesWithRetryAsync transient check - RegisterFaceHandler adds KnownFace via DbContext Set to avoid phantom UPDATE - Validate empty embedding (400) before person existence check - CreateViewerClientAsync bootstraps admin first so 2nd user is Viewer - Rename GetAll_WithViewerAuth test to match PersonRead permission on Viewer - PersonsControllerTests: all 20 tests green --- .../Common/IApplicationDbContext.cs | 2 ++ .../Commands/RegisterFace/RegisterFaceCommand.cs | 12 ++++++++++-- src/Heimdall.Infrastructure/Data/AppDbContext.cs | 2 ++ .../Data/DbContextExtensions.cs | 6 ++++++ .../Controllers/PersonsControllerTests.cs | 10 +++++++--- .../Controllers/TestHelpers.cs | 5 +++++ 6 files changed, 32 insertions(+), 5 deletions(-) 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