Roles #21
@@ -54,13 +54,6 @@ public class JwtAuthorizationAttribute_Tests
|
|||||||
return new AuthorizationFilterContext(actionContext, []);
|
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]
|
[TestMethod]
|
||||||
public void OnAuthorization_AllowAnonymous_SkipsAuthorization()
|
public void OnAuthorization_AllowAnonymous_SkipsAuthorization()
|
||||||
{
|
{
|
||||||
@@ -81,7 +74,7 @@ public class JwtAuthorizationAttribute_Tests
|
|||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void OnAuthorization_NoAuthenticatedUser_ReturnsUnauthorized()
|
public void OnAuthorization_NoAuthenticatedUser_ReturnsUnauthorized()
|
||||||
{
|
{
|
||||||
var context = CreateAuthorizationContext();
|
var context = TestUtils.CreateAuthorizationContext();
|
||||||
IConfiguration configuration = TestUtils.CreateConfiguration();
|
IConfiguration configuration = TestUtils.CreateConfiguration();
|
||||||
|
|
||||||
context.HttpContext.RequestServices = new ServiceCollection()
|
context.HttpContext.RequestServices = new ServiceCollection()
|
||||||
@@ -96,7 +89,7 @@ public class JwtAuthorizationAttribute_Tests
|
|||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void OnAuthorization_EmptyAuthorizationHeader_ReturnsUnauthorized()
|
public void OnAuthorization_EmptyAuthorizationHeader_ReturnsUnauthorized()
|
||||||
{
|
{
|
||||||
var context = CreateAuthorizationContext();
|
var context = TestUtils.CreateAuthorizationContext();
|
||||||
IConfiguration configuration = TestUtils.CreateConfiguration();
|
IConfiguration configuration = TestUtils.CreateConfiguration();
|
||||||
|
|
||||||
context.HttpContext.RequestServices = new ServiceCollection()
|
context.HttpContext.RequestServices = new ServiceCollection()
|
||||||
@@ -115,7 +108,7 @@ public class JwtAuthorizationAttribute_Tests
|
|||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void OnAuthorization_InvalidToken_ReturnsUnauthorized()
|
public void OnAuthorization_InvalidToken_ReturnsUnauthorized()
|
||||||
{
|
{
|
||||||
var context = CreateAuthorizationContext();
|
var context = TestUtils.CreateAuthorizationContext();
|
||||||
IConfiguration configuration = TestUtils.CreateConfiguration();
|
IConfiguration configuration = TestUtils.CreateConfiguration();
|
||||||
|
|
||||||
context.HttpContext.RequestServices = new ServiceCollection()
|
context.HttpContext.RequestServices = new ServiceCollection()
|
||||||
@@ -135,7 +128,7 @@ public class JwtAuthorizationAttribute_Tests
|
|||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void OnAuthorization_ValidToken()
|
public void OnAuthorization_ValidToken()
|
||||||
{
|
{
|
||||||
var context = CreateAuthorizationContext();
|
var context = TestUtils.CreateAuthorizationContext();
|
||||||
IConfiguration configuration = TestUtils.CreateConfiguration();
|
IConfiguration configuration = TestUtils.CreateConfiguration();
|
||||||
|
|
||||||
context.HttpContext.RequestServices = new ServiceCollection()
|
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]
|
[TestMethod]
|
||||||
public async Task DeleteRoleAsync()
|
public async Task DeleteRoleAsync()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -19,12 +19,24 @@ using BasicDotnetTemplate.MainProject.Core.Database;
|
|||||||
using BasicDotnetTemplate.MainProject.Services;
|
using BasicDotnetTemplate.MainProject.Services;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Newtonsoft.Json;
|
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;
|
namespace BasicDotnetTemplate.MainProject.Tests;
|
||||||
|
|
||||||
public static class TestUtils
|
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()
|
public static IConfiguration CreateConfiguration()
|
||||||
{
|
{
|
||||||
WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty<string>());
|
WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty<string>());
|
||||||
@@ -64,6 +76,16 @@ public static class TestUtils
|
|||||||
return context;
|
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()
|
public static AuthService CreateAuthService()
|
||||||
{
|
{
|
||||||
IConfiguration configuration = CreateConfiguration();
|
IConfiguration configuration = CreateConfiguration();
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ public interface IRoleService
|
|||||||
|
|
||||||
public class RoleService : BaseService, IRoleService
|
public class RoleService : BaseService, IRoleService
|
||||||
{
|
{
|
||||||
|
private readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
|
||||||
public RoleService(
|
public RoleService(
|
||||||
IHttpContextAccessor httpContextAccessor,
|
IHttpContextAccessor httpContextAccessor,
|
||||||
IConfiguration configuration,
|
IConfiguration configuration,
|
||||||
@@ -92,14 +93,22 @@ public class RoleService : BaseService, IRoleService
|
|||||||
{
|
{
|
||||||
Role? role = null;
|
Role? role = null;
|
||||||
|
|
||||||
using (var transaction = _sqlServerContext.Database.BeginTransactionAsync())
|
using var transaction = await _sqlServerContext.Database.BeginTransactionAsync();
|
||||||
|
|
||||||
|
try
|
||||||
{
|
{
|
||||||
var tempRole = this.CreateRoleData(data);
|
var tempRole = this.CreateRoleData(data);
|
||||||
await _sqlServerContext.Roles.AddAsync(tempRole);
|
await _sqlServerContext.Roles.AddAsync(tempRole);
|
||||||
await _sqlServerContext.SaveChangesAsync();
|
await _sqlServerContext.SaveChangesAsync();
|
||||||
await (await transaction).CommitAsync();
|
await transaction.CommitAsync();
|
||||||
role = tempRole;
|
role = tempRole;
|
||||||
}
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
await transaction.RollbackAsync();
|
||||||
|
Logger.Error(exception, $"[RoleService][CreateRoleAsync]");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
|
||||||
return role;
|
return role;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ public interface IUserService
|
|||||||
{
|
{
|
||||||
Task<User?> GetUserByIdAsync(int id);
|
Task<User?> GetUserByIdAsync(int id);
|
||||||
Task<User?> GetUserByGuidAsync(string guid);
|
Task<User?> GetUserByGuidAsync(string guid);
|
||||||
Task<User?> GetUserByEmailAsync(string email);
|
|
||||||
Task<User?> GetUserByUsernameAndPassword(string email, string password);
|
Task<User?> GetUserByUsernameAndPassword(string email, string password);
|
||||||
Task<bool> CheckIfEmailIsValid(string email, string? guid = "");
|
Task<bool> CheckIfEmailIsValid(string email, string? guid = "");
|
||||||
Task<User?> CreateUserAsync(CreateUserRequestData data, Role role);
|
Task<User?> CreateUserAsync(CreateUserRequestData data, Role role);
|
||||||
@@ -29,19 +28,19 @@ public class UserService : BaseService, IUserService
|
|||||||
{ }
|
{ }
|
||||||
|
|
||||||
private IQueryable<User> GetUsersQueryable()
|
private IQueryable<User> GetUsersQueryable()
|
||||||
{ //NOSONAR
|
{
|
||||||
return this._sqlServerContext.Users.Where(x => !x.IsDeleted);
|
return this._sqlServerContext.Users.Where(x => !x.IsDeleted);
|
||||||
} //NOSONAR
|
}
|
||||||
|
|
||||||
private IQueryable<User> GetUserByEmailQueryable(string email)
|
private IQueryable<User> GetUserByEmailQueryable(string email)
|
||||||
{ //NOSONAR
|
{
|
||||||
return this.GetUsersQueryable().Where(x =>
|
return this.GetUsersQueryable().Where(x =>
|
||||||
x.Email.ToString() == email.ToString()
|
x.Email.ToString() == email.ToString()
|
||||||
);
|
);
|
||||||
} //NOSONAR
|
}
|
||||||
|
|
||||||
private User CreateUserData(CreateUserRequestData data, Role role)
|
private User CreateUserData(CreateUserRequestData data, Role role)
|
||||||
{ //NOSONAR
|
{
|
||||||
User user = new()
|
User user = new()
|
||||||
{
|
{
|
||||||
CreationTime = DateTime.UtcNow,
|
CreationTime = DateTime.UtcNow,
|
||||||
@@ -59,35 +58,30 @@ public class UserService : BaseService, IUserService
|
|||||||
};
|
};
|
||||||
|
|
||||||
return user;
|
return user;
|
||||||
} //NOSONAR
|
}
|
||||||
|
|
||||||
|
|
||||||
public async Task<User?> GetUserByIdAsync(int id)
|
public async Task<User?> GetUserByIdAsync(int id)
|
||||||
{ //NOSONAR
|
{
|
||||||
return await this.GetUsersQueryable().Where(x => x.Id == id).FirstOrDefaultAsync();
|
return await this.GetUsersQueryable().Where(x => x.Id == id).FirstOrDefaultAsync();
|
||||||
} //NOSONAR
|
}
|
||||||
|
|
||||||
public async Task<User?> GetUserByGuidAsync(string guid)
|
public async Task<User?> GetUserByGuidAsync(string guid)
|
||||||
{ //NOSONAR
|
{
|
||||||
return await this.GetUsersQueryable().Where(x => x.Guid == guid).FirstOrDefaultAsync();
|
return await this.GetUsersQueryable().Where(x => x.Guid == guid).FirstOrDefaultAsync();
|
||||||
} //NOSONAR
|
}
|
||||||
|
|
||||||
public async Task<User?> GetUserByEmailAsync(string email)
|
|
||||||
{ //NOSONAR
|
|
||||||
return await this.GetUserByEmailQueryable(email).FirstOrDefaultAsync();
|
|
||||||
} //NOSONAR
|
|
||||||
|
|
||||||
public async Task<User?> GetUserByUsernameAndPassword(string email, string password)
|
public async Task<User?> GetUserByUsernameAndPassword(string email, string password)
|
||||||
{ //NOSONAR
|
{
|
||||||
User? user = await this.GetUserByEmailQueryable(email).FirstOrDefaultAsync();
|
User? user = await this.GetUserByEmailQueryable(email).FirstOrDefaultAsync();
|
||||||
if (user != null)
|
if (user != null)
|
||||||
{ //NOSONAR
|
{
|
||||||
var encryptedPassword = user.PasswordHash;
|
var encryptedPassword = user.PasswordHash;
|
||||||
Console.WriteLine(encryptedPassword);
|
Console.WriteLine(encryptedPassword);
|
||||||
} //NOSONAR
|
}
|
||||||
|
|
||||||
return user;
|
return user;
|
||||||
} //NOSONAR
|
}
|
||||||
|
|
||||||
public async Task<bool> CheckIfEmailIsValid(string email, string? guid = "")
|
public async Task<bool> CheckIfEmailIsValid(string email, string? guid = "")
|
||||||
{
|
{
|
||||||
@@ -109,28 +103,28 @@ public class UserService : BaseService, IUserService
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async Task<User?> CreateUserAsync(CreateUserRequestData data, Role role)
|
public async Task<User?> CreateUserAsync(CreateUserRequestData data, Role role)
|
||||||
{ //NOSONAR
|
{
|
||||||
using var transaction = await _sqlServerContext.Database.BeginTransactionAsync();
|
using var transaction = await _sqlServerContext.Database.BeginTransactionAsync();
|
||||||
|
|
||||||
User? user;
|
User? user;
|
||||||
var tempUser = CreateUserData(data, role);
|
var tempUser = CreateUserData(data, role);
|
||||||
try
|
try
|
||||||
{ //NOSONAR
|
{
|
||||||
await _sqlServerContext.Users.AddAsync(tempUser);
|
await _sqlServerContext.Users.AddAsync(tempUser);
|
||||||
await _sqlServerContext.SaveChangesAsync();
|
await _sqlServerContext.SaveChangesAsync();
|
||||||
await transaction.CommitAsync();
|
await transaction.CommitAsync();
|
||||||
user = tempUser;
|
user = tempUser;
|
||||||
} //NOSONAR
|
}
|
||||||
catch (Exception exception)
|
catch (Exception exception)
|
||||||
{ //NOSONAR
|
{
|
||||||
await transaction.RollbackAsync();
|
await transaction.RollbackAsync();
|
||||||
Logger.Error(exception, $"[UserService][CreateUserAsync]");
|
Logger.Error(exception, $"[UserService][CreateUserAsync]");
|
||||||
throw;
|
throw;
|
||||||
} //NOSONAR
|
}
|
||||||
|
|
||||||
|
|
||||||
return user;
|
return user;
|
||||||
} //NOSONAR
|
}
|
||||||
|
|
||||||
public async Task<bool?> DeleteUserAsync(User user)
|
public async Task<bool?> DeleteUserAsync(User user)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user