Adding role creation during startup + minor fixes in tests

This commit is contained in:
2025-03-16 22:41:44 +01:00
parent 7f5178883d
commit 18e713153b
48 changed files with 1449 additions and 340 deletions

View File

@@ -22,18 +22,15 @@ public class UserRole_Tests
{
try
{
DatabaseSqlServer.Role role = new DatabaseSqlServer.Role()
{
Name = "test"
};
DatabaseSqlServer.Role role = ModelsInit.CreateRole();
UserRole userRole = new UserRole(role);
Assert.IsTrue(userRole.Name == role.Name);
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex.Message}");
Assert.Fail($"An exception was thrown: {ex}");
}
}
}

View File

@@ -22,24 +22,9 @@ public class AuthenticatedUser_Tests
{
try
{
DatabaseSqlServer.User user = new DatabaseSqlServer.User()
{
Username = "test",
FirstName = "test",
LastName = "test",
Email = "test",
PasswordHash = "test",
PasswordSalt = "test",
Password = "test",
Role = new DatabaseSqlServer.Role()
{
Name = "test"
},
IsTestUser = true
};
DatabaseSqlServer.User user = ModelsInit.CreateUser();
AuthenticatedUser authenticatedUser = new AuthenticatedUser(user);
Assert.IsTrue(authenticatedUser.Username == user.Username);
Assert.IsTrue(authenticatedUser.FirstName == user.FirstName);
Assert.IsTrue(authenticatedUser.LastName == user.LastName);
Assert.IsTrue(authenticatedUser.Email == user.Email);
@@ -48,7 +33,7 @@ public class AuthenticatedUser_Tests
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex.Message}");
Assert.Fail($"An exception was thrown: {ex}");
}
}
}

View File

@@ -6,6 +6,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting;
using BasicDotnetTemplate.MainProject;
using BasicDotnetTemplate.MainProject.Models.Api.Response;
using Microsoft.Extensions.DependencyModel.Resolution;
using Microsoft.AspNetCore.Http;
namespace BasicDotnetTemplate.MainProject.Tests;
@@ -19,12 +20,12 @@ public class ApiResponse_Tests
try
{
var baseResponse = new BaseResponse<object>(200, null, null);
Assert.IsTrue(baseResponse.Status == 200 && String.IsNullOrEmpty(baseResponse.Message) && baseResponse.Data == null);
Assert.IsTrue(baseResponse.Status == StatusCodes.Status200OK && String.IsNullOrEmpty(baseResponse.Message) && baseResponse.Data == null);
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex.Message}");
Assert.Fail($"An exception was thrown: {ex}");
}
}
@@ -34,12 +35,12 @@ public class ApiResponse_Tests
try
{
var baseResponse = new BaseResponse<object>(201, null, null);
Assert.IsFalse(baseResponse.Status == 200);
Assert.IsFalse(baseResponse.Status == StatusCodes.Status200OK);
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex.Message}");
Assert.Fail($"An exception was thrown: {ex}");
}
}
@@ -49,12 +50,12 @@ public class ApiResponse_Tests
try
{
var baseResponse = new BaseResponse<object>(200, "This is a test message", null);
Assert.IsTrue(baseResponse.Status == 200 && baseResponse.Message == "This is a test message" && baseResponse.Data == null);
Assert.IsTrue(baseResponse.Status == StatusCodes.Status200OK && baseResponse.Message == "This is a test message" && baseResponse.Data == null);
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex.Message}");
Assert.Fail($"An exception was thrown: {ex}");
}
}
@@ -65,12 +66,12 @@ public class ApiResponse_Tests
{
string[] data = { "Volvo", "BMW", "Ford", "Mazda" };
var baseResponse = new BaseResponse<string[]>(200, "This is a test message", data);
Assert.IsTrue(baseResponse.Status == 200 && baseResponse.Message == "This is a test message" && baseResponse.Data == data);
Assert.IsTrue(baseResponse.Status == StatusCodes.Status200OK && baseResponse.Message == "This is a test message" && baseResponse.Data == data);
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex.Message}");
Assert.Fail($"An exception was thrown: {ex}");
}
}
}

View File

@@ -10,6 +10,7 @@ using BasicDotnetTemplate.MainProject.Models.Api.Common.User;
using BasicDotnetTemplate.MainProject.Models.Api.Common.Role;
using DatabaseSqlServer = BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
using BasicDotnetTemplate.MainProject.Models.Api.Response.Auth;
using Microsoft.AspNetCore.Http;
namespace BasicDotnetTemplate.MainProject.Tests;
@@ -23,12 +24,12 @@ public class AuthenticateResponse_Tests
try
{
var authenticateResponse = new AuthenticateResponse(200, null, null);
Assert.IsTrue(authenticateResponse.Status == 200 && String.IsNullOrEmpty(authenticateResponse.Message) && authenticateResponse.Data == null);
Assert.IsTrue(authenticateResponse.Status == StatusCodes.Status200OK && String.IsNullOrEmpty(authenticateResponse.Message) && authenticateResponse.Data == null);
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex.Message}");
Assert.Fail($"An exception was thrown: {ex}");
}
}
@@ -38,12 +39,12 @@ public class AuthenticateResponse_Tests
try
{
var authenticateResponse = new AuthenticateResponse(201, null, null);
Assert.IsFalse(authenticateResponse.Status == 200);
Assert.IsFalse(authenticateResponse.Status == StatusCodes.Status200OK);
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex.Message}");
Assert.Fail($"An exception was thrown: {ex}");
}
}
@@ -53,12 +54,12 @@ public class AuthenticateResponse_Tests
try
{
var authenticateResponse = new AuthenticateResponse(200, "This is a test message", null);
Assert.IsTrue(authenticateResponse.Status == 200 && authenticateResponse.Message == "This is a test message" && authenticateResponse.Data == null);
Assert.IsTrue(authenticateResponse.Status == StatusCodes.Status200OK && authenticateResponse.Message == "This is a test message" && authenticateResponse.Data == null);
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex.Message}");
Assert.Fail($"An exception was thrown: {ex}");
}
}
@@ -67,29 +68,15 @@ public class AuthenticateResponse_Tests
{
try
{
DatabaseSqlServer.User user = new DatabaseSqlServer.User()
{
Username = "test",
FirstName = "test",
LastName = "test",
Email = "test",
PasswordHash = "test",
PasswordSalt = "test",
Password = "test",
Role = new DatabaseSqlServer.Role()
{
Name = "test"
},
IsTestUser = true
};
DatabaseSqlServer.User user = ModelsInit.CreateUser();
AuthenticatedUser data = new AuthenticatedUser(user);
var authenticateResponse = new AuthenticateResponse(200, "This is a test message", data);
Assert.IsTrue(authenticateResponse.Status == 200 && authenticateResponse.Message == "This is a test message" && authenticateResponse.Data == data);
Assert.IsTrue(authenticateResponse.Status == StatusCodes.Status200OK && authenticateResponse.Message == "This is a test message" && authenticateResponse.Data == data);
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex.Message}");
Assert.Fail($"An exception was thrown: {ex}");
}
}
}

View File

@@ -12,6 +12,7 @@ using DatabaseSqlServer = BasicDotnetTemplate.MainProject.Models.Database.SqlSer
using BasicDotnetTemplate.MainProject.Models.Api.Response.Auth;
using BasicDotnetTemplate.MainProject.Core.Middlewares;
using AutoMapper;
using Microsoft.AspNetCore.Http;
namespace BasicDotnetTemplate.MainProject.Tests;
@@ -38,12 +39,12 @@ public class GetUserResponse_Tests
try
{
var getUserResponse = new GetUserResponse(200, null, null);
Assert.IsTrue(getUserResponse.Status == 200 && String.IsNullOrEmpty(getUserResponse.Message) && getUserResponse.Data == null);
Assert.IsTrue(getUserResponse.Status == StatusCodes.Status200OK && String.IsNullOrEmpty(getUserResponse.Message) && getUserResponse.Data == null);
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex.Message}");
Assert.Fail($"An exception was thrown: {ex}");
}
}
@@ -53,12 +54,12 @@ public class GetUserResponse_Tests
try
{
var getUserResponse = new GetUserResponse(201, null, null);
Assert.IsFalse(getUserResponse.Status == 200);
Assert.IsFalse(getUserResponse.Status == StatusCodes.Status200OK);
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex.Message}");
Assert.Fail($"An exception was thrown: {ex}");
}
}
@@ -68,12 +69,12 @@ public class GetUserResponse_Tests
try
{
var getUserResponse = new GetUserResponse(200, "This is a test message", null);
Assert.IsTrue(getUserResponse.Status == 200 && getUserResponse.Message == "This is a test message" && getUserResponse.Data == null);
Assert.IsTrue(getUserResponse.Status == StatusCodes.Status200OK && getUserResponse.Message == "This is a test message" && getUserResponse.Data == null);
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex.Message}");
Assert.Fail($"An exception was thrown: {ex}");
}
}
@@ -82,29 +83,15 @@ public class GetUserResponse_Tests
{
try
{
DatabaseSqlServer.User user = new DatabaseSqlServer.User()
{
Username = "test",
FirstName = "test",
LastName = "test",
Email = "test",
PasswordHash = "test",
PasswordSalt = "test",
Password = "test",
Role = new DatabaseSqlServer.Role()
{
Name = "test"
},
IsTestUser = true
};
DatabaseSqlServer.User user = ModelsInit.CreateUser();
UserDto? data = _mapper?.Map<UserDto>(user);
var getUserResponse = new GetUserResponse(200, "This is a test message", data);
Assert.IsTrue(getUserResponse.Status == 200 && getUserResponse.Message == "This is a test message" && getUserResponse.Data == data);
Assert.IsTrue(getUserResponse.Status == StatusCodes.Status200OK && getUserResponse.Message == "This is a test message" && getUserResponse.Data == data);
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex.Message}");
Assert.Fail($"An exception was thrown: {ex}");
}
}