fix: corriger les 8 issues du code review notifications
🔴 EmailService: implémentation SMTP réelle (System.Net.Mail) 🔴 GetNotificationConfigHandler: CQS — retourne DTO par défaut sans écrire en DB 🟠 NotificationRateLimiter: increment atomique (Interlocked) 🟠 DailyDigestJob/WeeklyDigestJob: comptes événements user-specific 🟠 docker-compose.prod: auth ntfy via NTFY_AUTH_FILE 🟠 Namespace: Heimdall.Web.Controllers (consistant) 🟠 Commentaire 'gotify' → 'ntfy' 🟠 Tests: NotificationsControllerTests (7 tests)
This commit is contained in:
1 parent
f800103239
commit
c1eac5431b
15 files changed
+451
-30
No files matched your search
@@ -0,0 +1,149 @@
|
||||
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.Queries.GetConfig;
|
||||
using Heimdall.Application.Notifications.Queries.GetNotifications;
|
||||
using Heimdall.Application.Notifications.Queries.GetUnreadCount;
|
||||
using Heimdall.Web.Controllers;
|
||||
using MediatR;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using NSubstitute;
|
||||
using Shouldly;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace Heimdall.Web.Tests.Controllers;
|
||||
|
||||
public class NotificationsControllerTests
|
||||
{
|
||||
private readonly IMediator _mediator;
|
||||
private readonly NotificationsController _controller;
|
||||
|
||||
public NotificationsControllerTests()
|
||||
{
|
||||
_mediator = Substitute.For<IMediator>();
|
||||
_controller = new NotificationsController(_mediator);
|
||||
|
||||
var user = new ClaimsPrincipal(new ClaimsIdentity([
|
||||
new Claim(ClaimTypes.NameIdentifier, Guid.NewGuid().ToString())
|
||||
], "test"));
|
||||
_controller.ControllerContext = new ControllerContext
|
||||
{
|
||||
HttpContext = new DefaultHttpContext { User = user }
|
||||
};
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetNotifications_ReturnsOk()
|
||||
{
|
||||
// Arrange
|
||||
var expected = new PagedResult<NotificationDto>([], 0, 1, 20);
|
||||
_mediator.Send(Arg.Any<GetNotificationsQuery>(), Arg.Any<CancellationToken>())
|
||||
.Returns(expected);
|
||||
|
||||
// Act
|
||||
var result = await _controller.GetNotifications(null, 1, 20);
|
||||
|
||||
// Assert
|
||||
result.ShouldBeOfType<OkObjectResult>();
|
||||
var okResult = (OkObjectResult)result;
|
||||
okResult.Value.ShouldBe(expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetUnreadCount_ReturnsOk()
|
||||
{
|
||||
// Arrange
|
||||
var dto = new UnreadCountDto(5);
|
||||
_mediator.Send(Arg.Any<GetUnreadCountQuery>(), Arg.Any<CancellationToken>())
|
||||
.Returns(ServiceResult<UnreadCountDto>.Success(dto));
|
||||
|
||||
// Act
|
||||
var result = await _controller.GetUnreadCount();
|
||||
|
||||
// Assert
|
||||
result.ShouldBeOfType<OkObjectResult>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task MarkAsRead_ReturnsOk()
|
||||
{
|
||||
// Arrange
|
||||
var id = Guid.NewGuid();
|
||||
var dto = new MarkAsReadResponse(true);
|
||||
_mediator.Send(Arg.Any<MarkAsReadCommand>(), Arg.Any<CancellationToken>())
|
||||
.Returns(ServiceResult<MarkAsReadResponse>.Success(dto));
|
||||
|
||||
// Act
|
||||
var result = await _controller.MarkAsRead(id);
|
||||
|
||||
// Assert
|
||||
result.ShouldBeOfType<OkObjectResult>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task MarkAsRead_NotFound_Returns404()
|
||||
{
|
||||
// Arrange
|
||||
_mediator.Send(Arg.Any<MarkAsReadCommand>(), Arg.Any<CancellationToken>())
|
||||
.Returns(ServiceResult<MarkAsReadResponse>.NotFound("Not found"));
|
||||
|
||||
// Act
|
||||
var result = await _controller.MarkAsRead(Guid.NewGuid());
|
||||
|
||||
// Assert
|
||||
result.ShouldBeOfType<ObjectResult>();
|
||||
((ObjectResult)result).StatusCode.ShouldBe(404);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task MarkAllAsRead_ReturnsOk()
|
||||
{
|
||||
// Arrange
|
||||
_mediator.Send(Arg.Any<MarkAllAsReadCommand>(), Arg.Any<CancellationToken>())
|
||||
.Returns(ServiceResult<MarkAsReadResponse>.Success(new MarkAsReadResponse(true)));
|
||||
|
||||
// Act
|
||||
var result = await _controller.MarkAllAsRead();
|
||||
|
||||
// Assert
|
||||
result.ShouldBeOfType<OkObjectResult>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetConfig_ReturnsOk()
|
||||
{
|
||||
// Arrange
|
||||
var userId = Guid.NewGuid();
|
||||
var dto = new UserNotificationConfigDto(Guid.Empty, userId, false, null, false, null, "fr", false, false, null);
|
||||
_mediator.Send(Arg.Any<GetNotificationConfigQuery>(), Arg.Any<CancellationToken>())
|
||||
.Returns(ServiceResult<UserNotificationConfigDto>.Success(dto));
|
||||
|
||||
// Act
|
||||
var result = await _controller.GetConfig();
|
||||
|
||||
// Assert
|
||||
result.ShouldBeOfType<OkObjectResult>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateConfig_ReturnsOk()
|
||||
{
|
||||
// Arrange
|
||||
var request = new UpdateNotificationConfigRequest(
|
||||
true, "test@test.com", true, "http://ntfy:80", "fr", true, false, null);
|
||||
var dto = new UserNotificationConfigDto(
|
||||
Guid.NewGuid(), Guid.NewGuid(), true, "test@test.com",
|
||||
true, "http://ntfy:80", "fr", true, false, null);
|
||||
_mediator.Send(Arg.Any<UpdateNotificationConfigCommand>(), Arg.Any<CancellationToken>())
|
||||
.Returns(ServiceResult<UserNotificationConfigDto>.Success(dto));
|
||||
|
||||
// Act
|
||||
var result = await _controller.UpdateConfig(request);
|
||||
|
||||
// Assert
|
||||
result.ShouldBeOfType<OkObjectResult>();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user