67 lines
2.1 KiB
C#
67 lines
2.1 KiB
C#
// Copyright (c) 2020 Tudor Stanciu
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Tuitio.Application.Services.Abstractions;
|
|
using Tuitio.Application.Tests.Fixtures;
|
|
using Tuitio.Domain.Entities;
|
|
using Xunit;
|
|
|
|
namespace Tuitio.Application.Tests
|
|
{
|
|
public class TokenServiceTests : IClassFixture<DependencyInjectionFixture>, IDisposable
|
|
{
|
|
private readonly IServiceScope _tuitioScope;
|
|
private readonly AppUser _userMock;
|
|
|
|
public TokenServiceTests(DependencyInjectionFixture fixture)
|
|
{
|
|
_tuitioScope = fixture.ServiceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope();
|
|
_userMock = MockAppUser();
|
|
}
|
|
|
|
private AppUser MockAppUser()
|
|
{
|
|
var user = new AppUser()
|
|
{
|
|
UserId = 0,
|
|
UserName = "tuitio.test",
|
|
Password = "9B8769A4A742959A2D0298C36FB70623F2DFACDA8436237DF08D8DFD5B37374C", //pass123
|
|
Email = "tuitio.test@test.test",
|
|
FirstName = "tuitio",
|
|
LastName = "test",
|
|
StatusId = 1,
|
|
FailedLoginAttempts = 0,
|
|
SecurityStamp = "A93650FF-1FC4-4999-BAB6-3EEB174F6892",
|
|
CreationDate = DateTime.Now
|
|
};
|
|
|
|
return user;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_tuitioScope.Dispose();
|
|
}
|
|
|
|
[Fact]
|
|
public void TokenGenerationTest()
|
|
{
|
|
// Arrange
|
|
var _tokenService = _tuitioScope.ServiceProvider.GetRequiredService<ITokenService>();
|
|
|
|
// Act
|
|
var token = _tokenService.GenerateToken(_userMock);
|
|
var raw = _tokenService.GenerateTokenRaw(token);
|
|
var extracted = _tokenService.ExtractToken(raw);
|
|
|
|
// Assert
|
|
Assert.NotNull(token);
|
|
Assert.NotNull(raw);
|
|
Assert.NotNull(extracted);
|
|
|
|
Assert.True(_userMock.UserName == extracted.UserName);
|
|
Assert.True(_userMock.SecurityStamp == extracted.SecurityStamp);
|
|
}
|
|
}
|
|
}
|