Adding role creation during startup + minor fixes in tests
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user