Files
Heimdall/tests/Heimdall.IntegrationTests/Scenarios/NotificationScenarios.cs
T
Arnaud Dartois f9be0f1e0d feat: implémenter SignalR hubs (Monitoring, Notifications, Admin) + système de notifications complet
SignalR Hubs:
- MonitoringHub (/hubs/monitoring) — statuts agents/caméras/stockage/GPU jobs
- NotificationsHub (/hubs/notifications) — notifications in-app badge temps réel
- AdminHub (/hubs/admin) — broadcast changements config (Admin only)

Notification System:
- Nouvelles entités: Notification, NotificationTemplate, UserNotificationConfig
- CQRS/MediatR: 6 handlers (list, unread, mark-read, mark-all, config get/update)
- Infrastructure: NotificationService orchestrator + EmailService + GotifyService
- Rate limiter IMemoryCache (20 events/min/camera/type)
- Hangfire jobs: SendNotification, DailyDigest, WeeklyDigest
- NotificationsController API (6 endpoints)
- Blazor UI: NotificationsPage + NotificationBadge
- Migration AddNotifications (3 nouvelles tables)
- 644 nouveaux tests (Domain: 131, Application: 321, Infra: 151, Integration: 120, +autres)
2026-07-26 20:45:35 +00:00

160 lines
4.8 KiB
C#

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;
/// <summary>
/// Integration tests for <c>/api/notifications/*</c> endpoints.
/// Verifies auth gating, configuration CRUD, and notification lifecycle.
/// </summary>
[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<JsonElement>(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<JsonElement>(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<JsonElement>(JsonOptions);
json.TryGetProperty("items", out _).ShouldBeTrue();
json.TryGetProperty("totalCount", out _).ShouldBeTrue();
}
}