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

68 lines
2.1 KiB
C#
Raw Normal View History

2023-03-13 18:36:59 +02:00
// Copyright (c) 2020 Tudor Stanciu
using Microsoft.Extensions.DependencyInjection;
using System;
2023-03-10 20:11:11 +02:00
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);
}
}
}