- Nouveau NtfyService: topic isolé par user (heimdall_{userId})
- Configuration globale Ntfy:BaseUrl + Ntfy:Token (plus de token par user)
- Renommage UserNotificationConfig: Gotify* → Ntfy*
- Migration des colonnes DB: gotify_enabled/url/token → ntfy_enabled/url
- Ajout service ntfy dans docker-compose (binwiederhier/ntfy)
- Mise à jour .env.example avec NTFY__BASE_URL et NTFY__TOKEN
- Suppression GotifyService et IGotifyService
245 lines
7.6 KiB
C#
245 lines
7.6 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");
|
|
}
|
|
}
|