150 lines
4.8 KiB
C#
150 lines
4.8 KiB
C#
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, 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", null, "fr", true, false, null);
|
|
var dto = new UserNotificationConfigDto(
|
|
Guid.NewGuid(), Guid.NewGuid(), true, "test@test.com",
|
|
true, "http://ntfy:80", null, "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>();
|
|
}
|
|
}
|