Revert "Fixing coverage"

This reverts commit 61f7504e66.
This commit is contained in:
2025-03-23 20:34:45 +01:00
parent 8b5b3a198f
commit 5d104d5dc8

View File

@@ -2,11 +2,10 @@ using BasicDotnetTemplate.MainProject.Services;
using BasicDotnetTemplate.MainProject.Models.Api.Data.User;
using BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
namespace BasicDotnetTemplate.MainProject.Tests;
[TestClass]
[TestCategory("Integration")]
public class UserService_Tests
{
[TestMethod]
@@ -31,122 +30,33 @@ public class UserService_Tests
}
}
[TestMethod]
public async Task GetUserByUsernameAndPassword_Null()
public async Task CreateUser_And_GetById_ReturnsCorrectUserAsync()
{
try
// Arrange
var userService = TestUtils.CreateUserService();
var user = ModelsInit.CreateUser();
var role = ModelsInit.CreateRole();
CreateUserRequestData data = new CreateUserRequestData()
{
var userService = TestUtils.CreateUserService();
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}");
}
FirstName = user.FirstName,
LastName = user.LastName,
Email = user.Email,
Password = user.Password
};
// Act
var createdUser = await userService.CreateUserAsync(data, role);
Assert.IsNotNull(createdUser);
// Act
var retrievedUser = await userService.GetUserByIdAsync(createdUser.Id);
Assert.IsNotNull(retrievedUser);
// Assert
Assert.AreEqual(createdUser.Id, retrievedUser.Id);
Assert.AreEqual(createdUser.Guid, retrievedUser.Guid);
}
[TestMethod]
public async Task CheckIfEmailIsValid_EmailNotExists()
{
try
{
var userService = TestUtils.CreateUserService();
var user = ModelsInit.CreateUser();
if (userService != null)
{
var valid = await userService.CheckIfEmailIsValid(user.Email);
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 CreateUserAsync_Success()
{
try
{
var userService = TestUtils.CreateUserService();
var roleService = TestUtils.CreateRoleService();
var expectedUser = ModelsInit.CreateUser();
CreateUserRequestData data = new CreateUserRequestData()
{
FirstName = expectedUser.FirstName ?? String.Empty,
LastName = expectedUser.LastName ?? String.Empty,
Email = expectedUser.Email ?? String.Empty
};
Role role = new()
{
Name = expectedUser.Role?.Name ?? String.Empty,
IsNotEditable = expectedUser.Role?.IsNotEditable ?? false,
Guid = expectedUser.Role?.Guid ?? String.Empty
};
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);
Assert.IsTrue(valid);
// CheckIfEmailIsValid_EmailAlreadyExists
valid = await userService.CheckIfEmailIsValid(realUser.Email);
Assert.IsFalse(valid);
//GetUserByIdAsync
var getUserById = await userService.GetUserByIdAsync(realUser.Id);
Assert.IsNotNull(getUserById);
Assert.IsTrue(getUserById.Id == realUser?.Id);
//GetUserByGuidAsync
var getUserByGuid = await userService.GetUserByGuidAsync(realUser.Guid);
Assert.IsNotNull(getUserByGuid);
Assert.IsTrue(getUserByGuid.Guid == realUser?.Guid);
//DeleteUserAsync
var deleted = await userService.DeleteUserAsync(realUser);
Assert.IsTrue(deleted);
//DeleteRoleAsync
deleted = await roleService.DeleteRoleAsync(realRole);
Assert.IsTrue(deleted);
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex}");
}
}
}