This commit is contained in:
2025-03-23 23:34:54 +01:00
parent 030882f3e1
commit b7398fd910
3 changed files with 289 additions and 40 deletions

View File

@@ -49,6 +49,7 @@ jobs:
shell: powershell shell: powershell
run: | run: |
.\.sonar\scanner\dotnet-sonarscanner begin /k:"csimonapastore_BasicDotnetTemplate" /o:"csimonapastore-github" /d:sonar.token="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.cs.vscoveragexml.reportsPaths=coverage.xml /d:sonar.exclusions="**/Migrations/**.cs, **/Models/Database/**.cs, **/Core/Database/**.cs" .\.sonar\scanner\dotnet-sonarscanner begin /k:"csimonapastore_BasicDotnetTemplate" /o:"csimonapastore-github" /d:sonar.token="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.cs.vscoveragexml.reportsPaths=coverage.xml /d:sonar.exclusions="**/Migrations/**.cs, **/Models/Database/**.cs, **/Core/Database/**.cs"
dotnet clean
dotnet restore dotnet restore
dotnet build --no-incremental dotnet build --no-incremental
dotnet-coverage collect "dotnet test" -f xml -o "coverage.xml" dotnet-coverage collect "dotnet test" -f xml -o "coverage.xml"

View File

@@ -1,14 +1,17 @@
using BasicDotnetTemplate.MainProject.Services; using BasicDotnetTemplate.MainProject.Services;
using BasicDotnetTemplate.MainProject.Models.Api.Data.User; using BasicDotnetTemplate.MainProject.Models.Api.Data.User;
using BasicDotnetTemplate.MainProject.Models.Database.SqlServer; using BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
using Microsoft.EntityFrameworkCore;
namespace BasicDotnetTemplate.MainProject.Tests; namespace BasicDotnetTemplate.MainProject.Tests;
[TestClass] [TestClass]
[TestCategory("Integration")]
public class UserService_Tests public class UserService_Tests
{ {
private static User _user = ModelsInit.CreateUser();
private static Role _role = ModelsInit.CreateRole();
[TestMethod] [TestMethod]
public void Inizialize() public void Inizialize()
{ {
@@ -31,35 +34,281 @@ public class UserService_Tests
} }
} }
// [TestInitialize]
// public void Setup()
// {
// _expectedUser = ModelsInit.CreateUser();
// _expectedRole = ModelsInit.CreateRole();
// _roleService = TestUtils.CreateRoleService();
// _userService = TestUtils.CreateUserService();
// }
// [TestMethod]
// public void Inizialize()
// {
// try
// {
// if (_userService != null)
// {
// Assert.IsInstanceOfType(_userService, typeof(UserService));
// }
// else
// {
// Assert.Fail($"UserService is null");
// }
// }
// catch (Exception ex)
// {
// Console.WriteLine(ex.InnerException);
// Assert.Fail($"An exception was thrown: {ex}");
// }
// }
[TestMethod] [TestMethod]
public async Task CreateUser_And_GetById_ReturnsCorrectUserAsync() public async Task GetUserByUsernameAndPassword_Null()
{
try
{ {
// Arrange
var userService = TestUtils.CreateUserService(); var userService = TestUtils.CreateUserService();
var user = ModelsInit.CreateUser(); var testString = "test";
var role = ModelsInit.CreateRole(); if (userService != null)
CreateUserRequestData data = new()
{ {
FirstName = user.FirstName, var user = await userService.GetUserByUsernameAndPassword(testString, testString);
LastName = user.LastName, Assert.IsTrue(user == null);
Email = user.Email, }
Password = user.Password else
{
Assert.Fail($"UserService is null");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex}");
}
}
// // TODO
// // [TestMethod]
// public async Task GetUserByUsernameAndPassword_Success()
// {
// 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}");
// }
// }
[TestMethod]
public async Task CheckIfEmailIsValid_EmailNotExists()
{
try
{
var userService = TestUtils.CreateUserService();
if (userService != null)
{
var valid = await userService.CheckIfEmailIsValid(_user.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 CreateUserAsync_Success()
{
try
{
var userService = TestUtils.CreateUserService();
var expectedUser = ModelsInit.CreateUser();
CreateUserRequestData data = new CreateUserRequestData()
{
FirstName = expectedUser.FirstName ?? String.Empty,
LastName = expectedUser.LastName ?? String.Empty,
Email = expectedUser.Email ?? String.Empty
}; };
// Act Role role = new()
var createdUser = await userService.CreateUserAsync(data, role); {
Assert.IsNotNull(createdUser); Name = expectedUser.Role?.Name ?? String.Empty,
IsNotEditable = expectedUser.Role?.IsNotEditable ?? false,
Guid = expectedUser.Role?.Guid ?? String.Empty
};
// Act var user = await userService.CreateUserAsync(data, role);
var retrievedUser = await userService.GetUserByIdAsync(createdUser!.Id); Assert.IsInstanceOfType(user, typeof(User));
Assert.IsNotNull(retrievedUser); 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;
// Assert }
Assert.AreEqual(createdUser!.Id, retrievedUser!.Id); catch (Exception ex)
Assert.AreEqual(createdUser!.Guid, retrievedUser!.Guid); {
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex}");
}
}
[TestMethod]
public async Task CheckIfEmailIsValid_EmailCurrentUser()
{
try
{
var userService = TestUtils.CreateUserService();
if (userService != null)
{
var valid = await userService.CheckIfEmailIsValid(_user.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
{
var userService = TestUtils.CreateUserService();
if (userService != null)
{
var valid = await userService.CheckIfEmailIsValid(_user.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
{
var userService = TestUtils.CreateUserService();
if (userService != null)
{
var user = await userService.GetUserByIdAsync(_user.Id);
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
{
var userService = TestUtils.CreateUserService();
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
{
var userService = TestUtils.CreateUserService();
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}");
} }
} }
[TestMethod]
public static async Task CleanupAsync()
{
var roleService = TestUtils.CreateRoleService();
var role = await roleService.GetRoleByGuidAsync(_role.Guid ?? String.Empty);
Assert.IsNotNull(role);
var deleted = await roleService.DeleteRoleAsync(role);
Assert.IsTrue(deleted);
}
}

View File

@@ -29,19 +29,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,7 +59,7 @@ public class UserService : BaseService, IUserService
}; };
return user; return user;
} } //NOSONAR
public async Task<User?> GetUserByIdAsync(int id) public async Task<User?> GetUserByIdAsync(int id)
@@ -109,29 +109,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
User? user = null;
using var transaction = await _sqlServerContext.Database.BeginTransactionAsync(); using var transaction = await _sqlServerContext.Database.BeginTransactionAsync();
User? user;
try try
{ { //NOSONAR
var tempUser = CreateUserData(data, role); var tempUser = CreateUserData(data, role);
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)
{ {