Adding tests
This commit is contained in:
@@ -54,13 +54,6 @@ public class JwtAuthorizationAttribute_Tests
|
||||
return new AuthorizationFilterContext(actionContext, []);
|
||||
}
|
||||
|
||||
private static AuthorizationFilterContext CreateAuthorizationContext()
|
||||
{
|
||||
var httpContext = new DefaultHttpContext();
|
||||
var actionContext = new ActionContext(httpContext, new RouteData(), new ControllerActionDescriptor());
|
||||
return new AuthorizationFilterContext(actionContext, new List<IFilterMetadata>());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void OnAuthorization_AllowAnonymous_SkipsAuthorization()
|
||||
{
|
||||
@@ -81,7 +74,7 @@ public class JwtAuthorizationAttribute_Tests
|
||||
[TestMethod]
|
||||
public void OnAuthorization_NoAuthenticatedUser_ReturnsUnauthorized()
|
||||
{
|
||||
var context = CreateAuthorizationContext();
|
||||
var context = TestUtils.CreateAuthorizationContext();
|
||||
IConfiguration configuration = TestUtils.CreateConfiguration();
|
||||
|
||||
context.HttpContext.RequestServices = new ServiceCollection()
|
||||
@@ -96,7 +89,7 @@ public class JwtAuthorizationAttribute_Tests
|
||||
[TestMethod]
|
||||
public void OnAuthorization_EmptyAuthorizationHeader_ReturnsUnauthorized()
|
||||
{
|
||||
var context = CreateAuthorizationContext();
|
||||
var context = TestUtils.CreateAuthorizationContext();
|
||||
IConfiguration configuration = TestUtils.CreateConfiguration();
|
||||
|
||||
context.HttpContext.RequestServices = new ServiceCollection()
|
||||
@@ -115,7 +108,7 @@ public class JwtAuthorizationAttribute_Tests
|
||||
[TestMethod]
|
||||
public void OnAuthorization_InvalidToken_ReturnsUnauthorized()
|
||||
{
|
||||
var context = CreateAuthorizationContext();
|
||||
var context = TestUtils.CreateAuthorizationContext();
|
||||
IConfiguration configuration = TestUtils.CreateConfiguration();
|
||||
|
||||
context.HttpContext.RequestServices = new ServiceCollection()
|
||||
@@ -135,7 +128,7 @@ public class JwtAuthorizationAttribute_Tests
|
||||
[TestMethod]
|
||||
public void OnAuthorization_ValidToken()
|
||||
{
|
||||
var context = CreateAuthorizationContext();
|
||||
var context = TestUtils.CreateAuthorizationContext();
|
||||
IConfiguration configuration = TestUtils.CreateConfiguration();
|
||||
|
||||
context.HttpContext.RequestServices = new ServiceCollection()
|
||||
|
||||
@@ -186,6 +186,81 @@ public class RoleService_Tests
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task GetRoleByGuidAsync_CurrentRole()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_roleService != null)
|
||||
{
|
||||
var role = await _roleService.GetRoleForUser(_role?.Guid);
|
||||
Assert.IsNotNull(role);
|
||||
Assert.IsTrue(role.Guid == _role?.Guid);
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"RoleService is null");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.InnerException);
|
||||
Assert.Fail($"An exception was thrown: {ex}");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task GetRoleByGuidAsync_Default()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_roleService != null)
|
||||
{
|
||||
CreateRoleRequestData data = new()
|
||||
{
|
||||
Name = "Default",
|
||||
IsNotEditable = true
|
||||
};
|
||||
var roleCreated = await _roleService.CreateRoleAsync(data);
|
||||
var role = await _roleService.GetRoleForUser(String.Empty);
|
||||
Assert.IsNotNull(role);
|
||||
Assert.IsTrue(roleCreated?.Guid == role?.Guid);
|
||||
Assert.IsTrue(role?.Name == "Default");
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"RoleService is null");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.InnerException);
|
||||
Assert.Fail($"An exception was thrown: {ex}");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task GetRoleByGuidAsync_Null()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_roleService != null)
|
||||
{
|
||||
var role = await _roleService.GetRoleForUser(Guid.NewGuid().ToString());
|
||||
Assert.IsNull(role);
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"RoleService is null");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.InnerException);
|
||||
Assert.Fail($"An exception was thrown: {ex}");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task DeleteRoleAsync()
|
||||
{
|
||||
|
||||
@@ -19,12 +19,24 @@ using BasicDotnetTemplate.MainProject.Core.Database;
|
||||
using BasicDotnetTemplate.MainProject.Services;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Newtonsoft.Json;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Routing;
|
||||
using Microsoft.AspNetCore.Mvc.Controllers;
|
||||
|
||||
|
||||
namespace BasicDotnetTemplate.MainProject.Tests;
|
||||
|
||||
public static class TestUtils
|
||||
{
|
||||
|
||||
public static AuthorizationFilterContext CreateAuthorizationContext()
|
||||
{
|
||||
var httpContext = new DefaultHttpContext();
|
||||
var actionContext = new ActionContext(httpContext, new RouteData(), new ControllerActionDescriptor());
|
||||
return new AuthorizationFilterContext(actionContext, new List<IFilterMetadata>());
|
||||
}
|
||||
|
||||
public static IConfiguration CreateConfiguration()
|
||||
{
|
||||
WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty<string>());
|
||||
@@ -64,6 +76,16 @@ public static class TestUtils
|
||||
return context;
|
||||
}
|
||||
|
||||
public static BaseService CreateBaseService()
|
||||
{
|
||||
IConfiguration configuration = CreateConfiguration();
|
||||
var optionsBuilder = new DbContextOptionsBuilder<SqlServerContext>();
|
||||
optionsBuilder.UseSqlServer(GetSqlConnectionString(configuration));
|
||||
SqlServerContext sqlServerContext = CreateInMemorySqlContext();
|
||||
var httpContextAccessor = new Mock<IHttpContextAccessor>();
|
||||
return new BaseService(httpContextAccessor.Object, configuration, sqlServerContext);
|
||||
}
|
||||
|
||||
public static AuthService CreateAuthService()
|
||||
{
|
||||
IConfiguration configuration = CreateConfiguration();
|
||||
|
||||
Reference in New Issue
Block a user