MinimalAPIAsp/tests/Application.UnitTests/Common/Behaviours/RequestLoggerTests.cs

46 lines
1.6 KiB
C#

using MiniSkeletonAPI.Application.Common.Behaviours;
using MiniSkeletonAPI.Application.Common.Interfaces;
using MiniSkeletonAPI.Application.TodoItems.Commands.CreateTodoItem;
using Microsoft.Extensions.Logging;
using Moq;
using NUnit.Framework;
namespace Application.UnitTests.Common.Behaviours;
public class RequestLoggerTests
{
private Mock<ILogger<CreateTodoItemCommand>> _logger = null!;
private Mock<IUser> _user = null!;
private Mock<IIdentityService> _identityService = null!;
[SetUp]
public void Setup()
{
_logger = new Mock<ILogger<CreateTodoItemCommand>>();
_user = new Mock<IUser>();
_identityService = new Mock<IIdentityService>();
}
[Test]
public async Task ShouldCallGetUserNameAsyncOnceIfAuthenticated()
{
_user.Setup(x => x.Id).Returns(Guid.NewGuid().ToString());
var requestLogger = new LoggingBehaviour<CreateTodoItemCommand>(_logger.Object, _user.Object, _identityService.Object);
await requestLogger.Process(new CreateTodoItemCommand { ListId = new Guid(), Title = "title" }, new CancellationToken());
_identityService.Verify(i => i.GetUserNameAsync(It.IsAny<string>()), Times.Once);
}
[Test]
public async Task ShouldNotCallGetUserNameAsyncOnceIfUnauthenticated()
{
var requestLogger = new LoggingBehaviour<CreateTodoItemCommand>(_logger.Object, _user.Object, _identityService.Object);
await requestLogger.Process(new CreateTodoItemCommand { ListId = new Guid(), Title = "title" }, new CancellationToken());
_identityService.Verify(i => i.GetUserNameAsync(It.IsAny<string>()), Times.Never);
}
}