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)
246 lines
7.7 KiB
C#
246 lines
7.7 KiB
C#
using Heimdall.Domain.Entities;
|
|
using Heimdall.Domain.Enums;
|
|
using Shouldly;
|
|
|
|
namespace Heimdall.Domain.Tests.Entities;
|
|
|
|
public class NotificationTests
|
|
{
|
|
[Fact]
|
|
public void Create_notification_should_set_properties()
|
|
{
|
|
var userId = Guid.NewGuid();
|
|
var cameraId = Guid.NewGuid();
|
|
var siteId = Guid.NewGuid();
|
|
var now = DateTime.UtcNow;
|
|
|
|
var notification = new Notification(
|
|
userId, "Test Title", "Test Message", NotificationType.Warning,
|
|
EventType.PersonDetected, cameraId, siteId,
|
|
"http://snap.url", "/events/123");
|
|
|
|
notification.Id.ShouldNotBe(Guid.Empty);
|
|
notification.UserId.ShouldBe(userId);
|
|
notification.Title.ShouldBe("Test Title");
|
|
notification.Message.ShouldBe("Test Message");
|
|
notification.Type.ShouldBe(NotificationType.Warning);
|
|
notification.EventType.ShouldBe(EventType.PersonDetected);
|
|
notification.CameraId.ShouldBe(cameraId);
|
|
notification.SiteId.ShouldBe(siteId);
|
|
notification.SnapshotUrl.ShouldBe("http://snap.url");
|
|
notification.Link.ShouldBe("/events/123");
|
|
notification.IsRead.ShouldBeFalse();
|
|
notification.ReadAt.ShouldBeNull();
|
|
notification.CreatedAt.ShouldBe(default);
|
|
notification.UpdatedAt.ShouldBe(default);
|
|
}
|
|
|
|
[Fact]
|
|
public void Create_notification_with_minimal_params_should_succeed()
|
|
{
|
|
var userId = Guid.NewGuid();
|
|
var notification = new Notification(userId, "Title", "Message", NotificationType.Info);
|
|
|
|
notification.Id.ShouldNotBe(Guid.Empty);
|
|
notification.UserId.ShouldBe(userId);
|
|
notification.Title.ShouldBe("Title");
|
|
notification.Message.ShouldBe("Message");
|
|
notification.Type.ShouldBe(NotificationType.Info);
|
|
notification.EventType.ShouldBeNull();
|
|
notification.CameraId.ShouldBeNull();
|
|
notification.SiteId.ShouldBeNull();
|
|
notification.SnapshotUrl.ShouldBeNull();
|
|
notification.Link.ShouldBeNull();
|
|
notification.IsRead.ShouldBeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public void MarkAsRead_should_set_properties()
|
|
{
|
|
var userId = Guid.NewGuid();
|
|
var notification = new Notification(userId, "Title", "Message", NotificationType.Info);
|
|
|
|
notification.MarkAsRead();
|
|
|
|
notification.IsRead.ShouldBeTrue();
|
|
notification.ReadAt.ShouldNotBeNull();
|
|
notification.ReadAt.Value.ShouldBeLessThanOrEqualTo(DateTime.UtcNow);
|
|
notification.ReadAt.Value.ShouldBeGreaterThan(DateTime.UtcNow.AddMinutes(-1));
|
|
}
|
|
|
|
[Fact]
|
|
public void MarkAsRead_twice_should_update_read_at()
|
|
{
|
|
var userId = Guid.NewGuid();
|
|
var notification = new Notification(userId, "Title", "Message", NotificationType.Info);
|
|
|
|
notification.MarkAsRead();
|
|
var firstReadAt = notification.ReadAt!.Value;
|
|
|
|
Thread.Sleep(10); // Ensure time passes
|
|
notification.MarkAsRead();
|
|
|
|
notification.IsRead.ShouldBeTrue();
|
|
notification.ReadAt!.Value.ShouldBeGreaterThan(firstReadAt);
|
|
}
|
|
|
|
[Fact]
|
|
public void Enum_NotificationType_has_expected_values()
|
|
{
|
|
((int)NotificationType.Info).ShouldBe(0);
|
|
((int)NotificationType.Success).ShouldBe(1);
|
|
((int)NotificationType.Warning).ShouldBe(2);
|
|
((int)NotificationType.Error).ShouldBe(3);
|
|
((int)NotificationType.Alert).ShouldBe(4);
|
|
}
|
|
|
|
[Fact]
|
|
public void Enum_NotificationChannel_has_expected_values()
|
|
{
|
|
((int)NotificationChannel.InApp).ShouldBe(0);
|
|
((int)NotificationChannel.Email).ShouldBe(1);
|
|
((int)NotificationChannel.Gotify).ShouldBe(2);
|
|
}
|
|
}
|
|
|
|
public class NotificationTemplateTests
|
|
{
|
|
[Fact]
|
|
public void Create_template_should_set_properties()
|
|
{
|
|
var template = new NotificationTemplate(
|
|
EventType.PersonDetected, NotificationChannel.Email,
|
|
"Personne détectée", "Une personne a été détectée sur {CameraName}",
|
|
"fr", true);
|
|
|
|
template.Id.ShouldNotBe(Guid.Empty);
|
|
template.EventType.ShouldBe(EventType.PersonDetected);
|
|
template.Channel.ShouldBe(NotificationChannel.Email);
|
|
template.Language.ShouldBe("fr");
|
|
template.Subject.ShouldBe("Personne détectée");
|
|
template.BodyMarkdown.ShouldBe("Une personne a été détectée sur {CameraName}");
|
|
template.IsDefault.ShouldBeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void Create_template_with_default_language_should_be_french()
|
|
{
|
|
var template = new NotificationTemplate(
|
|
EventType.MotionDetected, NotificationChannel.InApp,
|
|
"Mouvement", "Mouvement détecté");
|
|
|
|
template.Language.ShouldBe("fr");
|
|
template.IsDefault.ShouldBeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public void Update_template_should_modify_properties()
|
|
{
|
|
var template = new NotificationTemplate(
|
|
EventType.PersonDetected, NotificationChannel.Email,
|
|
"Old Subject", "Old Body", "fr", false);
|
|
|
|
template.Update("New Subject", "New Body", true);
|
|
|
|
template.Subject.ShouldBe("New Subject");
|
|
template.BodyMarkdown.ShouldBe("New Body");
|
|
template.IsDefault.ShouldBeTrue();
|
|
}
|
|
}
|
|
|
|
public class UserNotificationConfigTests
|
|
{
|
|
[Fact]
|
|
public void Create_config_should_set_user_id()
|
|
{
|
|
var userId = Guid.NewGuid();
|
|
var config = new UserNotificationConfig(userId);
|
|
|
|
config.Id.ShouldNotBe(Guid.Empty);
|
|
config.UserId.ShouldBe(userId);
|
|
config.EmailEnabled.ShouldBeFalse();
|
|
config.GotifyEnabled.ShouldBeFalse();
|
|
config.Language.ShouldBe("fr");
|
|
config.DailyDigest.ShouldBeFalse();
|
|
config.WeeklyDigest.ShouldBeFalse();
|
|
config.SubscribedEventsJson.ShouldBeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public void UpdateEmail_should_set_properties()
|
|
{
|
|
var config = new UserNotificationConfig(Guid.NewGuid());
|
|
|
|
config.UpdateEmail("test@example.com", true);
|
|
|
|
config.EmailAddress.ShouldBe("test@example.com");
|
|
config.EmailEnabled.ShouldBeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void UpdateEmail_with_null_address_should_disable()
|
|
{
|
|
var config = new UserNotificationConfig(Guid.NewGuid());
|
|
config.UpdateEmail("test@example.com", true);
|
|
|
|
config.UpdateEmail(null, false);
|
|
|
|
config.EmailAddress.ShouldBeNull();
|
|
config.EmailEnabled.ShouldBeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public void UpdateGotify_should_set_properties()
|
|
{
|
|
var config = new UserNotificationConfig(Guid.NewGuid());
|
|
|
|
config.UpdateGotify("http://gotify:8080", "token123", true);
|
|
|
|
config.GotifyUrl.ShouldBe("http://gotify:8080");
|
|
config.GotifyToken.ShouldBe("token123");
|
|
config.GotifyEnabled.ShouldBeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void UpdateDigests_should_set_properties()
|
|
{
|
|
var config = new UserNotificationConfig(Guid.NewGuid());
|
|
|
|
config.UpdateDigests(true, true);
|
|
|
|
config.DailyDigest.ShouldBeTrue();
|
|
config.WeeklyDigest.ShouldBeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void SetSubscribedEvents_should_store_json()
|
|
{
|
|
var config = new UserNotificationConfig(Guid.NewGuid());
|
|
var json = "[\"PersonDetected\",\"MotionDetected\"]";
|
|
|
|
config.SetSubscribedEvents(json);
|
|
|
|
config.SubscribedEventsJson.ShouldBe(json);
|
|
}
|
|
|
|
[Fact]
|
|
public void SetSubscribedEvents_with_null_should_clear()
|
|
{
|
|
var config = new UserNotificationConfig(Guid.NewGuid());
|
|
config.SetSubscribedEvents("[\"test\"]");
|
|
config.SetSubscribedEvents(null);
|
|
|
|
config.SubscribedEventsJson.ShouldBeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public void SetLanguage_should_update_language()
|
|
{
|
|
var config = new UserNotificationConfig(Guid.NewGuid());
|
|
|
|
config.SetLanguage("en");
|
|
|
|
config.Language.ShouldBe("en");
|
|
}
|
|
}
|