2026-07-23 15:27:34 +00:00
|
|
|
using System.Net.Http.Json;
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using Heimdall.IntegrationTests.Factories;
|
|
|
|
|
using Shouldly;
|
|
|
|
|
|
|
|
|
|
namespace Heimdall.IntegrationTests.Controllers;
|
|
|
|
|
|
|
|
|
|
public class PersonsControllerTests : PostgresTestBase
|
|
|
|
|
{
|
|
|
|
|
public PersonsControllerTests(PostgreSqlContainerFixture postgres) : base(postgres) { }
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task GetAll_WithoutAuth_ReturnsUnauthorized()
|
|
|
|
|
{
|
|
|
|
|
var client = Factory.CreateClient();
|
|
|
|
|
var response = await client.GetAsync("api/persons");
|
|
|
|
|
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.Unauthorized);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task GetAll_WithAdminAuth_ReturnsOk()
|
|
|
|
|
{
|
|
|
|
|
var client = await Factory.CreateAdminClientAsync();
|
|
|
|
|
var response = await client.GetAsync("api/persons");
|
|
|
|
|
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
|
|
|
|
|
|
|
|
|
|
var content = await response.Content.ReadFromJsonAsync<JsonElement>();
|
|
|
|
|
content.ValueKind.ShouldBe(System.Text.Json.JsonValueKind.Array);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Create_WithoutAuth_ReturnsUnauthorized()
|
|
|
|
|
{
|
|
|
|
|
var client = Factory.CreateClient();
|
|
|
|
|
var response = await client.PostAsJsonAsync("api/persons", new
|
|
|
|
|
{
|
|
|
|
|
displayName = "Test Person",
|
|
|
|
|
category = "employee"
|
|
|
|
|
});
|
|
|
|
|
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.Unauthorized);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Create_WithAdminAuth_ReturnsOk()
|
|
|
|
|
{
|
|
|
|
|
var client = await Factory.CreateAdminClientAsync();
|
|
|
|
|
var response = await client.PostAsJsonAsync("api/persons", new
|
|
|
|
|
{
|
|
|
|
|
displayName = $"Person {Guid.NewGuid():N}",
|
|
|
|
|
category = "employee"
|
|
|
|
|
});
|
|
|
|
|
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Create_GetById_ReturnsPerson()
|
|
|
|
|
{
|
|
|
|
|
var client = await Factory.CreateAdminClientAsync();
|
|
|
|
|
var createResponse = await client.PostAsJsonAsync("api/persons", new
|
|
|
|
|
{
|
|
|
|
|
displayName = $"Get Person {Guid.NewGuid():N}",
|
|
|
|
|
category = "visitor"
|
|
|
|
|
});
|
|
|
|
|
var createContent = await createResponse.Content.ReadFromJsonAsync<JsonElement>();
|
|
|
|
|
var personId = createContent.GetProperty("id").GetGuid();
|
|
|
|
|
|
|
|
|
|
var getResponse = await client.GetAsync($"api/persons/{personId}");
|
|
|
|
|
getResponse.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
|
|
|
|
|
|
|
|
|
|
var getContent = await getResponse.Content.ReadFromJsonAsync<JsonElement>();
|
|
|
|
|
getContent.GetProperty("id").GetGuid().ShouldBe(personId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task GetById_NonExistent_ReturnsNotFound()
|
|
|
|
|
{
|
|
|
|
|
var client = await Factory.CreateAdminClientAsync();
|
|
|
|
|
var response = await client.GetAsync($"api/persons/{Guid.NewGuid()}");
|
|
|
|
|
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.NotFound);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Update_WithAdminAuth_ReturnsOk()
|
|
|
|
|
{
|
|
|
|
|
var client = await Factory.CreateAdminClientAsync();
|
|
|
|
|
var createResponse = await client.PostAsJsonAsync("api/persons", new
|
|
|
|
|
{
|
|
|
|
|
displayName = $"Update Person {Guid.NewGuid():N}",
|
|
|
|
|
category = "employee"
|
|
|
|
|
});
|
|
|
|
|
var createContent = await createResponse.Content.ReadFromJsonAsync<JsonElement>();
|
|
|
|
|
var personId = createContent.GetProperty("id").GetGuid();
|
|
|
|
|
|
|
|
|
|
var updateResponse = await client.PutAsJsonAsync($"api/persons/{personId}", new
|
|
|
|
|
{
|
|
|
|
|
displayName = $"Updated Person {Guid.NewGuid():N}",
|
|
|
|
|
category = "employee",
|
|
|
|
|
isWatchlisted = false
|
|
|
|
|
});
|
|
|
|
|
updateResponse.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Delete_WithAdminAuth_ReturnsOk()
|
|
|
|
|
{
|
|
|
|
|
var client = await Factory.CreateAdminClientAsync();
|
|
|
|
|
var createResponse = await client.PostAsJsonAsync("api/persons", new
|
|
|
|
|
{
|
|
|
|
|
displayName = $"Delete Person {Guid.NewGuid():N}",
|
|
|
|
|
category = "employee"
|
|
|
|
|
});
|
|
|
|
|
var createContent = await createResponse.Content.ReadFromJsonAsync<JsonElement>();
|
|
|
|
|
var personId = createContent.GetProperty("id").GetGuid();
|
|
|
|
|
|
|
|
|
|
var deleteResponse = await client.DeleteAsync($"api/persons/{personId}");
|
|
|
|
|
deleteResponse.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task GetAll_WithCategoryFilter_ReturnsOk()
|
|
|
|
|
{
|
|
|
|
|
var client = await Factory.CreateAdminClientAsync();
|
|
|
|
|
var response = await client.GetAsync("api/persons?category=employee");
|
|
|
|
|
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
|
|
|
|
|
|
|
|
|
|
var content = await response.Content.ReadFromJsonAsync<JsonElement>();
|
|
|
|
|
content.ValueKind.ShouldBe(System.Text.Json.JsonValueKind.Array);
|
|
|
|
|
}
|
2026-10-10 17:50:43 +02:00
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task RegisterFace_WithAdminAuth_ReturnsOk()
|
|
|
|
|
{
|
|
|
|
|
var client = await Factory.CreateAdminClientAsync();
|
|
|
|
|
var createResponse = await client.PostAsJsonAsync("api/persons", new
|
|
|
|
|
{
|
|
|
|
|
displayName = $"Face Person {Guid.NewGuid():N}",
|
|
|
|
|
category = "family"
|
|
|
|
|
});
|
|
|
|
|
var createContent = await createResponse.Content.ReadFromJsonAsync<JsonElement>();
|
|
|
|
|
var personId = createContent.GetProperty("id").GetGuid();
|
|
|
|
|
|
|
|
|
|
var response = await client.PostAsJsonAsync($"api/persons/{personId}/faces", new
|
|
|
|
|
{
|
|
|
|
|
embedding = new float[128],
|
|
|
|
|
thumbnailUrl = "https://example.com/face.jpg",
|
|
|
|
|
sourceEventId = "evt-001"
|
|
|
|
|
});
|
|
|
|
|
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task RegisterFace_WithoutAuth_ReturnsUnauthorized()
|
|
|
|
|
{
|
|
|
|
|
var client = Factory.CreateClient();
|
|
|
|
|
var response = await client.PostAsJsonAsync($"api/persons/{Guid.NewGuid()}/faces", new
|
|
|
|
|
{
|
|
|
|
|
embedding = new float[128]
|
|
|
|
|
});
|
|
|
|
|
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.Unauthorized);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task RegisterFace_EmptyEmbedding_ReturnsBadRequest()
|
|
|
|
|
{
|
|
|
|
|
var client = await Factory.CreateAdminClientAsync();
|
2026-10-10 19:47:09 +02:00
|
|
|
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
|
2026-10-10 17:50:43 +02:00
|
|
|
{
|
|
|
|
|
embedding = Array.Empty<float>()
|
|
|
|
|
});
|
|
|
|
|
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.BadRequest);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task GetFaces_WithAdminAuth_ReturnsOk()
|
|
|
|
|
{
|
|
|
|
|
var client = await Factory.CreateAdminClientAsync();
|
|
|
|
|
var createResponse = await client.PostAsJsonAsync("api/persons", new
|
|
|
|
|
{
|
|
|
|
|
displayName = $"Faces Person {Guid.NewGuid():N}",
|
|
|
|
|
category = "family"
|
|
|
|
|
});
|
|
|
|
|
var createContent = await createResponse.Content.ReadFromJsonAsync<JsonElement>();
|
|
|
|
|
var personId = createContent.GetProperty("id").GetGuid();
|
|
|
|
|
|
|
|
|
|
var response = await client.GetAsync($"api/persons/{personId}/faces");
|
|
|
|
|
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
|
|
|
|
|
|
|
|
|
|
var content = await response.Content.ReadFromJsonAsync<JsonElement>();
|
|
|
|
|
content.ValueKind.ShouldBe(System.Text.Json.JsonValueKind.Array);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task DeleteFace_WithAdminAuth_ReturnsOk()
|
|
|
|
|
{
|
|
|
|
|
var client = await Factory.CreateAdminClientAsync();
|
|
|
|
|
var createResponse = await client.PostAsJsonAsync("api/persons", new
|
|
|
|
|
{
|
|
|
|
|
displayName = $"DeleteFace Person {Guid.NewGuid():N}",
|
|
|
|
|
category = "family"
|
|
|
|
|
});
|
|
|
|
|
var createContent = await createResponse.Content.ReadFromJsonAsync<JsonElement>();
|
|
|
|
|
var personId = createContent.GetProperty("id").GetGuid();
|
|
|
|
|
|
|
|
|
|
var faceResponse = await client.PostAsJsonAsync($"api/persons/{personId}/faces", new
|
|
|
|
|
{
|
|
|
|
|
embedding = new float[128]
|
|
|
|
|
});
|
|
|
|
|
var faceContent = await faceResponse.Content.ReadFromJsonAsync<JsonElement>();
|
|
|
|
|
var faceId = faceContent.GetProperty("id").GetGuid();
|
|
|
|
|
|
|
|
|
|
var response = await client.DeleteAsync($"api/persons/faces/{faceId}");
|
|
|
|
|
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task GetVehicles_ReturnsOk()
|
|
|
|
|
{
|
|
|
|
|
var client = await Factory.CreateAdminClientAsync();
|
|
|
|
|
var response = await client.GetAsync("api/vehicles");
|
|
|
|
|
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task GetEvents_ForPerson_ReturnsOnlyFaceRecognized()
|
|
|
|
|
{
|
|
|
|
|
var client = await Factory.CreateAdminClientAsync();
|
|
|
|
|
var response = await client.GetAsync("api/events?type=FaceRecognized");
|
|
|
|
|
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
2026-10-10 19:47:09 +02:00
|
|
|
public async Task GetAll_WithViewerAuth_ReturnsOk()
|
2026-10-10 17:50:43 +02:00
|
|
|
{
|
|
|
|
|
var client = await Factory.CreateViewerClientAsync();
|
|
|
|
|
var response = await client.GetAsync("api/persons");
|
2026-10-10 19:47:09 +02:00
|
|
|
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
|
2026-10-10 17:50:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Create_WithViewerAuth_ReturnsForbidden()
|
|
|
|
|
{
|
|
|
|
|
var client = await Factory.CreateViewerClientAsync();
|
|
|
|
|
var response = await client.PostAsJsonAsync("api/persons", new
|
|
|
|
|
{
|
|
|
|
|
displayName = "Viewer Person",
|
|
|
|
|
category = "family"
|
|
|
|
|
});
|
|
|
|
|
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.Forbidden);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task DeletePerson_CascadeDeleteFaces()
|
|
|
|
|
{
|
|
|
|
|
var client = await Factory.CreateAdminClientAsync();
|
|
|
|
|
var createResponse = await client.PostAsJsonAsync("api/persons", new
|
|
|
|
|
{
|
|
|
|
|
displayName = $"Cascade Person {Guid.NewGuid():N}",
|
|
|
|
|
category = "family"
|
|
|
|
|
});
|
|
|
|
|
var createContent = await createResponse.Content.ReadFromJsonAsync<JsonElement>();
|
|
|
|
|
var personId = createContent.GetProperty("id").GetGuid();
|
|
|
|
|
|
|
|
|
|
await client.PostAsJsonAsync($"api/persons/{personId}/faces", new { embedding = new float[128] });
|
|
|
|
|
|
|
|
|
|
var deleteResponse = await client.DeleteAsync($"api/persons/{personId}");
|
|
|
|
|
deleteResponse.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
|
|
|
|
|
|
|
|
|
|
var getResponse = await client.GetAsync($"api/persons/{personId}/faces");
|
|
|
|
|
getResponse.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
|
|
|
|
|
var content = await getResponse.Content.ReadFromJsonAsync<JsonElement>();
|
|
|
|
|
content.GetArrayLength().ShouldBe(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task CreatePerson_AuditLogEntryCreated()
|
|
|
|
|
{
|
|
|
|
|
var client = await Factory.CreateAdminClientAsync();
|
|
|
|
|
var response = await client.PostAsJsonAsync("api/persons", new
|
|
|
|
|
{
|
|
|
|
|
displayName = $"Audit Person {Guid.NewGuid():N}",
|
|
|
|
|
category = "family"
|
|
|
|
|
});
|
|
|
|
|
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
|
|
|
|
|
|
|
|
|
|
var auditResponse = await client.GetAsync("api/audit-logs");
|
|
|
|
|
auditResponse.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
|
|
|
|
|
}
|
2026-07-23 15:27:34 +00:00
|
|
|
}
|