Code refactoring + tests
This commit is contained in:
@@ -44,7 +44,7 @@ public class JwtService_Tests
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task GenerateToken()
|
||||
public void GenerateToken()
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -52,7 +52,7 @@ public class JwtService_Tests
|
||||
var testString = "test";
|
||||
if(jwtService != null)
|
||||
{
|
||||
var jwt = jwtService.GenerateToken(testString, testString);
|
||||
var jwt = jwtService.GenerateToken(testString);
|
||||
Assert.IsTrue(jwt != null);
|
||||
Assert.IsInstanceOfType(jwt, typeof(string));
|
||||
}
|
||||
@@ -68,6 +68,31 @@ public class JwtService_Tests
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ValidateToken_Null()
|
||||
{
|
||||
try
|
||||
{
|
||||
var jwtService = TestUtils.CreateJwtService();
|
||||
var testString = "test";
|
||||
if(jwtService != null)
|
||||
{
|
||||
var jwt = jwtService.GenerateToken(testString);
|
||||
var user = jwtService.ValidateToken($"Bearer {jwt}");
|
||||
Assert.IsTrue(user == null);
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"JwtService is null");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.InnerException);
|
||||
Assert.Fail($"An exception was thrown: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -46,7 +46,7 @@ public class UserService_Tests
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task GetUsers()
|
||||
public async Task GetUserByUsernameAndPassword_Null()
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -69,6 +69,33 @@ public class UserService_Tests
|
||||
}
|
||||
}
|
||||
|
||||
// TODO
|
||||
// [TestMethod]
|
||||
public async Task GetUserByUsernameAndPassword_Success()
|
||||
{
|
||||
try
|
||||
{
|
||||
var userService = TestUtils.CreateUserService();
|
||||
var testUsername = "test@email.it";
|
||||
var testPassword = "password";
|
||||
if(userService != null)
|
||||
{
|
||||
var user = await userService.GetUserByUsernameAndPassword(testUsername, testPassword);
|
||||
Assert.IsTrue(user != null);
|
||||
Assert.IsTrue(user.Username == testUsername);
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"UserService is null");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.InnerException);
|
||||
Assert.Fail($"An exception was thrown: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -68,7 +68,8 @@ public static class TestUtils
|
||||
var optionsBuilder = new DbContextOptionsBuilder<SqlServerContext>();
|
||||
optionsBuilder.UseSqlServer("test");
|
||||
SqlServerContext sqlServerContext = new SqlServerContext(optionsBuilder.Options);
|
||||
return new JwtService(configuration, sqlServerContext);
|
||||
var userServiceMock = new Mock<IUserService>();
|
||||
return new JwtService(configuration, sqlServerContext, userServiceMock.Object);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
76
MainProject.Tests/Utils/JwtTokenUtils_Tests.cs
Normal file
76
MainProject.Tests/Utils/JwtTokenUtils_Tests.cs
Normal file
@@ -0,0 +1,76 @@
|
||||
using System;
|
||||
using BasicDotnetTemplate.MainProject.Models.Settings;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using BasicDotnetTemplate.MainProject.Utils;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
|
||||
namespace BasicDotnetTemplate.MainProject.Tests;
|
||||
|
||||
[TestClass]
|
||||
public class JwtTokenUtils_Tests
|
||||
{
|
||||
private static string _guid = "15e4be58-e655-475e-b4b8-a9779b359f57";
|
||||
|
||||
[TestMethod]
|
||||
public void GenerateToken()
|
||||
{
|
||||
try
|
||||
{
|
||||
WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty<string>());
|
||||
AppSettings appSettings = ProgramUtils.AddConfiguration(ref builder, System.AppDomain.CurrentDomain.BaseDirectory + "/JsonData");
|
||||
JwtTokenUtils jwtUtils = new JwtTokenUtils(appSettings);
|
||||
var jwt = jwtUtils.GenerateToken(_guid);
|
||||
Assert.IsTrue(!String.IsNullOrEmpty(jwt));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.InnerException);
|
||||
Assert.Fail($"An exception was thrown: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ValidateToken()
|
||||
{
|
||||
try
|
||||
{
|
||||
WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty<string>());
|
||||
AppSettings appSettings = ProgramUtils.AddConfiguration(ref builder, System.AppDomain.CurrentDomain.BaseDirectory + "/JsonData");
|
||||
JwtTokenUtils jwtUtils = new JwtTokenUtils(appSettings);
|
||||
var jwt = jwtUtils.GenerateToken(_guid);
|
||||
var guid = jwtUtils.ValidateToken($"Bearer {jwt}");
|
||||
Assert.IsTrue(_guid == guid);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.InnerException);
|
||||
Assert.Fail($"An exception was thrown: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ValidateToken_Empty()
|
||||
{
|
||||
try
|
||||
{
|
||||
WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty<string>());
|
||||
AppSettings appSettings = ProgramUtils.AddConfiguration(ref builder, System.AppDomain.CurrentDomain.BaseDirectory + "/JsonData");
|
||||
JwtTokenUtils jwtUtils = new JwtTokenUtils(appSettings);
|
||||
var jwt = jwtUtils.GenerateToken(_guid);
|
||||
var guid = jwtUtils.ValidateToken(jwt);
|
||||
Assert.IsTrue(String.IsNullOrEmpty(guid));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.InnerException);
|
||||
Assert.Fail($"An exception was thrown: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user