From 652e6bc1421768926780ff5a41b8a8b70fe02064 Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Mon, 13 Mar 2023 23:24:32 +0200 Subject: [PATCH] Added unit tests --- .../CommandHandlerTests.cs | 88 +++++++++++++++++++ .../UserServiceTests.cs | 4 +- 2 files changed, 90 insertions(+), 2 deletions(-) diff --git a/test/UnitTests/Tuitio.Application.Tests/CommandHandlerTests.cs b/test/UnitTests/Tuitio.Application.Tests/CommandHandlerTests.cs index df88b34..eaf77ad 100644 --- a/test/UnitTests/Tuitio.Application.Tests/CommandHandlerTests.cs +++ b/test/UnitTests/Tuitio.Application.Tests/CommandHandlerTests.cs @@ -3,8 +3,10 @@ using MediatR; using Microsoft.Extensions.DependencyInjection; using Tuitio.Application.CommandHandlers; +using Tuitio.Application.Services.Abstractions; using Tuitio.Application.Tests.Fixtures; using Tuitio.PublishedLanguage.Constants; +using Tuitio.PublishedLanguage.Dto; using Xunit; namespace Tuitio.Application.Tests @@ -62,5 +64,91 @@ namespace Tuitio.Application.Tests Assert.NotEmpty(result.Error); Assert.Equal(EnvelopeError.BAD_CREDENTIALS, result.Error); } + + [Fact] + public async Task AccountLogoutHandler_Handle_Success() + { + // Arrange + var userName = "tuitio.user"; + var password = "pass123"; + var loginCommand = new AccountLoginHandler.Command(userName, password); + var loginResult = await _mediator.Send(loginCommand); + var logoutCommand = new AccountLogoutHandler.Command(loginResult.Result.Token); + + // Act + Envelope logoutResult; + using (var scope = _tuitioScope.ServiceProvider.CreateScope()) + { + var newMediator = scope.ServiceProvider.GetRequiredService(); + logoutResult = await newMediator.Send(logoutCommand); + } + + // Assert + Assert.NotNull(logoutResult); + Assert.NotNull(logoutResult.Result); + Assert.Null(logoutResult.Error); + Assert.Equal(userName, logoutResult.Result.UserName); + Assert.True((DateTime.UtcNow - logoutResult.Result.LogoutDate).TotalMinutes <= 1, "Logout date must be within the last minute."); + } + + [Fact] + public async Task AccountLogoutHandler_Handle_Failed() + { + // Arrange + var unauthenticatedToken = "unauthenticated-token"; + var command = new AccountLogoutHandler.Command(unauthenticatedToken); + + // Act + var result = await _mediator.Send(command); + + // Assert + Assert.NotNull(result); + Assert.Null(result.Result); + Assert.NotNull(result.Error); + Assert.NotEmpty(result.Error); + Assert.Equal(EnvelopeError.UNAUTHENTICATED, result.Error); + } + + [Fact] + public async Task AuthorizationHandler_Handle_Success() + { + // Arrange + var userName = "tuitio.user"; + var password = "pass123"; + var loginCommand = new AccountLoginHandler.Command(userName, password); + var loginResult = await _mediator.Send(loginCommand); + var authorizationCommand = new AuthorizationHandler.Command(loginResult.Result.Token); + + // Act + var authorizationResult = await _mediator.Send(authorizationCommand); + + // Assert + Assert.NotNull(authorizationResult); + Assert.NotNull(authorizationResult.Result); + Assert.Null(authorizationResult.Error); + Assert.Equal(userName, authorizationResult.Result.UserName); + Assert.NotNull(authorizationResult.Result.SecurityStamp); + Assert.NotNull(authorizationResult.Result.LockStamp); + Assert.True(authorizationResult.Result.TokenId != Guid.Empty, "Token id cannot be an empty guid."); + Assert.True(authorizationResult.Result.ExpiresIn > 0, "Token expiration must be a positive number."); + } + + [Fact] + public async Task AuthorizationHandler_Handle_Failed() + { + // Arrange + var unauthorizedToken = "unauthorized-token"; + var command = new AuthorizationHandler.Command(unauthorizedToken); + + // Act + var result = await _mediator.Send(command); + + // Assert + Assert.NotNull(result); + Assert.Null(result.Result); + Assert.NotNull(result.Error); + Assert.NotEmpty(result.Error); + Assert.Equal(EnvelopeError.UNAUTHORIZED, result.Error); + } } } diff --git a/test/UnitTests/Tuitio.Application.Tests/UserServiceTests.cs b/test/UnitTests/Tuitio.Application.Tests/UserServiceTests.cs index 59dab95..72ddc9b 100644 --- a/test/UnitTests/Tuitio.Application.Tests/UserServiceTests.cs +++ b/test/UnitTests/Tuitio.Application.Tests/UserServiceTests.cs @@ -171,8 +171,8 @@ namespace Tuitio.Application.Tests LogoutResult result; using (var scope = _tuitioScope.ServiceProvider.CreateScope()) { - var _newUserService = scope.ServiceProvider.GetRequiredService(); - result = await _newUserService.Logout(loginResult.Raw); + var newUserService = scope.ServiceProvider.GetRequiredService(); + result = await newUserService.Logout(loginResult.Raw); } // Assert