2026-07-19 13:14:05 +00:00
|
|
|
using System.Net.Http.Json;
|
|
|
|
|
using System.Text.Json;
|
2026-10-20 21:00:38 +02:00
|
|
|
using Heimdall.Domain.Entities;
|
|
|
|
|
using Heimdall.Domain.Enums;
|
|
|
|
|
using Heimdall.Infrastructure.Data;
|
2026-07-19 13:14:05 +00:00
|
|
|
using Heimdall.IntegrationTests.Factories;
|
2026-10-20 21:00:38 +02:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2026-07-19 13:14:05 +00:00
|
|
|
using Shouldly;
|
|
|
|
|
|
|
|
|
|
namespace Heimdall.IntegrationTests.Controllers;
|
|
|
|
|
|
2026-07-21 10:24:11 +00:00
|
|
|
public class EventsControllerTests : PostgresTestBase
|
2026-07-19 13:14:05 +00:00
|
|
|
{
|
|
|
|
|
private HttpClient? _cachedClient;
|
|
|
|
|
|
2026-07-21 10:24:11 +00:00
|
|
|
public EventsControllerTests(PostgreSqlContainerFixture postgres) : base(postgres) { }
|
2026-07-19 13:14:05 +00:00
|
|
|
|
|
|
|
|
private async Task<HttpClient> AdminClient()
|
|
|
|
|
{
|
|
|
|
|
if (_cachedClient is not null) return _cachedClient;
|
2026-07-21 10:24:11 +00:00
|
|
|
_cachedClient = await Factory.CreateAdminClientAsync();
|
2026-07-19 13:14:05 +00:00
|
|
|
return _cachedClient;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task CreateEvent_WithoutAuth_ReturnsUnauthorized()
|
|
|
|
|
{
|
|
|
|
|
// Events endpoint requires ApiKey auth (agents), not JWT
|
2026-07-21 10:24:11 +00:00
|
|
|
var client = Factory.CreateClient();
|
2026-07-19 13:14:05 +00:00
|
|
|
var response = await client.PostAsJsonAsync("api/events", new
|
|
|
|
|
{
|
|
|
|
|
cameraId = Guid.NewGuid(),
|
|
|
|
|
type = 0,
|
|
|
|
|
confidence = 0.95
|
|
|
|
|
});
|
|
|
|
|
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.Unauthorized);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task GetEventById_RequiresAuth_ReturnsUnauthorized()
|
|
|
|
|
{
|
2026-07-21 10:24:11 +00:00
|
|
|
var client = Factory.CreateClient();
|
2026-07-19 13:14:05 +00:00
|
|
|
var response = await client.GetAsync($"api/events/{Guid.NewGuid()}");
|
|
|
|
|
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.Unauthorized);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task QueryEvents_RequiresAuth_ReturnsUnauthorized()
|
|
|
|
|
{
|
2026-07-21 10:24:11 +00:00
|
|
|
var client = Factory.CreateClient();
|
2026-07-19 13:14:05 +00:00
|
|
|
var response = await client.GetAsync("api/events");
|
|
|
|
|
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.Unauthorized);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
2026-10-20 07:38:05 +02:00
|
|
|
public async Task GetEventById_UnknownId_ReturnsForbidden()
|
2026-07-19 13:14:05 +00:00
|
|
|
{
|
|
|
|
|
var client = await AdminClient();
|
|
|
|
|
var response = await client.GetAsync($"api/events/{Guid.NewGuid()}");
|
2026-10-20 07:38:05 +02:00
|
|
|
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.Forbidden);
|
2026-07-19 13:14:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task QueryEvents_NoFilters_ReturnsOk()
|
|
|
|
|
{
|
|
|
|
|
var client = await AdminClient();
|
|
|
|
|
var response = await client.GetAsync("api/events?page=1&pageSize=10");
|
|
|
|
|
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
|
|
|
|
|
|
|
|
|
|
var content = await response.Content.ReadFromJsonAsync<JsonElement>();
|
|
|
|
|
content.GetProperty("items").GetArrayLength().ShouldBe(0);
|
|
|
|
|
content.GetProperty("totalCount").GetInt32().ShouldBe(0);
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-20 21:00:38 +02:00
|
|
|
[Fact]
|
|
|
|
|
public async Task QuerySnapshots_SearchesCameraNameAndFiltersBeforePagination_OnPostgres()
|
|
|
|
|
{
|
|
|
|
|
var client = await AdminClient();
|
|
|
|
|
var site = new Site($"Gallery {Guid.NewGuid():N}");
|
|
|
|
|
var matchingCamera = new Camera(site.Id, "North Gate");
|
|
|
|
|
var otherCamera = new Camera(site.Id, "South Hall");
|
|
|
|
|
var now = DateTime.UtcNow;
|
|
|
|
|
|
|
|
|
|
var newestSnapshot = Snapshot(matchingCamera.Id, now.AddMinutes(-3), "North newest", hasSnapshot: true);
|
|
|
|
|
var noSnapshot = Snapshot(matchingCamera.Id, now.AddMinutes(-2), "North no image", hasSnapshot: false);
|
|
|
|
|
var otherCameraSnapshot = Snapshot(otherCamera.Id, now.AddMinutes(-1), "South image", hasSnapshot: true);
|
|
|
|
|
var secondSnapshot = Snapshot(matchingCamera.Id, now.AddMinutes(-4), "North second", hasSnapshot: true);
|
|
|
|
|
|
|
|
|
|
await using (var scope = Factory.Services.CreateAsyncScope())
|
|
|
|
|
{
|
|
|
|
|
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
|
|
|
|
db.Sites.Add(site);
|
|
|
|
|
db.Cameras.AddRange(matchingCamera, otherCamera);
|
|
|
|
|
db.CameraEvents.AddRange(newestSnapshot, noSnapshot, otherCameraSnapshot, secondSnapshot);
|
|
|
|
|
await db.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var response = await client.GetAsync("api/events?q=gAtE&hasSnapshot=true&page=2&pageSize=1");
|
|
|
|
|
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
|
|
|
|
|
var content = await response.Content.ReadFromJsonAsync<JsonElement>();
|
|
|
|
|
content.GetProperty("totalCount").GetInt32().ShouldBe(2);
|
|
|
|
|
content.GetProperty("page").GetInt32().ShouldBe(2);
|
|
|
|
|
content.GetProperty("pageSize").GetInt32().ShouldBe(1);
|
|
|
|
|
var items = content.GetProperty("items");
|
|
|
|
|
items.GetArrayLength().ShouldBe(1);
|
|
|
|
|
items[0].GetProperty("label").GetString().ShouldBe("North second");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static CameraEvent Snapshot(Guid cameraId, DateTime occurredAt, string label, bool hasSnapshot)
|
|
|
|
|
{
|
|
|
|
|
var cameraEvent = new CameraEvent(cameraId, EventType.PersonDetected, 0.9, occurredAt);
|
|
|
|
|
cameraEvent.SetMetadata(label, null);
|
|
|
|
|
if (hasSnapshot)
|
|
|
|
|
cameraEvent.SetSnapshot($"snapshots/{label}.jpg");
|
|
|
|
|
return cameraEvent;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-19 13:14:05 +00:00
|
|
|
[Fact]
|
2026-10-20 07:38:05 +02:00
|
|
|
public async Task GetRecentByCamera_UnknownCamera_ReturnsForbidden()
|
2026-07-19 13:14:05 +00:00
|
|
|
{
|
|
|
|
|
var client = await AdminClient();
|
|
|
|
|
var response = await client.GetAsync($"api/events/camera/{Guid.NewGuid()}");
|
2026-10-20 07:38:05 +02:00
|
|
|
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.Forbidden);
|
2026-07-19 13:14:05 +00:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|