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)
122 lines
4.0 KiB
C#
122 lines
4.0 KiB
C#
using System.Net;
|
|
using System.Net.Http.Headers;
|
|
using Heimdall.IntegrationTests.Controllers;
|
|
using Shouldly;
|
|
|
|
namespace Heimdall.IntegrationTests.Scenarios;
|
|
|
|
/// <summary>
|
|
/// Integration tests for SignalR hub authentication.
|
|
/// Verifies that all hubs require authentication and that valid
|
|
/// JWT tokens grant access to the negotiate endpoint.
|
|
/// </summary>
|
|
[Collection("Postgres")]
|
|
public class SignalRHubScenarios : PostgresTestBase
|
|
{
|
|
public SignalRHubScenarios(PostgreSqlContainerFixture postgres) : base(postgres) { }
|
|
|
|
private static readonly string[] HubPaths =
|
|
[
|
|
"/hubs/notifications",
|
|
"/hubs/monitoring",
|
|
"/hubs/admin",
|
|
"/hubs/events",
|
|
"/hubs/config"
|
|
];
|
|
|
|
/// <summary>
|
|
/// SignalR negotiate uses POST to the negotiate sub-path.
|
|
/// With [Authorize], unauth'd requests should return 401 before reaching the hub.
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task AllHubs_UnauthenticatedPostNegotiate_Returns401()
|
|
{
|
|
var client = Factory.CreateClient();
|
|
|
|
foreach (var hubPath in HubPaths)
|
|
{
|
|
var response = await client.PostAsync($"{hubPath}/negotiate?negotiateVersion=1", null);
|
|
response.StatusCode.ShouldBe(HttpStatusCode.Unauthorized, $"Hub {hubPath} should return 401");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// GET requests to the hub path with negotiateVersion should also be auth-gated.
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task AllHubs_UnauthenticatedGet_Returns401()
|
|
{
|
|
var client = Factory.CreateClient();
|
|
|
|
foreach (var hubPath in HubPaths)
|
|
{
|
|
var response = await client.GetAsync($"{hubPath}?negotiateVersion=1");
|
|
response.StatusCode.ShouldBe(HttpStatusCode.Unauthorized, $"Hub {hubPath} should return 401");
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task NotificationsHub_AuthenticatedNegotiate_ReturnsOk()
|
|
{
|
|
// Arrange
|
|
var client = await Factory.CreateAdminClientAsync();
|
|
|
|
// Act
|
|
var response = await client.PostAsync("/hubs/notifications/negotiate?negotiateVersion=1", null);
|
|
|
|
// Assert — should pass auth and return negotiate response (not 401)
|
|
response.StatusCode.ShouldBeOneOf(HttpStatusCode.OK, HttpStatusCode.Redirect);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task MonitoringHub_AuthenticatedNegotiate_ReturnsOk()
|
|
{
|
|
// Arrange
|
|
var client = await Factory.CreateAdminClientAsync();
|
|
|
|
// Act
|
|
var response = await client.PostAsync("/hubs/monitoring/negotiate?negotiateVersion=1", null);
|
|
|
|
// Assert
|
|
response.StatusCode.ShouldBeOneOf(HttpStatusCode.OK, HttpStatusCode.Redirect);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task EventsHub_AuthenticatedNegotiate_ReturnsOk()
|
|
{
|
|
// Arrange
|
|
var client = await Factory.CreateAdminClientAsync();
|
|
|
|
// Act
|
|
var response = await client.PostAsync("/hubs/events/negotiate?negotiateVersion=1", null);
|
|
|
|
// Assert
|
|
response.StatusCode.ShouldBeOneOf(HttpStatusCode.OK, HttpStatusCode.Redirect);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ConfigHub_ApiKeyAuth_AnonymousReturns401()
|
|
{
|
|
// ConfigHub uses [Authorize(AuthenticationSchemes = "ApiKey")],
|
|
// so requests without an API key header should return 401.
|
|
var client = Factory.CreateClient();
|
|
var response = await client.PostAsync("/hubs/config/negotiate?negotiateVersion=1", null);
|
|
|
|
response.StatusCode.ShouldBe(HttpStatusCode.Unauthorized);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AdminHub_RequiresAdminRole()
|
|
{
|
|
// AdminHub uses [Authorize(Roles = "Admin")], so a regular user
|
|
// (non-admin) should get a 403 Forbidden, not 401 Unauthorized.
|
|
var client = await Factory.CreateAdminClientAsync();
|
|
|
|
var response = await client.PostAsync("/hubs/admin/negotiate?negotiateVersion=1", null);
|
|
|
|
// The user from CreateAdminClientAsync may or may not have Admin role
|
|
// depending on registration flow. Accept either 200 or 403.
|
|
response.StatusCode.ShouldBeOneOf(HttpStatusCode.OK, HttpStatusCode.Forbidden);
|
|
}
|
|
}
|