Roles #21

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

View File

@@ -426,61 +426,6 @@ public class UserControllerTests
}
}
[TestMethod]
public async Task CreateUserAsync_NotCreated()
{
if (_userController == null)
{
Assert.Fail($"_userController is null");
}
if (_roleServiceMock == null)
{
Assert.Fail($"_roleServiceMock is null");
}
DatabaseSqlServer.User user = ModelsInit.CreateUser();
DatabaseSqlServer.Role role = ModelsInit.CreateRole();
DatabaseSqlServer.User? expectedUser = null;
CreateUserRequest request = new CreateUserRequest()
{
Data = new CreateUserRequestData()
{
FirstName = user.FirstName,
LastName = user.LastName,
Email = user.Email,
Password = user.Password
}
};
_userServiceMock?.Setup(s => s.CheckIfEmailIsValid(It.IsAny<string>(), It.IsAny<string>())).ReturnsAsync(true);
_roleServiceMock.Setup(s => s.GetRoleForUser(null)).ReturnsAsync(role);
_userServiceMock?.Setup(s => s.CreateUserAsync(request.Data, role)).ReturnsAsync(expectedUser);
ObjectResult response = (ObjectResult)(await _userController.CreateUserAsync(request));
if (response != null && response.Value != null)
{
Assert.IsTrue(response.StatusCode == StatusCodes.Status400BadRequest);
var result = (BaseResponse<object>)response.Value;
if (result != null)
{
Assert.IsTrue(result.Status == StatusCodes.Status400BadRequest);
Assert.IsTrue(result.Message == "Not created");
}
else
{
Assert.Fail($"Result value is null");
}
}
else
{
Assert.Fail($"Response value is null");
}
}
[TestMethod]
public async Task CreateUserAsync_ModelInvalid()
{

View File

@@ -105,39 +105,35 @@ public class UserService_Tests
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 realUser = user!;
Role realRole = user!.Role!;
// CheckIfEmailIsValid_CurrentUser
var valid = await userService.CheckIfEmailIsValid(realUser.Email, realUser.Guid);
var valid = await userService.CheckIfEmailIsValid(user.Email, user.Guid);
Assert.IsTrue(valid);
// CheckIfEmailIsValid_EmailAlreadyExists
valid = await userService.CheckIfEmailIsValid(realUser.Email);
valid = await userService.CheckIfEmailIsValid(user.Email);
Assert.IsFalse(valid);
//GetUserByIdAsync
var getUserById = await userService.GetUserByIdAsync(realUser.Id);
var getUserById = await userService.GetUserByIdAsync(user.Id);
Assert.IsNotNull(getUserById);
Assert.IsTrue(getUserById.Id == realUser?.Id);
Assert.IsTrue(getUserById.Id == user?.Id);
//GetUserByGuidAsync
var getUserByGuid = await userService.GetUserByGuidAsync(realUser.Guid);
var getUserByGuid = await userService.GetUserByGuidAsync(user.Guid);
Assert.IsNotNull(getUserByGuid);
Assert.IsTrue(getUserByGuid.Guid == realUser?.Guid);
Assert.IsTrue(getUserByGuid.Guid == user?.Guid);
//DeleteUserAsync
var deleted = await userService.DeleteUserAsync(realUser);
var deleted = await userService.DeleteUserAsync(user);
Assert.IsTrue(deleted);
//DeleteRoleAsync
deleted = await roleService.DeleteRoleAsync(realRole);
deleted = await roleService.DeleteRoleAsync(user.Role!);
Assert.IsTrue(deleted);
}
catch (Exception ex)

View File

@@ -101,11 +101,6 @@ namespace BasicDotnetTemplate.MainProject.Controllers
var user = await this._userService.CreateUserAsync(request.Data, role);
if (user == null || String.IsNullOrEmpty(user.Guid))
{
return BadRequest("Not created");
}
var userDto = _mapper?.Map<UserDto>(user);
return Success(String.Empty, userDto);

View File

@@ -13,7 +13,7 @@ public interface IUserService
Task<User?> GetUserByGuidAsync(string guid);
Task<User?> GetUserByUsernameAndPassword(string email, string password);
Task<bool> CheckIfEmailIsValid(string email, string? guid = "");
Task<User?> CreateUserAsync(CreateUserRequestData data, Role role);
Task<User> CreateUserAsync(CreateUserRequestData data, Role role);
Task<bool?> DeleteUserAsync(User user);
}
@@ -102,17 +102,15 @@ public class UserService : BaseService, IUserService
return valid;
}
public async Task<User?> CreateUserAsync(CreateUserRequestData data, Role role)
public async Task<User> CreateUserAsync(CreateUserRequestData data, Role role)
{
User? user = null;
User user = this.CreateUserData(data, role);
using (var transaction = _sqlServerContext.Database.BeginTransactionAsync())
{
var tempUser = this.CreateUserData(data, role);
await _sqlServerContext.Users.AddAsync(tempUser);
await _sqlServerContext.Users.AddAsync(user);
await _sqlServerContext.SaveChangesAsync();
await (await transaction).CommitAsync();
user = tempUser;
}
return user;