## Contexte Issue #61 : Gestion des types d'alertes — abonnement par type d'événement + sévérité/priorité. Les utilisateurs ne peuvent pas filtrer quelles notifications push/email ils reçoivent. Tous les événements (mouvement, personne, caméra hors ligne...) sont envoyés sans distinction. ## Changements ### Domaine - **EventTypePriorityMap** (`src/Heimdall.Domain/Helpers/`) : mapping `EventType` → priorité ntfy (1-5) - CameraOffline/UnknownPerson = 5 (urgent), PersonDetected/FaceRecognized = 4 (high), MotionDetected/VehicleDetected/LicensePlateDetected = 3 (default), CameraOnline = 2 (low) - **UserNotificationConfig** : ajout `SubscribedEventsJson` (JSON array), `GetSubscribedEvents()`, `IsEventTypeSubscribed()`, `SetSubscribedEvents()` - `null` ou `[]` = recevoir tous les événements (comportement par défaut) ### Infrastructure - **NotificationService.DispatchToExternalChannelsAsync** : filtre par `IsEventTypeSubscribed()` avant envoi email/ntfy - Priorité ntfy calculée via `EventTypePriorityMap.GetPriority()` et passée à `NtfyService` ### UI (Profile.razor) - Checkboxes par type d'événement dans la section Notifications - Texte explicatif : « Si aucun n'est sélectionné, tous les événements sont notifiés » - Sauvegarde via `UpdateNotificationConfigRequest.SubscribedEvents` ### Tests - 10 tests Domain : `UserNotificationConfig` (GetSubscribedEvents, IsEventTypeSubscribed, cas limites JSON invalide/vidé/unknown) - 1 test Domain : `EventTypePriorityMap` (couvre les 8 EventTypes + null) - 3 tests Infrastructure : filtrage par abonnement + priorité ntfy + filtre vide ## Vérification - `dotnet build` : 0 erreur, 0 warning - Domain.Tests : 161 passed - Infrastructure.Tests : 180 passed - Application.Tests : 421 passed - Web.Tests : 243 passed - Agent.Tests : 206 passed - Total : 1211 tests ✅ Closes #61 Reviewed-on: https://forgejo.tordais.cc/nonobis/Heimdall/pulls/154
330 lines
11 KiB
C#
330 lines
11 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.Ntfy).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.NtfyEnabled.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 UpdateNtfy_should_set_properties()
|
|
{
|
|
var config = new UserNotificationConfig(Guid.NewGuid());
|
|
|
|
config.UpdateNtfy("http://ntfy:80", true);
|
|
|
|
config.NtfyUrl.ShouldBe("http://ntfy:80");
|
|
config.NtfyEnabled.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");
|
|
}
|
|
|
|
[Fact]
|
|
public void GetSubscribedEvents_with_null_json_should_return_null()
|
|
{
|
|
var config = new UserNotificationConfig(Guid.NewGuid());
|
|
config.GetSubscribedEvents().ShouldBeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public void GetSubscribedEvents_with_empty_json_should_return_null()
|
|
{
|
|
var config = new UserNotificationConfig(Guid.NewGuid());
|
|
config.SetSubscribedEvents("[]");
|
|
config.GetSubscribedEvents().ShouldBeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public void GetSubscribedEvents_with_valid_json_should_parse()
|
|
{
|
|
var config = new UserNotificationConfig(Guid.NewGuid());
|
|
config.SetSubscribedEvents("[\"PersonDetected\",\"CameraOffline\"]");
|
|
|
|
var events = config.GetSubscribedEvents();
|
|
events.ShouldNotBeNull();
|
|
events.Count.ShouldBe(2);
|
|
events.ShouldContain(EventType.PersonDetected);
|
|
events.ShouldContain(EventType.CameraOffline);
|
|
}
|
|
|
|
[Fact]
|
|
public void GetSubscribedEvents_with_invalid_json_should_return_null()
|
|
{
|
|
var config = new UserNotificationConfig(Guid.NewGuid());
|
|
config.SetSubscribedEvents("not-json");
|
|
|
|
config.GetSubscribedEvents().ShouldBeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public void GetSubscribedEvents_with_unknown_values_should_filter_them_out()
|
|
{
|
|
var config = new UserNotificationConfig(Guid.NewGuid());
|
|
config.SetSubscribedEvents("[\"PersonDetected\",\"BogusType\"]");
|
|
|
|
var events = config.GetSubscribedEvents();
|
|
events.ShouldNotBeNull();
|
|
events.Count.ShouldBe(1);
|
|
events.ShouldContain(EventType.PersonDetected);
|
|
}
|
|
|
|
[Fact]
|
|
public void IsEventTypeSubscribed_with_no_filter_should_return_true()
|
|
{
|
|
var config = new UserNotificationConfig(Guid.NewGuid());
|
|
// No subscribed events → all events pass
|
|
config.IsEventTypeSubscribed(EventType.PersonDetected).ShouldBeTrue();
|
|
config.IsEventTypeSubscribed(EventType.CameraOffline).ShouldBeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void IsEventTypeSubscribed_with_matching_event_should_return_true()
|
|
{
|
|
var config = new UserNotificationConfig(Guid.NewGuid());
|
|
config.SetSubscribedEvents("[\"PersonDetected\"]");
|
|
|
|
config.IsEventTypeSubscribed(EventType.PersonDetected).ShouldBeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void IsEventTypeSubscribed_with_non_matching_event_should_return_false()
|
|
{
|
|
var config = new UserNotificationConfig(Guid.NewGuid());
|
|
config.SetSubscribedEvents("[\"PersonDetected\"]");
|
|
|
|
config.IsEventTypeSubscribed(EventType.CameraOffline).ShouldBeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public void IsEventTypeSubscribed_with_null_event_type_should_return_true()
|
|
{
|
|
var config = new UserNotificationConfig(Guid.NewGuid());
|
|
config.SetSubscribedEvents("[\"PersonDetected\"]");
|
|
|
|
// null EventType (system events) always passes
|
|
config.IsEventTypeSubscribed(null).ShouldBeTrue();
|
|
}
|
|
}
|