committed by
GitHub
parent
ac20664446
commit
79549bea05
31
MainProject.Tests/Config/invalid-permissions.json
Normal file
31
MainProject.Tests/Config/invalid-permissions.json
Normal file
@@ -0,0 +1,31 @@
|
||||
{
|
||||
"PermissionInfos": [
|
||||
{
|
||||
"System": "base",
|
||||
"RolePermissionModuleOperations": [
|
||||
{
|
||||
"Module": "roles",
|
||||
"Operations": [
|
||||
{ "Operation": "create", "Roles": [] },
|
||||
{ "Operation": "read", "Roles": [] },
|
||||
{ "Operation": "update", "Roles": [] },
|
||||
{ "Operation": "delete", "Roles": [] },
|
||||
{ "Operation": "list", "Roles": [] },
|
||||
{ "Operation": "use", "Roles": [] }
|
||||
]
|
||||
},
|
||||
{
|
||||
"Module": "users",
|
||||
"Operations": [
|
||||
{ "Operation": "create", "Roles": [] },
|
||||
{ "Operation": "read", "Roles": [] },
|
||||
{ "Operation": "update", "Roles": [] },
|
||||
{ "Operation": "delete", "Roles": [] },
|
||||
{ "Operation": "list", "Roles": [] },
|
||||
{ "Operation": "use", "Roles": [] }
|
||||
]
|
||||
}
|
||||
]
|
||||
|
||||
]
|
||||
}
|
||||
31
MainProject.Tests/Config/permissions.json
Normal file
31
MainProject.Tests/Config/permissions.json
Normal file
@@ -0,0 +1,31 @@
|
||||
{
|
||||
"PermissionInfos": [
|
||||
{
|
||||
"System": "base",
|
||||
"RolePermissionModuleOperations": [
|
||||
{
|
||||
"Module": "roles",
|
||||
"Operations": [
|
||||
{ "Operation": "create", "Roles": ["Admin"] },
|
||||
{ "Operation": "read", "Roles": [] },
|
||||
{ "Operation": "update", "Roles": ["Admin"] },
|
||||
{ "Operation": "delete", "Roles": ["Admin"] },
|
||||
{ "Operation": "list", "Roles": [] },
|
||||
{ "Operation": "use", "Roles": [] }
|
||||
]
|
||||
},
|
||||
{
|
||||
"Module": "users",
|
||||
"Operations": [
|
||||
{ "Operation": "create", "Roles": [] },
|
||||
{ "Operation": "read", "Roles": [] },
|
||||
{ "Operation": "update", "Roles": [] },
|
||||
{ "Operation": "delete", "Roles": [] },
|
||||
{ "Operation": "list", "Roles": [] }
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
]
|
||||
}
|
||||
999
MainProject.Tests/Controllers/RoleController_Tests.cs
Normal file
999
MainProject.Tests/Controllers/RoleController_Tests.cs
Normal file
@@ -0,0 +1,999 @@
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Infrastructure;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Moq;
|
||||
using BasicDotnetTemplate.MainProject.Controllers;
|
||||
using BasicDotnetTemplate.MainProject.Services;
|
||||
using BasicDotnetTemplate.MainProject.Models.Api.Response;
|
||||
using BasicDotnetTemplate.MainProject.Models.Api.Common.Role;
|
||||
using DatabaseSqlServer = BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using BasicDotnetTemplate.MainProject.Models.Api.Request.Role;
|
||||
using BasicDotnetTemplate.MainProject.Models.Api.Data.Role;
|
||||
using BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
|
||||
namespace BasicDotnetTemplate.MainProject.Tests;
|
||||
|
||||
[TestClass]
|
||||
public class RoleController_Tests
|
||||
{
|
||||
private Mock<IRoleService>? _roleServiceMock;
|
||||
private RoleController? _roleController;
|
||||
|
||||
[TestInitialize]
|
||||
public void Setup()
|
||||
{
|
||||
IConfiguration configuration = TestUtils.CreateConfiguration();
|
||||
_roleServiceMock = new Mock<IRoleService>();
|
||||
_roleController = new RoleController(configuration, _roleServiceMock.Object);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void RoleController_NullConfiguration()
|
||||
{
|
||||
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development");
|
||||
var exception = true;
|
||||
try
|
||||
{
|
||||
var roleServiceMock = new Mock<IRoleService>();
|
||||
_ = new RoleController(null, roleServiceMock.Object);
|
||||
exception = false;
|
||||
Assert.Fail($"This test should not pass as configuration is null");
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
Assert.IsTrue(exception);
|
||||
}
|
||||
}
|
||||
|
||||
#region "GET"
|
||||
|
||||
[TestMethod]
|
||||
public async Task GetRoleByGuidAsync_Should_Return_200_When_Successful()
|
||||
{
|
||||
if (_roleController == null)
|
||||
{
|
||||
Assert.Fail($"_roleController is null");
|
||||
}
|
||||
var guid = Guid.NewGuid().ToString();
|
||||
DatabaseSqlServer.Role role = ModelsInit.CreateRole();
|
||||
|
||||
_roleServiceMock?.Setup(s => s.GetRoleByGuidAsync(It.IsAny<string>())).ReturnsAsync(role);
|
||||
ObjectResult response = (ObjectResult)(await _roleController.GetRoleByGuidAsync(guid));
|
||||
if (response != null && response.Value != null)
|
||||
{
|
||||
Assert.IsTrue(response.StatusCode == StatusCodes.Status200OK);
|
||||
|
||||
var result = (BaseResponse<object>)response.Value;
|
||||
if (result != null)
|
||||
{
|
||||
Assert.IsTrue(result.Status == StatusCodes.Status200OK);
|
||||
Assert.IsInstanceOfType(result.Data, typeof(RoleDto));
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"Result value is null");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"Response value is null");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task GetRoleByGuidAsync_GuidIsEmpty()
|
||||
{
|
||||
if (_roleController == null)
|
||||
{
|
||||
Assert.Fail($"_roleController is null");
|
||||
}
|
||||
|
||||
var guid = String.Empty;
|
||||
DatabaseSqlServer.Role? role = null;
|
||||
|
||||
_roleServiceMock?.Setup(s => s.GetRoleByGuidAsync(It.IsAny<string>())).ReturnsAsync(role);
|
||||
ObjectResult response = (ObjectResult)(await _roleController.GetRoleByGuidAsync(guid));
|
||||
|
||||
if (response != null && response.Value != null)
|
||||
{
|
||||
Assert.IsTrue(response.StatusCode == StatusCodes.Status400BadRequest);
|
||||
|
||||
var result = (BaseResponse<object>)response.Value;
|
||||
if (result != null)
|
||||
{
|
||||
Assert.IsTrue(result.Status == StatusCodes.Status400BadRequest);
|
||||
Assert.IsTrue(result.Message == "Request is not well formed");
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"Result value is null");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"Response value is null");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task GetRoleByGuidAsync_NotFound()
|
||||
{
|
||||
if (_roleController == null)
|
||||
{
|
||||
Assert.Fail($"_roleController is null");
|
||||
}
|
||||
|
||||
var guid = Guid.NewGuid().ToString();
|
||||
DatabaseSqlServer.Role? role = null;
|
||||
_roleServiceMock?.Setup(s => s.GetRoleByGuidAsync(It.IsAny<string>())).ReturnsAsync(role);
|
||||
NotFoundResult response = (NotFoundResult)(await _roleController.GetRoleByGuidAsync(guid));
|
||||
|
||||
Assert.IsInstanceOfType(response, typeof(NotFoundResult));
|
||||
|
||||
if (response != null)
|
||||
{
|
||||
Assert.IsTrue(response.StatusCode == StatusCodes.Status404NotFound);
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"Response is null");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task GetRoleByGuidAsync_ModelInvalid()
|
||||
{
|
||||
if (_roleController == null)
|
||||
{
|
||||
Assert.Fail($"_roleController is null");
|
||||
}
|
||||
|
||||
var guid = Guid.NewGuid().ToString();
|
||||
DatabaseSqlServer.Role? role = null;
|
||||
_roleServiceMock?.Setup(s => s.GetRoleByGuidAsync(It.IsAny<string>())).ReturnsAsync(role);
|
||||
_roleController.ModelState.AddModelError("Data", "Invalid data");
|
||||
ObjectResult response = (ObjectResult)(await _roleController.GetRoleByGuidAsync(guid));
|
||||
|
||||
Assert.IsInstanceOfType(response, typeof(ObjectResult));
|
||||
|
||||
if (response != null && response.Value != null)
|
||||
{
|
||||
Assert.IsTrue(response.StatusCode == StatusCodes.Status400BadRequest);
|
||||
|
||||
var result = (BaseResponse<object>)response.Value;
|
||||
if (result != null)
|
||||
{
|
||||
Assert.IsTrue(result.Status == StatusCodes.Status400BadRequest);
|
||||
Assert.IsTrue(result.Message == "Request is not well formed");
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"Result value is null");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"Response is null");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task GetRoleByGuidAsync_Exception()
|
||||
{
|
||||
if (_roleController == null)
|
||||
{
|
||||
Assert.Fail($"_roleController is null");
|
||||
}
|
||||
|
||||
var guid = Guid.NewGuid().ToString();
|
||||
_roleServiceMock?.Setup(s => s.GetRoleByGuidAsync(It.IsAny<string>())).ThrowsAsync(new Exception("Unexpected error"));
|
||||
ObjectResult response = (ObjectResult)(await _roleController.GetRoleByGuidAsync(guid));
|
||||
|
||||
Assert.IsInstanceOfType(response, typeof(ObjectResult));
|
||||
|
||||
if (response != null && response.Value != null)
|
||||
{
|
||||
Assert.IsTrue(response.StatusCode == StatusCodes.Status500InternalServerError);
|
||||
|
||||
var result = (BaseResponse<object>)response.Value;
|
||||
if (result != null)
|
||||
{
|
||||
Assert.IsTrue(result.Status == StatusCodes.Status500InternalServerError);
|
||||
Assert.IsTrue(result.Message == "Something went wrong. Unexpected error");
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"Result value is null");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"Response is null");
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region "CREATE"
|
||||
|
||||
[TestMethod]
|
||||
public async Task CreateRoleAsync_Should_Return_200_When_Successful()
|
||||
{
|
||||
if (_roleController == null)
|
||||
{
|
||||
Assert.Fail($"_roleController is null");
|
||||
}
|
||||
|
||||
if (_roleServiceMock == null)
|
||||
{
|
||||
Assert.Fail($"_roleServiceMock is null");
|
||||
}
|
||||
|
||||
DatabaseSqlServer.Role role = ModelsInit.CreateRole();
|
||||
|
||||
CreateRoleRequest request = new CreateRoleRequest()
|
||||
{
|
||||
Data = new CreateRoleRequestData()
|
||||
{
|
||||
Name = "RoleTest",
|
||||
IsNotEditable = true
|
||||
}
|
||||
};
|
||||
|
||||
_roleServiceMock?.Setup(s => s.CheckIfNameIsValid(It.IsAny<string>(), It.IsAny<string>())).ReturnsAsync(true);
|
||||
_roleServiceMock?.Setup(s => s.CreateRoleAsync(request.Data)).ReturnsAsync(role);
|
||||
|
||||
ObjectResult response = (ObjectResult)(await _roleController.CreateRoleAsync(request));
|
||||
if (response != null && response.Value != null)
|
||||
{
|
||||
Assert.IsTrue(response.StatusCode == StatusCodes.Status200OK);
|
||||
|
||||
var result = (BaseResponse<object>)response.Value;
|
||||
if (result != null)
|
||||
{
|
||||
Assert.IsTrue(result.Status == StatusCodes.Status200OK);
|
||||
Assert.IsInstanceOfType(result.Data, typeof(RoleDto));
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"Result value is null");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"Response value is null");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task CreateRoleAsync_InvalidName()
|
||||
{
|
||||
if (_roleController == null)
|
||||
{
|
||||
Assert.Fail($"_roleController is null");
|
||||
}
|
||||
|
||||
CreateRoleRequest request = new CreateRoleRequest()
|
||||
{
|
||||
Data = new CreateRoleRequestData()
|
||||
{
|
||||
Name = "RoleTest",
|
||||
IsNotEditable = true
|
||||
}
|
||||
};
|
||||
|
||||
_roleServiceMock?.Setup(s => s.CheckIfNameIsValid(It.IsAny<string>(), It.IsAny<string>())).ReturnsAsync(false);
|
||||
|
||||
ObjectResult response = (ObjectResult)(await _roleController.CreateRoleAsync(request));
|
||||
|
||||
if (response != null && response.Value != null)
|
||||
{
|
||||
Assert.IsTrue(response.StatusCode == StatusCodes.Status400BadRequest);
|
||||
|
||||
var result = (BaseResponse<object>)response.Value;
|
||||
if (result != null)
|
||||
{
|
||||
Assert.IsTrue(result.Status == StatusCodes.Status400BadRequest);
|
||||
Assert.IsTrue(result.Message == "Invalid name");
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"Result value is null");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"Response value is null");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task CreateRoleAsync_CreateRoleRequestDataNull()
|
||||
{
|
||||
if (_roleController == null)
|
||||
{
|
||||
Assert.Fail($"_roleController is null");
|
||||
}
|
||||
|
||||
DatabaseSqlServer.Role role = ModelsInit.CreateRole();
|
||||
|
||||
CreateRoleRequest request = new CreateRoleRequest()
|
||||
{
|
||||
Data = null
|
||||
};
|
||||
|
||||
_roleServiceMock?.Setup(s => s.CheckIfNameIsValid(It.IsAny<string>(), It.IsAny<string>())).ReturnsAsync(true);
|
||||
|
||||
_roleServiceMock?.Setup(s => s.CreateRoleAsync(
|
||||
It.IsAny<CreateRoleRequestData>()
|
||||
)).ReturnsAsync(role);
|
||||
|
||||
ObjectResult response = (ObjectResult)(await _roleController.CreateRoleAsync(request));
|
||||
|
||||
if (response != null && response.Value != null)
|
||||
{
|
||||
Assert.IsTrue(response.StatusCode == StatusCodes.Status400BadRequest);
|
||||
|
||||
var result = (BaseResponse<object>)response.Value;
|
||||
if (result != null)
|
||||
{
|
||||
Assert.IsTrue(result.Status == StatusCodes.Status400BadRequest);
|
||||
Assert.IsTrue(result.Message == "Request is not well formed");
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"Result value is null");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"Response value is null");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task CreateRoleAsync_NotCreated()
|
||||
{
|
||||
if (_roleController == null)
|
||||
{
|
||||
Assert.Fail($"_roleController is null");
|
||||
}
|
||||
|
||||
if (_roleServiceMock == null)
|
||||
{
|
||||
Assert.Fail($"_roleServiceMock is null");
|
||||
}
|
||||
|
||||
DatabaseSqlServer.Role role = ModelsInit.CreateRole();
|
||||
DatabaseSqlServer.Role? expectedRole = null;
|
||||
|
||||
CreateRoleRequest request = new CreateRoleRequest()
|
||||
{
|
||||
Data = new CreateRoleRequestData()
|
||||
{
|
||||
Name = "RoleTest",
|
||||
IsNotEditable = true
|
||||
}
|
||||
};
|
||||
|
||||
_roleServiceMock?.Setup(s => s.CheckIfNameIsValid(It.IsAny<string>(), It.IsAny<string>())).ReturnsAsync(true);
|
||||
_roleServiceMock?.Setup(s => s.CreateRoleAsync(request.Data)).ReturnsAsync(expectedRole);
|
||||
|
||||
ObjectResult response = (ObjectResult)(await _roleController.CreateRoleAsync(request));
|
||||
if (response != null && response.Value != null)
|
||||
{
|
||||
Assert.IsTrue(response.StatusCode == StatusCodes.Status400BadRequest);
|
||||
|
||||
var result = (BaseResponse<object>)response.Value;
|
||||
if (result != null)
|
||||
{
|
||||
Assert.IsTrue(result.Status == StatusCodes.Status400BadRequest);
|
||||
Assert.IsTrue(result.Message == "Not created");
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"Result value is null");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"Response value is null");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task CreateRoleAsync_ModelInvalid()
|
||||
{
|
||||
if (_roleController == null)
|
||||
{
|
||||
Assert.Fail($"_roleController is null");
|
||||
}
|
||||
|
||||
DatabaseSqlServer.Role role = ModelsInit.CreateRole();
|
||||
|
||||
CreateRoleRequest request = new CreateRoleRequest()
|
||||
{
|
||||
Data = new CreateRoleRequestData()
|
||||
{
|
||||
Name = "RoleTest",
|
||||
IsNotEditable = true
|
||||
}
|
||||
};
|
||||
|
||||
_roleServiceMock?.Setup(s => s.CheckIfNameIsValid(It.IsAny<string>(), It.IsAny<string>())).ReturnsAsync(true);
|
||||
|
||||
_roleServiceMock?.Setup(s => s.CreateRoleAsync(
|
||||
It.IsAny<CreateRoleRequestData>()
|
||||
)).ReturnsAsync(role);
|
||||
_roleController.ModelState.AddModelError("Data", "Invalid data");
|
||||
ObjectResult response = (ObjectResult)(await _roleController.CreateRoleAsync(request));
|
||||
|
||||
Assert.IsInstanceOfType(response, typeof(ObjectResult));
|
||||
|
||||
if (response != null && response.Value != null)
|
||||
{
|
||||
Assert.IsTrue(response.StatusCode == StatusCodes.Status400BadRequest);
|
||||
|
||||
var result = (BaseResponse<object>)response.Value;
|
||||
if (result != null)
|
||||
{
|
||||
Assert.IsTrue(result.Status == StatusCodes.Status400BadRequest);
|
||||
Assert.IsTrue(result.Message == "Request is not well formed");
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"Result value is null");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"Response is null");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task CreateRoleAsync_Exception()
|
||||
{
|
||||
if (_roleController == null)
|
||||
{
|
||||
Assert.Fail($"_roleController is null");
|
||||
}
|
||||
|
||||
if (_roleServiceMock == null)
|
||||
{
|
||||
Assert.Fail($"_roleServiceMock is null");
|
||||
}
|
||||
|
||||
DatabaseSqlServer.Role role = ModelsInit.CreateRole();
|
||||
|
||||
CreateRoleRequest request = new CreateRoleRequest()
|
||||
{
|
||||
Data = new CreateRoleRequestData()
|
||||
{
|
||||
Name = "RoleTest",
|
||||
IsNotEditable = true
|
||||
}
|
||||
};
|
||||
|
||||
_roleServiceMock?.Setup(s => s.CheckIfNameIsValid(It.IsAny<string>(), It.IsAny<string>())).ReturnsAsync(true);
|
||||
_roleServiceMock?.Setup(s => s.CreateRoleAsync(
|
||||
It.IsAny<CreateRoleRequestData>()
|
||||
)).ThrowsAsync(new Exception("Unexpected error"));
|
||||
|
||||
ObjectResult response = (ObjectResult)(await _roleController.CreateRoleAsync(request));
|
||||
Assert.IsInstanceOfType(response, typeof(ObjectResult));
|
||||
|
||||
if (response != null && response.Value != null)
|
||||
{
|
||||
Assert.IsTrue(response.StatusCode == StatusCodes.Status500InternalServerError);
|
||||
|
||||
var result = (BaseResponse<object>)response.Value;
|
||||
if (result != null)
|
||||
{
|
||||
Assert.IsTrue(result.Status == StatusCodes.Status500InternalServerError);
|
||||
Assert.IsTrue(result.Message == "Something went wrong. Unexpected error");
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"Result value is null");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"Response is null");
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region "DELETE"
|
||||
|
||||
[TestMethod]
|
||||
public async Task DeleteRoleByGuidAsync_Success()
|
||||
{
|
||||
if (_roleController == null)
|
||||
{
|
||||
Assert.Fail($"_roleController is null");
|
||||
}
|
||||
var guid = Guid.NewGuid().ToString();
|
||||
DatabaseSqlServer.Role role = ModelsInit.CreateRole();
|
||||
|
||||
_roleServiceMock?.Setup(s => s.GetRoleByGuidAsync(It.IsAny<string>())).ReturnsAsync(role);
|
||||
ObjectResult response = (ObjectResult)(await _roleController.DeleteRoleByGuidAsync(guid));
|
||||
if (response != null && response.Value != null)
|
||||
{
|
||||
Assert.IsTrue(response.StatusCode == StatusCodes.Status200OK);
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"Response value is null");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task DeleteRoleByGuidAsync_GuidIsEmpty()
|
||||
{
|
||||
if (_roleController == null)
|
||||
{
|
||||
Assert.Fail($"_roleController is null");
|
||||
}
|
||||
|
||||
var guid = String.Empty;
|
||||
DatabaseSqlServer.Role? role = null;
|
||||
|
||||
_roleServiceMock?.Setup(s => s.GetRoleByGuidAsync(It.IsAny<string>())).ReturnsAsync(role);
|
||||
ObjectResult response = (ObjectResult)(await _roleController.DeleteRoleByGuidAsync(guid));
|
||||
|
||||
if (response != null && response.Value != null)
|
||||
{
|
||||
Assert.IsTrue(response.StatusCode == StatusCodes.Status400BadRequest);
|
||||
|
||||
var result = (BaseResponse<object>)response.Value;
|
||||
if (result != null)
|
||||
{
|
||||
Assert.IsTrue(result.Status == StatusCodes.Status400BadRequest);
|
||||
Assert.IsTrue(result.Message == "Request is not well formed");
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"Result value is null");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"Response value is null");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task DeleteRoleByGuidAsync_NotFound()
|
||||
{
|
||||
if (_roleController == null)
|
||||
{
|
||||
Assert.Fail($"_roleController is null");
|
||||
}
|
||||
|
||||
var guid = Guid.NewGuid().ToString();
|
||||
DatabaseSqlServer.Role? role = null;
|
||||
_roleServiceMock?.Setup(s => s.GetRoleByGuidAsync(It.IsAny<string>())).ReturnsAsync(role);
|
||||
NotFoundResult response = (NotFoundResult)(await _roleController.DeleteRoleByGuidAsync(guid));
|
||||
|
||||
Assert.IsInstanceOfType(response, typeof(NotFoundResult));
|
||||
|
||||
if (response != null)
|
||||
{
|
||||
Assert.IsTrue(response.StatusCode == StatusCodes.Status404NotFound);
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"Response is null");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task DeleteRoleByGuidAsync_ModelInvalid()
|
||||
{
|
||||
if (_roleController == null)
|
||||
{
|
||||
Assert.Fail($"_roleController is null");
|
||||
}
|
||||
|
||||
var guid = Guid.NewGuid().ToString();
|
||||
DatabaseSqlServer.Role? role = null;
|
||||
_roleServiceMock?.Setup(s => s.GetRoleByGuidAsync(It.IsAny<string>())).ReturnsAsync(role);
|
||||
_roleController.ModelState.AddModelError("Data", "Invalid data");
|
||||
ObjectResult response = (ObjectResult)(await _roleController.DeleteRoleByGuidAsync(guid));
|
||||
|
||||
Assert.IsInstanceOfType(response, typeof(ObjectResult));
|
||||
|
||||
if (response != null && response.Value != null)
|
||||
{
|
||||
Assert.IsTrue(response.StatusCode == StatusCodes.Status400BadRequest);
|
||||
|
||||
var result = (BaseResponse<object>)response.Value;
|
||||
if (result != null)
|
||||
{
|
||||
Assert.IsTrue(result.Status == StatusCodes.Status400BadRequest);
|
||||
Assert.IsTrue(result.Message == "Request is not well formed");
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"Result value is null");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"Response is null");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task DeleteRoleByGuidAsync_Exception()
|
||||
{
|
||||
if (_roleController == null)
|
||||
{
|
||||
Assert.Fail($"_roleController is null");
|
||||
}
|
||||
|
||||
var guid = Guid.NewGuid().ToString();
|
||||
_roleServiceMock?.Setup(s => s.GetRoleByGuidAsync(It.IsAny<string>())).ThrowsAsync(new Exception("Unexpected error"));
|
||||
ObjectResult response = (ObjectResult)(await _roleController.DeleteRoleByGuidAsync(guid));
|
||||
|
||||
Assert.IsInstanceOfType(response, typeof(ObjectResult));
|
||||
|
||||
if (response != null && response.Value != null)
|
||||
{
|
||||
Assert.IsTrue(response.StatusCode == StatusCodes.Status500InternalServerError);
|
||||
|
||||
var result = (BaseResponse<object>)response.Value;
|
||||
if (result != null)
|
||||
{
|
||||
Assert.IsTrue(result.Status == StatusCodes.Status500InternalServerError);
|
||||
Assert.IsTrue(result.Message == "Something went wrong. Unexpected error");
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"Result value is null");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"Response is null");
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region "UPDATE"
|
||||
|
||||
[TestMethod]
|
||||
public async Task UpdateRoleAsync_Should_Return_200_When_Successful()
|
||||
{
|
||||
if (_roleController == null)
|
||||
{
|
||||
Assert.Fail($"_roleController is null");
|
||||
}
|
||||
|
||||
if (_roleServiceMock == null)
|
||||
{
|
||||
Assert.Fail($"_roleServiceMock is null");
|
||||
}
|
||||
|
||||
DatabaseSqlServer.Role role = ModelsInit.CreateRole();
|
||||
|
||||
CreateRoleRequest request = new CreateRoleRequest()
|
||||
{
|
||||
Data = new CreateRoleRequestData()
|
||||
{
|
||||
Name = "RoleTest",
|
||||
IsNotEditable = true
|
||||
}
|
||||
};
|
||||
|
||||
_roleServiceMock?.Setup(s => s.GetRoleByGuidAsync(It.IsAny<string>())).ReturnsAsync(role);
|
||||
_roleServiceMock?.Setup(s => s.CheckIfNameIsValid(It.IsAny<string>(), It.IsAny<string>())).ReturnsAsync(true);
|
||||
_roleServiceMock?.Setup(s => s.UpdateRoleAsync(It.IsAny<CreateRoleRequestData>(), It.IsAny<Role>())).ReturnsAsync(role);
|
||||
|
||||
ObjectResult response = (ObjectResult)(await _roleController.UpdateRoleAsync(request, role.Guid));
|
||||
if (response != null && response.Value != null)
|
||||
{
|
||||
Assert.IsTrue(response.StatusCode == StatusCodes.Status200OK);
|
||||
|
||||
var result = (BaseResponse<object>)response.Value;
|
||||
if (result != null)
|
||||
{
|
||||
Assert.IsTrue(result.Status == StatusCodes.Status200OK);
|
||||
Assert.IsInstanceOfType(result.Data, typeof(RoleDto));
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"Result value is null");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"Response value is null");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task UpdateRoleAsync_RoleNotFound()
|
||||
{
|
||||
if (_roleController == null)
|
||||
{
|
||||
Assert.Fail($"_roleController is null");
|
||||
}
|
||||
|
||||
DatabaseSqlServer.Role? role = null;
|
||||
|
||||
CreateRoleRequest request = new CreateRoleRequest()
|
||||
{
|
||||
Data = new CreateRoleRequestData()
|
||||
{
|
||||
Name = "RoleTest",
|
||||
IsNotEditable = true
|
||||
}
|
||||
};
|
||||
|
||||
_roleServiceMock?.Setup(s => s.GetRoleByGuidAsync(It.IsAny<string>())).ReturnsAsync(role);
|
||||
|
||||
NotFoundResult response = (NotFoundResult)(await _roleController.UpdateRoleAsync(request, Guid.NewGuid().ToString()));
|
||||
|
||||
if (response != null)
|
||||
{
|
||||
Assert.IsTrue(response.StatusCode == StatusCodes.Status404NotFound);
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"Response is null");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task UpdateRoleAsync_InvalidName()
|
||||
{
|
||||
if (_roleController == null)
|
||||
{
|
||||
Assert.Fail($"_roleController is null");
|
||||
}
|
||||
|
||||
DatabaseSqlServer.Role role = ModelsInit.CreateRole();
|
||||
|
||||
CreateRoleRequest request = new CreateRoleRequest()
|
||||
{
|
||||
Data = new CreateRoleRequestData()
|
||||
{
|
||||
Name = "RoleTest",
|
||||
IsNotEditable = true
|
||||
}
|
||||
};
|
||||
|
||||
_roleServiceMock?.Setup(s => s.GetRoleByGuidAsync(It.IsAny<string>())).ReturnsAsync(role);
|
||||
_roleServiceMock?.Setup(s => s.CheckIfNameIsValid(It.IsAny<string>(), It.IsAny<string>())).ReturnsAsync(false);
|
||||
|
||||
ObjectResult response = (ObjectResult)(await _roleController.UpdateRoleAsync(request, role.Guid));
|
||||
|
||||
if (response != null && response.Value != null)
|
||||
{
|
||||
Assert.IsTrue(response.StatusCode == StatusCodes.Status400BadRequest);
|
||||
|
||||
var result = (BaseResponse<object>)response.Value;
|
||||
if (result != null)
|
||||
{
|
||||
Assert.IsTrue(result.Status == StatusCodes.Status400BadRequest);
|
||||
Assert.IsTrue(result.Message == "Invalid name");
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"Result value is null");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"Response value is null");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task UpdateRoleAsync_NotEditable()
|
||||
{
|
||||
if (_roleController == null)
|
||||
{
|
||||
Assert.Fail($"_roleController is null");
|
||||
}
|
||||
|
||||
DatabaseSqlServer.Role role = ModelsInit.CreateRole();
|
||||
role.IsNotEditable = true;
|
||||
|
||||
CreateRoleRequest request = new CreateRoleRequest()
|
||||
{
|
||||
Data = new CreateRoleRequestData()
|
||||
{
|
||||
Name = "RoleTest",
|
||||
IsNotEditable = true
|
||||
}
|
||||
};
|
||||
|
||||
_roleServiceMock?.Setup(s => s.GetRoleByGuidAsync(It.IsAny<string>())).ReturnsAsync(role);
|
||||
_roleServiceMock?.Setup(s => s.CheckIfNameIsValid(It.IsAny<string>(), It.IsAny<string>())).ReturnsAsync(false);
|
||||
|
||||
ObjectResult response = (ObjectResult)(await _roleController.UpdateRoleAsync(request, role.Guid));
|
||||
|
||||
if (response != null && response.Value != null)
|
||||
{
|
||||
Assert.IsTrue(response.StatusCode == StatusCodes.Status400BadRequest);
|
||||
|
||||
var result = (BaseResponse<object>)response.Value;
|
||||
if (result != null)
|
||||
{
|
||||
Assert.IsTrue(result.Status == StatusCodes.Status400BadRequest);
|
||||
Assert.IsTrue(result.Message == "This role is not editable");
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"Result value is null");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"Response value is null");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task UpdateRoleAsync_CreateRoleRequestDataNull()
|
||||
{
|
||||
if (_roleController == null)
|
||||
{
|
||||
Assert.Fail($"_roleController is null");
|
||||
}
|
||||
|
||||
DatabaseSqlServer.Role role = ModelsInit.CreateRole();
|
||||
|
||||
CreateRoleRequest request = new CreateRoleRequest()
|
||||
{
|
||||
Data = null
|
||||
};
|
||||
|
||||
_roleServiceMock?.Setup(s => s.GetRoleByGuidAsync(It.IsAny<string>())).ReturnsAsync(role);
|
||||
_roleServiceMock?.Setup(s => s.CheckIfNameIsValid(It.IsAny<string>(), It.IsAny<string>())).ReturnsAsync(true);
|
||||
_roleServiceMock?.Setup(s => s.UpdateRoleAsync(It.IsAny<CreateRoleRequestData>(), It.IsAny<Role>())).ReturnsAsync(role);
|
||||
|
||||
ObjectResult response = (ObjectResult)(await _roleController.UpdateRoleAsync(request, role.Guid));
|
||||
|
||||
if (response != null && response.Value != null)
|
||||
{
|
||||
Assert.IsTrue(response.StatusCode == StatusCodes.Status400BadRequest);
|
||||
|
||||
var result = (BaseResponse<object>)response.Value;
|
||||
if (result != null)
|
||||
{
|
||||
Assert.IsTrue(result.Status == StatusCodes.Status400BadRequest);
|
||||
Assert.IsTrue(result.Message == "Request is not well formed");
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"Result value is null");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"Response value is null");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task UpdateRoleAsync_ModelInvalid()
|
||||
{
|
||||
if (_roleController == null)
|
||||
{
|
||||
Assert.Fail($"_roleController is null");
|
||||
}
|
||||
|
||||
DatabaseSqlServer.Role role = ModelsInit.CreateRole();
|
||||
|
||||
CreateRoleRequest request = new CreateRoleRequest()
|
||||
{
|
||||
Data = new CreateRoleRequestData()
|
||||
{
|
||||
Name = "RoleTest",
|
||||
IsNotEditable = true
|
||||
}
|
||||
};
|
||||
|
||||
_roleServiceMock?.Setup(s => s.GetRoleByGuidAsync(It.IsAny<string>())).ReturnsAsync(role);
|
||||
_roleServiceMock?.Setup(s => s.CheckIfNameIsValid(It.IsAny<string>(), It.IsAny<string>())).ReturnsAsync(true);
|
||||
_roleServiceMock?.Setup(s => s.UpdateRoleAsync(It.IsAny<CreateRoleRequestData>(), It.IsAny<Role>())).ReturnsAsync(role);
|
||||
_roleController.ModelState.AddModelError("Data", "Invalid data");
|
||||
ObjectResult response = (ObjectResult)(await _roleController.UpdateRoleAsync(request, role.Guid));
|
||||
|
||||
Assert.IsInstanceOfType(response, typeof(ObjectResult));
|
||||
|
||||
if (response != null && response.Value != null)
|
||||
{
|
||||
Assert.IsTrue(response.StatusCode == StatusCodes.Status400BadRequest);
|
||||
|
||||
var result = (BaseResponse<object>)response.Value;
|
||||
if (result != null)
|
||||
{
|
||||
Assert.IsTrue(result.Status == StatusCodes.Status400BadRequest);
|
||||
Assert.IsTrue(result.Message == "Request is not well formed");
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"Result value is null");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"Response is null");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task UpdateRoleAsync_Exception()
|
||||
{
|
||||
if (_roleController == null)
|
||||
{
|
||||
Assert.Fail($"_roleController is null");
|
||||
}
|
||||
|
||||
if (_roleServiceMock == null)
|
||||
{
|
||||
Assert.Fail($"_roleServiceMock is null");
|
||||
}
|
||||
|
||||
DatabaseSqlServer.Role role = ModelsInit.CreateRole();
|
||||
|
||||
CreateRoleRequest request = new CreateRoleRequest()
|
||||
{
|
||||
Data = new CreateRoleRequestData()
|
||||
{
|
||||
Name = "RoleTest",
|
||||
IsNotEditable = true
|
||||
}
|
||||
};
|
||||
|
||||
_roleServiceMock?.Setup(s => s.GetRoleByGuidAsync(It.IsAny<string>())).ReturnsAsync(role);
|
||||
_roleServiceMock?.Setup(s => s.CheckIfNameIsValid(It.IsAny<string>(), It.IsAny<string>())).ReturnsAsync(true);
|
||||
_roleServiceMock?.Setup(s => s.UpdateRoleAsync(
|
||||
It.IsAny<CreateRoleRequestData>(), It.IsAny<Role>()
|
||||
)).ThrowsAsync(new Exception("Unexpected error"));
|
||||
|
||||
ObjectResult response = (ObjectResult)(await _roleController.UpdateRoleAsync(request, role.Guid));
|
||||
Assert.IsInstanceOfType(response, typeof(ObjectResult));
|
||||
|
||||
if (response != null && response.Value != null)
|
||||
{
|
||||
Assert.IsTrue(response.StatusCode == StatusCodes.Status500InternalServerError);
|
||||
|
||||
var result = (BaseResponse<object>)response.Value;
|
||||
if (result != null)
|
||||
{
|
||||
Assert.IsTrue(result.Status == StatusCodes.Status500InternalServerError);
|
||||
Assert.IsTrue(result.Message == "Something went wrong. Unexpected error");
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"Result value is null");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"Response is null");
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -39,7 +39,6 @@ public class RootController_Test
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.InnerException);
|
||||
Assert.Fail($"An exception was thrown: {ex}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ using BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||
namespace BasicDotnetTemplate.MainProject.Tests;
|
||||
|
||||
[TestClass]
|
||||
public class UserControllerTests
|
||||
public class UserController_Tests
|
||||
{
|
||||
private Mock<IUserService>? _userServiceMock;
|
||||
private Mock<IRoleService>? _roleServiceMock;
|
||||
@@ -43,7 +43,7 @@ public class UserControllerTests
|
||||
IConfiguration configuration = TestUtils.CreateConfiguration();
|
||||
_userServiceMock = new Mock<IUserService>();
|
||||
_roleServiceMock = new Mock<IRoleService>();
|
||||
_userController = new UserController(configuration, _userServiceMock?.Object, _roleServiceMock.Object);
|
||||
_userController = new UserController(configuration, _userServiceMock.Object, _roleServiceMock.Object);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
@@ -100,7 +100,7 @@ public class UserControllerTests
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task GetUserByGuidAsync_AuthenticateRequestDataNull()
|
||||
public async Task GetUserByGuidAsync_GuidIsEmpty()
|
||||
{
|
||||
if (_userController == null)
|
||||
{
|
||||
@@ -232,7 +232,7 @@ public class UserControllerTests
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task CreateUserAsync_Should_Return_200_When_Successful()
|
||||
public async Task CreateUserAsync_Success()
|
||||
{
|
||||
if (_userController == null)
|
||||
{
|
||||
|
||||
@@ -68,7 +68,6 @@ public class VersionController_Tests
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.InnerException);
|
||||
Assert.Fail($"An exception was thrown: {ex}");
|
||||
}
|
||||
}
|
||||
@@ -80,7 +79,6 @@ public class VersionController_Tests
|
||||
|
||||
try
|
||||
{
|
||||
Console.WriteLine(System.AppDomain.CurrentDomain.BaseDirectory);
|
||||
var configuration = TestUtils.CreateEmptyConfiguration(System.AppDomain.CurrentDomain.BaseDirectory + "/JsonData", "emptyAppsettings.json");
|
||||
VersionController versionController = new VersionController(configuration);
|
||||
var result = versionController.GetVersion();
|
||||
@@ -97,7 +95,6 @@ public class VersionController_Tests
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.InnerException);
|
||||
Assert.Fail($"An exception was thrown: {ex}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,9 @@
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="JsonData/**" CopyToOutputDirectory="Always" CopyToPublishDirectory="Always" />
|
||||
</ItemGroup>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="Config/**" CopyToOutputDirectory="Always" CopyToPublishDirectory="Always" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using BasicDotnetTemplate.MainProject;
|
||||
using BasicDotnetTemplate.MainProject.Models.Api.Response;
|
||||
using Microsoft.Extensions.DependencyModel.Resolution;
|
||||
using BasicDotnetTemplate.MainProject.Models.Api.Common.Role;
|
||||
using BasicDotnetTemplate.MainProject.Models.Api.Response.Role;
|
||||
using DatabaseSqlServer = BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||
using BasicDotnetTemplate.MainProject.Models.Api.Response.Auth;
|
||||
using BasicDotnetTemplate.MainProject.Core.Middlewares;
|
||||
using AutoMapper;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
|
||||
namespace BasicDotnetTemplate.MainProject.Tests;
|
||||
|
||||
[TestClass]
|
||||
public class GetRoleResponse_Tests
|
||||
{
|
||||
private IMapper? _mapper;
|
||||
|
||||
[TestInitialize]
|
||||
public void Setup()
|
||||
{
|
||||
var config = new MapperConfiguration(cfg =>
|
||||
{
|
||||
cfg.AddProfile<AutoMapperConfiguration>();
|
||||
});
|
||||
|
||||
_mapper = config.CreateMapper();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IstantiateGetRoleResponse_OnlyStatus_Valid()
|
||||
{
|
||||
try
|
||||
{
|
||||
var getRoleResponse = new GetRoleResponse(200, null, null);
|
||||
Assert.IsTrue(getRoleResponse.Status == StatusCodes.Status200OK && String.IsNullOrEmpty(getRoleResponse.Message) && getRoleResponse.Data == null);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.InnerException);
|
||||
Assert.Fail($"An exception was thrown: {ex}");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IstantiateGetRoleResponse_OnlyStatus_IsInvalid()
|
||||
{
|
||||
try
|
||||
{
|
||||
var getRoleResponse = new GetRoleResponse(201, null, null);
|
||||
Assert.IsFalse(getRoleResponse.Status == StatusCodes.Status200OK);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.InnerException);
|
||||
Assert.Fail($"An exception was thrown: {ex}");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IstantiateGetRoleResponse_StatusAndMessage_Valid()
|
||||
{
|
||||
try
|
||||
{
|
||||
var getRoleResponse = new GetRoleResponse(200, "This is a test message", null);
|
||||
Assert.IsTrue(getRoleResponse.Status == StatusCodes.Status200OK && getRoleResponse.Message == "This is a test message" && getRoleResponse.Data == null);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.InnerException);
|
||||
Assert.Fail($"An exception was thrown: {ex}");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IstantiateGetRoleResponse_AllFields_Valid()
|
||||
{
|
||||
try
|
||||
{
|
||||
DatabaseSqlServer.Role role = ModelsInit.CreateRole();
|
||||
RoleDto? data = _mapper?.Map<RoleDto>(role);
|
||||
var getRoleResponse = new GetRoleResponse(200, "This is a test message", data);
|
||||
Assert.IsTrue(getRoleResponse.Status == StatusCodes.Status200OK && getRoleResponse.Message == "This is a test message" && getRoleResponse.Data == data);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.InnerException);
|
||||
Assert.Fail($"An exception was thrown: {ex}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -69,7 +69,6 @@ public class Program_Tests
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.InnerException);
|
||||
Assert.Fail($"An exception was thrown: {ex}");
|
||||
}
|
||||
}
|
||||
|
||||
1135
MainProject.Tests/Services/PermissionService_Tests.cs
Normal file
1135
MainProject.Tests/Services/PermissionService_Tests.cs
Normal file
File diff suppressed because it is too large
Load Diff
@@ -2,6 +2,8 @@
|
||||
using BasicDotnetTemplate.MainProject.Services;
|
||||
using BasicDotnetTemplate.MainProject.Models.Api.Data.Role;
|
||||
using BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||
using Newtonsoft.Json;
|
||||
using BasicDotnetTemplate.MainProject.Models.Api.Common.Exceptions;
|
||||
|
||||
|
||||
|
||||
@@ -62,7 +64,7 @@ public class RoleService_Tests
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task CreateRoleData()
|
||||
public async Task CreateRoleAsync_Success()
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -93,6 +95,43 @@ public class RoleService_Tests
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task CreateRoleAsync_Exception()
|
||||
{
|
||||
try
|
||||
{
|
||||
CreateRoleRequestData data = new CreateRoleRequestData()
|
||||
{
|
||||
Name = "Exception",
|
||||
IsNotEditable = false
|
||||
};
|
||||
|
||||
var exceptionRoleService = TestUtils.CreateRoleServiceException();
|
||||
|
||||
if (exceptionRoleService != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var role = await exceptionRoleService.CreateRoleAsync(data);
|
||||
Assert.Fail($"Expected exception instead of response: {role?.Guid}");
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Assert.IsInstanceOfType(exception, typeof(Exception));
|
||||
Assert.IsInstanceOfType(exception, typeof(CreateException));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"RoleService is null");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Assert.Fail($"An exception was thrown: {ex}");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task CheckIfNameIsValid_NameCurrentRole()
|
||||
{
|
||||
@@ -261,6 +300,111 @@ public class RoleService_Tests
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task UpdateRoleAsync_Success()
|
||||
{
|
||||
try
|
||||
{
|
||||
CreateRoleRequestData data = new CreateRoleRequestData()
|
||||
{
|
||||
Name = "ChangedRoleName",
|
||||
IsNotEditable = false
|
||||
};
|
||||
|
||||
if (_roleService != null)
|
||||
{
|
||||
Assert.IsNotNull(_role);
|
||||
var role = await _roleService.UpdateRoleAsync(data, _role!);
|
||||
Assert.IsInstanceOfType(role, typeof(Role));
|
||||
Assert.IsNotNull(role);
|
||||
Assert.IsTrue(data.Name == role.Name);
|
||||
Assert.IsTrue(data.IsNotEditable == role.IsNotEditable);
|
||||
_role = role;
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"RoleService is null");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.InnerException);
|
||||
Assert.Fail($"An exception was thrown: {ex}");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task UpdateRoleAsync_NotEditable()
|
||||
{
|
||||
try
|
||||
{
|
||||
CreateRoleRequestData createRoleData = new CreateRoleRequestData()
|
||||
{
|
||||
Name = "NotEditableRole",
|
||||
IsNotEditable = true
|
||||
};
|
||||
|
||||
|
||||
if (_roleService != null)
|
||||
{
|
||||
var role = await _roleService.CreateRoleAsync(createRoleData);
|
||||
Assert.IsNotNull(role);
|
||||
|
||||
CreateRoleRequestData updateRoleData = new CreateRoleRequestData()
|
||||
{
|
||||
Name = "TryingToEditRole",
|
||||
IsNotEditable = false
|
||||
};
|
||||
|
||||
var roleUpdatedRole = await _roleService.UpdateRoleAsync(updateRoleData, role!);
|
||||
Assert.IsInstanceOfType(roleUpdatedRole, typeof(Role));
|
||||
Assert.IsNotNull(roleUpdatedRole);
|
||||
Assert.IsTrue(roleUpdatedRole.Name == createRoleData.Name);
|
||||
Assert.IsTrue(roleUpdatedRole.IsNotEditable == createRoleData.IsNotEditable);
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"RoleService is null");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.InnerException);
|
||||
Assert.Fail($"An exception was thrown: {ex}");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task UpdateRoleAsync_Exception()
|
||||
{
|
||||
try
|
||||
{
|
||||
CreateRoleRequestData data = new CreateRoleRequestData()
|
||||
{
|
||||
Name = "Exception",
|
||||
IsNotEditable = false
|
||||
};
|
||||
|
||||
var exceptionRoleService = TestUtils.CreateRoleServiceException();
|
||||
|
||||
if (exceptionRoleService != null)
|
||||
{
|
||||
Assert.IsNotNull(_role);
|
||||
var role = await exceptionRoleService.UpdateRoleAsync(data, _role!);
|
||||
Assert.Fail($"Expected exception instead of response: {role?.Guid}");
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"RoleService is null");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Assert.IsInstanceOfType(ex, typeof(Exception));
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task DeleteRoleAsync()
|
||||
{
|
||||
|
||||
@@ -2,6 +2,7 @@ using BasicDotnetTemplate.MainProject.Services;
|
||||
using BasicDotnetTemplate.MainProject.Models.Api.Data.User;
|
||||
using BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||
using Newtonsoft.Json;
|
||||
using BasicDotnetTemplate.MainProject.Models.Api.Common.Exceptions;
|
||||
|
||||
|
||||
|
||||
@@ -119,6 +120,53 @@ public class UserService_Tests
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task CreateUserAsync_Exception()
|
||||
{
|
||||
try
|
||||
{
|
||||
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 exceptionUserService = TestUtils.CreateUserServiceException();
|
||||
|
||||
if (exceptionUserService != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var user = await exceptionUserService.CreateUserAsync(data, role);
|
||||
Assert.Fail($"Expected exception instead of response: {user?.Guid}");
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Assert.IsInstanceOfType(exception, typeof(Exception));
|
||||
Assert.IsInstanceOfType(exception, typeof(CreateException));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Fail($"UserService is null");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Assert.Fail($"An exception was thrown: {ex}");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task CheckIfEmailIsValid_EmailCurrentUser()
|
||||
{
|
||||
|
||||
24
MainProject.Tests/TestsUtils/ExceptionSqlServerContext.cs
Normal file
24
MainProject.Tests/TestsUtils/ExceptionSqlServerContext.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using BasicDotnetTemplate.MainProject.Core.Database;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace BasicDotnetTemplate.MainProject.Tests;
|
||||
|
||||
public class ExceptionSqlServerContext : SqlServerContext
|
||||
{
|
||||
public bool ThrowExceptionOnSave { get; set; }
|
||||
|
||||
public ExceptionSqlServerContext() : base(TestUtils.CreateInMemorySqlContextOptions())
|
||||
{
|
||||
}
|
||||
|
||||
public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (ThrowExceptionOnSave)
|
||||
{
|
||||
throw new Exception("Database error");
|
||||
}
|
||||
return base.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -64,11 +64,21 @@ public static class TestUtils
|
||||
return _appSettings.DatabaseSettings?.SqlServerConnectionString ?? String.Empty;
|
||||
}
|
||||
|
||||
public static SqlServerContext CreateInMemorySqlContext()
|
||||
public static string GetFakeConnectionString()
|
||||
{
|
||||
var options = new DbContextOptionsBuilder<SqlServerContext>()
|
||||
return "Server=127.0.0.1;Initial Catalog=MyFakeDatabase;User Id=MyFakeUser;Password='MyFakePassword';MultipleActiveResultSets=True;Encrypt=True;TrustServerCertificate=True;Connection Timeout=30";
|
||||
}
|
||||
|
||||
public static DbContextOptions<SqlServerContext> CreateInMemorySqlContextOptions()
|
||||
{
|
||||
return new DbContextOptionsBuilder<SqlServerContext>()
|
||||
.UseSqlite("DataSource=:memory:") // Database in-memory
|
||||
.Options;
|
||||
}
|
||||
|
||||
public static SqlServerContext CreateInMemorySqlContext()
|
||||
{
|
||||
var options = CreateInMemorySqlContextOptions();
|
||||
|
||||
var context = new SqlServerContext(options);
|
||||
context.Database.OpenConnection();
|
||||
@@ -79,8 +89,6 @@ public static class TestUtils
|
||||
public static BaseService CreateBaseService()
|
||||
{
|
||||
IConfiguration configuration = CreateConfiguration();
|
||||
var optionsBuilder = new DbContextOptionsBuilder<SqlServerContext>();
|
||||
optionsBuilder.UseSqlServer(GetSqlConnectionString(configuration));
|
||||
SqlServerContext sqlServerContext = CreateInMemorySqlContext();
|
||||
var httpContextAccessor = new Mock<IHttpContextAccessor>();
|
||||
return new BaseService(httpContextAccessor.Object, configuration, sqlServerContext);
|
||||
@@ -89,8 +97,6 @@ public static class TestUtils
|
||||
public static AuthService CreateAuthService()
|
||||
{
|
||||
IConfiguration configuration = CreateConfiguration();
|
||||
var optionsBuilder = new DbContextOptionsBuilder<SqlServerContext>();
|
||||
optionsBuilder.UseSqlServer(GetSqlConnectionString(configuration));
|
||||
SqlServerContext sqlServerContext = CreateInMemorySqlContext();
|
||||
var userServiceMock = new Mock<IUserService>();
|
||||
var httpContextAccessor = new Mock<IHttpContextAccessor>();
|
||||
@@ -105,6 +111,15 @@ public static class TestUtils
|
||||
return new UserService(httpContextAccessor.Object, configuration, sqlServerContext);
|
||||
}
|
||||
|
||||
public static UserService CreateUserServiceException()
|
||||
{
|
||||
var sqlServerContext = new ExceptionSqlServerContext();
|
||||
sqlServerContext.ThrowExceptionOnSave = true;
|
||||
IConfiguration configuration = CreateConfiguration();
|
||||
var httpContextAccessor = new Mock<IHttpContextAccessor>();
|
||||
return new UserService(httpContextAccessor.Object, configuration, sqlServerContext);
|
||||
}
|
||||
|
||||
public static JwtService CreateJwtService()
|
||||
{
|
||||
IConfiguration configuration = CreateConfiguration();
|
||||
@@ -124,6 +139,32 @@ public static class TestUtils
|
||||
var httpContextAccessor = new Mock<IHttpContextAccessor>();
|
||||
return new RoleService(httpContextAccessor.Object, configuration, sqlServerContext);
|
||||
}
|
||||
|
||||
public static RoleService CreateRoleServiceException()
|
||||
{
|
||||
var sqlServerContext = new ExceptionSqlServerContext();
|
||||
sqlServerContext.ThrowExceptionOnSave = true;
|
||||
IConfiguration configuration = CreateConfiguration();
|
||||
var httpContextAccessor = new Mock<IHttpContextAccessor>();
|
||||
return new RoleService(httpContextAccessor.Object, configuration, sqlServerContext);
|
||||
}
|
||||
|
||||
public static PermissionService CreatePermissionService()
|
||||
{
|
||||
IConfiguration configuration = CreateConfiguration();
|
||||
SqlServerContext sqlServerContext = CreateInMemorySqlContext();
|
||||
var httpContextAccessor = new Mock<IHttpContextAccessor>();
|
||||
return new PermissionService(httpContextAccessor.Object, configuration, sqlServerContext);
|
||||
}
|
||||
|
||||
public static PermissionService CreatePermissionServiceException()
|
||||
{
|
||||
var sqlServerContext = new ExceptionSqlServerContext();
|
||||
sqlServerContext.ThrowExceptionOnSave = true;
|
||||
IConfiguration configuration = CreateConfiguration();
|
||||
var httpContextAccessor = new Mock<IHttpContextAccessor>();
|
||||
return new PermissionService(httpContextAccessor.Object, configuration, sqlServerContext);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -151,8 +151,6 @@ public class CryptoUtils_Tests
|
||||
AppSettings appSettings = ProgramUtils.AddConfiguration(ref builder, System.AppDomain.CurrentDomain.BaseDirectory + "/JsonData");
|
||||
CryptUtils cryptoUtils = new CryptUtils(appSettings);
|
||||
var verified = cryptoUtils.VerifyPassword(password, salt, 0, hashedPassword);
|
||||
Console.WriteLine(cryptoUtils.GeneratePassword(password, salt, 0));
|
||||
|
||||
Assert.IsTrue(verified);
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
82
MainProject.Tests/Utils/FileUtils_Tests.cs
Normal file
82
MainProject.Tests/Utils/FileUtils_Tests.cs
Normal file
@@ -0,0 +1,82 @@
|
||||
using BasicDotnetTemplate.MainProject.Utils;
|
||||
using BasicDotnetTemplate.MainProject.Models.Common;
|
||||
|
||||
|
||||
namespace BasicDotnetTemplate.MainProject.Tests;
|
||||
|
||||
[TestClass]
|
||||
public class FileUtils_Tests
|
||||
{
|
||||
[TestMethod]
|
||||
public void ConvertFileToObject_NoFilePath()
|
||||
{
|
||||
try
|
||||
{
|
||||
PermissionsFile? permissionsFile = FileUtils.ConvertFileToObject<PermissionsFile>(String.Empty);
|
||||
Assert.Fail($"Expected exception instead of response: {permissionsFile}");
|
||||
}
|
||||
catch (ArgumentException argumentException)
|
||||
{
|
||||
Assert.IsInstanceOfType(argumentException, typeof(ArgumentException));
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Assert.Fail($"An exception was thrown: {exception}");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ConvertFileToObject_NoFile()
|
||||
{
|
||||
try
|
||||
{
|
||||
PermissionsFile? permissionsFile = FileUtils.ConvertFileToObject<PermissionsFile>(System.AppDomain.CurrentDomain.BaseDirectory + "Config/no-permissions.json");
|
||||
Assert.Fail($"Expected exception instead of response: {permissionsFile}");
|
||||
}
|
||||
catch (FileNotFoundException fileNotFoundException)
|
||||
{
|
||||
Assert.IsInstanceOfType(fileNotFoundException, typeof(FileNotFoundException));
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Assert.Fail($"An exception was thrown: {exception}");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ConvertFileToObject()
|
||||
{
|
||||
try
|
||||
{
|
||||
PermissionsFile? permissionsFile = FileUtils.ConvertFileToObject<PermissionsFile>(System.AppDomain.CurrentDomain.BaseDirectory + "Config/permissions.json");
|
||||
Assert.IsTrue(permissionsFile != null);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Assert.Fail($"An exception was thrown: {exception}");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ConvertFileToObject_InvalidOperationException()
|
||||
{
|
||||
try
|
||||
{
|
||||
PermissionsFile? permissionsFile = FileUtils.ConvertFileToObject<PermissionsFile>(System.AppDomain.CurrentDomain.BaseDirectory + "Config/invalid-permissions.json");
|
||||
Assert.Fail($"Expected exception instead of response: {permissionsFile}");
|
||||
}
|
||||
catch (InvalidOperationException invalidOperationException)
|
||||
{
|
||||
Assert.IsInstanceOfType(invalidOperationException, typeof(InvalidOperationException));
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Assert.Fail($"An exception was thrown: {exception}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -299,7 +299,6 @@ public class ProgramUtils_Tests
|
||||
ProgramUtils.AddDbContext(ref builder, realAppSettings);
|
||||
|
||||
var areEquals = expectedDbSettings.SqlServerConnectionString == realAppSettings.DatabaseSettings?.SqlServerConnectionString;
|
||||
Console.WriteLine(realAppSettings.DatabaseSettings?.SqlServerConnectionString);
|
||||
Assert.IsTrue(areEquals);
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
Reference in New Issue
Block a user