Roles #21

Merged
csimonapastore merged 36 commits from roles into main 2025-03-26 23:52:19 +01:00
Showing only changes of commit a4e3b0666b - Show all commits

View File

@@ -17,278 +17,300 @@ public class UserService_Tests
private static RoleService? _roleService; private static RoleService? _roleService;
private static UserService? _userService; private static UserService? _userService;
[TestInitialize]
public void Setup()
{
_expectedUser = ModelsInit.CreateUser();
_expectedRole = ModelsInit.CreateRole();
_roleService = TestUtils.CreateRoleService();
_userService = TestUtils.CreateUserService();
}
[TestMethod] [TestMethod]
public void Inizialize() public void Inizialize()
{ {
try try
{ {
if (_userService != null) var userService = TestUtils.CreateUserService();
if(userService != null)
{ {
Assert.IsInstanceOfType(_userService, typeof(UserService)); Assert.IsInstanceOfType(userService, typeof(UserService));
} }
else else
{ {
Assert.Fail($"UserService is null"); Assert.Fail($"UserService is null");
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine(ex.InnerException); Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex}"); Assert.Fail($"An exception was thrown: {ex.Message}");
} }
} }
[TestMethod] // [TestInitialize]
public async Task GetUserByUsernameAndPassword_Null() // public void Setup()
{ // {
try // _expectedUser = ModelsInit.CreateUser();
{ // _expectedRole = ModelsInit.CreateRole();
_expectedUser = ModelsInit.CreateUser(); // _roleService = TestUtils.CreateRoleService();
_userService = TestUtils.CreateUserService(); // _userService = TestUtils.CreateUserService();
_roleService = TestUtils.CreateRoleService(); // }
var testString = "test";
if (_userService != null)
{
var user = await _userService.GetUserByUsernameAndPassword(testString, testString);
Assert.IsTrue(user == null);
}
else
{
Assert.Fail($"UserService is null");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex}");
}
}
// TODO
// [TestMethod] // [TestMethod]
public async Task GetUserByUsernameAndPassword_Success() // public void Inizialize()
{ // {
try // try
{ // {
var testEmail = "test@email.it"; // if (_userService != null)
var testPassword = "password"; // {
if (_userService != null) // Assert.IsInstanceOfType(_userService, typeof(UserService));
{ // }
var user = await _userService.GetUserByUsernameAndPassword(testEmail, testPassword); // else
Assert.IsTrue(user != null); // {
Assert.IsTrue(user.Email == testEmail); // Assert.Fail($"UserService is null");
} // }
else // }
{ // catch (Exception ex)
Assert.Fail($"UserService is null"); // {
} // Console.WriteLine(ex.InnerException);
} // Assert.Fail($"An exception was thrown: {ex}");
catch (Exception ex) // }
{ // }
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex}");
}
}
[TestMethod] // [TestMethod]
public async Task CheckIfEmailIsValid_EmailNotExists() // public async Task GetUserByUsernameAndPassword_Null()
{ // {
try // try
{ // {
if (_userService != null) // _expectedUser = ModelsInit.CreateUser();
{ // _userService = TestUtils.CreateUserService();
var valid = await _userService.CheckIfEmailIsValid(_expectedUser?.Email ?? String.Empty); // _roleService = TestUtils.CreateRoleService();
Assert.IsTrue(valid);
}
else
{
Assert.Fail($"UserService is null");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex}");
}
}
[TestMethod] // var testString = "test";
public async Task CreateUserData() // if (_userService != null)
{ // {
try // var user = await _userService.GetUserByUsernameAndPassword(testString, testString);
{ // Assert.IsTrue(user == null);
CreateUserRequestData data = new CreateUserRequestData() // }
{ // else
FirstName = _expectedUser?.FirstName ?? String.Empty, // {
LastName = _expectedUser?.LastName ?? String.Empty, // Assert.Fail($"UserService is null");
Email = _expectedUser?.Email ?? String.Empty // }
}; // }
// catch (Exception ex)
// {
// Console.WriteLine(ex.InnerException);
// Assert.Fail($"An exception was thrown: {ex}");
// }
// }
Role role = new Role() // // TODO
{ // // [TestMethod]
Name = _expectedUser?.Role?.Name ?? String.Empty, // public async Task GetUserByUsernameAndPassword_Success()
IsNotEditable = _expectedUser?.Role?.IsNotEditable ?? false, // {
Guid = _expectedUser?.Role?.Guid ?? String.Empty // try
}; // {
// var testEmail = "test@email.it";
// var testPassword = "password";
// if (_userService != null)
// {
// var user = await _userService.GetUserByUsernameAndPassword(testEmail, testPassword);
// Assert.IsTrue(user != null);
// Assert.IsTrue(user.Email == testEmail);
// }
// else
// {
// Assert.Fail($"UserService is null");
// }
// }
// catch (Exception ex)
// {
// Console.WriteLine(ex.InnerException);
// Assert.Fail($"An exception was thrown: {ex}");
// }
// }
if (_userService != null) // [TestMethod]
{ // public async Task CheckIfEmailIsValid_EmailNotExists()
var user = await _userService.CreateUserAsync(data, role); // {
Assert.IsInstanceOfType(user, typeof(User)); // try
Assert.IsNotNull(user); // {
Assert.IsTrue(_expectedUser?.FirstName == user.FirstName); // if (_userService != null)
Assert.IsTrue(_expectedUser?.LastName == user.LastName); // {
Assert.IsTrue(_expectedUser?.Email == user.Email); // var valid = await _userService.CheckIfEmailIsValid(_expectedUser?.Email ?? String.Empty);
Assert.IsTrue(_expectedUser?.Role?.Name == user.Role?.Name); // Assert.IsTrue(valid);
_user = user; // }
_role = user.Role; // else
} // {
else // Assert.Fail($"UserService is null");
{ // }
Assert.Fail($"UserService is null"); // }
} // catch (Exception ex)
} // {
catch (Exception ex) // Console.WriteLine(ex.InnerException);
{ // Assert.Fail($"An exception was thrown: {ex}");
Console.WriteLine(ex.InnerException); // }
Assert.Fail($"An exception was thrown: {ex}"); // }
}
}
[TestMethod] // [TestMethod]
public async Task CheckIfEmailIsValid_EmailCurrentUser() // public async Task CreateUserData()
{ // {
try // try
{ // {
if (_userService != null) // CreateUserRequestData data = new CreateUserRequestData()
{ // {
var valid = await _userService.CheckIfEmailIsValid(_expectedUser?.Email ?? String.Empty, _user?.Guid ?? String.Empty); // FirstName = _expectedUser?.FirstName ?? String.Empty,
Assert.IsTrue(valid); // LastName = _expectedUser?.LastName ?? String.Empty,
} // Email = _expectedUser?.Email ?? String.Empty
else // };
{
Assert.Fail($"UserService is null");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex}");
}
}
[TestMethod] // Role role = new Role()
public async Task CheckIfEmailIsValid_EmailAlreadyExists() // {
{ // Name = _expectedUser?.Role?.Name ?? String.Empty,
try // IsNotEditable = _expectedUser?.Role?.IsNotEditable ?? false,
{ // Guid = _expectedUser?.Role?.Guid ?? String.Empty
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] // if (_userService != null)
public async Task GetUserByIdAsync() // {
{ // var user = await _userService.CreateUserAsync(data, role);
try // Assert.IsInstanceOfType(user, typeof(User));
{ // Assert.IsNotNull(user);
if (_userService != null) // Assert.IsTrue(_expectedUser?.FirstName == user.FirstName);
{ // Assert.IsTrue(_expectedUser?.LastName == user.LastName);
var user = await _userService.GetUserByIdAsync(_user?.Id ?? 0); // Assert.IsTrue(_expectedUser?.Email == user.Email);
Assert.IsNotNull(user); // Assert.IsTrue(_expectedUser?.Role?.Name == user.Role?.Name);
Assert.IsTrue(user.Id == _user?.Id); // _user = user;
} // _role = user.Role;
else // }
{ // else
Assert.Fail($"UserService is null"); // {
} // Assert.Fail($"UserService is null");
} // }
catch (Exception ex) // }
{ // catch (Exception ex)
Console.WriteLine(ex.InnerException); // {
Assert.Fail($"An exception was thrown: {ex}"); // Console.WriteLine(ex.InnerException);
} // Assert.Fail($"An exception was thrown: {ex}");
} // }
// }
[TestMethod] // [TestMethod]
public async Task GetUserByGuidAsync() // public async Task CheckIfEmailIsValid_EmailCurrentUser()
{ // {
try // try
{ // {
if (_userService != null) // if (_userService != null)
{ // {
var user = await _userService.GetUserByGuidAsync(_user?.Guid ?? String.Empty); // var valid = await _userService.CheckIfEmailIsValid(_expectedUser?.Email ?? String.Empty, _user?.Guid ?? String.Empty);
Assert.IsNotNull(user); // Assert.IsTrue(valid);
Assert.IsTrue(user.Guid == _user?.Guid); // }
} // else
else // {
{ // Assert.Fail($"UserService is null");
Assert.Fail($"UserService is null"); // }
} // }
} // catch (Exception ex)
catch (Exception ex) // {
{ // Console.WriteLine(ex.InnerException);
Console.WriteLine(ex.InnerException); // Assert.Fail($"An exception was thrown: {ex}");
Assert.Fail($"An exception was thrown: {ex}"); // }
} // }
}
[TestMethod] // [TestMethod]
public async Task DeleteUser() // public async Task CheckIfEmailIsValid_EmailAlreadyExists()
{ // {
try // try
{ // {
if (_userService != null) // if (_userService != null)
{ // {
var user = await _userService.GetUserByGuidAsync(_user?.Guid ?? String.Empty); // var valid = await _userService.CheckIfEmailIsValid(_expectedUser?.Email ?? String.Empty);
Assert.IsNotNull(user); // Assert.IsFalse(valid);
var deleted = await _userService.DeleteUserAsync(user); // }
Assert.IsTrue(deleted); // else
} // {
else // Assert.Fail($"UserService is null");
{ // }
Assert.Fail($"UserService is null"); // }
} // catch (Exception ex)
} // {
catch (Exception ex) // Console.WriteLine(ex.InnerException);
{ // Assert.Fail($"An exception was thrown: {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] // [TestCleanup]
public static async Task CleanupAsync() // public static async Task CleanupAsync()
{ // {
var role = await _roleService?.GetRoleByGuidAsync(_role?.Guid ?? String.Empty); // var role = await _roleService?.GetRoleByGuidAsync(_role?.Guid ?? String.Empty);
Assert.IsNotNull(role); // Assert.IsNotNull(role);
var deleted = await _roleService?.DeleteRoleAsync(role); // var deleted = await _roleService?.DeleteRoleAsync(role);
Assert.IsTrue(deleted); // Assert.IsTrue(deleted);
} // }
} }