using System.Net.Http.Json; using System.Text.Json; using Heimdall.Domain.Entities; using Heimdall.Domain.Enums; using Heimdall.Infrastructure.Data; using Heimdall.IntegrationTests.Factories; using Microsoft.Extensions.DependencyInjection; using Shouldly; namespace Heimdall.IntegrationTests.Controllers; public class EventsControllerTests : PostgresTestBase { private HttpClient? _cachedClient; public EventsControllerTests(PostgreSqlContainerFixture postgres) : base(postgres) { } private async Task AdminClient() { if (_cachedClient is not null) return _cachedClient; _cachedClient = await Factory.CreateAdminClientAsync(); return _cachedClient; } [Fact] public async Task CreateEvent_WithoutAuth_ReturnsUnauthorized() { // Events endpoint requires ApiKey auth (agents), not JWT var client = Factory.CreateClient(); 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() { var client = Factory.CreateClient(); var response = await client.GetAsync($"api/events/{Guid.NewGuid()}"); response.StatusCode.ShouldBe(System.Net.HttpStatusCode.Unauthorized); } [Fact] public async Task QueryEvents_RequiresAuth_ReturnsUnauthorized() { var client = Factory.CreateClient(); var response = await client.GetAsync("api/events"); response.StatusCode.ShouldBe(System.Net.HttpStatusCode.Unauthorized); } [Fact] public async Task GetEventById_UnknownId_ReturnsNotFound() { var client = await AdminClient(); var response = await client.GetAsync($"api/events/{Guid.NewGuid()}"); response.StatusCode.ShouldBe(System.Net.HttpStatusCode.NotFound); } [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(); content.GetProperty("items").GetArrayLength().ShouldBe(0); content.GetProperty("totalCount").GetInt32().ShouldBe(0); } [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(); 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(); 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; } [Fact] public async Task GetRecentByCamera_UnknownCamera_ReturnsEmpty() { var client = await AdminClient(); var response = await client.GetAsync($"api/events/camera/{Guid.NewGuid()}"); response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK); var content = await response.Content.ReadFromJsonAsync(); content.GetArrayLength().ShouldBe(0); } }