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

@@ -20,6 +20,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;
@@ -52,34 +53,20 @@ public class AuthController_Tests
IConfiguration configuration = TestUtils.CreateConfiguration();
var authServiceMock = new Mock<IAuthService>();
var controller = new AuthController(configuration, authServiceMock.Object);
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);
var request = new AuthenticateRequest { Data = new AuthenticateRequestData { Username = "user", Password = "pass" } };
var request = new AuthenticateRequest { Data = new AuthenticateRequestData { Email = "user", Password = "pass" } };
authServiceMock.Setup(s => s.AuthenticateAsync(It.IsAny<AuthenticateRequestData>())).ReturnsAsync(authenticatedUser);
ObjectResult response = (ObjectResult)(await controller.AuthenticateAsync(request));
if (response != null && response.Value != null)
{
Assert.IsTrue(response.StatusCode == 200);
Assert.IsTrue(response.StatusCode == StatusCodes.Status200OK);
var result = (BaseResponse<object>)response.Value;
if (result != null)
{
Assert.IsTrue(result.Status == 200);
Assert.IsTrue(result.Status == StatusCodes.Status200OK);
Assert.IsInstanceOfType(result.Data, typeof(AuthenticatedUser));
}
else
@@ -111,12 +98,12 @@ public class AuthController_Tests
if (response != null && response.Value != null)
{
Assert.IsTrue(response.StatusCode == 400);
Assert.IsTrue(response.StatusCode == StatusCodes.Status400BadRequest);
var result = (BaseResponse<object>)response.Value;
if (result != null)
{
Assert.IsTrue(result.Status == 400);
Assert.IsTrue(result.Status == StatusCodes.Status400BadRequest);
Assert.IsTrue(result.Message == "Request is not well formed");
}
else
@@ -141,8 +128,8 @@ public class AuthController_Tests
{
Data = new AuthenticateRequestData()
{
Username = "d2ejdI1f4GYpq2kTB1nmeQkZXqR3QSxH8Yqkl7",
Password = "d2ejdI1f4GYpq2kTB1nmeQkZXqR3QSxH8Yqkl7"
Email = "d2ejdI1f4GYpq2kTB1nmeQkZXqR3QSxH8Yqkl7iv7zgfQ13qG/0dUUsreG/WGHWRBE5mVWaV43A=",
Password = "d2ejdI1f4GYpq2kTB1nmeQkZXqR3QSxH8Yqkl7iv7zgfQ13qG/0dUUsreG/WGHWRBE5mVWaV43A="
}
};
AuthenticatedUser? authenticatedUser = null;
@@ -153,7 +140,7 @@ public class AuthController_Tests
if (response != null)
{
Assert.IsTrue(response.StatusCode == 404);
Assert.IsTrue(response.StatusCode == StatusCodes.Status404NotFound);
}
else
{
@@ -181,12 +168,12 @@ public class AuthController_Tests
if (response != null && response.Value != null)
{
Assert.IsTrue(response.StatusCode == 400);
Assert.IsTrue(response.StatusCode == StatusCodes.Status400BadRequest);
var result = (BaseResponse<object>)response.Value;
if (result != null)
{
Assert.IsTrue(result.Status == 400);
Assert.IsTrue(result.Status == StatusCodes.Status400BadRequest);
Assert.IsTrue(result.Message == "Request is not well formed");
}
else
@@ -209,7 +196,7 @@ public class AuthController_Tests
var request = new AuthenticateRequest
{
Data = new AuthenticateRequestData { Username = "user", Password = "pass" }
Data = new AuthenticateRequestData { Email = "user", Password = "pass" }
};
authServiceMock.Setup(s => s.AuthenticateAsync(It.IsAny<AuthenticateRequestData>())).ThrowsAsync(new Exception("Unexpected error"));
@@ -220,12 +207,12 @@ public class AuthController_Tests
if (response != null && response.Value != null)
{
Assert.IsTrue(response.StatusCode == 500);
Assert.IsTrue(response.StatusCode == StatusCodes.Status500InternalServerError);
var result = (BaseResponse<object>)response.Value;
if (result != null)
{
Assert.IsTrue(result.Status == 500);
Assert.IsTrue(result.Status == StatusCodes.Status500InternalServerError);
Assert.IsTrue(result.Message == "Something went wrong. Unexpected error");
}
else

View File

@@ -10,6 +10,7 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using BasicDotnetTemplate.MainProject.Models.Api.Response;
using BasicDotnetTemplate.MainProject.Models.Settings;
using Microsoft.AspNetCore.Http;
namespace BasicDotnetTemplate.MainProject.Tests;
@@ -29,7 +30,7 @@ public class RootController_Test
if (result != null)
{
var data = (OkResult)result;
Assert.IsTrue(data.StatusCode == 200);
Assert.IsTrue(data.StatusCode == StatusCodes.Status200OK);
}
else
{
@@ -39,7 +40,7 @@ public class RootController_Test
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,6 +22,7 @@ using DatabaseSqlServer = BasicDotnetTemplate.MainProject.Models.Database.SqlSer
using BasicDotnetTemplate.MainProject.Models.Api.Response.Auth;
using AutoMapper;
using BasicDotnetTemplate.MainProject.Core.Middlewares;
using Microsoft.AspNetCore.Http;
namespace BasicDotnetTemplate.MainProject.Tests;
@@ -50,7 +51,8 @@ public class UserControllerTests
try
{
var userServiceMock = new Mock<IUserService>();
_ = new UserController(null, userServiceMock.Object);
var roleServiceMock = new Mock<IRoleService>();
_ = new UserController(null, userServiceMock.Object, roleServiceMock.Object);
exception = false;
Assert.Fail($"This test should not pass as configuration is null");
}
@@ -66,35 +68,21 @@ public class UserControllerTests
{
IConfiguration configuration = TestUtils.CreateConfiguration();
var userServiceMock = new Mock<IUserService>();
var controller = new UserController(configuration, userServiceMock.Object);
var roleServiceMock = new Mock<IRoleService>();
var controller = new UserController(configuration, userServiceMock.Object, roleServiceMock.Object);
var guid = Guid.NewGuid().ToString();
DatabaseSqlServer.User user = new DatabaseSqlServer.User()
{
Guid = guid,
Username = "Username",
FirstName = "FirstName",
LastName = "LastName",
Email = "Email",
PasswordHash = "PasswordHash",
PasswordSalt = "PasswordSalt",
Password = "Password",
Role = new DatabaseSqlServer.Role()
{
Name = "Role.Name"
},
IsTestUser = true
};
DatabaseSqlServer.User user = ModelsInit.CreateUser();
userServiceMock.Setup(s => s.GetUserByGuidAsync(It.IsAny<string>())).ReturnsAsync(user);
ObjectResult response = (ObjectResult)(await controller.GetUserByGuidAsync(guid));
if (response != null && response.Value != null)
{
Assert.IsTrue(response.StatusCode == 200);
Assert.IsTrue(response.StatusCode == StatusCodes.Status200OK);
var result = (BaseResponse<object>)response.Value;
if (result != null)
{
Assert.IsTrue(result.Status == 200);
Assert.IsTrue(result.Status == StatusCodes.Status200OK);
Assert.IsInstanceOfType(result.Data, typeof(DatabaseSqlServer.User));
}
else
@@ -113,7 +101,8 @@ public class UserControllerTests
{
IConfiguration configuration = TestUtils.CreateConfiguration();
var userServiceMock = new Mock<IUserService>();
var controller = new UserController(configuration, userServiceMock.Object);
var roleServiceMock = new Mock<IRoleService>();
var controller = new UserController(configuration, userServiceMock.Object, roleServiceMock.Object);
var guid = String.Empty;
DatabaseSqlServer.User? user = null;
@@ -123,12 +112,12 @@ public class UserControllerTests
if (response != null && response.Value != null)
{
Assert.IsTrue(response.StatusCode == 400);
Assert.IsTrue(response.StatusCode == StatusCodes.Status400BadRequest);
var result = (BaseResponse<object>)response.Value;
if (result != null)
{
Assert.IsTrue(result.Status == 400);
Assert.IsTrue(result.Status == StatusCodes.Status400BadRequest);
Assert.IsTrue(result.Message == "Request is not well formed");
}
else
@@ -147,7 +136,8 @@ public class UserControllerTests
{
IConfiguration configuration = TestUtils.CreateConfiguration();
var userServiceMock = new Mock<IUserService>();
var controller = new UserController(configuration, userServiceMock.Object);
var roleServiceMock = new Mock<IRoleService>();
var controller = new UserController(configuration, userServiceMock.Object, roleServiceMock.Object);
var guid = Guid.NewGuid().ToString();
DatabaseSqlServer.User? user = null;
@@ -158,7 +148,7 @@ public class UserControllerTests
if (response != null)
{
Assert.IsTrue(response.StatusCode == 404);
Assert.IsTrue(response.StatusCode == StatusCodes.Status404NotFound);
}
else
{
@@ -171,7 +161,8 @@ public class UserControllerTests
{
IConfiguration configuration = TestUtils.CreateConfiguration();
var userServiceMock = new Mock<IUserService>();
var controller = new UserController(configuration, userServiceMock.Object);
var roleServiceMock = new Mock<IRoleService>();
var controller = new UserController(configuration, userServiceMock.Object, roleServiceMock.Object);
var guid = Guid.NewGuid().ToString();
DatabaseSqlServer.User? user = null;
@@ -183,12 +174,12 @@ public class UserControllerTests
if (response != null && response.Value != null)
{
Assert.IsTrue(response.StatusCode == 400);
Assert.IsTrue(response.StatusCode == StatusCodes.Status400BadRequest);
var result = (BaseResponse<object>)response.Value;
if (result != null)
{
Assert.IsTrue(result.Status == 400);
Assert.IsTrue(result.Status == StatusCodes.Status400BadRequest);
Assert.IsTrue(result.Message == "Request is not well formed");
}
else
@@ -207,7 +198,8 @@ public class UserControllerTests
{
IConfiguration configuration = TestUtils.CreateConfiguration();
var userServiceMock = new Mock<IUserService>();
var controller = new UserController(configuration, userServiceMock.Object);
var roleServiceMock = new Mock<IRoleService>();
var controller = new UserController(configuration, userServiceMock.Object, roleServiceMock.Object);
var guid = Guid.NewGuid().ToString();
userServiceMock.Setup(s => s.GetUserByGuidAsync(It.IsAny<string>())).ThrowsAsync(new Exception("Unexpected error"));
@@ -217,12 +209,12 @@ public class UserControllerTests
if (response != null && response.Value != null)
{
Assert.IsTrue(response.StatusCode == 500);
Assert.IsTrue(response.StatusCode == StatusCodes.Status500InternalServerError);
var result = (BaseResponse<object>)response.Value;
if (result != null)
{
Assert.IsTrue(result.Status == 500);
Assert.IsTrue(result.Status == StatusCodes.Status500InternalServerError);
Assert.IsTrue(result.Message == "Something went wrong. Unexpected error");
}
else

View File

@@ -10,6 +10,7 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using BasicDotnetTemplate.MainProject.Models.Api.Response;
using BasicDotnetTemplate.MainProject.Models.Settings;
using Microsoft.AspNetCore.Http;
namespace BasicDotnetTemplate.MainProject.Tests;
@@ -53,7 +54,7 @@ public class VersionController_Tests
AppSettings appSettings = new AppSettings();
configuration.GetSection("AppSettings").Bind(appSettings);
string version = data.Data != null ? (string)data.Data : "";
Assert.IsTrue((((IStatusCodeActionResult)result).StatusCode == 200) && (version == appSettings.Settings?.Version));
Assert.IsTrue((((IStatusCodeActionResult)result).StatusCode == StatusCodes.Status200OK) && (version == appSettings.Settings?.Version));
}
else
{
@@ -68,7 +69,7 @@ public class VersionController_Tests
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex.Message}");
Assert.Fail($"An exception was thrown: {ex}");
}
}
@@ -87,7 +88,7 @@ public class VersionController_Tests
if (objectResult != null)
{
var data = (BaseResponse<object>)objectResult;
Assert.IsTrue((((IStatusCodeActionResult)result).StatusCode == 200) && String.IsNullOrEmpty(data.Data));
Assert.IsTrue((((IStatusCodeActionResult)result).StatusCode == StatusCodes.Status200OK) && String.IsNullOrEmpty(data.Data));
}
else
{
@@ -97,7 +98,7 @@ public class VersionController_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

@@ -30,22 +30,7 @@ public class JwtAuthorizationAttribute_Tests
{
_attribute = new JwtAuthorizationAttribute();
DatabaseSqlServer.User user = new DatabaseSqlServer.User()
{
Guid = Guid.NewGuid().ToString(),
Username = "Username",
FirstName = "FirstName",
LastName = "LastName",
Email = "Email",
PasswordHash = "PasswordHash",
PasswordSalt = "PasswordSalt",
Password = "Password",
Role = new DatabaseSqlServer.Role()
{
Name = "Role.Name"
},
IsTestUser = true
};
DatabaseSqlServer.User user = ModelsInit.CreateUser();
_authenticatedUser = new AuthenticatedUser(user);
WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty<string>());
@@ -89,7 +74,7 @@ public class JwtAuthorizationAttribute_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

@@ -37,26 +37,10 @@ public class AutoMapperConfiguration_Tests
{
try
{
DatabaseSqlServer.User user = new DatabaseSqlServer.User()
{
Guid = Guid.NewGuid().ToString(),
Username = "Username",
FirstName = "FirstName",
LastName = "LastName",
Email = "Email",
PasswordHash = "PasswordHash",
PasswordSalt = "PasswordSalt",
Password = "Password",
Role = new DatabaseSqlServer.Role()
{
Name = "Role.Name"
},
IsTestUser = true
};
DatabaseSqlServer.User user = ModelsInit.CreateUser();
UserDto? data = _mapper?.Map<UserDto>(user);
Assert.IsTrue(data?.Guid == user.Guid);
Assert.IsTrue(data?.Username == user.Username);
Assert.IsTrue(data?.FirstName == user.FirstName);
Assert.IsTrue(data?.LastName == user.LastName);
Assert.IsTrue(data?.Email == user.Email);
@@ -64,7 +48,7 @@ public class AutoMapperConfiguration_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

@@ -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}");
}
}

View File

@@ -7,6 +7,7 @@ using BasicDotnetTemplate.MainProject;
using BasicDotnetTemplate.MainProject.Models.Api.Response;
using Microsoft.Extensions.DependencyModel.Resolution;
using BasicDotnetTemplate.MainProject.Models.Settings;
using Microsoft.AspNetCore.Http;
namespace BasicDotnetTemplate.MainProject.Tests;
@@ -43,7 +44,7 @@ public class Settings_Tests
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 Settings_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}");
}
}
@@ -68,12 +69,12 @@ public class Settings_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}");
}
}
@@ -84,12 +85,12 @@ public class Settings_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

@@ -70,7 +70,7 @@ public class Program_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

@@ -29,19 +29,19 @@ public class AuthService_Tests
try
{
var authService = TestUtils.CreateAuthService();
if(authService != null)
if (authService != null)
{
Assert.IsInstanceOfType(authService, typeof(AuthService));
}
else
{
Assert.Fail($"AuthService is null");
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex.Message}");
Assert.Fail($"An exception was thrown: {ex}");
}
}
@@ -50,16 +50,16 @@ public class AuthService_Tests
{
try
{
var request = new AuthenticateRequest
{
Data = new AuthenticateRequestData
{
Username = "d2ejdI1f4GYpq2kTB1nmeQkZXqR3QSxH8Yqkl7iv7zgfQ13qG/0dUUsreG/WGHWRBE5mVWaV43A=",
Password = "d2ejdI1f4GYpq2kTB1nmeQkZXqR3QSxH8Yqkl7iv7zgfQ13qG/0dUUsreG/WGHWRBE5mVWaV43A="
}
var request = new AuthenticateRequest
{
Data = new AuthenticateRequestData
{
Email = "d2ejdI1f4GYpq2kTB1nmeQkZXqR3QSxH8Yqkl7iv7zgfQ13qG/0dUUsreG/WGHWRBE5mVWaV43A=",
Password = "d2ejdI1f4GYpq2kTB1nmeQkZXqR3QSxH8Yqkl7iv7zgfQ13qG/0dUUsreG/WGHWRBE5mVWaV43A="
}
};
var authService = TestUtils.CreateAuthService();
if(authService != null)
if (authService != null)
{
var authenticatedUser = await authService.AuthenticateAsync(request.Data);
Assert.IsTrue(authenticatedUser == null);
@@ -72,36 +72,25 @@ public class AuthService_Tests
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex.Message}");
Assert.Fail($"An exception was thrown: {ex}");
}
}
// if(authenticatedUser == null)
// {
// Console.WriteLine(JsonConvert.SerializeObject(authenticatedUser));
// Assert.IsTrue(authenticatedUser.Username == expectedAuthenticatedUser.Username);
// }
// else
// {
// Console.WriteLine(JsonConvert.SerializeObject(authenticatedUser));
// Assert.Fail($"authenticatedUser is null");
// }
[TestMethod]
public async Task AuthenticateAsync_UsernamePasswordInvalid()
{
try
{
var request = new AuthenticateRequest
{
Data = new AuthenticateRequestData
{
Username = "WGHWRBE5mVWaV=",
Password = "WGHWRBE5mVWaV="
}
var request = new AuthenticateRequest
{
Data = new AuthenticateRequestData
{
Email = "WGHWRBE5mVWaV=",
Password = "WGHWRBE5mVWaV="
}
};
var authService = TestUtils.CreateAuthService();
if(authService != null)
if (authService != null)
{
var authenticatedUser = await authService.AuthenticateAsync(request.Data);
Assert.IsTrue(authenticatedUser == null);
@@ -114,7 +103,7 @@ public class AuthService_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

@@ -39,7 +39,7 @@ public class JwtService_Tests
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex.Message}");
Assert.Fail($"An exception was thrown: {ex}");
}
}
@@ -64,7 +64,7 @@ public class JwtService_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

@@ -29,19 +29,19 @@ public class UserService_Tests
try
{
var userService = TestUtils.CreateUserService();
if(userService != null)
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.Message}");
Assert.Fail($"An exception was thrown: {ex}");
}
}
@@ -52,7 +52,7 @@ public class UserService_Tests
{
var userService = TestUtils.CreateUserService();
var testString = "test";
if(userService != null)
if (userService != null)
{
var user = await userService.GetUserByUsernameAndPassword(testString, testString);
Assert.IsTrue(user == null);
@@ -65,7 +65,7 @@ public class UserService_Tests
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex.Message}");
Assert.Fail($"An exception was thrown: {ex}");
}
}
@@ -76,13 +76,13 @@ public class UserService_Tests
try
{
var userService = TestUtils.CreateUserService();
var testUsername = "test@email.it";
var testEmail = "test@email.it";
var testPassword = "password";
if(userService != null)
if (userService != null)
{
var user = await userService.GetUserByUsernameAndPassword(testUsername, testPassword);
var user = await userService.GetUserByUsernameAndPassword(testEmail, testPassword);
Assert.IsTrue(user != null);
Assert.IsTrue(user.Username == testUsername);
Assert.IsTrue(user.Email == testEmail);
}
else
{
@@ -92,7 +92,7 @@ public class UserService_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

@@ -0,0 +1,35 @@
using DatabaseSqlServer = BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
namespace BasicDotnetTemplate.MainProject.Tests;
public static class ModelsInit
{
public static DatabaseSqlServer.User CreateUser()
{
DatabaseSqlServer.User user = new DatabaseSqlServer.User()
{
Guid = Guid.NewGuid().ToString(),
FirstName = "FirstName",
LastName = "LastName",
Email = "Email",
PasswordHash = "PasswordHash",
PasswordSalt = "PasswordSalt",
Password = "Password",
Role = CreateRole(),
IsTestUser = true
};
return user;
}
public static DatabaseSqlServer.Role CreateRole()
{
DatabaseSqlServer.Role role = new DatabaseSqlServer.Role()
{
Guid = Guid.NewGuid().ToString(),
Name = "Name",
IsNotEditable = false
};
return role;
}
}

View File

@@ -17,6 +17,7 @@ using Microsoft.EntityFrameworkCore;
using Moq;
using BasicDotnetTemplate.MainProject.Core.Database;
using BasicDotnetTemplate.MainProject.Services;
using Microsoft.AspNetCore.Http;
namespace BasicDotnetTemplate.MainProject.Tests;
@@ -43,33 +44,42 @@ public static class TestUtils
.Build();
}
public static string GetSqlConnectionString(IConfiguration configuration)
{
AppSettings _appSettings = new AppSettings();
configuration.GetSection("AppSettings").Bind(_appSettings);
return _appSettings.DatabaseSettings?.SqlServerConnectionString ?? String.Empty;
}
public static AuthService CreateAuthService()
{
IConfiguration configuration = CreateConfiguration();
var optionsBuilder = new DbContextOptionsBuilder<SqlServerContext>();
optionsBuilder.UseSqlServer("test");
optionsBuilder.UseSqlServer(GetSqlConnectionString(configuration));
SqlServerContext sqlServerContext = new SqlServerContext(optionsBuilder.Options);
var userServiceMock = new Mock<IUserService>();
return new AuthService(configuration, sqlServerContext, userServiceMock.Object);
var httpContextAccessor = new Mock<IHttpContextAccessor>();
return new AuthService(httpContextAccessor.Object, configuration, sqlServerContext, userServiceMock.Object);
}
public static UserService CreateUserService()
{
IConfiguration configuration = CreateConfiguration();
var optionsBuilder = new DbContextOptionsBuilder<SqlServerContext>();
optionsBuilder.UseSqlServer("test");
optionsBuilder.UseSqlServer(GetSqlConnectionString(configuration));
SqlServerContext sqlServerContext = new SqlServerContext(optionsBuilder.Options);
return new UserService(configuration, sqlServerContext);
var httpContextAccessor = new Mock<IHttpContextAccessor>();
return new UserService(httpContextAccessor.Object, configuration, sqlServerContext);
}
public static JwtService CreateJwtService()
{
IConfiguration configuration = CreateConfiguration();
var optionsBuilder = new DbContextOptionsBuilder<SqlServerContext>();
optionsBuilder.UseSqlServer("test");
optionsBuilder.UseSqlServer(GetSqlConnectionString(configuration));
SqlServerContext sqlServerContext = new SqlServerContext(optionsBuilder.Options);
var userServiceMock = new Mock<IUserService>();
return new JwtService(configuration, sqlServerContext, userServiceMock.Object);
var httpContextAccessor = new Mock<IHttpContextAccessor>();
return new JwtService(httpContextAccessor.Object, configuration, sqlServerContext);
}
}

View File

@@ -27,7 +27,7 @@ public class CryptoUtils_Tests
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex.Message}");
Assert.Fail($"An exception was thrown: {ex}");
}
}
@@ -47,7 +47,7 @@ public class CryptoUtils_Tests
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex.Message}");
Assert.Fail($"An exception was thrown: {ex}");
}
}
@@ -77,7 +77,7 @@ public class CryptoUtils_Tests
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex.Message}");
Assert.Fail($"An exception was thrown: {ex}");
}
}
@@ -97,7 +97,7 @@ public class CryptoUtils_Tests
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex.Message}");
Assert.Fail($"An exception was thrown: {ex}");
}
}
@@ -112,7 +112,7 @@ public class CryptoUtils_Tests
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex.Message}");
Assert.Fail($"An exception was thrown: {ex}");
}
}
@@ -129,12 +129,12 @@ public class CryptoUtils_Tests
AppSettings appSettings = ProgramUtils.AddConfiguration(ref builder, System.AppDomain.CurrentDomain.BaseDirectory + "/JsonData");
CryptUtils cryptoUtils = new CryptUtils(appSettings);
var encryptedPassword = cryptoUtils.GeneratePassword(password, salt, 0);
Assert.IsTrue(password != encryptedPassword);
Assert.IsTrue(password != encryptedPassword);
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex.Message}");
Assert.Fail($"An exception was thrown: {ex}");
}
}
@@ -158,7 +158,7 @@ public class CryptoUtils_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

@@ -27,7 +27,7 @@ public class JwtTokenUtils_Tests
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex.Message}");
Assert.Fail($"An exception was thrown: {ex}");
}
}
@@ -46,7 +46,7 @@ public class JwtTokenUtils_Tests
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex.Message}");
Assert.Fail($"An exception was thrown: {ex}");
}
}
@@ -55,7 +55,7 @@ public class JwtTokenUtils_Tests
{
try
{
WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty<string>());
WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty<string>());
AppSettings appSettings = ProgramUtils.AddConfiguration(ref builder, System.AppDomain.CurrentDomain.BaseDirectory + "/JsonData");
JwtTokenUtils jwtUtils = new JwtTokenUtils(appSettings);
var jwt = jwtUtils.GenerateToken(_guid);
@@ -65,7 +65,7 @@ public class JwtTokenUtils_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

@@ -44,7 +44,7 @@ public class ProgramUtils_Tests
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex.Message}");
Assert.Fail($"An exception was thrown: {ex}");
}
}
@@ -80,7 +80,7 @@ public class ProgramUtils_Tests
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex.Message}");
Assert.Fail($"An exception was thrown: {ex}");
}
}
@@ -97,7 +97,7 @@ public class ProgramUtils_Tests
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex.Message}");
Assert.Fail($"An exception was thrown: {ex}");
}
}
@@ -126,7 +126,7 @@ public class ProgramUtils_Tests
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex.Message}");
Assert.Fail($"An exception was thrown: {ex}");
}
}
@@ -160,7 +160,7 @@ public class ProgramUtils_Tests
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex.Message}");
Assert.Fail($"An exception was thrown: {ex}");
}
}
@@ -196,7 +196,7 @@ public class ProgramUtils_Tests
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex.Message}");
Assert.Fail($"An exception was thrown: {ex}");
}
}
@@ -220,7 +220,7 @@ public class ProgramUtils_Tests
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex.Message}");
Assert.Fail($"An exception was thrown: {ex}");
}
}
@@ -240,7 +240,7 @@ public class ProgramUtils_Tests
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex.Message}");
Assert.Fail($"An exception was thrown: {ex}");
}
}
@@ -260,7 +260,7 @@ public class ProgramUtils_Tests
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex.Message}");
Assert.Fail($"An exception was thrown: {ex}");
}
}
@@ -280,7 +280,7 @@ public class ProgramUtils_Tests
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex.Message}");
Assert.Fail($"An exception was thrown: {ex}");
}
}
@@ -305,7 +305,7 @@ public class ProgramUtils_Tests
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Assert.Fail($"An exception was thrown: {ex.Message}");
Assert.Fail($"An exception was thrown: {ex}");
}
}