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> _logger = null!; private Mock _user = null!; private Mock _identityService = null!; [SetUp] public void Setup() { _logger = new Mock>(); _user = new Mock(); _identityService = new Mock(); } [Test] public async Task ShouldCallGetUserNameAsyncOnceIfAuthenticated() { _user.Setup(x => x.Id).Returns(Guid.NewGuid().ToString()); var requestLogger = new LoggingBehaviour(_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()), Times.Once); } [Test] public async Task ShouldNotCallGetUserNameAsyncOnceIfUnauthenticated() { var requestLogger = new LoggingBehaviour(_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()), Times.Never); } }