Files
Heimdall/tests/Heimdall.Application.Tests/Handlers/Notifications/NotificationHandlerTests.cs
T

367 lines
15 KiB
C#
Raw Normal View History

using AutoMapper;
using Heimdall.Application.Common;
using Heimdall.Application.DTOs;
using Heimdall.Application.Notifications.Commands.MarkAllAsRead;
using Heimdall.Application.Notifications.Commands.MarkAsRead;
using Heimdall.Application.Notifications.Commands.UpdateConfig;
using Heimdall.Application.Notifications.Mapping;
using Heimdall.Application.Notifications.Queries.GetConfig;
using Heimdall.Application.Notifications.Queries.GetNotifications;
using Heimdall.Application.Notifications.Queries.GetUnreadCount;
using Heimdall.Application.Tests.Common;
using Heimdall.Domain.Entities;
using Heimdall.Domain.Enums;
using NSubstitute;
namespace Heimdall.Application.Tests.Handlers.Notifications;
public class NotificationHandlerTests
{
private static readonly IMapper Mapper = new MapperConfiguration(cfg =>
cfg.AddProfile(new NotificationMappingProfile())).CreateMapper();
private readonly IApplicationDbContext _context;
private readonly CancellationToken _ct = CancellationToken.None;
public NotificationHandlerTests()
{
_context = Substitute.For<IApplicationDbContext>();
_context.SaveChangesWithRetryAsync(Arg.Any<int>(), Arg.Any<CancellationToken>())
.Returns(Task.CompletedTask);
}
// ===================================================================
// GetNotificationsHandler
// ===================================================================
[Fact]
public async Task Handle_GetNotifications_ShouldReturnPagedResults()
{
var userId = Guid.NewGuid();
var notifications = new List<Notification>
{
new(userId, "Title1", "Message1", NotificationType.Info),
new(userId, "Title2", "Message2", NotificationType.Warning),
};
_context.Notifications.Returns(MockDbSetHelper.CreateFor(notifications));
var handler = new GetNotificationsHandler(_context, Mapper);
var result = await handler.Handle(new GetNotificationsQuery(userId, null, 1, 20), _ct);
result.ShouldNotBeNull();
result.Items.Count.ShouldBe(2);
result.TotalCount.ShouldBe(2);
result.Page.ShouldBe(1);
result.PageSize.ShouldBe(20);
}
[Fact]
public async Task Handle_GetNotifications_WithIsReadFilter_ShouldFilter()
{
var userId = Guid.NewGuid();
var notifications = new List<Notification>
{
new(userId, "Unread", "Msg1", NotificationType.Info),
new(userId, "Read", "Msg2", NotificationType.Success),
};
notifications[1].MarkAsRead();
_context.Notifications.Returns(MockDbSetHelper.CreateFor(notifications));
var handler = new GetNotificationsHandler(_context, Mapper);
var result = await handler.Handle(new GetNotificationsQuery(userId, true, 1, 20), _ct);
result.ShouldNotBeNull();
result.Items.Count.ShouldBe(1);
result.Items[0].IsRead.ShouldBeTrue();
result.Items[0].Title.ShouldBe("Read");
}
[Fact]
public async Task Handle_GetNotifications_WithNoResults_ShouldReturnEmptyPage()
{
var userId = Guid.NewGuid();
_context.Notifications.Returns(MockDbSetHelper.CreateFor<Notification>());
var handler = new GetNotificationsHandler(_context, Mapper);
var result = await handler.Handle(new GetNotificationsQuery(userId, null, 1, 20), _ct);
result.ShouldNotBeNull();
result.Items.ShouldBeEmpty();
result.TotalCount.ShouldBe(0);
result.Page.ShouldBe(1);
result.PageSize.ShouldBe(20);
}
// ===================================================================
// GetUnreadCountHandler
// ===================================================================
[Fact]
public async Task Handle_GetUnreadCount_ShouldReturnUnreadCount()
{
var userId = Guid.NewGuid();
var notifications = new List<Notification>
{
new(userId, "Unread1", "Msg1", NotificationType.Info),
new(userId, "Unread2", "Msg2", NotificationType.Warning),
new(userId, "Read", "Msg3", NotificationType.Success),
};
notifications[2].MarkAsRead();
_context.Notifications.Returns(MockDbSetHelper.CreateFor(notifications));
var handler = new GetUnreadCountHandler(_context);
var result = await handler.Handle(new GetUnreadCountQuery(userId), _ct);
result.IsSuccess.ShouldBeTrue();
result.Data.ShouldNotBeNull();
result.Data.Count.ShouldBe(2);
}
[Fact]
public async Task Handle_GetUnreadCount_WithNoUnread_ShouldReturnZero()
{
var userId = Guid.NewGuid();
var notification = new Notification(userId, "Read1", "Msg1", NotificationType.Info);
notification.MarkAsRead();
_context.Notifications.Returns(MockDbSetHelper.CreateFor([notification]));
var handler = new GetUnreadCountHandler(_context);
var result = await handler.Handle(new GetUnreadCountQuery(userId), _ct);
result.IsSuccess.ShouldBeTrue();
result.Data.ShouldNotBeNull();
result.Data.Count.ShouldBe(0);
}
// ===================================================================
// MarkAsReadHandler
// ===================================================================
[Fact]
public async Task Handle_MarkAsRead_ShouldMarkNotificationAsRead()
{
var userId = Guid.NewGuid();
var notification = new Notification(userId, "Test", "Message", NotificationType.Info);
_context.Notifications.Returns(MockDbSetHelper.CreateFor([notification]));
var handler = new MarkAsReadHandler(_context);
var result = await handler.Handle(new MarkAsReadCommand(notification.Id, userId), _ct);
result.IsSuccess.ShouldBeTrue();
result.Data.ShouldNotBeNull();
result.Data.Success.ShouldBeTrue();
notification.IsRead.ShouldBeTrue();
notification.ReadAt.ShouldNotBeNull();
await _context.Received(1).SaveChangesWithRetryAsync(Arg.Any<int>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task Handle_MarkAsRead_WithNonExistentNotification_ShouldReturnNotFound()
{
var userId = Guid.NewGuid();
_context.Notifications.Returns(MockDbSetHelper.CreateFor<Notification>());
var handler = new MarkAsReadHandler(_context);
var result = await handler.Handle(new MarkAsReadCommand(Guid.NewGuid(), userId), _ct);
result.IsSuccess.ShouldBeFalse();
result.StatusCode.ShouldBe(404);
result.Error.ShouldBe("Notification not found");
}
// ===================================================================
// MarkAllAsReadHandler
// ===================================================================
[Fact]
public async Task Handle_MarkAllAsRead_ShouldMarkAllNotificationsAsRead()
{
var userId = Guid.NewGuid();
var notifications = new List<Notification>
{
new(userId, "Unread1", "Msg1", NotificationType.Info),
new(userId, "Unread2", "Msg2", NotificationType.Warning),
};
_context.Notifications.Returns(MockDbSetHelper.CreateFor(notifications));
var handler = new MarkAllAsReadHandler(_context);
var result = await handler.Handle(new MarkAllAsReadCommand(userId), _ct);
result.IsSuccess.ShouldBeTrue();
result.Data.ShouldNotBeNull();
result.Data.Success.ShouldBeTrue();
notifications.All(n => n.IsRead).ShouldBeTrue();
await _context.Received(1).SaveChangesWithRetryAsync(Arg.Any<int>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task Handle_MarkAllAsRead_WithAlreadyReadNotifications_ShouldStillReturnSuccess()
{
var userId = Guid.NewGuid();
var notification = new Notification(userId, "AlreadyRead", "Msg", NotificationType.Info);
notification.MarkAsRead();
_context.Notifications.Returns(MockDbSetHelper.CreateFor([notification]));
var handler = new MarkAllAsReadHandler(_context);
var result = await handler.Handle(new MarkAllAsReadCommand(userId), _ct);
result.IsSuccess.ShouldBeTrue();
result.Data.ShouldNotBeNull();
result.Data.Success.ShouldBeTrue();
}
// ===================================================================
// UpdateNotificationConfigHandler
// ===================================================================
[Fact]
public async Task Handle_UpdateNotificationConfig_ShouldCreateConfigIfNotExists()
{
var userId = Guid.NewGuid();
_context.UserNotificationConfigs.Returns(MockDbSetHelper.CreateFor<UserNotificationConfig>());
var handler = new UpdateNotificationConfigHandler(_context, Mapper);
var result = await handler.Handle(new UpdateNotificationConfigCommand(
userId, true, "user@example.com", null, null, null, null, null, null), _ct);
result.IsSuccess.ShouldBeTrue();
result.Data.ShouldNotBeNull();
result.Data.EmailEnabled.ShouldBeTrue();
result.Data.EmailAddress.ShouldBe("user@example.com");
await _context.Received(1).SaveChangesWithRetryAsync(Arg.Any<int>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task Handle_UpdateNotificationConfig_ShouldUpdateExistingConfig()
{
var userId = Guid.NewGuid();
var existing = new UserNotificationConfig(userId);
existing.UpdateEmail("old@example.com", false);
_context.UserNotificationConfigs.Returns(MockDbSetHelper.CreateFor([existing]));
var handler = new UpdateNotificationConfigHandler(_context, Mapper);
var result = await handler.Handle(new UpdateNotificationConfigCommand(
userId, true, "new@example.com", null, null, null, null, null, null), _ct);
result.IsSuccess.ShouldBeTrue();
result.Data.ShouldNotBeNull();
result.Data.EmailEnabled.ShouldBeTrue();
result.Data.EmailAddress.ShouldBe("new@example.com");
}
[Fact]
public async Task Handle_UpdateNotificationConfig_PartialUpdate_ShouldOnlyUpdateSpecifiedFields()
{
var userId = Guid.NewGuid();
var existing = new UserNotificationConfig(userId);
existing.UpdateEmail("existing@example.com", true);
existing.UpdateNtfy("https://ntfy.example.com", true);
existing.UpdateDigests(true, false);
_context.UserNotificationConfigs.Returns(MockDbSetHelper.CreateFor([existing]));
var handler = new UpdateNotificationConfigHandler(_context, Mapper);
// Only update email address, leave everything else unchanged
var result = await handler.Handle(new UpdateNotificationConfigCommand(
userId, null, "updated@example.com", null, null, null, null, null, null), _ct);
result.IsSuccess.ShouldBeTrue();
result.Data.ShouldNotBeNull();
result.Data.EmailAddress.ShouldBe("updated@example.com");
result.Data.EmailEnabled.ShouldBeTrue(); // unchanged
result.Data.NtfyEnabled.ShouldBeTrue(); // unchanged
}
[Fact]
public async Task Handle_UpdateNotificationConfig_ShouldUpdateNtfySettings()
{
var userId = Guid.NewGuid();
var existing = new UserNotificationConfig(userId);
existing.UpdateEmail("user@example.com", true);
_context.UserNotificationConfigs.Returns(MockDbSetHelper.CreateFor([existing]));
var handler = new UpdateNotificationConfigHandler(_context, Mapper);
var result = await handler.Handle(new UpdateNotificationConfigCommand(
userId, null, null, true, "https://ntfy.example.com", null, null, null, null), _ct);
result.IsSuccess.ShouldBeTrue();
result.Data.ShouldNotBeNull();
result.Data.NtfyEnabled.ShouldBeTrue();
result.Data.NtfyUrl.ShouldBe("https://ntfy.example.com");
}
[Fact]
public async Task Handle_UpdateNotificationConfig_ShouldUpdateDigestPreferences()
{
var userId = Guid.NewGuid();
_context.UserNotificationConfigs.Returns(MockDbSetHelper.CreateFor<UserNotificationConfig>());
var handler = new UpdateNotificationConfigHandler(_context, Mapper);
var result = await handler.Handle(new UpdateNotificationConfigCommand(
userId, null, null, null, null, null, true, true, null), _ct);
result.IsSuccess.ShouldBeTrue();
result.Data.ShouldNotBeNull();
result.Data.DailyDigest.ShouldBeTrue();
result.Data.WeeklyDigest.ShouldBeTrue();
}
[Fact]
public async Task Handle_UpdateNotificationConfig_ShouldUpdateLanguage()
{
var userId = Guid.NewGuid();
_context.UserNotificationConfigs.Returns(MockDbSetHelper.CreateFor<UserNotificationConfig>());
var handler = new UpdateNotificationConfigHandler(_context, Mapper);
var result = await handler.Handle(new UpdateNotificationConfigCommand(
userId, null, null, null, null, "de", null, null, null), _ct);
result.IsSuccess.ShouldBeTrue();
result.Data.ShouldNotBeNull();
result.Data.Language.ShouldBe("de");
}
// ===================================================================
// GetNotificationConfigHandler
// ===================================================================
[Fact]
public async Task Handle_GetNotificationConfig_ShouldReturnExistingConfig()
{
var userId = Guid.NewGuid();
var config = new UserNotificationConfig(userId);
config.UpdateEmail("user@example.com", true);
config.SetLanguage("en");
_context.UserNotificationConfigs.Returns(MockDbSetHelper.CreateFor([config]));
var handler = new GetNotificationConfigHandler(_context, Mapper);
var result = await handler.Handle(new GetNotificationConfigQuery(userId), _ct);
result.IsSuccess.ShouldBeTrue();
result.Data.ShouldNotBeNull();
result.Data.EmailEnabled.ShouldBeTrue();
result.Data.EmailAddress.ShouldBe("user@example.com");
result.Data.Language.ShouldBe("en");
}
[Fact]
public async Task Handle_GetNotificationConfig_ShouldReturnDefaultConfigWhenNotExists()
{
var userId = Guid.NewGuid();
_context.UserNotificationConfigs.Returns(MockDbSetHelper.CreateFor<UserNotificationConfig>());
var handler = new GetNotificationConfigHandler(_context, Mapper);
var result = await handler.Handle(new GetNotificationConfigQuery(userId), _ct);
result.IsSuccess.ShouldBeTrue();
result.Data.ShouldNotBeNull();
result.Data.UserId.ShouldBe(userId);
result.Data.EmailEnabled.ShouldBeFalse(); // default
result.Data.Language.ShouldBe("fr"); // default language
result.Data.DailyDigest.ShouldBeFalse(); // default
result.Data.WeeklyDigest.ShouldBeFalse(); // default
result.Data.EmailAddress.ShouldBeNull();
result.Data.NtfyEnabled.ShouldBeFalse();
result.Data.NtfyUrl.ShouldBeNull();
result.Data.SubscribedEvents.ShouldBeNull();
}
}