tuitio/test/UnitTests/Tuitio.Application.Tests/CommandHandlerTests.cs

67 lines
2.1 KiB
C#
Raw Normal View History

2023-03-13 18:36:59 +02:00
// Copyright (c) 2020 Tudor Stanciu
using MediatR;
using Microsoft.Extensions.DependencyInjection;
using Tuitio.Application.CommandHandlers;
using Tuitio.Application.Tests.Fixtures;
using Tuitio.PublishedLanguage.Constants;
using Xunit;
namespace Tuitio.Application.Tests
{
public class CommandHandlerTests : IClassFixture<DependencyInjectionFixture>, IDisposable
{
private readonly IServiceScope _tuitioScope;
private readonly IMediator _mediator;
public CommandHandlerTests(DependencyInjectionFixture fixture)
{
_tuitioScope = fixture.ServiceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope();
_mediator = _tuitioScope.ServiceProvider.GetRequiredService<IMediator>();
}
public void Dispose()
{
_tuitioScope.Dispose();
}
[Fact]
public async Task AccountLoginHandler_Handle_Success()
{
// Arrange
var userName = "tuitio.user";
var password = "pass123";
var command = new AccountLoginHandler.Command(userName, password);
// Act
var result = await _mediator.Send(command);
// Assert
Assert.NotNull(result);
Assert.NotNull(result.Result);
Assert.Null(result.Error);
Assert.NotEmpty(result.Result.Token);
Assert.True(result.Result.ExpiresIn > 0, "Token expiration must be a positive number.");
}
[Fact]
public async Task AccountLoginHandler_Handle_Failed()
{
// Arrange
var userName = "tuitio.user";
var password = "wrong_password";
var command = new AccountLoginHandler.Command(userName, password);
// 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.BAD_CREDENTIALS, result.Error);
}
}
}