tuitio/test/UnitTests/Tuitio.Wrapper.Tests/TuitioServiceTests.cs

95 lines
3.9 KiB
C#
Raw Normal View History

2023-03-14 01:06:09 +02:00
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Tuitio.PublishedLanguage.Dto;
using Tuitio.Wrapper.Services;
using Tuitio.Wrapper.Tests.Stubs;
using Xunit;
namespace Tuitio.Wrapper.Tests
{
public class TuitioServiceTests
{
private readonly Models.ServiceConfiguration _serviceConfiguration;
public TuitioServiceTests()
{
_serviceConfiguration = new Models.ServiceConfiguration("http://localhost:5063");
}
[Fact]
public async Task Login_ShouldReturnAValidToken()
{
// Arrange
var loginResultMock = new AccountLoginResult("dGVzdA==", 600000);
var envelopeMock = Envelope<AccountLoginResult>.Success(loginResultMock);
var httpMessageHandler = new HttpMessageHandlerStub<AccountLoginResult>(envelopeMock);
var httpClient = new HttpClient(httpMessageHandler);
var service = new TuitioService(httpClient, _serviceConfiguration);
// Act
var loginResult = await service.Login("tuitio.user", "pass123");
// Assert
Assert.NotNull(loginResult);
Assert.NotNull(loginResult.Result);
Assert.Null(loginResult.Error);
Assert.NotEmpty(loginResult.Result.Token);
Assert.True(loginResult.Result.ExpiresIn > 0, "Token expiration must be a positive number.");
}
[Fact]
public async Task Authorize_ShouldReturnAValidToken()
{
// Arrange
var authorizationResultMock = new AuthorizationResult()
{
TokenId = Guid.NewGuid(),
UserId = 0,
UserName = "test.tuitio",
CreatedAt = DateTime.Now,
SecurityStamp = Guid.NewGuid().ToString(),
LockStamp = "lock-stamp",
ExpiresIn = 600000
};
var envelopeMock = Envelope<AuthorizationResult>.Success(authorizationResultMock);
var httpMessageHandler = new HttpMessageHandlerStub<AuthorizationResult>(envelopeMock);
var httpClient = new HttpClient(httpMessageHandler);
var service = new TuitioService(httpClient, _serviceConfiguration);
// Act
var authorizationResult = await service.Authorize("tuitio-token");
// Assert
Assert.NotNull(authorizationResult);
Assert.NotNull(authorizationResult.Result);
Assert.Null(authorizationResult.Error);
Assert.Equal(authorizationResultMock.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 Logout_ShouldSuccessfullyLogoutTheUser()
{
// Arrange
var logoutResultMock = new AccountLogoutResult(1, "test.tuitio", DateTime.Now);
var envelopeMock = Envelope<AccountLogoutResult>.Success(logoutResultMock);
var httpMessageHandler = new HttpMessageHandlerStub<AccountLogoutResult>(envelopeMock);
var httpClient = new HttpClient(httpMessageHandler);
var service = new TuitioService(httpClient, _serviceConfiguration);
// Act
var logoutResult = await service.Logout("tuitio-token");
// Assert
Assert.NotNull(logoutResult);
Assert.NotNull(logoutResult.Result);
Assert.Null(logoutResult.Error);
Assert.Equal(logoutResultMock.UserName, logoutResult.Result.UserName);
Assert.True((DateTime.UtcNow - logoutResult.Result.LogoutDate).TotalMinutes <= 1, "Logout date must be within the last minute.");
}
}
}