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
This commit is contained in:
1 parent
fa0b61837a
commit
80e21d63e6
6 files changed
+32
-5
No files matched your search
@@ -46,4 +46,6 @@ public interface IApplicationDbContext
|
||||
|
||||
Task<int> SaveChangesAsync(CancellationToken cancellationToken);
|
||||
Task SaveChangesWithRetryAsync(int maxRetries = 3, CancellationToken ct = default);
|
||||
|
||||
DbSet<TEntity> GetSet<TEntity>() where TEntity : class;
|
||||
}
|
||||
@@ -17,8 +17,12 @@ public class RegisterFaceCommandHandler : IRequestHandler<RegisterFaceCommand, S
|
||||
|
||||
public RegisterFaceCommandHandler(IApplicationDbContext db) => _db = db;
|
||||
|
||||
public async Task<ServiceResult<KnownFaceResponse>> Handle(RegisterFaceCommand request, CancellationToken ct)
|
||||
public async Task<ServiceResult<KnownFaceResponse>> 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<KnownFaceResponse>.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<RegisterFaceCommand, S
|
||||
|
||||
var face = new KnownFace(request.PersonId, request.Embedding,
|
||||
request.ThumbnailUrl, request.SourceEventId);
|
||||
person.AddFace(face);
|
||||
|
||||
// Add directly to DbContext to avoid EF Core change tracking confusion
|
||||
// when adding to a collection navigation property
|
||||
_db.GetSet<KnownFace>().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);
|
||||
|
||||
@@ -79,6 +79,8 @@ public class AppDbContext : IdentityDbContext<AppUser, AppRole, Guid>, IApplicat
|
||||
// Analysis job scheduling
|
||||
public DbSet<AnalysisJob> AnalysisJobs => Set<AnalysisJob>();
|
||||
|
||||
public DbSet<TEntity> GetSet<TEntity>() where TEntity : class => Set<TEntity>();
|
||||
|
||||
public async Task SaveChangesWithRetryAsync(int maxRetries = 3, CancellationToken ct = default)
|
||||
{
|
||||
await DbContextExtensions.SaveChangesWithRetryAsync(this, maxRetries, ct).ConfigureAwait(false);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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<JsonElement>();
|
||||
var personId = content.GetProperty("id").GetGuid();
|
||||
|
||||
var response = await client.PostAsJsonAsync($"api/persons/{personId}/faces", new
|
||||
{
|
||||
embedding = Array.Empty<float>()
|
||||
});
|
||||
@@ -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]
|
||||
|
||||
@@ -65,6 +65,11 @@ public static class TestHelpers
|
||||
/// </summary>
|
||||
public static async Task<HttpClient> 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
|
||||
|
||||
Reference in new issue
Block a user