90 lines
2.9 KiB
C#
90 lines
2.9 KiB
C#
// Copyright (c) 2020 Tudor Stanciu
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using System;
|
|
using System.Text;
|
|
using Tuitio.Application.Services.Abstractions;
|
|
using Tuitio.Application.Tests.Fixtures;
|
|
using Tuitio.Domain.Entities;
|
|
using Tuitio.Domain.Models;
|
|
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 = 1,
|
|
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,
|
|
UserRoles = new UserXUserRole[]
|
|
{
|
|
new UserXUserRole()
|
|
{
|
|
UserId = 1,
|
|
UserRoleId = 1,
|
|
UserRole = new UserRole()
|
|
{
|
|
UserRoleId = 1,
|
|
UserRoleCode = "MOCK_ROLE",
|
|
UserRoleName = "Mock role"
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
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 = token.Token.Export();
|
|
var extracted = Token.Import(raw);
|
|
|
|
// Assert
|
|
Assert.NotNull(token);
|
|
Assert.NotNull(raw);
|
|
Assert.NotNull(extracted);
|
|
|
|
Assert.Equal(_userMock.UserName, extracted.UserName);
|
|
Assert.Equal(_userMock.FirstName, extracted.FirstName);
|
|
Assert.Equal(_userMock.LastName, extracted.LastName);
|
|
Assert.Equal(_userMock.Email, extracted.Email);
|
|
Assert.Equal(_userMock.SecurityStamp, extracted.SecurityStamp);
|
|
|
|
var decodedTokenString = Encoding.UTF8.GetString(Convert.FromBase64String(raw));
|
|
Assert.DoesNotContain("UserRoles", decodedTokenString);
|
|
}
|
|
}
|
|
}
|