using System.Net; using System.Net.Http.Headers; using System.Net.Http.Json; using System.Text.Json; using Heimdall.IntegrationTests.Controllers; using Shouldly; namespace Heimdall.IntegrationTests.Scenarios; /// /// Integration tests for /api/notifications/* endpoints. /// Verifies auth gating, configuration CRUD, and notification lifecycle. /// [Collection("Postgres")] public class NotificationScenarios : PostgresTestBase { private static readonly JsonSerializerOptions JsonOptions = new() { PropertyNameCaseInsensitive = true }; public NotificationScenarios(PostgreSqlContainerFixture postgres) : base(postgres) { } [Fact] public async Task GetNotifications_Unauthenticated_Returns401() { var client = Factory.CreateClient(); var response = await client.GetAsync("/api/notifications"); response.StatusCode.ShouldBe(HttpStatusCode.Unauthorized); } [Fact] public async Task GetUnreadCount_Unauthenticated_Returns401() { var client = Factory.CreateClient(); var response = await client.GetAsync("/api/notifications/unread-count"); response.StatusCode.ShouldBe(HttpStatusCode.Unauthorized); } [Fact] public async Task MarkAllAsRead_Unauthenticated_Returns401() { var client = Factory.CreateClient(); var response = await client.PostAsync("/api/notifications/mark-all-read", null); response.StatusCode.ShouldBe(HttpStatusCode.Unauthorized); } [Fact] public async Task GetConfig_Unauthenticated_Returns401() { var client = Factory.CreateClient(); var response = await client.GetAsync("/api/notifications/config"); response.StatusCode.ShouldBe(HttpStatusCode.Unauthorized); } [Fact] public async Task Authenticated_GetNotifications_ReturnsOk() { // Arrange — login as admin var client = await Factory.CreateAdminClientAsync(); // Act var response = await client.GetAsync("/api/notifications"); // Assert response.StatusCode.ShouldBe(HttpStatusCode.OK); var json = await response.Content.ReadFromJsonAsync(JsonOptions); json.ValueKind.ShouldBe(JsonValueKind.Object); } [Fact] public async Task Authenticated_GetUnreadCount_ReturnsOkWithCount() { // Arrange var client = await Factory.CreateAdminClientAsync(); // Act var response = await client.GetAsync("/api/notifications/unread-count"); // Assert response.StatusCode.ShouldBe(HttpStatusCode.OK); var json = await response.Content.ReadFromJsonAsync(JsonOptions); json.GetProperty("count").GetInt32().ShouldBeGreaterThanOrEqualTo(0); } [Fact] public async Task Authenticated_GetConfig_ReturnsOk() { // Arrange var client = await Factory.CreateAdminClientAsync(); // Act var response = await client.GetAsync("/api/notifications/config"); // Assert response.StatusCode.ShouldBe(HttpStatusCode.OK); } [Fact] public async Task Authenticated_MarkAllAsRead_ReturnsOk() { // Arrange var client = await Factory.CreateAdminClientAsync(); // Act var response = await client.PostAsync("/api/notifications/mark-all-read", null); // Assert response.StatusCode.ShouldBe(HttpStatusCode.OK); } [Fact] public async Task Authenticated_MarkAsRead_NonExistent_ReturnsNotFound() { // Arrange var client = await Factory.CreateAdminClientAsync(); var nonExistentId = Guid.NewGuid(); // Act var response = await client.PostAsync($"/api/notifications/{nonExistentId}/read", null); // Assert response.StatusCode.ShouldBe(HttpStatusCode.NotFound); } [Fact] public async Task Authenticated_UpdateConfig_ReturnsOk() { // Arrange var client = await Factory.CreateAdminClientAsync(); // Act var response = await client.PutAsJsonAsync("/api/notifications/config", new { emailEnabled = true, emailAddress = "test@example.com", dailyDigest = true }); // Assert response.StatusCode.ShouldBe(HttpStatusCode.OK); } [Fact] public async Task Authenticated_GetNotifications_ShouldPage() { // Arrange var client = await Factory.CreateAdminClientAsync(); // Act var response = await client.GetAsync("/api/notifications?page=1&pageSize=5"); // Assert response.StatusCode.ShouldBe(HttpStatusCode.OK); var json = await response.Content.ReadFromJsonAsync(JsonOptions); json.TryGetProperty("items", out _).ShouldBeTrue(); json.TryGetProperty("totalCount", out _).ShouldBeTrue(); } }