Roles #21

Merged
csimonapastore merged 36 commits from roles into main 2025-03-26 23:52:19 +01:00
7 changed files with 501 additions and 24 deletions
Showing only changes of commit db37ebbdce - Show all commits

View File

@@ -33,20 +33,13 @@ namespace BasicDotnetTemplate.MainProject.Tests;
[TestClass]
public class UserControllerTests
{
private IMapper? _mapper;
private Mock<IUserService> _userServiceMock;
private Mock<IRoleService> _roleServiceMock;
private UserController _userController;
private Mock<IUserService>? _userServiceMock;
private Mock<IRoleService>? _roleServiceMock;
private UserController? _userController;
[TestInitialize]
public void Setup()
{
var config = new MapperConfiguration(cfg =>
{
cfg.AddProfile<AutoMapperConfiguration>();
});
_mapper = config.CreateMapper();
IConfiguration configuration = TestUtils.CreateConfiguration();
_userServiceMock = new Mock<IUserService>();
_roleServiceMock = new Mock<IRoleService>();
@@ -527,5 +520,4 @@ public class UserControllerTests
Assert.Fail($"Response is null");
}
}
}

View File

@@ -0,0 +1,234 @@
using BasicDotnetTemplate.MainProject.Models.Settings;
using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using BasicDotnetTemplate.MainProject.Utils;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Moq;
using Newtonsoft.Json;
using BasicDotnetTemplate.MainProject.Core.Database;
using BasicDotnetTemplate.MainProject.Services;
using BasicDotnetTemplate.MainProject.Models.Api.Response;
using BasicDotnetTemplate.MainProject.Models.Api.Request.Auth;
using BasicDotnetTemplate.MainProject.Models.Api.Data.Auth;
using BasicDotnetTemplate.MainProject.Models.Api.Common.Role;
using BasicDotnetTemplate.MainProject.Models.Api.Common.Role;
using BasicDotnetTemplate.MainProject.Models.Api.Response.Auth;
using DatabaseSqlServer = BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
using BasicDotnetTemplate.MainProject.Models.Api.Data.Role;
using BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
using BasicDotnetTemplate.MainProject.Models.Api.Data.Role;
namespace BasicDotnetTemplate.MainProject.Tests;
[TestClass]
public class RoleService_Tests
{
protected static Role? _expectedRole;
protected static Role? _role;
protected static RoleService? _roleService;
[TestInitialize]
public void Setup()
{
_expectedRole = ModelsInit.CreateRole();
_roleService = TestUtils.CreateRoleService();
}
[TestMethod]
public void Inizialize()
{
try
{
if (_roleService != null)
{
Assert.IsInstanceOfType(_roleService, typeof(RoleService));
}
else
{
Assert.Fail($"RoleService is null");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex}");
}
}
[TestMethod]
public async Task CheckIfNameIsValid_NameNotExists()
{
try
{
if (_roleService != null)
{
var valid = await _roleService.CheckIfNameIsValid(_expectedRole?.Name ?? String.Empty);
Assert.IsTrue(valid);
}
else
{
Assert.Fail($"RoleService is null");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex}");
}
}
[TestMethod]
public async Task CreateRoleData()
{
try
{
CreateRoleRequestData data = new CreateRoleRequestData()
{
Name = _expectedRole?.Name ?? String.Empty,
IsNotEditable = false
};
if (_roleService != null)
{
var role = await _roleService.CreateRoleAsync(data);
Assert.IsInstanceOfType(role, typeof(Role));
Assert.IsNotNull(role);
Assert.IsTrue(_expectedRole?.Name == role.Name);
Assert.IsTrue(_expectedRole?.IsNotEditable == role.IsNotEditable);
_role = 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 CheckIfNameIsValid_NameCurrentRole()
{
try
{
if (_roleService != null)
{
var valid = await _roleService.CheckIfNameIsValid(_expectedRole?.Name ?? String.Empty, _role?.Guid ?? String.Empty);
Assert.IsTrue(valid);
}
else
{
Assert.Fail($"RoleService is null");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex}");
}
}
[TestMethod]
public async Task CheckIfNameIsValid_NameAlreadyExists()
{
try
{
if (_roleService != null)
{
var valid = await _roleService.CheckIfNameIsValid(_expectedRole?.Name ?? String.Empty);
Assert.IsFalse(valid);
}
else
{
Assert.Fail($"RoleService is null");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex}");
}
}
[TestMethod]
public async Task GetRoleByIdAsync()
{
try
{
if (_roleService != null)
{
var role = await _roleService.GetRoleByIdAsync(_role?.Id ?? 0);
Assert.IsNotNull(role);
Assert.IsTrue(role.Id == _role?.Id);
}
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()
{
try
{
if (_roleService != null)
{
var role = await _roleService.GetRoleByGuidAsync(_role?.Guid ?? String.Empty);
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 DeleteRole()
{
try
{
if (_roleService != null)
{
var role = await _roleService.GetRoleByGuidAsync(_role?.Guid ?? String.Empty);
Assert.IsNotNull(role);
var deleted = await _roleService.DeleteRoleAsync(role);
Assert.IsTrue(deleted);
}
else
{
Assert.Fail($"RoleService is null");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex}");
}
}
}

View File

@@ -15,6 +15,8 @@ using BasicDotnetTemplate.MainProject.Models.Api.Common.User;
using BasicDotnetTemplate.MainProject.Models.Api.Common.Role;
using BasicDotnetTemplate.MainProject.Models.Api.Response.Auth;
using DatabaseSqlServer = BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
using BasicDotnetTemplate.MainProject.Models.Api.Data.User;
using BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
@@ -23,15 +25,29 @@ namespace BasicDotnetTemplate.MainProject.Tests;
[TestClass]
public class UserService_Tests
{
protected static User? _expectedUser;
protected static User? _user;
protected static Role? _role;
protected static UserService? _userService;
protected static RoleService? _roleService;
[TestInitialize]
public void Setup()
{
_expectedUser = ModelsInit.CreateUser();
_userService = TestUtils.CreateUserService();
_roleService = TestUtils.CreateRoleService();
}
[TestMethod]
public void Inizialize()
{
try
{
var userService = TestUtils.CreateUserService();
if (userService != null)
if (_userService != null)
{
Assert.IsInstanceOfType(userService, typeof(UserService));
Assert.IsInstanceOfType(_userService, typeof(UserService));
}
else
{
@@ -50,11 +66,10 @@ public class UserService_Tests
{
try
{
var userService = TestUtils.CreateUserService();
var testString = "test";
if (userService != null)
if (_userService != null)
{
var user = await userService.GetUserByUsernameAndPassword(testString, testString);
var user = await _userService.GetUserByUsernameAndPassword(testString, testString);
Assert.IsTrue(user == null);
}
else
@@ -75,12 +90,12 @@ public class UserService_Tests
{
try
{
var userService = TestUtils.CreateUserService();
var testEmail = "test@email.it";
var testPassword = "password";
if (userService != null)
if (_userService != null)
{
var user = await userService.GetUserByUsernameAndPassword(testEmail, testPassword);
var user = await _userService.GetUserByUsernameAndPassword(testEmail, testPassword);
Assert.IsTrue(user != null);
Assert.IsTrue(user.Email == testEmail);
}
@@ -96,6 +111,195 @@ public class UserService_Tests
}
}
[TestMethod]
public async Task CheckIfEmailIsValid_EmailNotExists()
{
try
{
if (_userService != null)
{
var valid = await _userService.CheckIfEmailIsValid(_expectedUser?.Email ?? String.Empty);
Assert.IsTrue(valid);
}
else
{
Assert.Fail($"UserService is null");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex}");
}
}
[TestMethod]
public async Task CreateUserData()
{
try
{
CreateUserRequestData data = new CreateUserRequestData()
{
FirstName = _expectedUser?.FirstName ?? String.Empty,
LastName = _expectedUser?.LastName ?? String.Empty,
Email = _expectedUser?.Email ?? String.Empty
};
Role role = new Role()
{
Name = _expectedUser?.Role?.Name ?? String.Empty,
IsNotEditable = _expectedUser?.Role?.IsNotEditable ?? false,
Guid = _expectedUser?.Role?.Guid ?? String.Empty
};
if (_userService != null)
{
var user = await _userService.CreateUserAsync(data, role);
Assert.IsInstanceOfType(user, typeof(User));
Assert.IsNotNull(user);
Assert.IsTrue(_expectedUser?.FirstName == user.FirstName);
Assert.IsTrue(_expectedUser?.LastName == user.LastName);
Assert.IsTrue(_expectedUser?.Email == user.Email);
Assert.IsTrue(_expectedUser?.Role?.Name == user.Role?.Name);
_user = user;
_role = user.Role;
}
else
{
Assert.Fail($"UserService is null");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex}");
}
}
[TestMethod]
public async Task CheckIfEmailIsValid_EmailCurrentUser()
{
try
{
if (_userService != null)
{
var valid = await _userService.CheckIfEmailIsValid(_expectedUser?.Email ?? String.Empty, _user?.Guid ?? String.Empty);
Assert.IsTrue(valid);
}
else
{
Assert.Fail($"UserService is null");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex}");
}
}
[TestMethod]
public async Task CheckIfEmailIsValid_EmailAlreadyExists()
{
try
{
if (_userService != null)
{
var valid = await _userService.CheckIfEmailIsValid(_expectedUser?.Email ?? String.Empty);
Assert.IsFalse(valid);
}
else
{
Assert.Fail($"UserService is null");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex}");
}
}
[TestMethod]
public async Task GetUserByIdAsync()
{
try
{
if (_userService != null)
{
var user = await _userService.GetUserByIdAsync(_user?.Id ?? 0);
Assert.IsNotNull(user);
Assert.IsTrue(user.Id == _user?.Id);
}
else
{
Assert.Fail($"UserService is null");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex}");
}
}
[TestMethod]
public async Task GetUserByGuidAsync()
{
try
{
if (_userService != null)
{
var user = await _userService.GetUserByGuidAsync(_user?.Guid ?? String.Empty);
Assert.IsNotNull(user);
Assert.IsTrue(user.Guid == _user?.Guid);
}
else
{
Assert.Fail($"UserService is null");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex}");
}
}
[TestMethod]
public async Task DeleteUser()
{
try
{
if (_userService != null)
{
var user = await _userService.GetUserByGuidAsync(_user?.Guid ?? String.Empty);
Assert.IsNotNull(user);
var deleted = await _userService.DeleteUserAsync(user);
Assert.IsTrue(deleted);
}
else
{
Assert.Fail($"UserService is null");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex}");
}
}
[TestCleanup]
public static async Task CleanupAsync()
{
var role = await _roleService?.GetRoleByGuidAsync(_role?.Guid ?? String.Empty);
Assert.IsNotNull(role);
var deleted = await _roleService?.DeleteRoleAsync(role);
Assert.IsTrue(deleted);
}
}

View File

@@ -81,6 +81,16 @@ public static class TestUtils
var httpContextAccessor = new Mock<IHttpContextAccessor>();
return new JwtService(httpContextAccessor.Object, configuration, sqlServerContext);
}
public static RoleService CreateRoleService()
{
IConfiguration configuration = CreateConfiguration();
var optionsBuilder = new DbContextOptionsBuilder<SqlServerContext>();
optionsBuilder.UseSqlServer(GetSqlConnectionString(configuration));
SqlServerContext sqlServerContext = new SqlServerContext(optionsBuilder.Options);
var httpContextAccessor = new Mock<IHttpContextAccessor>();
return new RoleService(httpContextAccessor.Object, configuration, sqlServerContext);
}
}

View File

@@ -12,8 +12,9 @@ public interface IRoleService
Task<Role?> GetRoleByIdAsync(int id);
Task<Role?> GetRoleByGuidAsync(string guid);
Task<bool> CheckIfNameIsValid(string name, string? guid = "");
Task<Role?> CreateRole(CreateRoleRequestData data);
Task<Role?> CreateRoleAsync(CreateRoleRequestData data);
Task<Role?> GetRoleForUser(string? guid);
Task<bool?> DeleteRoleAsync(Role role);
}
public class RoleService : BaseService, IRoleService
@@ -29,7 +30,6 @@ public class RoleService : BaseService, IRoleService
{
return this._sqlServerContext.Roles.Where(x => !x.IsDeleted);
}
private IQueryable<Role> GetRoleByNameQueryable(string name)
{
return this.GetRolesQueryable().Where(x =>
@@ -88,7 +88,7 @@ public class RoleService : BaseService, IRoleService
return valid;
}
public async Task<Role?> CreateRole(CreateRoleRequestData data)
public async Task<Role?> CreateRoleAsync(CreateRoleRequestData data)
{
Role? role = null;
@@ -119,5 +119,24 @@ public class RoleService : BaseService, IRoleService
return role;
}
public async Task<bool?> DeleteRoleAsync(Role role)
{
bool? deleted = false;
using (var transaction = _sqlServerContext.Database.BeginTransactionAsync())
{
role.IsDeleted = true;
role.DeletionTime = DateTime.UtcNow;
_sqlServerContext.Update(role);
await _sqlServerContext.SaveChangesAsync();
await (await transaction).CommitAsync();
deleted = true;
}
return deleted;
}
}

View File

@@ -14,6 +14,7 @@ public interface IUserService
Task<User?> GetUserByUsernameAndPassword(string email, string password);
Task<bool> CheckIfEmailIsValid(string email, string? guid = "");
Task<User?> CreateUserAsync(CreateUserRequestData data, Role role);
Task<bool?> DeleteUserAsync(User user);
}
public class UserService : BaseService, IUserService
@@ -117,6 +118,23 @@ public class UserService : BaseService, IUserService
return user;
}
public async Task<bool?> DeleteUserAsync(User user)
{
bool? deleted = false;
using (var transaction = _sqlServerContext.Database.BeginTransactionAsync())
{
user.IsDeleted = true;
user.DeletionTime = DateTime.UtcNow;
_sqlServerContext.Update(user);
await _sqlServerContext.SaveChangesAsync();
await (await transaction).CommitAsync();
deleted = true;
}
return deleted;
}
}

View File

@@ -256,7 +256,7 @@ public static class ProgramUtils
Name = roleName,
IsNotEditable = true
};
var createThread = Task.Run(() => roleService!.Invoke()?.CreateRole(data));
var createThread = Task.Run(() => roleService!.Invoke()?.CreateRoleAsync(data));
Role? role = createThread.Result;
if (role != null)
{