Compare commits
73 Commits
connection
...
sprints/7
| Author | SHA1 | Date | |
|---|---|---|---|
| 118a9bb6e4 | |||
| a3938fa5f0 | |||
| ca8fd67428 | |||
| fbd09b56c8 | |||
|
|
79549bea05 | ||
|
|
ac20664446 | ||
| c9390479bb | |||
| 9f595779ee | |||
| fa1ee76d35 | |||
| 82d5ede421 | |||
| b7398fd910 | |||
| 030882f3e1 | |||
| 9d2b2405d5 | |||
| 39e856f522 | |||
| 64f4cdc85e | |||
| 5d104d5dc8 | |||
| 8b5b3a198f | |||
| e5a3e6fdcf | |||
| 61f7504e66 | |||
| de2c914a0c | |||
| 0e50393455 | |||
| d92d1c7de9 | |||
| ad9633ca8b | |||
| b1ad6c942b | |||
| fe010e2baa | |||
| a8e51fa6b5 | |||
| 764f0a1511 | |||
| 86cf6f2a27 | |||
| 8f98ee5954 | |||
| cfe877a066 | |||
| 422bbf24fb | |||
| aefaefd64f | |||
| caa2cf2035 | |||
| a4e3b0666b | |||
| ae6cb2b2a4 | |||
| fdf97c7c42 | |||
| f27f7733f8 | |||
| db37ebbdce | |||
| e7b3acb91e | |||
| 5bc75de87c | |||
| f6cd629fe2 | |||
| 18e713153b | |||
| 7f5178883d | |||
| bd175da738 | |||
| 962de4df9e | |||
| 61b9c732bc | |||
| 0b354988fd | |||
| 78911eb877 | |||
| f73fe748ed | |||
| a0c93ea587 | |||
| 433e97938d | |||
| 67affa8b82 | |||
| b2619f6acc | |||
| e105e4731d | |||
| 98dbf0fdde | |||
| 08182dc9ef | |||
| 505a2cb6cc | |||
| 114e7c7c50 | |||
| 1969852a2c | |||
| 3858ec20c4 | |||
| a83c9c104a | |||
| 0bf3e081fb | |||
| c9382e9cde | |||
| c2df8a545d | |||
| c194a49170 | |||
| 70bc34b117 | |||
| 5591f6516b | |||
| d0cb4666d4 | |||
| 7321ab2901 | |||
| fbf5ef8c16 | |||
| 7926cf2f65 | |||
| 76779afd2e | |||
| 176f149be3 |
15
.github/dependabot.yml
vendored
Normal file
15
.github/dependabot.yml
vendored
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
version: 2
|
||||||
|
updates:
|
||||||
|
- package-ecosystem: "nuget"
|
||||||
|
directory: "/MainProject"
|
||||||
|
schedule:
|
||||||
|
interval: "daily"
|
||||||
|
open-pull-requests-limit: 5
|
||||||
|
target-branch: "sprints/7"
|
||||||
|
|
||||||
|
- package-ecosystem: "nuget"
|
||||||
|
directory: "/MainProject.Tests"
|
||||||
|
schedule:
|
||||||
|
interval: "daily"
|
||||||
|
open-pull-requests-limit: 5
|
||||||
|
target-branch: "sprints/7"
|
||||||
1
.github/workflows/build.yml
vendored
1
.github/workflows/build.yml
vendored
@@ -49,6 +49,7 @@ jobs:
|
|||||||
shell: powershell
|
shell: powershell
|
||||||
run: |
|
run: |
|
||||||
.\.sonar\scanner\dotnet-sonarscanner begin /k:"csimonapastore_BasicDotnetTemplate" /o:"csimonapastore-github" /d:sonar.token="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.cs.vscoveragexml.reportsPaths=coverage.xml /d:sonar.exclusions="**/Migrations/**.cs, **/Models/Database/**.cs, **/Core/Database/**.cs"
|
.\.sonar\scanner\dotnet-sonarscanner begin /k:"csimonapastore_BasicDotnetTemplate" /o:"csimonapastore-github" /d:sonar.token="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.cs.vscoveragexml.reportsPaths=coverage.xml /d:sonar.exclusions="**/Migrations/**.cs, **/Models/Database/**.cs, **/Core/Database/**.cs"
|
||||||
|
dotnet clean
|
||||||
dotnet restore
|
dotnet restore
|
||||||
dotnet build --no-incremental
|
dotnet build --no-incremental
|
||||||
dotnet-coverage collect "dotnet test" -f xml -o "coverage.xml"
|
dotnet-coverage collect "dotnet test" -f xml -o "coverage.xml"
|
||||||
|
|||||||
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": [] }
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
]
|
||||||
|
}
|
||||||
229
MainProject.Tests/Controllers/AuthController_Tests.cs
Normal file
229
MainProject.Tests/Controllers/AuthController_Tests.cs
Normal file
@@ -0,0 +1,229 @@
|
|||||||
|
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 Newtonsoft.Json;
|
||||||
|
using BasicDotnetTemplate.MainProject;
|
||||||
|
using BasicDotnetTemplate.MainProject.Controllers;
|
||||||
|
using BasicDotnetTemplate.MainProject.Services;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Response;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Settings;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Request.Auth;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Data.Auth;
|
||||||
|
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;
|
||||||
|
|
||||||
|
[TestClass]
|
||||||
|
public class AuthController_Tests
|
||||||
|
{
|
||||||
|
[TestMethod]
|
||||||
|
public void AuthController_NullConfiguration()
|
||||||
|
{
|
||||||
|
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development");
|
||||||
|
var exception = true;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var authServiceMock = new Mock<IAuthService>();
|
||||||
|
_ = new AuthController(null, authServiceMock.Object);
|
||||||
|
exception = false;
|
||||||
|
Assert.Fail($"This test should not pass as configuration is null");
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
Assert.IsTrue(exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task AuthenticateAsync_Should_Return_200_When_Successful()
|
||||||
|
{
|
||||||
|
IConfiguration configuration = TestUtils.CreateConfiguration();
|
||||||
|
var authServiceMock = new Mock<IAuthService>();
|
||||||
|
var controller = new AuthController(configuration, authServiceMock.Object);
|
||||||
|
DatabaseSqlServer.User user = ModelsInit.CreateUser();
|
||||||
|
AuthenticatedUser authenticatedUser = new AuthenticatedUser(user);
|
||||||
|
|
||||||
|
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 == StatusCodes.Status200OK);
|
||||||
|
|
||||||
|
var result = (BaseResponse<object>)response.Value;
|
||||||
|
if (result != null)
|
||||||
|
{
|
||||||
|
Assert.IsTrue(result.Status == StatusCodes.Status200OK);
|
||||||
|
Assert.IsInstanceOfType(result.Data, typeof(AuthenticatedUser));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.Fail($"Result value is null");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.Fail($"Response value is null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task AuthenticateAsync_AuthenticateRequestDataNull()
|
||||||
|
{
|
||||||
|
IConfiguration configuration = TestUtils.CreateConfiguration();
|
||||||
|
var authServiceMock = new Mock<IAuthService>();
|
||||||
|
var controller = new AuthController(configuration, authServiceMock.Object);
|
||||||
|
|
||||||
|
var request = new AuthenticateRequest
|
||||||
|
{
|
||||||
|
Data = null
|
||||||
|
};
|
||||||
|
AuthenticatedUser? authenticatedUser = null;
|
||||||
|
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 == 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 AuthenticateAsync_NotFound()
|
||||||
|
{
|
||||||
|
IConfiguration configuration = TestUtils.CreateConfiguration();
|
||||||
|
var authServiceMock = new Mock<IAuthService>();
|
||||||
|
var controller = new AuthController(configuration, authServiceMock.Object);
|
||||||
|
|
||||||
|
var request = new AuthenticateRequest
|
||||||
|
{
|
||||||
|
Data = new AuthenticateRequestData()
|
||||||
|
{
|
||||||
|
Email = "d2ejdI1f4GYpq2kTB1nmeQkZXqR3QSxH8Yqkl7iv7zgfQ13qG/0dUUsreG/WGHWRBE5mVWaV43A=",
|
||||||
|
Password = "d2ejdI1f4GYpq2kTB1nmeQkZXqR3QSxH8Yqkl7iv7zgfQ13qG/0dUUsreG/WGHWRBE5mVWaV43A="
|
||||||
|
}
|
||||||
|
};
|
||||||
|
AuthenticatedUser? authenticatedUser = null;
|
||||||
|
authServiceMock.Setup(s => s.AuthenticateAsync(It.IsAny<AuthenticateRequestData>())).ReturnsAsync(authenticatedUser);
|
||||||
|
NotFoundResult response = (NotFoundResult)(await controller.AuthenticateAsync(request));
|
||||||
|
|
||||||
|
Assert.IsInstanceOfType(response, typeof(NotFoundResult));
|
||||||
|
|
||||||
|
if (response != null)
|
||||||
|
{
|
||||||
|
Assert.IsTrue(response.StatusCode == StatusCodes.Status404NotFound);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.Fail($"Response is null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task AuthenticateAsync_ModelInvalid()
|
||||||
|
{
|
||||||
|
IConfiguration configuration = TestUtils.CreateConfiguration();
|
||||||
|
var authServiceMock = new Mock<IAuthService>();
|
||||||
|
var controller = new AuthController(configuration, authServiceMock.Object);
|
||||||
|
|
||||||
|
var request = new AuthenticateRequest
|
||||||
|
{
|
||||||
|
Data = null
|
||||||
|
};
|
||||||
|
AuthenticatedUser? authenticatedUser = null;
|
||||||
|
authServiceMock.Setup(s => s.AuthenticateAsync(It.IsAny<AuthenticateRequestData>())).ReturnsAsync(authenticatedUser);
|
||||||
|
controller.ModelState.AddModelError("Data", "Invalid data");
|
||||||
|
ObjectResult response = (ObjectResult)(await controller.AuthenticateAsync(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 AuthenticateAsync_Exception()
|
||||||
|
{
|
||||||
|
IConfiguration configuration = TestUtils.CreateConfiguration();
|
||||||
|
var authServiceMock = new Mock<IAuthService>();
|
||||||
|
var controller = new AuthController(configuration, authServiceMock.Object);
|
||||||
|
|
||||||
|
var request = new AuthenticateRequest
|
||||||
|
{
|
||||||
|
Data = new AuthenticateRequestData { Email = "user", Password = "pass" }
|
||||||
|
};
|
||||||
|
|
||||||
|
authServiceMock.Setup(s => s.AuthenticateAsync(It.IsAny<AuthenticateRequestData>())).ThrowsAsync(new Exception("Unexpected error"));
|
||||||
|
|
||||||
|
ObjectResult response = (ObjectResult)(await controller.AuthenticateAsync(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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
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
|
||||||
|
}
|
||||||
@@ -10,6 +10,7 @@ using Microsoft.AspNetCore.Mvc;
|
|||||||
using Microsoft.AspNetCore.Mvc.Infrastructure;
|
using Microsoft.AspNetCore.Mvc.Infrastructure;
|
||||||
using BasicDotnetTemplate.MainProject.Models.Api.Response;
|
using BasicDotnetTemplate.MainProject.Models.Api.Response;
|
||||||
using BasicDotnetTemplate.MainProject.Models.Settings;
|
using BasicDotnetTemplate.MainProject.Models.Settings;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
|
||||||
|
|
||||||
namespace BasicDotnetTemplate.MainProject.Tests;
|
namespace BasicDotnetTemplate.MainProject.Tests;
|
||||||
@@ -29,7 +30,7 @@ public class RootController_Test
|
|||||||
if (result != null)
|
if (result != null)
|
||||||
{
|
{
|
||||||
var data = (OkResult)result;
|
var data = (OkResult)result;
|
||||||
Assert.IsTrue(data.StatusCode == 200);
|
Assert.IsTrue(data.StatusCode == StatusCodes.Status200OK);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -38,8 +39,7 @@ public class RootController_Test
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine(ex.InnerException);
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
Assert.Fail($"An exception was thrown: {ex.Message}");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
597
MainProject.Tests/Controllers/UserController_Tests.cs
Normal file
597
MainProject.Tests/Controllers/UserController_Tests.cs
Normal file
@@ -0,0 +1,597 @@
|
|||||||
|
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 Newtonsoft.Json;
|
||||||
|
using BasicDotnetTemplate.MainProject;
|
||||||
|
using BasicDotnetTemplate.MainProject.Controllers;
|
||||||
|
using BasicDotnetTemplate.MainProject.Services;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Response;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Settings;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Request.Auth;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Data.Auth;
|
||||||
|
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 AutoMapper;
|
||||||
|
using BasicDotnetTemplate.MainProject.Core.Middlewares;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Request.User;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Data.User;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||||
|
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Tests;
|
||||||
|
|
||||||
|
[TestClass]
|
||||||
|
public class UserController_Tests
|
||||||
|
{
|
||||||
|
private Mock<IUserService>? _userServiceMock;
|
||||||
|
private Mock<IRoleService>? _roleServiceMock;
|
||||||
|
private UserController? _userController;
|
||||||
|
|
||||||
|
[TestInitialize]
|
||||||
|
public void Setup()
|
||||||
|
{
|
||||||
|
IConfiguration configuration = TestUtils.CreateConfiguration();
|
||||||
|
_userServiceMock = new Mock<IUserService>();
|
||||||
|
_roleServiceMock = new Mock<IRoleService>();
|
||||||
|
_userController = new UserController(configuration, _userServiceMock.Object, _roleServiceMock.Object);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void UserController_NullConfiguration()
|
||||||
|
{
|
||||||
|
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development");
|
||||||
|
var exception = true;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var userServiceMock = new Mock<IUserService>();
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
Assert.IsTrue(exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task GetUserByGuidAsync_Should_Return_200_When_Successful()
|
||||||
|
{
|
||||||
|
if (_userController == null)
|
||||||
|
{
|
||||||
|
Assert.Fail($"_userController is null");
|
||||||
|
}
|
||||||
|
var guid = Guid.NewGuid().ToString();
|
||||||
|
DatabaseSqlServer.User user = ModelsInit.CreateUser();
|
||||||
|
|
||||||
|
_userServiceMock?.Setup(s => s.GetUserByGuidAsync(It.IsAny<string>())).ReturnsAsync(user);
|
||||||
|
ObjectResult response = (ObjectResult)(await _userController.GetUserByGuidAsync(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(UserDto));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.Fail($"Result value is null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.Fail($"Response value is null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task GetUserByGuidAsync_GuidIsEmpty()
|
||||||
|
{
|
||||||
|
if (_userController == null)
|
||||||
|
{
|
||||||
|
Assert.Fail($"_userController is null");
|
||||||
|
}
|
||||||
|
|
||||||
|
var guid = String.Empty;
|
||||||
|
DatabaseSqlServer.User? user = null;
|
||||||
|
|
||||||
|
_userServiceMock?.Setup(s => s.GetUserByGuidAsync(It.IsAny<string>())).ReturnsAsync(user);
|
||||||
|
ObjectResult response = (ObjectResult)(await _userController.GetUserByGuidAsync(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 GetUserByGuidAsync_NotFound()
|
||||||
|
{
|
||||||
|
if (_userController == null)
|
||||||
|
{
|
||||||
|
Assert.Fail($"_userController is null");
|
||||||
|
}
|
||||||
|
|
||||||
|
var guid = Guid.NewGuid().ToString();
|
||||||
|
DatabaseSqlServer.User? user = null;
|
||||||
|
_userServiceMock?.Setup(s => s.GetUserByGuidAsync(It.IsAny<string>())).ReturnsAsync(user);
|
||||||
|
NotFoundResult response = (NotFoundResult)(await _userController.GetUserByGuidAsync(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 GetUserByGuidAsync_ModelInvalid()
|
||||||
|
{
|
||||||
|
if (_userController == null)
|
||||||
|
{
|
||||||
|
Assert.Fail($"_userController is null");
|
||||||
|
}
|
||||||
|
|
||||||
|
var guid = Guid.NewGuid().ToString();
|
||||||
|
DatabaseSqlServer.User? user = null;
|
||||||
|
_userServiceMock?.Setup(s => s.GetUserByGuidAsync(It.IsAny<string>())).ReturnsAsync(user);
|
||||||
|
_userController.ModelState.AddModelError("Data", "Invalid data");
|
||||||
|
ObjectResult response = (ObjectResult)(await _userController.GetUserByGuidAsync(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 GetUserByGuidAsync_Exception()
|
||||||
|
{
|
||||||
|
if (_userController == null)
|
||||||
|
{
|
||||||
|
Assert.Fail($"_userController is null");
|
||||||
|
}
|
||||||
|
|
||||||
|
var guid = Guid.NewGuid().ToString();
|
||||||
|
_userServiceMock?.Setup(s => s.GetUserByGuidAsync(It.IsAny<string>())).ThrowsAsync(new Exception("Unexpected error"));
|
||||||
|
ObjectResult response = (ObjectResult)(await _userController.GetUserByGuidAsync(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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task CreateUserAsync_Success()
|
||||||
|
{
|
||||||
|
if (_userController == null)
|
||||||
|
{
|
||||||
|
Assert.Fail($"_userController is null");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_roleServiceMock == null)
|
||||||
|
{
|
||||||
|
Assert.Fail($"_roleServiceMock is null");
|
||||||
|
}
|
||||||
|
|
||||||
|
DatabaseSqlServer.User user = ModelsInit.CreateUser();
|
||||||
|
DatabaseSqlServer.Role role = ModelsInit.CreateRole();
|
||||||
|
|
||||||
|
CreateUserRequest request = new CreateUserRequest()
|
||||||
|
{
|
||||||
|
Data = new CreateUserRequestData()
|
||||||
|
{
|
||||||
|
FirstName = user.FirstName,
|
||||||
|
LastName = user.LastName,
|
||||||
|
Email = user.Email,
|
||||||
|
Password = user.Password
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
_userServiceMock?.Setup(s => s.CheckIfEmailIsValid(It.IsAny<string>(), It.IsAny<string>())).ReturnsAsync(true);
|
||||||
|
_roleServiceMock.Setup(s => s.GetRoleForUser(null)).ReturnsAsync(role);
|
||||||
|
_userServiceMock?.Setup(s => s.CreateUserAsync(request.Data, role)).ReturnsAsync(user);
|
||||||
|
|
||||||
|
ObjectResult response = (ObjectResult)(await _userController.CreateUserAsync(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(UserDto));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.Fail($"Result value is null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.Fail($"Response value is null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task CreateUserAsync_InvalidEmail()
|
||||||
|
{
|
||||||
|
if (_userController == null)
|
||||||
|
{
|
||||||
|
Assert.Fail($"_userController is null");
|
||||||
|
}
|
||||||
|
|
||||||
|
DatabaseSqlServer.User user = ModelsInit.CreateUser();
|
||||||
|
|
||||||
|
CreateUserRequest request = new CreateUserRequest()
|
||||||
|
{
|
||||||
|
Data = new CreateUserRequestData()
|
||||||
|
{
|
||||||
|
FirstName = user.FirstName,
|
||||||
|
LastName = user.LastName,
|
||||||
|
Email = user.Email,
|
||||||
|
Password = user.Password
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
_userServiceMock?.Setup(s => s.CheckIfEmailIsValid(It.IsAny<string>(), It.IsAny<string>())).ReturnsAsync(false);
|
||||||
|
|
||||||
|
ObjectResult response = (ObjectResult)(await _userController.CreateUserAsync(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 email");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.Fail($"Result value is null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.Fail($"Response value is null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task CreateUserAsync_RoleNull()
|
||||||
|
{
|
||||||
|
if (_userController == null)
|
||||||
|
{
|
||||||
|
Assert.Fail($"_userController is null");
|
||||||
|
}
|
||||||
|
|
||||||
|
DatabaseSqlServer.User user = ModelsInit.CreateUser();
|
||||||
|
|
||||||
|
CreateUserRequest request = new CreateUserRequest()
|
||||||
|
{
|
||||||
|
Data = new CreateUserRequestData()
|
||||||
|
{
|
||||||
|
FirstName = user.FirstName,
|
||||||
|
LastName = user.LastName,
|
||||||
|
Email = user.Email,
|
||||||
|
Password = user.Password
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
_userServiceMock?.Setup(s => s.CheckIfEmailIsValid(It.IsAny<string>(), It.IsAny<string>())).ReturnsAsync(true);
|
||||||
|
|
||||||
|
_userServiceMock?.Setup(s => s.CreateUserAsync(
|
||||||
|
It.IsAny<CreateUserRequestData>(),
|
||||||
|
It.IsAny<Role>()
|
||||||
|
)).ReturnsAsync(user);
|
||||||
|
|
||||||
|
ObjectResult response = (ObjectResult)(await _userController.CreateUserAsync(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 == "Role not found");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.Fail($"Result value is null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.Fail($"Response value is null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task CreateUserAsync_CreateUserRequestDataNull()
|
||||||
|
{
|
||||||
|
if (_userController == null)
|
||||||
|
{
|
||||||
|
Assert.Fail($"_userController is null");
|
||||||
|
}
|
||||||
|
|
||||||
|
DatabaseSqlServer.User user = ModelsInit.CreateUser();
|
||||||
|
|
||||||
|
CreateUserRequest request = new CreateUserRequest()
|
||||||
|
{
|
||||||
|
Data = null
|
||||||
|
};
|
||||||
|
|
||||||
|
_userServiceMock?.Setup(s => s.CheckIfEmailIsValid(It.IsAny<string>(), It.IsAny<string>())).ReturnsAsync(true);
|
||||||
|
|
||||||
|
_userServiceMock?.Setup(s => s.CreateUserAsync(
|
||||||
|
It.IsAny<CreateUserRequestData>(),
|
||||||
|
It.IsAny<Role>()
|
||||||
|
)).ReturnsAsync(user);
|
||||||
|
|
||||||
|
ObjectResult response = (ObjectResult)(await _userController.CreateUserAsync(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 CreateUserAsync_NotCreated()
|
||||||
|
{
|
||||||
|
if (_userController == null)
|
||||||
|
{
|
||||||
|
Assert.Fail($"_userController is null");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_roleServiceMock == null)
|
||||||
|
{
|
||||||
|
Assert.Fail($"_roleServiceMock is null");
|
||||||
|
}
|
||||||
|
|
||||||
|
DatabaseSqlServer.User user = ModelsInit.CreateUser();
|
||||||
|
DatabaseSqlServer.Role role = ModelsInit.CreateRole();
|
||||||
|
DatabaseSqlServer.User? expectedUser = null;
|
||||||
|
|
||||||
|
CreateUserRequest request = new CreateUserRequest()
|
||||||
|
{
|
||||||
|
Data = new CreateUserRequestData()
|
||||||
|
{
|
||||||
|
FirstName = user.FirstName,
|
||||||
|
LastName = user.LastName,
|
||||||
|
Email = user.Email,
|
||||||
|
Password = user.Password
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
_userServiceMock?.Setup(s => s.CheckIfEmailIsValid(It.IsAny<string>(), It.IsAny<string>())).ReturnsAsync(true);
|
||||||
|
|
||||||
|
_roleServiceMock.Setup(s => s.GetRoleForUser(null)).ReturnsAsync(role);
|
||||||
|
_userServiceMock?.Setup(s => s.CreateUserAsync(request.Data, role)).ReturnsAsync(expectedUser);
|
||||||
|
|
||||||
|
ObjectResult response = (ObjectResult)(await _userController.CreateUserAsync(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 CreateUserAsync_ModelInvalid()
|
||||||
|
{
|
||||||
|
if (_userController == null)
|
||||||
|
{
|
||||||
|
Assert.Fail($"_userController is null");
|
||||||
|
}
|
||||||
|
|
||||||
|
DatabaseSqlServer.User user = ModelsInit.CreateUser();
|
||||||
|
|
||||||
|
CreateUserRequest request = new CreateUserRequest()
|
||||||
|
{
|
||||||
|
Data = new CreateUserRequestData()
|
||||||
|
{
|
||||||
|
FirstName = user.FirstName,
|
||||||
|
LastName = user.LastName,
|
||||||
|
Email = user.Email,
|
||||||
|
Password = user.Password
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
_userServiceMock?.Setup(s => s.CheckIfEmailIsValid(It.IsAny<string>(), It.IsAny<string>())).ReturnsAsync(true);
|
||||||
|
|
||||||
|
_userServiceMock?.Setup(s => s.CreateUserAsync(
|
||||||
|
It.IsAny<CreateUserRequestData>(),
|
||||||
|
It.IsAny<Role>()
|
||||||
|
)).ReturnsAsync(user);
|
||||||
|
_userController.ModelState.AddModelError("Data", "Invalid data");
|
||||||
|
ObjectResult response = (ObjectResult)(await _userController.CreateUserAsync(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 CreateUserAsync_Exception()
|
||||||
|
{
|
||||||
|
if (_userController == null)
|
||||||
|
{
|
||||||
|
Assert.Fail($"_userController is null");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_roleServiceMock == null)
|
||||||
|
{
|
||||||
|
Assert.Fail($"_roleServiceMock is null");
|
||||||
|
}
|
||||||
|
|
||||||
|
DatabaseSqlServer.User user = ModelsInit.CreateUser();
|
||||||
|
DatabaseSqlServer.Role role = ModelsInit.CreateRole();
|
||||||
|
|
||||||
|
CreateUserRequest request = new CreateUserRequest()
|
||||||
|
{
|
||||||
|
Data = new CreateUserRequestData()
|
||||||
|
{
|
||||||
|
FirstName = user.FirstName,
|
||||||
|
LastName = user.LastName,
|
||||||
|
Email = user.Email,
|
||||||
|
Password = user.Password
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
_userServiceMock?.Setup(s => s.CheckIfEmailIsValid(It.IsAny<string>(), It.IsAny<string>())).ReturnsAsync(true);
|
||||||
|
|
||||||
|
_roleServiceMock.Setup(s => s.GetRoleForUser(null)).ReturnsAsync(role);
|
||||||
|
_userServiceMock?.Setup(s => s.CreateUserAsync(request.Data, role)).ReturnsAsync(user);
|
||||||
|
|
||||||
|
_userServiceMock?.Setup(s => s.CreateUserAsync(
|
||||||
|
It.IsAny<CreateUserRequestData>(),
|
||||||
|
It.IsAny<Role>()
|
||||||
|
)).ThrowsAsync(new Exception("Unexpected error"));
|
||||||
|
|
||||||
|
ObjectResult response = (ObjectResult)(await _userController.CreateUserAsync(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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,15 +10,16 @@ using Microsoft.AspNetCore.Mvc;
|
|||||||
using Microsoft.AspNetCore.Mvc.Infrastructure;
|
using Microsoft.AspNetCore.Mvc.Infrastructure;
|
||||||
using BasicDotnetTemplate.MainProject.Models.Api.Response;
|
using BasicDotnetTemplate.MainProject.Models.Api.Response;
|
||||||
using BasicDotnetTemplate.MainProject.Models.Settings;
|
using BasicDotnetTemplate.MainProject.Models.Settings;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
|
||||||
|
|
||||||
namespace BasicDotnetTemplate.MainProject.Tests;
|
namespace BasicDotnetTemplate.MainProject.Tests;
|
||||||
|
|
||||||
[TestClass]
|
[TestClass]
|
||||||
public class BaseController_Tests
|
public class VersionController_Tests
|
||||||
{
|
{
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void BaseController_NullConfiguration()
|
public void VersionController_NullConfiguration()
|
||||||
{
|
{
|
||||||
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development");
|
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development");
|
||||||
var exception = true;
|
var exception = true;
|
||||||
@@ -34,7 +35,6 @@ public class BaseController_Tests
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void VersionController_GetVersion_Valid()
|
public void VersionController_GetVersion_Valid()
|
||||||
{
|
{
|
||||||
@@ -48,13 +48,13 @@ public class BaseController_Tests
|
|||||||
var objectResult = ((ObjectResult)result).Value;
|
var objectResult = ((ObjectResult)result).Value;
|
||||||
if (objectResult != null)
|
if (objectResult != null)
|
||||||
{
|
{
|
||||||
var data = (BaseResponse)objectResult;
|
var data = (BaseResponse<object>)objectResult;
|
||||||
if (data.Data != null)
|
if (data.Data != null)
|
||||||
{
|
{
|
||||||
AppSettings appSettings = new AppSettings();
|
AppSettings appSettings = new AppSettings();
|
||||||
configuration.GetSection("AppSettings").Bind(appSettings);
|
configuration.GetSection("AppSettings").Bind(appSettings);
|
||||||
string version = data.Data != null ? (string)data.Data : "";
|
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
|
else
|
||||||
{
|
{
|
||||||
@@ -68,8 +68,7 @@ public class BaseController_Tests
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine(ex.InnerException);
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
Assert.Fail($"An exception was thrown: {ex.Message}");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,15 +79,14 @@ public class BaseController_Tests
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Console.WriteLine(System.AppDomain.CurrentDomain.BaseDirectory);
|
|
||||||
var configuration = TestUtils.CreateEmptyConfiguration(System.AppDomain.CurrentDomain.BaseDirectory + "/JsonData", "emptyAppsettings.json");
|
var configuration = TestUtils.CreateEmptyConfiguration(System.AppDomain.CurrentDomain.BaseDirectory + "/JsonData", "emptyAppsettings.json");
|
||||||
VersionController versionController = new VersionController(configuration);
|
VersionController versionController = new VersionController(configuration);
|
||||||
var result = versionController.GetVersion();
|
var result = versionController.GetVersion();
|
||||||
var objectResult = ((ObjectResult)result).Value;
|
var objectResult = ((ObjectResult)result).Value;
|
||||||
if (objectResult != null)
|
if (objectResult != null)
|
||||||
{
|
{
|
||||||
var data = (BaseResponse)objectResult;
|
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
|
else
|
||||||
{
|
{
|
||||||
@@ -97,8 +95,7 @@ public class BaseController_Tests
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine(ex.InnerException);
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
Assert.Fail($"An exception was thrown: {ex.Message}");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,146 @@
|
|||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Common.User;
|
||||||
|
using DatabaseSqlServer = BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Settings;
|
||||||
|
using BasicDotnetTemplate.MainProject.Utils;
|
||||||
|
using Microsoft.AspNetCore.Mvc.Filters;
|
||||||
|
using BasicDotnetTemplate.MainProject.Core.Attributes;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Moq;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc.Controllers;
|
||||||
|
using Microsoft.AspNetCore.Routing;
|
||||||
|
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Tests;
|
||||||
|
|
||||||
|
[TestClass]
|
||||||
|
public class JwtAuthorizationAttribute_Tests
|
||||||
|
{
|
||||||
|
private static AuthenticatedUser? _authenticatedUser = null;
|
||||||
|
private static string? _token = null;
|
||||||
|
private static JwtAuthorizationAttribute? _attribute;
|
||||||
|
private static JwtTokenUtils? _jwtTokenUtils;
|
||||||
|
|
||||||
|
[TestInitialize]
|
||||||
|
public void Setup()
|
||||||
|
{
|
||||||
|
_attribute = new JwtAuthorizationAttribute();
|
||||||
|
|
||||||
|
DatabaseSqlServer.User user = ModelsInit.CreateUser();
|
||||||
|
_authenticatedUser = new AuthenticatedUser(user);
|
||||||
|
|
||||||
|
WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty<string>());
|
||||||
|
AppSettings appSettings = ProgramUtils.AddConfiguration(ref builder, System.AppDomain.CurrentDomain.BaseDirectory + "/JsonData");
|
||||||
|
_jwtTokenUtils = new JwtTokenUtils(appSettings);
|
||||||
|
_token = _jwtTokenUtils.GenerateToken(user.Guid);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static AuthorizationFilterContext CreateAuthorizationContextWithAllowAnonymous()
|
||||||
|
{
|
||||||
|
var httpContext = new DefaultHttpContext();
|
||||||
|
var routeData = new RouteData();
|
||||||
|
var actionDescriptor = new ControllerActionDescriptor
|
||||||
|
{
|
||||||
|
EndpointMetadata = new List<object> { new AllowAnonymousAttribute() }
|
||||||
|
};
|
||||||
|
var actionContext = new ActionContext(httpContext, routeData, actionDescriptor);
|
||||||
|
|
||||||
|
actionContext.ActionDescriptor.EndpointMetadata.Add(new AllowAnonymousAttribute());
|
||||||
|
|
||||||
|
return new AuthorizationFilterContext(actionContext, []);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void OnAuthorization_AllowAnonymous_SkipsAuthorization()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var context = CreateAuthorizationContextWithAllowAnonymous();
|
||||||
|
_attribute?.OnAuthorization(context);
|
||||||
|
|
||||||
|
Assert.IsNull(context.Result);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.InnerException);
|
||||||
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void OnAuthorization_NoAuthenticatedUser_ReturnsUnauthorized()
|
||||||
|
{
|
||||||
|
var context = TestUtils.CreateAuthorizationContext();
|
||||||
|
IConfiguration configuration = TestUtils.CreateConfiguration();
|
||||||
|
|
||||||
|
context.HttpContext.RequestServices = new ServiceCollection()
|
||||||
|
.AddSingleton(configuration)
|
||||||
|
.BuildServiceProvider();
|
||||||
|
|
||||||
|
context.HttpContext.Items["User"] = null;
|
||||||
|
_attribute?.OnAuthorization(context);
|
||||||
|
Assert.IsInstanceOfType(context.Result, typeof(UnauthorizedResult));
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void OnAuthorization_EmptyAuthorizationHeader_ReturnsUnauthorized()
|
||||||
|
{
|
||||||
|
var context = TestUtils.CreateAuthorizationContext();
|
||||||
|
IConfiguration configuration = TestUtils.CreateConfiguration();
|
||||||
|
|
||||||
|
context.HttpContext.RequestServices = new ServiceCollection()
|
||||||
|
.AddSingleton(configuration)
|
||||||
|
.BuildServiceProvider();
|
||||||
|
|
||||||
|
context.HttpContext.Items["User"] = _authenticatedUser;
|
||||||
|
context.HttpContext.Request.Headers.Authorization = string.Empty;
|
||||||
|
|
||||||
|
_attribute?.OnAuthorization(context);
|
||||||
|
|
||||||
|
Assert.IsInstanceOfType(context.Result, typeof(UnauthorizedResult));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void OnAuthorization_InvalidToken_ReturnsUnauthorized()
|
||||||
|
{
|
||||||
|
var context = TestUtils.CreateAuthorizationContext();
|
||||||
|
IConfiguration configuration = TestUtils.CreateConfiguration();
|
||||||
|
|
||||||
|
context.HttpContext.RequestServices = new ServiceCollection()
|
||||||
|
.AddSingleton(configuration)
|
||||||
|
.BuildServiceProvider();
|
||||||
|
|
||||||
|
var invalidToken = _jwtTokenUtils?.GenerateToken(Guid.NewGuid().ToString());
|
||||||
|
context.HttpContext.Request.Headers.Authorization = $"Bearer {invalidToken}";
|
||||||
|
|
||||||
|
context.HttpContext.Items["User"] = _authenticatedUser;
|
||||||
|
|
||||||
|
_attribute?.OnAuthorization(context);
|
||||||
|
|
||||||
|
Assert.IsInstanceOfType(context.Result, typeof(UnauthorizedResult));
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void OnAuthorization_ValidToken()
|
||||||
|
{
|
||||||
|
var context = TestUtils.CreateAuthorizationContext();
|
||||||
|
IConfiguration configuration = TestUtils.CreateConfiguration();
|
||||||
|
|
||||||
|
context.HttpContext.RequestServices = new ServiceCollection()
|
||||||
|
.AddSingleton(configuration)
|
||||||
|
.BuildServiceProvider();
|
||||||
|
context.HttpContext.Request.Headers.Authorization = $"Bearer {_token}";
|
||||||
|
|
||||||
|
context.HttpContext.Items["User"] = _authenticatedUser;
|
||||||
|
|
||||||
|
_attribute?.OnAuthorization(context);
|
||||||
|
|
||||||
|
Assert.IsNotInstanceOfType(context.Result, typeof(UnauthorizedResult));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
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.User;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Response.User;
|
||||||
|
using DatabaseSqlServer = BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Response.Auth;
|
||||||
|
using BasicDotnetTemplate.MainProject.Core.Middlewares;
|
||||||
|
using AutoMapper;
|
||||||
|
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Tests;
|
||||||
|
|
||||||
|
[TestClass]
|
||||||
|
public class AutoMapperConfiguration_Tests
|
||||||
|
{
|
||||||
|
private IMapper? _mapper;
|
||||||
|
|
||||||
|
[TestInitialize]
|
||||||
|
public void Setup()
|
||||||
|
{
|
||||||
|
var config = new MapperConfiguration(cfg =>
|
||||||
|
{
|
||||||
|
cfg.AddProfile<AutoMapperConfiguration>();
|
||||||
|
});
|
||||||
|
|
||||||
|
_mapper = config.CreateMapper();
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void Mapper_UserDto()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
DatabaseSqlServer.User user = ModelsInit.CreateUser();
|
||||||
|
UserDto? data = _mapper?.Map<UserDto>(user);
|
||||||
|
|
||||||
|
Assert.IsTrue(data?.Guid == user.Guid);
|
||||||
|
Assert.IsTrue(data?.FirstName == user.FirstName);
|
||||||
|
Assert.IsTrue(data?.LastName == user.LastName);
|
||||||
|
Assert.IsTrue(data?.Email == user.Email);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.InnerException);
|
||||||
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -21,6 +21,22 @@
|
|||||||
"Name": "MIT License",
|
"Name": "MIT License",
|
||||||
"Url": "https://github.com/csimonapastore/BasicDotnetTemplate/blob/main/LICENSE.md"
|
"Url": "https://github.com/csimonapastore/BasicDotnetTemplate/blob/main/LICENSE.md"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Information",
|
||||||
|
"Microsoft.AspNetCore": "Warning"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"JwtSettings": {
|
||||||
|
"ValidAudience": "http://localhost:4200",
|
||||||
|
"ValidIssuer": "http://localhost:5000",
|
||||||
|
"Secret": "JWTAuthenticationHIGHsecuredPasswordVVVp1OH7Xzyr",
|
||||||
|
"ExpiredAfterMinsOfInactivity": 15
|
||||||
|
},
|
||||||
|
"EncryptionSettings": {
|
||||||
|
"Salt": "S7VIidfXQf1tOQYX",
|
||||||
|
"Pepper": ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
43
MainProject.Tests/JsonData/invalidCryptAppsettings.json
Normal file
43
MainProject.Tests/JsonData/invalidCryptAppsettings.json
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
{
|
||||||
|
"AppSettings" :
|
||||||
|
{
|
||||||
|
"Settings": {
|
||||||
|
"Name": "MainProject",
|
||||||
|
"Version": "v1.0",
|
||||||
|
"Description": "This template contains basic configuration for a .Net 8 backend"
|
||||||
|
},
|
||||||
|
"DatabaseSettings": {
|
||||||
|
"SqlServerConnectionString": "SQLSERVER_DB_SERVER",
|
||||||
|
"MongoDbConnectionString": "MONGO_DB_SERVER",
|
||||||
|
"PostgreSQLConnectionString": "POSTGRESQL_DB_SERVER"
|
||||||
|
},
|
||||||
|
"OpenApiSettings": {
|
||||||
|
"TermsOfServiceUrl": "",
|
||||||
|
"OpenApiContact": {
|
||||||
|
"Name": "",
|
||||||
|
"Url": ""
|
||||||
|
},
|
||||||
|
"OpenApiLicense": {
|
||||||
|
"Name": "MIT License",
|
||||||
|
"Url": "https://github.com/csimonapastore/BasicDotnetTemplate/blob/main/LICENSE.md"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Information",
|
||||||
|
"Microsoft.AspNetCore": "Warning"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"JwtSettings": {
|
||||||
|
"ValidAudience": "http://localhost:4200",
|
||||||
|
"ValidIssuer": "http://localhost:5000",
|
||||||
|
"Secret": "JWTAuthenticationHIGHsecuredPasswordVVVp1OH7Xzyr",
|
||||||
|
"ExpiredAfterMinsOfInactivity": 15
|
||||||
|
},
|
||||||
|
"EncryptionSettings": {
|
||||||
|
"Salt": "AAAAA",
|
||||||
|
"Pepper": ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -11,9 +11,12 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="coverlet.collector" Version="6.0.0" />
|
<PackageReference Include="coverlet.collector" Version="6.0.0" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.3" />
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
|
||||||
|
<PackageReference Include="Moq" Version="4.20.72" />
|
||||||
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
|
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
|
||||||
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
|
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
|
||||||
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
@@ -27,5 +30,8 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Content Include="JsonData/**" CopyToOutputDirectory="Always" CopyToPublishDirectory="Always" />
|
<Content Include="JsonData/**" CopyToOutputDirectory="Always" CopyToPublishDirectory="Always" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Content Include="Config/**" CopyToOutputDirectory="Always" CopyToPublishDirectory="Always" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
36
MainProject.Tests/Models/Api/Common/Role/UserRole_Tests.cs
Normal file
36
MainProject.Tests/Models/Api/Common/Role/UserRole_Tests.cs
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
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.User;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Common.Role;
|
||||||
|
using DatabaseSqlServer = BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Response.Auth;
|
||||||
|
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Tests;
|
||||||
|
|
||||||
|
[TestClass]
|
||||||
|
public class UserRole_Tests
|
||||||
|
{
|
||||||
|
[TestMethod]
|
||||||
|
public void InitializeUserRoleFromRole()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
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}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
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.User;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Common.Role;
|
||||||
|
using DatabaseSqlServer = BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Response.Auth;
|
||||||
|
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Tests;
|
||||||
|
|
||||||
|
[TestClass]
|
||||||
|
public class AuthenticatedUser_Tests
|
||||||
|
{
|
||||||
|
[TestMethod]
|
||||||
|
public void InitializeAuthenticatedUserFromUser()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
DatabaseSqlServer.User user = ModelsInit.CreateUser();
|
||||||
|
AuthenticatedUser authenticatedUser = new AuthenticatedUser(user);
|
||||||
|
|
||||||
|
Assert.IsTrue(authenticatedUser.FirstName == user.FirstName);
|
||||||
|
Assert.IsTrue(authenticatedUser.LastName == user.LastName);
|
||||||
|
Assert.IsTrue(authenticatedUser.Email == user.Email);
|
||||||
|
Assert.IsInstanceOfType(authenticatedUser.Role, typeof(UserRole));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.InnerException);
|
||||||
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,6 +6,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|||||||
using BasicDotnetTemplate.MainProject;
|
using BasicDotnetTemplate.MainProject;
|
||||||
using BasicDotnetTemplate.MainProject.Models.Api.Response;
|
using BasicDotnetTemplate.MainProject.Models.Api.Response;
|
||||||
using Microsoft.Extensions.DependencyModel.Resolution;
|
using Microsoft.Extensions.DependencyModel.Resolution;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
|
||||||
|
|
||||||
namespace BasicDotnetTemplate.MainProject.Tests;
|
namespace BasicDotnetTemplate.MainProject.Tests;
|
||||||
@@ -18,13 +19,13 @@ public class ApiResponse_Tests
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var baseResponse = new BaseResponse(200, null, null);
|
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)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine(ex.InnerException);
|
Console.WriteLine(ex.InnerException);
|
||||||
Assert.Fail($"An exception was thrown: {ex.Message}");
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,13 +34,13 @@ public class ApiResponse_Tests
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var baseResponse = new BaseResponse(201, null, null);
|
var baseResponse = new BaseResponse<object>(201, null, null);
|
||||||
Assert.IsFalse(baseResponse.Status == 200);
|
Assert.IsFalse(baseResponse.Status == StatusCodes.Status200OK);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine(ex.InnerException);
|
Console.WriteLine(ex.InnerException);
|
||||||
Assert.Fail($"An exception was thrown: {ex.Message}");
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,13 +49,13 @@ public class ApiResponse_Tests
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var baseResponse = new BaseResponse(200, "This is a test message", null);
|
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)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine(ex.InnerException);
|
Console.WriteLine(ex.InnerException);
|
||||||
Assert.Fail($"An exception was thrown: {ex.Message}");
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,13 +65,13 @@ public class ApiResponse_Tests
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
string[] data = { "Volvo", "BMW", "Ford", "Mazda" };
|
string[] data = { "Volvo", "BMW", "Ford", "Mazda" };
|
||||||
var baseResponse = new BaseResponse(200, "This is a test message", data);
|
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)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine(ex.InnerException);
|
Console.WriteLine(ex.InnerException);
|
||||||
Assert.Fail($"An exception was thrown: {ex.Message}");
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,86 @@
|
|||||||
|
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.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;
|
||||||
|
|
||||||
|
[TestClass]
|
||||||
|
public class AuthenticateResponse_Tests
|
||||||
|
{
|
||||||
|
[TestMethod]
|
||||||
|
public void IstantiateAuthenticateResponse_OnlyStatus_Valid()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var authenticateResponse = new AuthenticateResponse(200, null, 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}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void IstantiateAuthenticateResponse_OnlyStatus_IsInvalid()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var authenticateResponse = new AuthenticateResponse(201, null, null);
|
||||||
|
Assert.IsFalse(authenticateResponse.Status == StatusCodes.Status200OK);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.InnerException);
|
||||||
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void IstantiateAuthenticateResponse_StatusAndMessage_Valid()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var authenticateResponse = new AuthenticateResponse(200, "This is a test message", 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}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void IstantiateAuthenticateResponse_AllFields_Valid()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
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 == 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}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -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}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -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.User;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Response.User;
|
||||||
|
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 GetUserResponse_Tests
|
||||||
|
{
|
||||||
|
private IMapper? _mapper;
|
||||||
|
|
||||||
|
[TestInitialize]
|
||||||
|
public void Setup()
|
||||||
|
{
|
||||||
|
var config = new MapperConfiguration(cfg =>
|
||||||
|
{
|
||||||
|
cfg.AddProfile<AutoMapperConfiguration>();
|
||||||
|
});
|
||||||
|
|
||||||
|
_mapper = config.CreateMapper();
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void IstantiateGetUserResponse_OnlyStatus_Valid()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var getUserResponse = new GetUserResponse(200, null, 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}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void IstantiateGetUserResponse_OnlyStatus_IsInvalid()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var getUserResponse = new GetUserResponse(201, null, null);
|
||||||
|
Assert.IsFalse(getUserResponse.Status == StatusCodes.Status200OK);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.InnerException);
|
||||||
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void IstantiateGetUserResponse_StatusAndMessage_Valid()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var getUserResponse = new GetUserResponse(200, "This is a test message", 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}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void IstantiateGetUserResponse_AllFields_Valid()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
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 == 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}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -7,6 +7,7 @@ using BasicDotnetTemplate.MainProject;
|
|||||||
using BasicDotnetTemplate.MainProject.Models.Api.Response;
|
using BasicDotnetTemplate.MainProject.Models.Api.Response;
|
||||||
using Microsoft.Extensions.DependencyModel.Resolution;
|
using Microsoft.Extensions.DependencyModel.Resolution;
|
||||||
using BasicDotnetTemplate.MainProject.Models.Settings;
|
using BasicDotnetTemplate.MainProject.Models.Settings;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
|
||||||
|
|
||||||
namespace BasicDotnetTemplate.MainProject.Tests;
|
namespace BasicDotnetTemplate.MainProject.Tests;
|
||||||
@@ -25,7 +26,8 @@ public class Settings_Tests
|
|||||||
|
|
||||||
var privateSettings = new PrivateSettings()
|
var privateSettings = new PrivateSettings()
|
||||||
{
|
{
|
||||||
DatabaseConnection = new DatabaseConnection() {
|
DatabaseConnection = new DatabaseConnection()
|
||||||
|
{
|
||||||
SqlServer = sqlServer,
|
SqlServer = sqlServer,
|
||||||
Mongodb = mongodb,
|
Mongodb = mongodb,
|
||||||
Postgres = postgres,
|
Postgres = postgres,
|
||||||
@@ -42,7 +44,7 @@ public class Settings_Tests
|
|||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine(ex.InnerException);
|
Console.WriteLine(ex.InnerException);
|
||||||
Assert.Fail($"An exception was thrown: {ex.Message}");
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,13 +53,13 @@ public class Settings_Tests
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var baseResponse = new BaseResponse(201, null, null);
|
var baseResponse = new BaseResponse<object>(201, null, null);
|
||||||
Assert.IsFalse(baseResponse.Status == 200);
|
Assert.IsFalse(baseResponse.Status == StatusCodes.Status200OK);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine(ex.InnerException);
|
Console.WriteLine(ex.InnerException);
|
||||||
Assert.Fail($"An exception was thrown: {ex.Message}");
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -66,13 +68,13 @@ public class Settings_Tests
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var baseResponse = new BaseResponse(200, "This is a test message", null);
|
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)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine(ex.InnerException);
|
Console.WriteLine(ex.InnerException);
|
||||||
Assert.Fail($"An exception was thrown: {ex.Message}");
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,13 +84,13 @@ public class Settings_Tests
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
string[] data = { "Volvo", "BMW", "Ford", "Mazda" };
|
string[] data = { "Volvo", "BMW", "Ford", "Mazda" };
|
||||||
var baseResponse = new BaseResponse(200, "This is a test message", data);
|
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)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine(ex.InnerException);
|
Console.WriteLine(ex.InnerException);
|
||||||
Assert.Fail($"An exception was thrown: {ex.Message}");
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -69,8 +69,7 @@ public class Program_Tests
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine(ex.InnerException);
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
Assert.Fail($"An exception was thrown: {ex.Message}");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
114
MainProject.Tests/Services/AuthService_Tests.cs
Normal file
114
MainProject.Tests/Services/AuthService_Tests.cs
Normal file
@@ -0,0 +1,114 @@
|
|||||||
|
using BasicDotnetTemplate.MainProject.Models.Settings;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using BasicDotnetTemplate.MainProject.Utils;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Moq;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using BasicDotnetTemplate.MainProject.Core.Database;
|
||||||
|
using BasicDotnetTemplate.MainProject.Services;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Response;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Request.Auth;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Data.Auth;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Common.User;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Common.Role;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Response.Auth;
|
||||||
|
using DatabaseSqlServer = BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Tests;
|
||||||
|
|
||||||
|
[TestClass]
|
||||||
|
public class AuthService_Tests
|
||||||
|
{
|
||||||
|
[TestMethod]
|
||||||
|
public void Inizialize()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var authService = TestUtils.CreateAuthService();
|
||||||
|
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}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task AuthenticateAsync_UserNotFound()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var request = new AuthenticateRequest
|
||||||
|
{
|
||||||
|
Data = new AuthenticateRequestData
|
||||||
|
{
|
||||||
|
Email = "d2ejdI1f4GYpq2kTB1nmeQkZXqR3QSxH8Yqkl7iv7zgfQ13qG/0dUUsreG/WGHWRBE5mVWaV43A=",
|
||||||
|
Password = "d2ejdI1f4GYpq2kTB1nmeQkZXqR3QSxH8Yqkl7iv7zgfQ13qG/0dUUsreG/WGHWRBE5mVWaV43A="
|
||||||
|
}
|
||||||
|
};
|
||||||
|
var authService = TestUtils.CreateAuthService();
|
||||||
|
if (authService != null)
|
||||||
|
{
|
||||||
|
var authenticatedUser = await authService.AuthenticateAsync(request.Data);
|
||||||
|
Assert.IsTrue(authenticatedUser == null);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.Fail($"AuthService is null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.InnerException);
|
||||||
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task AuthenticateAsync_UsernamePasswordInvalid()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var request = new AuthenticateRequest
|
||||||
|
{
|
||||||
|
Data = new AuthenticateRequestData
|
||||||
|
{
|
||||||
|
Email = "WGHWRBE5mVWaV=",
|
||||||
|
Password = "WGHWRBE5mVWaV="
|
||||||
|
}
|
||||||
|
};
|
||||||
|
var authService = TestUtils.CreateAuthService();
|
||||||
|
if (authService != null)
|
||||||
|
{
|
||||||
|
var authenticatedUser = await authService.AuthenticateAsync(request.Data);
|
||||||
|
Assert.IsTrue(authenticatedUser == null);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.Fail($"AuthService is null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.InnerException);
|
||||||
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
76
MainProject.Tests/Services/JwtService_Tests.cs
Normal file
76
MainProject.Tests/Services/JwtService_Tests.cs
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
using BasicDotnetTemplate.MainProject.Models.Settings;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using BasicDotnetTemplate.MainProject.Utils;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Moq;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using BasicDotnetTemplate.MainProject.Core.Database;
|
||||||
|
using BasicDotnetTemplate.MainProject.Services;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Response;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Request.Auth;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Data.Auth;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Response.Auth;
|
||||||
|
using DatabaseSqlServer = BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Tests;
|
||||||
|
|
||||||
|
[TestClass]
|
||||||
|
public class JwtService_Tests
|
||||||
|
{
|
||||||
|
[TestMethod]
|
||||||
|
public void Inizialize()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var jwtService = TestUtils.CreateJwtService();
|
||||||
|
if (jwtService != null)
|
||||||
|
{
|
||||||
|
Assert.IsInstanceOfType(jwtService, typeof(JwtService));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.Fail($"JwtService is null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.InnerException);
|
||||||
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void GenerateToken()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var jwtService = TestUtils.CreateJwtService();
|
||||||
|
var testString = "test";
|
||||||
|
if (jwtService != null)
|
||||||
|
{
|
||||||
|
var jwt = jwtService.GenerateToken(testString);
|
||||||
|
Assert.IsTrue(jwt != null);
|
||||||
|
Assert.IsInstanceOfType(jwt, typeof(string));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.Fail($"JwtService is null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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
437
MainProject.Tests/Services/RoleService_Tests.cs
Normal file
437
MainProject.Tests/Services/RoleService_Tests.cs
Normal file
@@ -0,0 +1,437 @@
|
|||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Tests;
|
||||||
|
|
||||||
|
[TestClass]
|
||||||
|
public class RoleService_Tests
|
||||||
|
{
|
||||||
|
private static Role? _expectedRole = ModelsInit.CreateRole();
|
||||||
|
private static Role? _role;
|
||||||
|
private static RoleService _roleService = TestUtils.CreateRoleService();
|
||||||
|
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void Inizialize()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var roleService = TestUtils.CreateRoleService();
|
||||||
|
if (roleService != null)
|
||||||
|
{
|
||||||
|
Assert.IsInstanceOfType(roleService, typeof(RoleService));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.Fail($"RoleService is null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.InnerException);
|
||||||
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task CheckIfNameIsValid_NameNotExists()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var expectedRole = ModelsInit.CreateRole();
|
||||||
|
|
||||||
|
if (_roleService != null)
|
||||||
|
{
|
||||||
|
var valid = await _roleService.CheckIfNameIsValid(expectedRole.Name);
|
||||||
|
Assert.IsTrue(valid);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.Fail($"RoleService is null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.InnerException);
|
||||||
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task CreateRoleAsync_Success()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
CreateRoleRequestData data = new CreateRoleRequestData()
|
||||||
|
{
|
||||||
|
Name = _expectedRole?.Name ?? String.Empty,
|
||||||
|
IsNotEditable = false
|
||||||
|
};
|
||||||
|
|
||||||
|
if (_roleService != null)
|
||||||
|
{
|
||||||
|
var role = await _roleService.CreateRoleAsync(data);
|
||||||
|
Assert.IsInstanceOfType(role, typeof(Role));
|
||||||
|
Assert.IsNotNull(role);
|
||||||
|
Assert.IsTrue(_expectedRole?.Name == role.Name);
|
||||||
|
Assert.IsTrue(_expectedRole?.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 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()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var expectedRole = ModelsInit.CreateRole();
|
||||||
|
if (_roleService != null)
|
||||||
|
{
|
||||||
|
var valid = await _roleService.CheckIfNameIsValid(expectedRole.Name, _role?.Guid ?? String.Empty);
|
||||||
|
Assert.IsTrue(valid);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.Fail($"RoleService is null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.InnerException);
|
||||||
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task CheckIfNameIsValid_NameAlreadyExists()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var expectedRole = ModelsInit.CreateRole();
|
||||||
|
|
||||||
|
if (_roleService != null)
|
||||||
|
{
|
||||||
|
var valid = await _roleService.CheckIfNameIsValid(expectedRole.Name);
|
||||||
|
Assert.IsFalse(valid);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.Fail($"RoleService is null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.InnerException);
|
||||||
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task GetRoleByIdAsync()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (_roleService != null)
|
||||||
|
{
|
||||||
|
var role = await _roleService.GetRoleByIdAsync(_role?.Id ?? 0);
|
||||||
|
Assert.IsNotNull(role);
|
||||||
|
Assert.IsTrue(role.Id == _role?.Id);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.Fail($"RoleService is null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.InnerException);
|
||||||
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task GetRoleByGuidAsync()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (_roleService != null)
|
||||||
|
{
|
||||||
|
var role = await _roleService.GetRoleByGuidAsync(_role?.Guid ?? String.Empty);
|
||||||
|
Assert.IsNotNull(role);
|
||||||
|
Assert.IsTrue(role.Guid == _role?.Guid);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.Fail($"RoleService is null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.InnerException);
|
||||||
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task GetRoleByGuidAsync_CurrentRole()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (_roleService != null)
|
||||||
|
{
|
||||||
|
var role = await _roleService.GetRoleForUser(_role?.Guid);
|
||||||
|
Assert.IsNotNull(role);
|
||||||
|
Assert.IsTrue(role.Guid == _role?.Guid);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.Fail($"RoleService is null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.InnerException);
|
||||||
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task GetRoleByGuidAsync_Default()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (_roleService != null)
|
||||||
|
{
|
||||||
|
CreateRoleRequestData data = new()
|
||||||
|
{
|
||||||
|
Name = "Default",
|
||||||
|
IsNotEditable = true
|
||||||
|
};
|
||||||
|
var roleCreated = await _roleService.CreateRoleAsync(data);
|
||||||
|
var role = await _roleService.GetRoleForUser(String.Empty);
|
||||||
|
Assert.IsNotNull(role);
|
||||||
|
Assert.IsTrue(roleCreated?.Guid == role?.Guid);
|
||||||
|
Assert.IsTrue(role?.Name == "Default");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.Fail($"RoleService is null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.InnerException);
|
||||||
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task GetRoleByGuidAsync_Null()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (_roleService != null)
|
||||||
|
{
|
||||||
|
var role = await _roleService.GetRoleForUser(Guid.NewGuid().ToString());
|
||||||
|
Assert.IsNull(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_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()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (_roleService != null)
|
||||||
|
{
|
||||||
|
var role = await _roleService.GetRoleByGuidAsync(_role?.Guid ?? String.Empty);
|
||||||
|
Assert.IsNotNull(role);
|
||||||
|
var deleted = await _roleService.DeleteRoleAsync(role);
|
||||||
|
Assert.IsTrue(deleted);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.Fail($"RoleService is null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.InnerException);
|
||||||
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
293
MainProject.Tests/Services/UserService_Tests.cs
Normal file
293
MainProject.Tests/Services/UserService_Tests.cs
Normal file
@@ -0,0 +1,293 @@
|
|||||||
|
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;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Tests;
|
||||||
|
|
||||||
|
[TestClass]
|
||||||
|
public class UserService_Tests
|
||||||
|
{
|
||||||
|
private static User _user = ModelsInit.CreateUser();
|
||||||
|
private static UserService _userService = TestUtils.CreateUserService();
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void Inizialize()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var userService = TestUtils.CreateUserService();
|
||||||
|
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}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task GetUserByUsernameAndPassword_Null()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var testString = "test";
|
||||||
|
if (_userService != null)
|
||||||
|
{
|
||||||
|
var user = await _userService.GetUserByUsernameAndPassword(testString, testString);
|
||||||
|
Assert.IsTrue(user == null);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.Fail($"UserService is null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.InnerException);
|
||||||
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task CheckIfEmailIsValid_EmailNotExists()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (_userService != null)
|
||||||
|
{
|
||||||
|
var valid = await _userService.CheckIfEmailIsValid(_user.Email ?? String.Empty);
|
||||||
|
Assert.IsTrue(valid);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.Fail($"UserService is null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.InnerException);
|
||||||
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task CreateUserAsync_Success()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
|
||||||
|
var 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 user = await _userService.CreateUserAsync(data, role);
|
||||||
|
Assert.IsInstanceOfType(user, typeof(User));
|
||||||
|
Assert.IsNotNull(user);
|
||||||
|
Assert.IsTrue(expectedUser.FirstName == user.FirstName);
|
||||||
|
Assert.IsTrue(expectedUser.LastName == user.LastName);
|
||||||
|
Assert.IsTrue(expectedUser.Email == user.Email);
|
||||||
|
Assert.IsTrue(expectedUser.Role?.Name == user.Role?.Name);
|
||||||
|
_user = user;
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.InnerException);
|
||||||
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[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()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
|
||||||
|
if (_userService != null)
|
||||||
|
{
|
||||||
|
var valid = await _userService.CheckIfEmailIsValid(_user.Email ?? String.Empty, _user.Guid ?? String.Empty);
|
||||||
|
Assert.IsTrue(valid);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.Fail($"UserService is null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.InnerException);
|
||||||
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task CheckIfEmailIsValid_EmailAlreadyExists()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
|
||||||
|
if (_userService != null)
|
||||||
|
{
|
||||||
|
var valid = await _userService.CheckIfEmailIsValid(_user.Email ?? String.Empty);
|
||||||
|
Assert.IsFalse(valid);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.Fail($"UserService is null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.InnerException);
|
||||||
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task GetUserByIdAsync()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
|
||||||
|
if (_userService != null)
|
||||||
|
{
|
||||||
|
var user = await _userService.GetUserByIdAsync(_user.Id);
|
||||||
|
Assert.IsNotNull(user);
|
||||||
|
Assert.IsTrue(user.Id == _user?.Id);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.Fail($"UserService is null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.InnerException);
|
||||||
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task GetUserByGuidAsync()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
|
||||||
|
if (_userService != null)
|
||||||
|
{
|
||||||
|
var user = await _userService.GetUserByGuidAsync(_user.Guid ?? String.Empty);
|
||||||
|
Assert.IsNotNull(user);
|
||||||
|
Assert.IsTrue(user.Guid == _user?.Guid);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.Fail($"UserService is null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.InnerException);
|
||||||
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task DeleteUser()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
|
||||||
|
if (_userService != null)
|
||||||
|
{
|
||||||
|
var user = await _userService.GetUserByGuidAsync(_user.Guid ?? String.Empty);
|
||||||
|
Assert.IsNotNull(user);
|
||||||
|
var deleted = await _userService.DeleteUserAsync(user);
|
||||||
|
Assert.IsTrue(deleted);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.Fail($"UserService is null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.InnerException);
|
||||||
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
35
MainProject.Tests/TestsUtils/ModelsInit.cs
Normal file
35
MainProject.Tests/TestsUtils/ModelsInit.cs
Normal 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 = "test-new@email.it",
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,12 +13,30 @@ using Microsoft.Extensions.DependencyInjection;
|
|||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.OpenApi.Models;
|
using Microsoft.OpenApi.Models;
|
||||||
using Microsoft.OpenApi.Interfaces;
|
using Microsoft.OpenApi.Interfaces;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Moq;
|
||||||
|
using BasicDotnetTemplate.MainProject.Core.Database;
|
||||||
|
using BasicDotnetTemplate.MainProject.Services;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Microsoft.AspNetCore.Mvc.Filters;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.AspNetCore.Routing;
|
||||||
|
using Microsoft.AspNetCore.Mvc.Controllers;
|
||||||
|
|
||||||
|
|
||||||
namespace BasicDotnetTemplate.MainProject.Tests;
|
namespace BasicDotnetTemplate.MainProject.Tests;
|
||||||
|
|
||||||
public static class TestUtils
|
public static class TestUtils
|
||||||
{
|
{
|
||||||
|
|
||||||
|
public static AuthorizationFilterContext CreateAuthorizationContext()
|
||||||
|
{
|
||||||
|
var httpContext = new DefaultHttpContext();
|
||||||
|
var actionContext = new ActionContext(httpContext, new RouteData(), new ControllerActionDescriptor());
|
||||||
|
return new AuthorizationFilterContext(actionContext, new List<IFilterMetadata>());
|
||||||
|
}
|
||||||
|
|
||||||
public static IConfiguration CreateConfiguration()
|
public static IConfiguration CreateConfiguration()
|
||||||
{
|
{
|
||||||
WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty<string>());
|
WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty<string>());
|
||||||
@@ -38,6 +56,115 @@ public static class TestUtils
|
|||||||
.AddEnvironmentVariables()
|
.AddEnvironmentVariables()
|
||||||
.Build();
|
.Build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static string GetSqlConnectionString(IConfiguration configuration)
|
||||||
|
{
|
||||||
|
AppSettings _appSettings = new AppSettings();
|
||||||
|
configuration.GetSection("AppSettings").Bind(_appSettings);
|
||||||
|
return _appSettings.DatabaseSettings?.SqlServerConnectionString ?? String.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string GetFakeConnectionString()
|
||||||
|
{
|
||||||
|
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();
|
||||||
|
context.Database.EnsureCreated();
|
||||||
|
return context;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static BaseService CreateBaseService()
|
||||||
|
{
|
||||||
|
IConfiguration configuration = CreateConfiguration();
|
||||||
|
SqlServerContext sqlServerContext = CreateInMemorySqlContext();
|
||||||
|
var httpContextAccessor = new Mock<IHttpContextAccessor>();
|
||||||
|
return new BaseService(httpContextAccessor.Object, configuration, sqlServerContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static AuthService CreateAuthService()
|
||||||
|
{
|
||||||
|
IConfiguration configuration = CreateConfiguration();
|
||||||
|
SqlServerContext sqlServerContext = CreateInMemorySqlContext();
|
||||||
|
var userServiceMock = new Mock<IUserService>();
|
||||||
|
var httpContextAccessor = new Mock<IHttpContextAccessor>();
|
||||||
|
return new AuthService(httpContextAccessor.Object, configuration, sqlServerContext, userServiceMock.Object);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static UserService CreateUserService()
|
||||||
|
{
|
||||||
|
IConfiguration configuration = CreateConfiguration();
|
||||||
|
SqlServerContext sqlServerContext = CreateInMemorySqlContext();
|
||||||
|
var httpContextAccessor = new Mock<IHttpContextAccessor>();
|
||||||
|
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();
|
||||||
|
var optionsBuilder = new DbContextOptionsBuilder<SqlServerContext>();
|
||||||
|
optionsBuilder.UseSqlServer(GetSqlConnectionString(configuration));
|
||||||
|
SqlServerContext sqlServerContext = CreateInMemorySqlContext();
|
||||||
|
var httpContextAccessor = new Mock<IHttpContextAccessor>();
|
||||||
|
return new JwtService(httpContextAccessor.Object, configuration, sqlServerContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static RoleService CreateRoleService()
|
||||||
|
{
|
||||||
|
IConfiguration configuration = CreateConfiguration();
|
||||||
|
var optionsBuilder = new DbContextOptionsBuilder<SqlServerContext>();
|
||||||
|
optionsBuilder.UseSqlServer(GetSqlConnectionString(configuration));
|
||||||
|
SqlServerContext sqlServerContext = CreateInMemorySqlContext();
|
||||||
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
167
MainProject.Tests/Utils/CryptoUtils_Tests.cs
Normal file
167
MainProject.Tests/Utils/CryptoUtils_Tests.cs
Normal file
@@ -0,0 +1,167 @@
|
|||||||
|
using System;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Settings;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using BasicDotnetTemplate.MainProject.Utils;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Tests;
|
||||||
|
|
||||||
|
[TestClass]
|
||||||
|
public class CryptoUtils_Tests
|
||||||
|
{
|
||||||
|
[TestMethod]
|
||||||
|
public void Decrypt_Success()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string encryptedData = "d2ejdI1f4GYpq2kTB1nmeQkZXqR3QSxH8Yqkl7iv7zgfQ13qG/0dUUsreG/WGHWRBE5mVWaV43A=";
|
||||||
|
WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty<string>());
|
||||||
|
AppSettings appSettings = ProgramUtils.AddConfiguration(ref builder, System.AppDomain.CurrentDomain.BaseDirectory + "/JsonData");
|
||||||
|
CryptUtils cryptoUtils = new CryptUtils(appSettings);
|
||||||
|
var decryptedData = cryptoUtils.Decrypt(encryptedData);
|
||||||
|
var isEqual = decryptedData == "ThisIsASuccessfullTest";
|
||||||
|
Assert.IsTrue(isEqual);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.InnerException);
|
||||||
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void Decrypt_Error()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string encryptedData = "d1ejdI1f4GYpq2kTB1nmeQkZXqR3QSxH8Yqkl7iv7zgfQ13qG/0dUUsreG/WGHWRBE5mVWaV43A=";
|
||||||
|
WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty<string>());
|
||||||
|
AppSettings appSettings = ProgramUtils.AddConfiguration(ref builder, System.AppDomain.CurrentDomain.BaseDirectory + "/JsonData");
|
||||||
|
CryptUtils cryptoUtils = new CryptUtils(appSettings);
|
||||||
|
var decryptedData = cryptoUtils.Decrypt(encryptedData);
|
||||||
|
var isEqual = decryptedData == "ThisIsASuccessfullTest";
|
||||||
|
Assert.IsFalse(isEqual);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.InnerException);
|
||||||
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void Decrypt_ArgumentException()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string encryptedData = "d1ejdI1f4GYpq2kTB1nmeQkZXqR3QSxH8Yqkl7iv7zgfQ13qG/0dUUsreG/WGHWRBE5mVWaV43A=";
|
||||||
|
WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty<string>());
|
||||||
|
AppSettings appSettings = ProgramUtils.AddConfiguration(ref builder, System.AppDomain.CurrentDomain.BaseDirectory + "/JsonData", "invalidCryptAppsettings.json");
|
||||||
|
CryptUtils cryptoUtils = new CryptUtils(appSettings);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var decryptedData = cryptoUtils.Decrypt(encryptedData);
|
||||||
|
Assert.Fail($"Expected exception instead of response: {decryptedData}");
|
||||||
|
}
|
||||||
|
catch (ArgumentException argumentException)
|
||||||
|
{
|
||||||
|
Assert.IsInstanceOfType(argumentException, typeof(ArgumentException));
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
Assert.IsInstanceOfType(exception, typeof(ArgumentException));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.InnerException);
|
||||||
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void Decrypt_Empty()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string encryptedData = "WGHWRBE5mVWaV=";
|
||||||
|
WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty<string>());
|
||||||
|
AppSettings appSettings = ProgramUtils.AddConfiguration(ref builder, System.AppDomain.CurrentDomain.BaseDirectory + "/JsonData");
|
||||||
|
CryptUtils cryptoUtils = new CryptUtils(appSettings);
|
||||||
|
var decryptedData = cryptoUtils.Decrypt(encryptedData);
|
||||||
|
var isEqual = decryptedData == String.Empty;
|
||||||
|
Assert.IsTrue(isEqual);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.InnerException);
|
||||||
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void GenerateSalt()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var salt = CryptUtils.GenerateSalt();
|
||||||
|
Assert.IsTrue(!String.IsNullOrEmpty(salt));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.InnerException);
|
||||||
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ComputeHash_Hashed()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var password = "P4ssw0rd@1!";
|
||||||
|
var salt = CryptUtils.GenerateSalt();
|
||||||
|
Assert.IsTrue(!String.IsNullOrEmpty(salt));
|
||||||
|
|
||||||
|
WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty<string>());
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.InnerException);
|
||||||
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void VerifyPassword_True()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var password = "P4ssw0rd@1!";
|
||||||
|
var salt = "Afi7PQYgEL2sPbNyVzduvg==";
|
||||||
|
var hashedPassword = "2lMeySZ9ciH1KtSg1Z7oSJRmJEjHMeDvdaNRcJcGutM=";
|
||||||
|
|
||||||
|
WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty<string>());
|
||||||
|
AppSettings appSettings = ProgramUtils.AddConfiguration(ref builder, System.AppDomain.CurrentDomain.BaseDirectory + "/JsonData");
|
||||||
|
CryptUtils cryptoUtils = new CryptUtils(appSettings);
|
||||||
|
var verified = cryptoUtils.VerifyPassword(password, salt, 0, hashedPassword);
|
||||||
|
Assert.IsTrue(verified);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.InnerException);
|
||||||
|
Assert.Fail($"An exception was thrown: {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}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
76
MainProject.Tests/Utils/JwtTokenUtils_Tests.cs
Normal file
76
MainProject.Tests/Utils/JwtTokenUtils_Tests.cs
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
using System;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Settings;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using BasicDotnetTemplate.MainProject.Utils;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Tests;
|
||||||
|
|
||||||
|
[TestClass]
|
||||||
|
public class JwtTokenUtils_Tests
|
||||||
|
{
|
||||||
|
private static string _guid = "15e4be58-e655-475e-b4b8-a9779b359f57";
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void GenerateToken()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
Assert.IsTrue(!String.IsNullOrEmpty(jwt));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.InnerException);
|
||||||
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ValidateToken()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
var guid = jwtUtils.ValidateToken($"Bearer {jwt}");
|
||||||
|
Assert.IsTrue(_guid == guid);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.InnerException);
|
||||||
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ValidateToken_Empty()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
var guid = jwtUtils.ValidateToken(jwt);
|
||||||
|
Assert.IsTrue(String.IsNullOrEmpty(guid));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.InnerException);
|
||||||
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,11 +1,3 @@
|
|||||||
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.Settings;
|
using BasicDotnetTemplate.MainProject.Models.Settings;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using BasicDotnetTemplate.MainProject.Utils;
|
using BasicDotnetTemplate.MainProject.Utils;
|
||||||
@@ -52,7 +44,7 @@ public class ProgramUtils_Tests
|
|||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine(ex.InnerException);
|
Console.WriteLine(ex.InnerException);
|
||||||
Assert.Fail($"An exception was thrown: {ex.Message}");
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,7 +80,7 @@ public class ProgramUtils_Tests
|
|||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine(ex.InnerException);
|
Console.WriteLine(ex.InnerException);
|
||||||
Assert.Fail($"An exception was thrown: {ex.Message}");
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,7 +97,7 @@ public class ProgramUtils_Tests
|
|||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine(ex.InnerException);
|
Console.WriteLine(ex.InnerException);
|
||||||
Assert.Fail($"An exception was thrown: {ex.Message}");
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,7 +126,7 @@ public class ProgramUtils_Tests
|
|||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine(ex.InnerException);
|
Console.WriteLine(ex.InnerException);
|
||||||
Assert.Fail($"An exception was thrown: {ex.Message}");
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -168,7 +160,7 @@ public class ProgramUtils_Tests
|
|||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine(ex.InnerException);
|
Console.WriteLine(ex.InnerException);
|
||||||
Assert.Fail($"An exception was thrown: {ex.Message}");
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -204,7 +196,7 @@ public class ProgramUtils_Tests
|
|||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine(ex.InnerException);
|
Console.WriteLine(ex.InnerException);
|
||||||
Assert.Fail($"An exception was thrown: {ex.Message}");
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -228,7 +220,7 @@ public class ProgramUtils_Tests
|
|||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine(ex.InnerException);
|
Console.WriteLine(ex.InnerException);
|
||||||
Assert.Fail($"An exception was thrown: {ex.Message}");
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -248,7 +240,7 @@ public class ProgramUtils_Tests
|
|||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine(ex.InnerException);
|
Console.WriteLine(ex.InnerException);
|
||||||
Assert.Fail($"An exception was thrown: {ex.Message}");
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -268,7 +260,7 @@ public class ProgramUtils_Tests
|
|||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine(ex.InnerException);
|
Console.WriteLine(ex.InnerException);
|
||||||
Assert.Fail($"An exception was thrown: {ex.Message}");
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -288,7 +280,7 @@ public class ProgramUtils_Tests
|
|||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine(ex.InnerException);
|
Console.WriteLine(ex.InnerException);
|
||||||
Assert.Fail($"An exception was thrown: {ex.Message}");
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -299,7 +291,7 @@ public class ProgramUtils_Tests
|
|||||||
{
|
{
|
||||||
DatabaseSettings expectedDbSettings = new DatabaseSettings()
|
DatabaseSettings expectedDbSettings = new DatabaseSettings()
|
||||||
{
|
{
|
||||||
SqlServerConnectionString = ""
|
SqlServerConnectionString = null
|
||||||
};
|
};
|
||||||
|
|
||||||
WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty<string>());
|
WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty<string>());
|
||||||
@@ -307,13 +299,12 @@ public class ProgramUtils_Tests
|
|||||||
ProgramUtils.AddDbContext(ref builder, realAppSettings);
|
ProgramUtils.AddDbContext(ref builder, realAppSettings);
|
||||||
|
|
||||||
var areEquals = expectedDbSettings.SqlServerConnectionString == realAppSettings.DatabaseSettings?.SqlServerConnectionString;
|
var areEquals = expectedDbSettings.SqlServerConnectionString == realAppSettings.DatabaseSettings?.SqlServerConnectionString;
|
||||||
Console.WriteLine(realAppSettings.DatabaseSettings?.SqlServerConnectionString);
|
|
||||||
Assert.IsTrue(areEquals);
|
Assert.IsTrue(areEquals);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine(ex.InnerException);
|
Console.WriteLine(ex.InnerException);
|
||||||
Assert.Fail($"An exception was thrown: {ex.Message}");
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
32
MainProject/Config/permissions.json
Normal file
32
MainProject/Config/permissions.json
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
{
|
||||||
|
"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": [] }
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
]
|
||||||
|
}
|
||||||
68
MainProject/Controllers/AuthController.cs
Normal file
68
MainProject/Controllers/AuthController.cs
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using BasicDotnetTemplate.MainProject.Core.Attributes;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Request.Auth;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Response;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Response.Auth;
|
||||||
|
using BasicDotnetTemplate.MainProject.Services;
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Controllers
|
||||||
|
{
|
||||||
|
[Route("[controller]")]
|
||||||
|
public class AuthController : BaseController
|
||||||
|
{
|
||||||
|
private readonly IAuthService _authService;
|
||||||
|
public AuthController(
|
||||||
|
IConfiguration configuration,
|
||||||
|
IAuthService authService
|
||||||
|
) : base(configuration)
|
||||||
|
{
|
||||||
|
this._authService = authService;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("authenticate")]
|
||||||
|
[ProducesResponseType<AuthenticateResponse>(StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status404NotFound)]
|
||||||
|
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status500InternalServerError)]
|
||||||
|
public async Task<IActionResult> AuthenticateAsync([FromBody] AuthenticateRequest request)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (!ModelState.IsValid)
|
||||||
|
{
|
||||||
|
return BadRequest(_requestNotWellFormed);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
request == null ||
|
||||||
|
request.Data == null ||
|
||||||
|
String.IsNullOrEmpty(request.Data.Email) ||
|
||||||
|
String.IsNullOrEmpty(request.Data.Password)
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return BadRequest(_requestNotWellFormed);
|
||||||
|
}
|
||||||
|
var data = await this._authService.AuthenticateAsync(request.Data);
|
||||||
|
|
||||||
|
if (data == null)
|
||||||
|
{
|
||||||
|
return NotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
return Success(String.Empty, data);
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
var message = this._somethingWentWrong;
|
||||||
|
if (!String.IsNullOrEmpty(exception.Message))
|
||||||
|
{
|
||||||
|
message += $". {exception.Message}";
|
||||||
|
}
|
||||||
|
return InternalServerError(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,13 +2,18 @@ using System.Net;
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using BasicDotnetTemplate.MainProject.Models.Api.Response;
|
using BasicDotnetTemplate.MainProject.Models.Api.Response;
|
||||||
using BasicDotnetTemplate.MainProject.Models.Settings;
|
using BasicDotnetTemplate.MainProject.Models.Settings;
|
||||||
|
using AutoMapper;
|
||||||
|
using BasicDotnetTemplate.MainProject.Core.Middlewares;
|
||||||
|
|
||||||
namespace BasicDotnetTemplate.MainProject.Controllers
|
namespace BasicDotnetTemplate.MainProject.Controllers
|
||||||
{
|
{
|
||||||
public abstract class BaseController : ControllerBase
|
public abstract class BaseController : ControllerBase
|
||||||
{
|
{
|
||||||
|
protected readonly IMapper? _mapper;
|
||||||
protected readonly IConfiguration _configuration;
|
protected readonly IConfiguration _configuration;
|
||||||
protected readonly AppSettings _appSettings;
|
protected readonly AppSettings _appSettings;
|
||||||
|
protected readonly string _requestNotWellFormed = "Request is not well formed";
|
||||||
|
protected readonly string _somethingWentWrong = "Something went wrong";
|
||||||
|
|
||||||
protected BaseController(
|
protected BaseController(
|
||||||
IConfiguration configuration
|
IConfiguration configuration
|
||||||
@@ -17,12 +22,19 @@ namespace BasicDotnetTemplate.MainProject.Controllers
|
|||||||
_configuration = configuration;
|
_configuration = configuration;
|
||||||
_appSettings = new AppSettings();
|
_appSettings = new AppSettings();
|
||||||
_configuration.GetSection("AppSettings").Bind(_appSettings);
|
_configuration.GetSection("AppSettings").Bind(_appSettings);
|
||||||
|
|
||||||
|
var config = new MapperConfiguration(cfg =>
|
||||||
|
{
|
||||||
|
cfg.AddProfile<AutoMapperConfiguration>();
|
||||||
|
});
|
||||||
|
|
||||||
|
_mapper = config.CreateMapper();
|
||||||
}
|
}
|
||||||
|
|
||||||
#nullable enable
|
#nullable enable
|
||||||
private static BaseResponse CreateResponse(HttpStatusCode status, string message, object? data = null)
|
private static BaseResponse<T> CreateResponse<T>(HttpStatusCode status, string message, T? data)
|
||||||
{
|
{
|
||||||
return new BaseResponse((int)status, message, data);
|
return new BaseResponse<T>((int)status, message, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected new IActionResult Created(string message, object? data = null)
|
protected new IActionResult Created(string message, object? data = null)
|
||||||
@@ -37,6 +49,12 @@ namespace BasicDotnetTemplate.MainProject.Controllers
|
|||||||
return StatusCode((int)HttpStatusCode.OK, CreateResponse(HttpStatusCode.OK, message, data));
|
return StatusCode((int)HttpStatusCode.OK, CreateResponse(HttpStatusCode.OK, message, data));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected IActionResult NotModified(string message, object? data = null)
|
||||||
|
{
|
||||||
|
message = String.IsNullOrEmpty(message) ? "Not modified" : message;
|
||||||
|
return StatusCode((int)HttpStatusCode.NotModified, CreateResponse(HttpStatusCode.NotModified, message, data));
|
||||||
|
}
|
||||||
|
|
||||||
protected IActionResult NotFound(string message, object? data = null)
|
protected IActionResult NotFound(string message, object? data = null)
|
||||||
{
|
{
|
||||||
message = String.IsNullOrEmpty(message) ? "Not found" : message;
|
message = String.IsNullOrEmpty(message) ? "Not found" : message;
|
||||||
@@ -52,7 +70,7 @@ namespace BasicDotnetTemplate.MainProject.Controllers
|
|||||||
protected IActionResult InternalServerError(string message)
|
protected IActionResult InternalServerError(string message)
|
||||||
{
|
{
|
||||||
message = String.IsNullOrEmpty(message) ? "Internal server error" : message;
|
message = String.IsNullOrEmpty(message) ? "Internal server error" : message;
|
||||||
return StatusCode((int)HttpStatusCode.InternalServerError, CreateResponse(HttpStatusCode.InternalServerError, message));
|
return StatusCode((int)HttpStatusCode.InternalServerError, CreateResponse(HttpStatusCode.InternalServerError, message, new object()));
|
||||||
}
|
}
|
||||||
|
|
||||||
#nullable disable
|
#nullable disable
|
||||||
|
|||||||
227
MainProject/Controllers/RoleController.cs
Normal file
227
MainProject/Controllers/RoleController.cs
Normal file
@@ -0,0 +1,227 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using BasicDotnetTemplate.MainProject.Core.Attributes;
|
||||||
|
using BasicDotnetTemplate.MainProject.Services;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Request.Role;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Response;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Response.Role;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Common.Role;
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Controllers
|
||||||
|
{
|
||||||
|
[Route("[controller]")]
|
||||||
|
public class RoleController : BaseController
|
||||||
|
{
|
||||||
|
private readonly IRoleService _roleService;
|
||||||
|
public RoleController(
|
||||||
|
IConfiguration configuration,
|
||||||
|
IRoleService roleService
|
||||||
|
) : base(configuration)
|
||||||
|
{
|
||||||
|
this._roleService = roleService;
|
||||||
|
}
|
||||||
|
|
||||||
|
[JwtAuthorization()]
|
||||||
|
[HttpGet("get/{guid}")]
|
||||||
|
[ProducesResponseType<GetRoleResponse>(StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status404NotFound)]
|
||||||
|
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status500InternalServerError)]
|
||||||
|
public async Task<IActionResult> GetRoleByGuidAsync(string guid)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (!ModelState.IsValid)
|
||||||
|
{
|
||||||
|
return BadRequest(_requestNotWellFormed);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (String.IsNullOrEmpty(guid))
|
||||||
|
{
|
||||||
|
return BadRequest(_requestNotWellFormed);
|
||||||
|
}
|
||||||
|
var role = await this._roleService.GetRoleByGuidAsync(guid);
|
||||||
|
|
||||||
|
if (role == null || String.IsNullOrEmpty(role.Guid))
|
||||||
|
{
|
||||||
|
return NotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
var roleDto = _mapper?.Map<RoleDto>(role);
|
||||||
|
|
||||||
|
return Success(String.Empty, roleDto);
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
var message = this._somethingWentWrong;
|
||||||
|
if (!String.IsNullOrEmpty(exception.Message))
|
||||||
|
{
|
||||||
|
message += $". {exception.Message}";
|
||||||
|
}
|
||||||
|
return InternalServerError(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
[JwtAuthorization()]
|
||||||
|
[HttpPost("create")]
|
||||||
|
[ProducesResponseType<GetRoleResponse>(StatusCodes.Status201Created)]
|
||||||
|
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status500InternalServerError)]
|
||||||
|
public async Task<IActionResult> CreateRoleAsync([FromBody] CreateRoleRequest request)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (!ModelState.IsValid)
|
||||||
|
{
|
||||||
|
return BadRequest(_requestNotWellFormed);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request == null || request.Data == null || String.IsNullOrEmpty(request.Data.Name)
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return BadRequest(_requestNotWellFormed);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (await this._roleService.CheckIfNameIsValid(request.Data.Name))
|
||||||
|
{
|
||||||
|
var role = await this._roleService.CreateRoleAsync(request.Data);
|
||||||
|
|
||||||
|
if (role == null || String.IsNullOrEmpty(role.Guid))
|
||||||
|
{
|
||||||
|
return BadRequest("Not created");
|
||||||
|
}
|
||||||
|
|
||||||
|
var roleDto = _mapper?.Map<RoleDto>(role);
|
||||||
|
|
||||||
|
return Success(String.Empty, roleDto);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return BadRequest("Invalid name");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
var message = this._somethingWentWrong;
|
||||||
|
if (!String.IsNullOrEmpty(exception.Message))
|
||||||
|
{
|
||||||
|
message += $". {exception.Message}";
|
||||||
|
}
|
||||||
|
return InternalServerError(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
[JwtAuthorization()]
|
||||||
|
[HttpPut("update/{guid}")]
|
||||||
|
[ProducesResponseType<GetRoleResponse>(StatusCodes.Status201Created)]
|
||||||
|
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status500InternalServerError)]
|
||||||
|
public async Task<IActionResult> UpdateRoleAsync([FromBody] CreateRoleRequest request, string guid)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (!ModelState.IsValid)
|
||||||
|
{
|
||||||
|
return BadRequest(_requestNotWellFormed);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
request == null ||
|
||||||
|
request.Data == null ||
|
||||||
|
String.IsNullOrEmpty(request.Data.Name) ||
|
||||||
|
String.IsNullOrEmpty(guid)
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return BadRequest(_requestNotWellFormed);
|
||||||
|
}
|
||||||
|
|
||||||
|
var role = await this._roleService.GetRoleByGuidAsync(guid);
|
||||||
|
|
||||||
|
if (role == null || String.IsNullOrEmpty(role.Guid))
|
||||||
|
{
|
||||||
|
return NotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (role.IsNotEditable)
|
||||||
|
{
|
||||||
|
return BadRequest("This role is not editable");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
await this._roleService.CheckIfNameIsValid(request.Data.Name) ||
|
||||||
|
await this._roleService.CheckIfNameIsValid(request.Data.Name, guid)
|
||||||
|
)
|
||||||
|
{
|
||||||
|
role = await this._roleService.UpdateRoleAsync(request.Data, role);
|
||||||
|
|
||||||
|
var roleDto = _mapper?.Map<RoleDto>(role);
|
||||||
|
|
||||||
|
return Success(String.Empty, roleDto);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return BadRequest("Invalid name");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
var message = this._somethingWentWrong;
|
||||||
|
if (!String.IsNullOrEmpty(exception.Message))
|
||||||
|
{
|
||||||
|
message += $". {exception.Message}";
|
||||||
|
}
|
||||||
|
return InternalServerError(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
[JwtAuthorization()]
|
||||||
|
[HttpDelete("{guid}")]
|
||||||
|
[ProducesResponseType<GetRoleResponse>(StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status404NotFound)]
|
||||||
|
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status500InternalServerError)]
|
||||||
|
public async Task<IActionResult> DeleteRoleByGuidAsync(string guid)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (!ModelState.IsValid)
|
||||||
|
{
|
||||||
|
return BadRequest(_requestNotWellFormed);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (String.IsNullOrEmpty(guid))
|
||||||
|
{
|
||||||
|
return BadRequest(_requestNotWellFormed);
|
||||||
|
}
|
||||||
|
var role = await this._roleService.GetRoleByGuidAsync(guid);
|
||||||
|
|
||||||
|
if (role == null || String.IsNullOrEmpty(role.Guid))
|
||||||
|
{
|
||||||
|
return NotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
await this._roleService.DeleteRoleAsync(role);
|
||||||
|
|
||||||
|
return Success(String.Empty);
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
var message = this._somethingWentWrong;
|
||||||
|
if (!String.IsNullOrEmpty(exception.Message))
|
||||||
|
{
|
||||||
|
message += $". {exception.Message}";
|
||||||
|
}
|
||||||
|
return InternalServerError(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
134
MainProject/Controllers/UserController.cs
Normal file
134
MainProject/Controllers/UserController.cs
Normal file
@@ -0,0 +1,134 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using BasicDotnetTemplate.MainProject.Core.Attributes;
|
||||||
|
using BasicDotnetTemplate.MainProject.Services;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Request.User;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Response;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Response.User;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Common.User;
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Controllers
|
||||||
|
{
|
||||||
|
[Route("[controller]")]
|
||||||
|
public class UserController : BaseController
|
||||||
|
{
|
||||||
|
private readonly IUserService _userService;
|
||||||
|
private readonly IRoleService _roleService;
|
||||||
|
public UserController(
|
||||||
|
IConfiguration configuration,
|
||||||
|
IUserService userService,
|
||||||
|
IRoleService roleService
|
||||||
|
) : base(configuration)
|
||||||
|
{
|
||||||
|
this._userService = userService;
|
||||||
|
this._roleService = roleService;
|
||||||
|
}
|
||||||
|
|
||||||
|
[JwtAuthorization()]
|
||||||
|
[HttpGet("get/{guid}")]
|
||||||
|
[ProducesResponseType<GetUserResponse>(StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status404NotFound)]
|
||||||
|
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status500InternalServerError)]
|
||||||
|
public async Task<IActionResult> GetUserByGuidAsync(string guid)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (!ModelState.IsValid)
|
||||||
|
{
|
||||||
|
return BadRequest(_requestNotWellFormed);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (String.IsNullOrEmpty(guid))
|
||||||
|
{
|
||||||
|
return BadRequest(_requestNotWellFormed);
|
||||||
|
}
|
||||||
|
var user = await this._userService.GetUserByGuidAsync(guid);
|
||||||
|
|
||||||
|
if (user == null || String.IsNullOrEmpty(user.Guid))
|
||||||
|
{
|
||||||
|
return NotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
var userDto = _mapper?.Map<UserDto>(user);
|
||||||
|
|
||||||
|
return Success(String.Empty, userDto);
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
var message = this._somethingWentWrong;
|
||||||
|
if (!String.IsNullOrEmpty(exception.Message))
|
||||||
|
{
|
||||||
|
message += $". {exception.Message}";
|
||||||
|
}
|
||||||
|
return InternalServerError(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
[JwtAuthorization()]
|
||||||
|
[HttpPost("create")]
|
||||||
|
[ProducesResponseType<GetUserResponse>(StatusCodes.Status201Created)]
|
||||||
|
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType<BaseResponse<object>>(StatusCodes.Status500InternalServerError)]
|
||||||
|
public async Task<IActionResult> CreateUserAsync([FromBody] CreateUserRequest request)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (!ModelState.IsValid)
|
||||||
|
{
|
||||||
|
return BadRequest(_requestNotWellFormed);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request == null || request.Data == null ||
|
||||||
|
String.IsNullOrEmpty(request.Data.FirstName) ||
|
||||||
|
String.IsNullOrEmpty(request.Data.LastName) ||
|
||||||
|
String.IsNullOrEmpty(request.Data.Email) ||
|
||||||
|
String.IsNullOrEmpty(request.Data.Password)
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return BadRequest(_requestNotWellFormed);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (await this._userService.CheckIfEmailIsValid(request.Data.Email))
|
||||||
|
{
|
||||||
|
var role = await this._roleService.GetRoleForUser(request.Data.RoleGuid);
|
||||||
|
if (role == null)
|
||||||
|
{
|
||||||
|
return BadRequest("Role not found");
|
||||||
|
}
|
||||||
|
|
||||||
|
var user = await this._userService.CreateUserAsync(request.Data, role);
|
||||||
|
|
||||||
|
if (user == null || String.IsNullOrEmpty(user.Guid))
|
||||||
|
{
|
||||||
|
return BadRequest("Not created");
|
||||||
|
}
|
||||||
|
|
||||||
|
var userDto = _mapper?.Map<UserDto>(user);
|
||||||
|
|
||||||
|
return Success(String.Empty, userDto);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return BadRequest("Invalid email");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
var message = this._somethingWentWrong;
|
||||||
|
if (!String.IsNullOrEmpty(exception.Message))
|
||||||
|
{
|
||||||
|
message += $". {exception.Message}";
|
||||||
|
}
|
||||||
|
return InternalServerError(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
85
MainProject/Core/Attributes/JwtAuthorizationAttribute .cs
Normal file
85
MainProject/Core/Attributes/JwtAuthorizationAttribute .cs
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.AspNetCore.Mvc.Filters;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.IdentityModel.Tokens;
|
||||||
|
using System;
|
||||||
|
using System.IdentityModel.Tokens.Jwt;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Settings;
|
||||||
|
using BasicDotnetTemplate.MainProject.Services;
|
||||||
|
using DatabaseSqlServer = BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||||
|
using BasicDotnetTemplate.MainProject.Utils;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Common.User;
|
||||||
|
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Core.Attributes
|
||||||
|
{
|
||||||
|
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
|
||||||
|
public class JwtAuthorizationAttribute : Attribute, IAuthorizationFilter
|
||||||
|
{
|
||||||
|
public JwtAuthorizationAttribute(
|
||||||
|
)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void Unauthorized(AuthorizationFilterContext context)
|
||||||
|
{
|
||||||
|
context.Result = new UnauthorizedResult();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnAuthorization(AuthorizationFilterContext context)
|
||||||
|
{
|
||||||
|
// If [AllowAnonymous], skip
|
||||||
|
if (context.ActionDescriptor.EndpointMetadata.Any(em => em is AllowAnonymousAttribute))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
string? userGuidFromToken = null;
|
||||||
|
|
||||||
|
var configuration = context.HttpContext.RequestServices.GetRequiredService<IConfiguration>();
|
||||||
|
var appSettings = new AppSettings();
|
||||||
|
configuration.GetSection("AppSettings").Bind(appSettings);
|
||||||
|
string? headerAuthorization = context.HttpContext.Request.Headers.Authorization.FirstOrDefault();
|
||||||
|
AuthenticatedUser? userContext = context.HttpContext.Items["User"] != null ? (AuthenticatedUser?)context.HttpContext.Items["User"] : null;
|
||||||
|
|
||||||
|
if (userContext == null)
|
||||||
|
{
|
||||||
|
Unauthorized(context);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (!String.IsNullOrEmpty(headerAuthorization))
|
||||||
|
{
|
||||||
|
userGuidFromToken = JwtAuthorizationAttribute.ValidateToken(headerAuthorization!, appSettings);
|
||||||
|
if (String.IsNullOrEmpty(userGuidFromToken))
|
||||||
|
{
|
||||||
|
Unauthorized(context);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (userContext!.Guid != userGuidFromToken)
|
||||||
|
{
|
||||||
|
Unauthorized(context);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Unauthorized(context);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string? ValidateToken(string headerAuthorization, AppSettings appSettings)
|
||||||
|
{
|
||||||
|
JwtTokenUtils _jwtTokenUtils = new(appSettings);
|
||||||
|
return _jwtTokenUtils.ValidateToken(headerAuthorization);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,13 +6,76 @@ namespace BasicDotnetTemplate.MainProject.Core.Database
|
|||||||
{
|
{
|
||||||
public class SqlServerContext : DbContext
|
public class SqlServerContext : DbContext
|
||||||
{
|
{
|
||||||
|
private const string _isDeletedFalse = "[IsDeleted] = 0";
|
||||||
|
private const string _isEnabled = "[Enabled] = 1";
|
||||||
|
|
||||||
public SqlServerContext(DbContextOptions<SqlServerContext> options)
|
public SqlServerContext(DbContextOptions<SqlServerContext> options)
|
||||||
: base(options)
|
: base(options)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public DbSet<PermissionModule> PermissionModules { get; set; }
|
||||||
|
public DbSet<PermissionOperation> PermissionOperations { get; set; }
|
||||||
|
public DbSet<PermissionSystem> PermissionSystems { get; set; }
|
||||||
|
public DbSet<PermissionSystemModule> PermissionSystemModules { get; set; }
|
||||||
|
public DbSet<PermissionSystemModuleOperation> PermissionSystemModuleOperations { get; set; }
|
||||||
|
public DbSet<RolePermissionSystemModuleOperation> RolePermissionSystemModuleOperations { get; set; }
|
||||||
|
public DbSet<Role> Roles { get; set; }
|
||||||
public DbSet<User> Users { get; set; }
|
public DbSet<User> Users { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||||
|
{
|
||||||
|
#region "INDEXES"
|
||||||
|
// Indexes
|
||||||
|
|
||||||
|
modelBuilder.Entity<User>()
|
||||||
|
.HasIndex(x => x.Email, "IX_Email");
|
||||||
|
|
||||||
|
modelBuilder.Entity<User>()
|
||||||
|
.HasIndex(x => new { x.IsDeleted, x.Guid }, "IX_IsDeleted_Guid")
|
||||||
|
.HasFilter(_isDeletedFalse);
|
||||||
|
|
||||||
|
modelBuilder.Entity<Role>()
|
||||||
|
.HasIndex(x => new { x.IsDeleted, x.Guid }, "IX_IsDeleted_Guid")
|
||||||
|
.HasFilter(_isDeletedFalse);
|
||||||
|
|
||||||
|
modelBuilder.Entity<PermissionSystem>()
|
||||||
|
.HasIndex(x => new { x.IsDeleted }, "IX_IsDeleted")
|
||||||
|
.HasFilter(_isDeletedFalse);
|
||||||
|
|
||||||
|
modelBuilder.Entity<PermissionSystem>()
|
||||||
|
.HasIndex(x => new { x.Enabled }, "IX_Enabled")
|
||||||
|
.HasFilter(_isEnabled);
|
||||||
|
|
||||||
|
modelBuilder.Entity<PermissionSystem>()
|
||||||
|
.HasIndex(x => new { x.IsDeleted, x.Name, x.Enabled }, "IX_IsDeleted_Name_Enabled")
|
||||||
|
.HasFilter(_isEnabled)
|
||||||
|
.HasFilter(_isDeletedFalse);
|
||||||
|
|
||||||
|
modelBuilder.Entity<PermissionModule>()
|
||||||
|
.HasIndex(x => new { x.IsDeleted }, "IX_IsDeleted")
|
||||||
|
.HasFilter(_isDeletedFalse);
|
||||||
|
|
||||||
|
modelBuilder.Entity<PermissionModule>()
|
||||||
|
.HasIndex(x => new { x.Enabled }, "IX_Enabled")
|
||||||
|
.HasFilter(_isEnabled);
|
||||||
|
|
||||||
|
modelBuilder.Entity<PermissionModule>()
|
||||||
|
.HasIndex(x => new { x.IsDeleted, x.Name, x.Enabled }, "IX_IsDeleted_Name_Enabled")
|
||||||
|
.HasFilter(_isEnabled)
|
||||||
|
.HasFilter(_isDeletedFalse);
|
||||||
|
|
||||||
|
modelBuilder.Entity<PermissionOperation>()
|
||||||
|
.HasIndex(x => new { x.IsDeleted, x.Name }, "IX_IsDeleted_Name");
|
||||||
|
|
||||||
|
modelBuilder.Entity<PermissionSystemModuleOperation>()
|
||||||
|
.HasIndex(x => new { x.IsDeleted, x.Enabled, x.Guid }, "IX_IsDeleted_Enabled_Guid");
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
16
MainProject/Core/Middlewares/AutoMapperConfiguration.cs
Normal file
16
MainProject/Core/Middlewares/AutoMapperConfiguration.cs
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Common.User;
|
||||||
|
using SqlServerDatabase = BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||||
|
using AutoMapper;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Common.Role;
|
||||||
|
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Core.Middlewares;
|
||||||
|
public class AutoMapperConfiguration : Profile
|
||||||
|
{
|
||||||
|
public AutoMapperConfiguration()
|
||||||
|
{
|
||||||
|
CreateMap<SqlServerDatabase.Role, RoleDto>();
|
||||||
|
CreateMap<SqlServerDatabase.User, UserDto>();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,43 +2,49 @@
|
|||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
|
<Configuration>Debug</Configuration>
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="coverlet.msbuild" Version="6.0.2">
|
<PackageReference Include="AutoMapper" Version="14.0.0" />
|
||||||
|
<PackageReference Include="coverlet.msbuild" Version="6.0.4">
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
<PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
|
<PackageReference Include="Microsoft.AspNetCore" Version="2.3.0" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.1" />
|
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.16" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="9.0.1" />
|
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.16" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.8">
|
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.5" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="9.0.5" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.5">
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.8" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="9.0.5" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.8">
|
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.5" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.5">
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.5" />
|
||||||
<PackageReference Include="MongoDB.Driver" Version="2.28.0" />
|
<PackageReference Include="Microsoft.Identity.Web" Version="3.9.2" />
|
||||||
<PackageReference Include="MongoDB.EntityFrameworkCore" Version="8.1.0" />
|
<PackageReference Include="MongoDB.Driver" Version="3.4.0" />
|
||||||
|
<PackageReference Include="MongoDB.EntityFrameworkCore" Version="9.0.0" />
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||||
<PackageReference Include="NLog" Version="5.2.8" />
|
<PackageReference Include="NLog" Version="5.4.0" />
|
||||||
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.8" />
|
<PackageReference Include="NLog.Extensions.Logging" Version="5.4.0" />
|
||||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.3" />
|
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="8.1.2" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.5.0" />
|
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="8.1.2" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore.Filters" Version="8.0.0" />
|
<PackageReference Include="Swashbuckle.AspNetCore.Filters" Version="8.0.3" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore.Filters.Abstractions" Version="8.0.0" />
|
<PackageReference Include="Swashbuckle.AspNetCore.Filters.Abstractions" Version="8.0.3" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="6.5.0" />
|
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="8.1.2" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="6.5.0" />
|
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="8.1.2" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.5.0" />
|
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="8.1.2" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.5.0" />
|
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="8.1.2" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
154
MainProject/Migrations/20250311195750_AlterTableUser.Designer.cs
generated
Normal file
154
MainProject/Migrations/20250311195750_AlterTableUser.Designer.cs
generated
Normal file
@@ -0,0 +1,154 @@
|
|||||||
|
// <auto-generated />
|
||||||
|
using System;
|
||||||
|
using BasicDotnetTemplate.MainProject.Core.Database;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||||
|
using Microsoft.EntityFrameworkCore.Metadata;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace MainProject.Migrations
|
||||||
|
{
|
||||||
|
[DbContext(typeof(SqlServerContext))]
|
||||||
|
[Migration("20250311195750_AlterTableUser")]
|
||||||
|
partial class AlterTableUser
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||||
|
{
|
||||||
|
#pragma warning disable 612, 618
|
||||||
|
modelBuilder
|
||||||
|
.HasAnnotation("ProductVersion", "9.0.2")
|
||||||
|
.HasAnnotation("Relational:MaxIdentifierLength", 128);
|
||||||
|
|
||||||
|
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
|
||||||
|
|
||||||
|
modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.Role", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||||
|
|
||||||
|
b.Property<DateTime>("CreationTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int>("CreationUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<DateTime>("DeletionTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int>("DeletionUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<string>("Guid")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<bool>("IsDeleted")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<string>("Name")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<DateTime>("UpdateTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int>("UpdateUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.ToTable("Role");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.User", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||||
|
|
||||||
|
b.Property<DateTime>("CreationTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int>("CreationUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<DateTime>("DeletionTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int>("DeletionUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<string>("Email")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<string>("FirstName")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<string>("Guid")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<bool>("IsDeleted")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<string>("LastName")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<string>("Password")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<string>("PasswordHash")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<string>("PasswordSalt")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<int>("RoleId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<DateTime>("UpdateTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int>("UpdateUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<string>("Username")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("RoleId");
|
||||||
|
|
||||||
|
b.ToTable("Users");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.User", b =>
|
||||||
|
{
|
||||||
|
b.HasOne("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.Role", "Role")
|
||||||
|
.WithMany()
|
||||||
|
.HasForeignKey("RoleId")
|
||||||
|
.OnDelete(DeleteBehavior.Cascade)
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
b.Navigation("Role");
|
||||||
|
});
|
||||||
|
#pragma warning restore 612, 618
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
84
MainProject/Migrations/20250311195750_AlterTableUser.cs
Normal file
84
MainProject/Migrations/20250311195750_AlterTableUser.cs
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace MainProject.Migrations
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
public partial class AlterTableUser : Migration
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.AddColumn<string>(
|
||||||
|
name: "Guid",
|
||||||
|
table: "Users",
|
||||||
|
type: "nvarchar(max)",
|
||||||
|
nullable: false,
|
||||||
|
defaultValue: "");
|
||||||
|
|
||||||
|
migrationBuilder.AddColumn<bool>(
|
||||||
|
name: "IsDeleted",
|
||||||
|
table: "Users",
|
||||||
|
type: "bit",
|
||||||
|
nullable: false,
|
||||||
|
defaultValue: false);
|
||||||
|
|
||||||
|
migrationBuilder.AddColumn<string>(
|
||||||
|
name: "Password",
|
||||||
|
table: "Users",
|
||||||
|
type: "nvarchar(max)",
|
||||||
|
nullable: false,
|
||||||
|
defaultValue: "");
|
||||||
|
|
||||||
|
migrationBuilder.AddColumn<string>(
|
||||||
|
name: "PasswordSalt",
|
||||||
|
table: "Users",
|
||||||
|
type: "nvarchar(max)",
|
||||||
|
nullable: false,
|
||||||
|
defaultValue: "");
|
||||||
|
|
||||||
|
migrationBuilder.AddColumn<string>(
|
||||||
|
name: "Guid",
|
||||||
|
table: "Role",
|
||||||
|
type: "nvarchar(max)",
|
||||||
|
nullable: false,
|
||||||
|
defaultValue: "");
|
||||||
|
|
||||||
|
migrationBuilder.AddColumn<bool>(
|
||||||
|
name: "IsDeleted",
|
||||||
|
table: "Role",
|
||||||
|
type: "bit",
|
||||||
|
nullable: false,
|
||||||
|
defaultValue: false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.DropColumn(
|
||||||
|
name: "Guid",
|
||||||
|
table: "Users");
|
||||||
|
|
||||||
|
migrationBuilder.DropColumn(
|
||||||
|
name: "IsDeleted",
|
||||||
|
table: "Users");
|
||||||
|
|
||||||
|
migrationBuilder.DropColumn(
|
||||||
|
name: "Password",
|
||||||
|
table: "Users");
|
||||||
|
|
||||||
|
migrationBuilder.DropColumn(
|
||||||
|
name: "PasswordSalt",
|
||||||
|
table: "Users");
|
||||||
|
|
||||||
|
migrationBuilder.DropColumn(
|
||||||
|
name: "Guid",
|
||||||
|
table: "Role");
|
||||||
|
|
||||||
|
migrationBuilder.DropColumn(
|
||||||
|
name: "IsDeleted",
|
||||||
|
table: "Role");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
170
MainProject/Migrations/20250312234517_AlterTableUserMaxLengthIndexes.Designer.cs
generated
Normal file
170
MainProject/Migrations/20250312234517_AlterTableUserMaxLengthIndexes.Designer.cs
generated
Normal file
@@ -0,0 +1,170 @@
|
|||||||
|
// <auto-generated />
|
||||||
|
using System;
|
||||||
|
using BasicDotnetTemplate.MainProject.Core.Database;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||||
|
using Microsoft.EntityFrameworkCore.Metadata;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace MainProject.Migrations
|
||||||
|
{
|
||||||
|
[DbContext(typeof(SqlServerContext))]
|
||||||
|
[Migration("20250312234517_AlterTableUserMaxLengthIndexes")]
|
||||||
|
partial class AlterTableUserMaxLengthIndexes
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||||
|
{
|
||||||
|
#pragma warning disable 612, 618
|
||||||
|
modelBuilder
|
||||||
|
.HasAnnotation("ProductVersion", "9.0.2")
|
||||||
|
.HasAnnotation("Relational:MaxIdentifierLength", 128);
|
||||||
|
|
||||||
|
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
|
||||||
|
|
||||||
|
modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.Role", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||||
|
|
||||||
|
b.Property<DateTime>("CreationTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int>("CreationUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<DateTime>("DeletionTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int>("DeletionUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<string>("Guid")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(45)
|
||||||
|
.HasColumnType("nvarchar(45)");
|
||||||
|
|
||||||
|
b.Property<bool>("IsDeleted")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<string>("Name")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<DateTime>("UpdateTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int>("UpdateUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.ToTable("Role");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.User", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||||
|
|
||||||
|
b.Property<DateTime>("CreationTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int>("CreationUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<DateTime>("DeletionTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int>("DeletionUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<string>("Email")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(200)
|
||||||
|
.HasColumnType("nvarchar(200)");
|
||||||
|
|
||||||
|
b.Property<string>("FirstName")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(200)
|
||||||
|
.HasColumnType("nvarchar(200)");
|
||||||
|
|
||||||
|
b.Property<string>("Guid")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(45)
|
||||||
|
.HasColumnType("nvarchar(45)");
|
||||||
|
|
||||||
|
b.Property<bool>("IsDeleted")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<bool>("IsTestUser")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<string>("LastName")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(200)
|
||||||
|
.HasColumnType("nvarchar(200)");
|
||||||
|
|
||||||
|
b.Property<string>("Password")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<string>("PasswordHash")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<string>("PasswordSalt")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<int>("RoleId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<DateTime>("UpdateTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int>("UpdateUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<string>("Username")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(200)
|
||||||
|
.HasColumnType("nvarchar(200)");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("RoleId");
|
||||||
|
|
||||||
|
b.HasIndex(new[] { "Email" }, "IX_Email");
|
||||||
|
|
||||||
|
b.HasIndex(new[] { "IsDeleted", "Guid" }, "IX_IsDeleted_Guid")
|
||||||
|
.HasFilter("[IsDeleted] = 0");
|
||||||
|
|
||||||
|
b.HasIndex(new[] { "Username" }, "IX_Username");
|
||||||
|
|
||||||
|
b.ToTable("Users");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.User", b =>
|
||||||
|
{
|
||||||
|
b.HasOne("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.Role", "Role")
|
||||||
|
.WithMany()
|
||||||
|
.HasForeignKey("RoleId")
|
||||||
|
.OnDelete(DeleteBehavior.Cascade)
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
b.Navigation("Role");
|
||||||
|
});
|
||||||
|
#pragma warning restore 612, 618
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,165 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace MainProject.Migrations
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
public partial class AlterTableUserMaxLengthIndexes : Migration
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.AlterColumn<string>(
|
||||||
|
name: "Username",
|
||||||
|
table: "Users",
|
||||||
|
type: "nvarchar(200)",
|
||||||
|
maxLength: 200,
|
||||||
|
nullable: false,
|
||||||
|
oldClrType: typeof(string),
|
||||||
|
oldType: "nvarchar(max)");
|
||||||
|
|
||||||
|
migrationBuilder.AlterColumn<string>(
|
||||||
|
name: "LastName",
|
||||||
|
table: "Users",
|
||||||
|
type: "nvarchar(200)",
|
||||||
|
maxLength: 200,
|
||||||
|
nullable: false,
|
||||||
|
oldClrType: typeof(string),
|
||||||
|
oldType: "nvarchar(max)");
|
||||||
|
|
||||||
|
migrationBuilder.AlterColumn<string>(
|
||||||
|
name: "Guid",
|
||||||
|
table: "Users",
|
||||||
|
type: "nvarchar(45)",
|
||||||
|
maxLength: 45,
|
||||||
|
nullable: false,
|
||||||
|
oldClrType: typeof(string),
|
||||||
|
oldType: "nvarchar(max)");
|
||||||
|
|
||||||
|
migrationBuilder.AlterColumn<string>(
|
||||||
|
name: "FirstName",
|
||||||
|
table: "Users",
|
||||||
|
type: "nvarchar(200)",
|
||||||
|
maxLength: 200,
|
||||||
|
nullable: false,
|
||||||
|
oldClrType: typeof(string),
|
||||||
|
oldType: "nvarchar(max)");
|
||||||
|
|
||||||
|
migrationBuilder.AlterColumn<string>(
|
||||||
|
name: "Email",
|
||||||
|
table: "Users",
|
||||||
|
type: "nvarchar(200)",
|
||||||
|
maxLength: 200,
|
||||||
|
nullable: false,
|
||||||
|
oldClrType: typeof(string),
|
||||||
|
oldType: "nvarchar(max)");
|
||||||
|
|
||||||
|
migrationBuilder.AddColumn<bool>(
|
||||||
|
name: "IsTestUser",
|
||||||
|
table: "Users",
|
||||||
|
type: "bit",
|
||||||
|
nullable: false,
|
||||||
|
defaultValue: false);
|
||||||
|
|
||||||
|
migrationBuilder.AlterColumn<string>(
|
||||||
|
name: "Guid",
|
||||||
|
table: "Role",
|
||||||
|
type: "nvarchar(45)",
|
||||||
|
maxLength: 45,
|
||||||
|
nullable: false,
|
||||||
|
oldClrType: typeof(string),
|
||||||
|
oldType: "nvarchar(max)");
|
||||||
|
|
||||||
|
migrationBuilder.CreateIndex(
|
||||||
|
name: "IX_Email",
|
||||||
|
table: "Users",
|
||||||
|
column: "Email");
|
||||||
|
|
||||||
|
migrationBuilder.CreateIndex(
|
||||||
|
name: "IX_IsDeleted_Guid",
|
||||||
|
table: "Users",
|
||||||
|
columns: new[] { "IsDeleted", "Guid" },
|
||||||
|
filter: "[IsDeleted] = 0");
|
||||||
|
|
||||||
|
migrationBuilder.CreateIndex(
|
||||||
|
name: "IX_Username",
|
||||||
|
table: "Users",
|
||||||
|
column: "Username");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.DropIndex(
|
||||||
|
name: "IX_Email",
|
||||||
|
table: "Users");
|
||||||
|
|
||||||
|
migrationBuilder.DropIndex(
|
||||||
|
name: "IX_IsDeleted_Guid",
|
||||||
|
table: "Users");
|
||||||
|
|
||||||
|
migrationBuilder.DropIndex(
|
||||||
|
name: "IX_Username",
|
||||||
|
table: "Users");
|
||||||
|
|
||||||
|
migrationBuilder.DropColumn(
|
||||||
|
name: "IsTestUser",
|
||||||
|
table: "Users");
|
||||||
|
|
||||||
|
migrationBuilder.AlterColumn<string>(
|
||||||
|
name: "Username",
|
||||||
|
table: "Users",
|
||||||
|
type: "nvarchar(max)",
|
||||||
|
nullable: false,
|
||||||
|
oldClrType: typeof(string),
|
||||||
|
oldType: "nvarchar(200)",
|
||||||
|
oldMaxLength: 200);
|
||||||
|
|
||||||
|
migrationBuilder.AlterColumn<string>(
|
||||||
|
name: "LastName",
|
||||||
|
table: "Users",
|
||||||
|
type: "nvarchar(max)",
|
||||||
|
nullable: false,
|
||||||
|
oldClrType: typeof(string),
|
||||||
|
oldType: "nvarchar(200)",
|
||||||
|
oldMaxLength: 200);
|
||||||
|
|
||||||
|
migrationBuilder.AlterColumn<string>(
|
||||||
|
name: "Guid",
|
||||||
|
table: "Users",
|
||||||
|
type: "nvarchar(max)",
|
||||||
|
nullable: false,
|
||||||
|
oldClrType: typeof(string),
|
||||||
|
oldType: "nvarchar(45)",
|
||||||
|
oldMaxLength: 45);
|
||||||
|
|
||||||
|
migrationBuilder.AlterColumn<string>(
|
||||||
|
name: "FirstName",
|
||||||
|
table: "Users",
|
||||||
|
type: "nvarchar(max)",
|
||||||
|
nullable: false,
|
||||||
|
oldClrType: typeof(string),
|
||||||
|
oldType: "nvarchar(200)",
|
||||||
|
oldMaxLength: 200);
|
||||||
|
|
||||||
|
migrationBuilder.AlterColumn<string>(
|
||||||
|
name: "Email",
|
||||||
|
table: "Users",
|
||||||
|
type: "nvarchar(max)",
|
||||||
|
nullable: false,
|
||||||
|
oldClrType: typeof(string),
|
||||||
|
oldType: "nvarchar(200)",
|
||||||
|
oldMaxLength: 200);
|
||||||
|
|
||||||
|
migrationBuilder.AlterColumn<string>(
|
||||||
|
name: "Guid",
|
||||||
|
table: "Role",
|
||||||
|
type: "nvarchar(max)",
|
||||||
|
nullable: false,
|
||||||
|
oldClrType: typeof(string),
|
||||||
|
oldType: "nvarchar(45)",
|
||||||
|
oldMaxLength: 45);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
167
MainProject/Migrations/20250316014620_AlterTablesUsersAndRoles.Designer.cs
generated
Normal file
167
MainProject/Migrations/20250316014620_AlterTablesUsersAndRoles.Designer.cs
generated
Normal file
@@ -0,0 +1,167 @@
|
|||||||
|
// <auto-generated />
|
||||||
|
using System;
|
||||||
|
using BasicDotnetTemplate.MainProject.Core.Database;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||||
|
using Microsoft.EntityFrameworkCore.Metadata;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace MainProject.Migrations
|
||||||
|
{
|
||||||
|
[DbContext(typeof(SqlServerContext))]
|
||||||
|
[Migration("20250316014620_AlterTablesUsersAndRoles")]
|
||||||
|
partial class AlterTablesUsersAndRoles
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||||
|
{
|
||||||
|
#pragma warning disable 612, 618
|
||||||
|
modelBuilder
|
||||||
|
.HasAnnotation("ProductVersion", "9.0.2")
|
||||||
|
.HasAnnotation("Relational:MaxIdentifierLength", 128);
|
||||||
|
|
||||||
|
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
|
||||||
|
|
||||||
|
modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.Role", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||||
|
|
||||||
|
b.Property<DateTime>("CreationTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("CreationUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<DateTime>("DeletionTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("DeletionUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<string>("Guid")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(45)
|
||||||
|
.HasColumnType("nvarchar(45)");
|
||||||
|
|
||||||
|
b.Property<bool>("IsDeleted")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<string>("Name")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(100)
|
||||||
|
.HasColumnType("nvarchar(100)");
|
||||||
|
|
||||||
|
b.Property<DateTime>("UpdateTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("UpdateUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex(new[] { "IsDeleted", "Guid" }, "IX_IsDeleted_Guid")
|
||||||
|
.HasFilter("[IsDeleted] = 0");
|
||||||
|
|
||||||
|
b.ToTable("Roles");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.User", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||||
|
|
||||||
|
b.Property<DateTime>("CreationTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("CreationUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<DateTime>("DeletionTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("DeletionUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<string>("Email")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(200)
|
||||||
|
.HasColumnType("nvarchar(200)");
|
||||||
|
|
||||||
|
b.Property<string>("FirstName")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(200)
|
||||||
|
.HasColumnType("nvarchar(200)");
|
||||||
|
|
||||||
|
b.Property<string>("Guid")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(45)
|
||||||
|
.HasColumnType("nvarchar(45)");
|
||||||
|
|
||||||
|
b.Property<bool>("IsDeleted")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<bool>("IsTestUser")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<string>("LastName")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(200)
|
||||||
|
.HasColumnType("nvarchar(200)");
|
||||||
|
|
||||||
|
b.Property<string>("Password")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<string>("PasswordHash")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<string>("PasswordSalt")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<int>("RoleId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<DateTime>("UpdateTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("UpdateUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("RoleId");
|
||||||
|
|
||||||
|
b.HasIndex(new[] { "Email" }, "IX_Email");
|
||||||
|
|
||||||
|
b.HasIndex(new[] { "IsDeleted", "Guid" }, "IX_IsDeleted_Guid")
|
||||||
|
.HasFilter("[IsDeleted] = 0");
|
||||||
|
|
||||||
|
b.ToTable("Users");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.User", b =>
|
||||||
|
{
|
||||||
|
b.HasOne("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.Role", "Role")
|
||||||
|
.WithMany()
|
||||||
|
.HasForeignKey("RoleId")
|
||||||
|
.OnDelete(DeleteBehavior.Cascade)
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
b.Navigation("Role");
|
||||||
|
});
|
||||||
|
#pragma warning restore 612, 618
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,225 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace MainProject.Migrations
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
public partial class AlterTablesUsersAndRoles : Migration
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.DropForeignKey(
|
||||||
|
name: "FK_Users_Role_RoleId",
|
||||||
|
table: "Users");
|
||||||
|
|
||||||
|
migrationBuilder.DropIndex(
|
||||||
|
name: "IX_Username",
|
||||||
|
table: "Users");
|
||||||
|
|
||||||
|
migrationBuilder.DropPrimaryKey(
|
||||||
|
name: "PK_Role",
|
||||||
|
table: "Role");
|
||||||
|
|
||||||
|
migrationBuilder.DropColumn(
|
||||||
|
name: "Username",
|
||||||
|
table: "Users");
|
||||||
|
|
||||||
|
migrationBuilder.RenameTable(
|
||||||
|
name: "Role",
|
||||||
|
newName: "Roles");
|
||||||
|
|
||||||
|
migrationBuilder.AlterColumn<int>(
|
||||||
|
name: "UpdateUserId",
|
||||||
|
table: "Users",
|
||||||
|
type: "int",
|
||||||
|
nullable: true,
|
||||||
|
oldClrType: typeof(int),
|
||||||
|
oldType: "int");
|
||||||
|
|
||||||
|
migrationBuilder.AlterColumn<int>(
|
||||||
|
name: "DeletionUserId",
|
||||||
|
table: "Users",
|
||||||
|
type: "int",
|
||||||
|
nullable: true,
|
||||||
|
oldClrType: typeof(int),
|
||||||
|
oldType: "int");
|
||||||
|
|
||||||
|
migrationBuilder.AlterColumn<int>(
|
||||||
|
name: "CreationUserId",
|
||||||
|
table: "Users",
|
||||||
|
type: "int",
|
||||||
|
nullable: true,
|
||||||
|
oldClrType: typeof(int),
|
||||||
|
oldType: "int");
|
||||||
|
|
||||||
|
migrationBuilder.AlterColumn<int>(
|
||||||
|
name: "UpdateUserId",
|
||||||
|
table: "Roles",
|
||||||
|
type: "int",
|
||||||
|
nullable: true,
|
||||||
|
oldClrType: typeof(int),
|
||||||
|
oldType: "int");
|
||||||
|
|
||||||
|
migrationBuilder.AlterColumn<string>(
|
||||||
|
name: "Name",
|
||||||
|
table: "Roles",
|
||||||
|
type: "nvarchar(100)",
|
||||||
|
maxLength: 100,
|
||||||
|
nullable: false,
|
||||||
|
oldClrType: typeof(string),
|
||||||
|
oldType: "nvarchar(max)");
|
||||||
|
|
||||||
|
migrationBuilder.AlterColumn<int>(
|
||||||
|
name: "DeletionUserId",
|
||||||
|
table: "Roles",
|
||||||
|
type: "int",
|
||||||
|
nullable: true,
|
||||||
|
oldClrType: typeof(int),
|
||||||
|
oldType: "int");
|
||||||
|
|
||||||
|
migrationBuilder.AlterColumn<int>(
|
||||||
|
name: "CreationUserId",
|
||||||
|
table: "Roles",
|
||||||
|
type: "int",
|
||||||
|
nullable: true,
|
||||||
|
oldClrType: typeof(int),
|
||||||
|
oldType: "int");
|
||||||
|
|
||||||
|
migrationBuilder.AddPrimaryKey(
|
||||||
|
name: "PK_Roles",
|
||||||
|
table: "Roles",
|
||||||
|
column: "Id");
|
||||||
|
|
||||||
|
migrationBuilder.CreateIndex(
|
||||||
|
name: "IX_IsDeleted_Guid",
|
||||||
|
table: "Roles",
|
||||||
|
columns: new[] { "IsDeleted", "Guid" },
|
||||||
|
filter: "[IsDeleted] = 0");
|
||||||
|
|
||||||
|
migrationBuilder.AddForeignKey(
|
||||||
|
name: "FK_Users_Roles_RoleId",
|
||||||
|
table: "Users",
|
||||||
|
column: "RoleId",
|
||||||
|
principalTable: "Roles",
|
||||||
|
principalColumn: "Id",
|
||||||
|
onDelete: ReferentialAction.Cascade);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.DropForeignKey(
|
||||||
|
name: "FK_Users_Roles_RoleId",
|
||||||
|
table: "Users");
|
||||||
|
|
||||||
|
migrationBuilder.DropPrimaryKey(
|
||||||
|
name: "PK_Roles",
|
||||||
|
table: "Roles");
|
||||||
|
|
||||||
|
migrationBuilder.DropIndex(
|
||||||
|
name: "IX_IsDeleted_Guid",
|
||||||
|
table: "Roles");
|
||||||
|
|
||||||
|
migrationBuilder.RenameTable(
|
||||||
|
name: "Roles",
|
||||||
|
newName: "Role");
|
||||||
|
|
||||||
|
migrationBuilder.AlterColumn<int>(
|
||||||
|
name: "UpdateUserId",
|
||||||
|
table: "Users",
|
||||||
|
type: "int",
|
||||||
|
nullable: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
oldClrType: typeof(int),
|
||||||
|
oldType: "int",
|
||||||
|
oldNullable: true);
|
||||||
|
|
||||||
|
migrationBuilder.AlterColumn<int>(
|
||||||
|
name: "DeletionUserId",
|
||||||
|
table: "Users",
|
||||||
|
type: "int",
|
||||||
|
nullable: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
oldClrType: typeof(int),
|
||||||
|
oldType: "int",
|
||||||
|
oldNullable: true);
|
||||||
|
|
||||||
|
migrationBuilder.AlterColumn<int>(
|
||||||
|
name: "CreationUserId",
|
||||||
|
table: "Users",
|
||||||
|
type: "int",
|
||||||
|
nullable: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
oldClrType: typeof(int),
|
||||||
|
oldType: "int",
|
||||||
|
oldNullable: true);
|
||||||
|
|
||||||
|
migrationBuilder.AddColumn<string>(
|
||||||
|
name: "Username",
|
||||||
|
table: "Users",
|
||||||
|
type: "nvarchar(200)",
|
||||||
|
maxLength: 200,
|
||||||
|
nullable: false,
|
||||||
|
defaultValue: "");
|
||||||
|
|
||||||
|
migrationBuilder.AlterColumn<int>(
|
||||||
|
name: "UpdateUserId",
|
||||||
|
table: "Role",
|
||||||
|
type: "int",
|
||||||
|
nullable: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
oldClrType: typeof(int),
|
||||||
|
oldType: "int",
|
||||||
|
oldNullable: true);
|
||||||
|
|
||||||
|
migrationBuilder.AlterColumn<string>(
|
||||||
|
name: "Name",
|
||||||
|
table: "Role",
|
||||||
|
type: "nvarchar(max)",
|
||||||
|
nullable: false,
|
||||||
|
oldClrType: typeof(string),
|
||||||
|
oldType: "nvarchar(100)",
|
||||||
|
oldMaxLength: 100);
|
||||||
|
|
||||||
|
migrationBuilder.AlterColumn<int>(
|
||||||
|
name: "DeletionUserId",
|
||||||
|
table: "Role",
|
||||||
|
type: "int",
|
||||||
|
nullable: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
oldClrType: typeof(int),
|
||||||
|
oldType: "int",
|
||||||
|
oldNullable: true);
|
||||||
|
|
||||||
|
migrationBuilder.AlterColumn<int>(
|
||||||
|
name: "CreationUserId",
|
||||||
|
table: "Role",
|
||||||
|
type: "int",
|
||||||
|
nullable: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
oldClrType: typeof(int),
|
||||||
|
oldType: "int",
|
||||||
|
oldNullable: true);
|
||||||
|
|
||||||
|
migrationBuilder.AddPrimaryKey(
|
||||||
|
name: "PK_Role",
|
||||||
|
table: "Role",
|
||||||
|
column: "Id");
|
||||||
|
|
||||||
|
migrationBuilder.CreateIndex(
|
||||||
|
name: "IX_Username",
|
||||||
|
table: "Users",
|
||||||
|
column: "Username");
|
||||||
|
|
||||||
|
migrationBuilder.AddForeignKey(
|
||||||
|
name: "FK_Users_Role_RoleId",
|
||||||
|
table: "Users",
|
||||||
|
column: "RoleId",
|
||||||
|
principalTable: "Role",
|
||||||
|
principalColumn: "Id",
|
||||||
|
onDelete: ReferentialAction.Cascade);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
167
MainProject/Migrations/20250316212343_AlterBaseUpdateTimeDeletionTimeNullable.Designer.cs
generated
Normal file
167
MainProject/Migrations/20250316212343_AlterBaseUpdateTimeDeletionTimeNullable.Designer.cs
generated
Normal file
@@ -0,0 +1,167 @@
|
|||||||
|
// <auto-generated />
|
||||||
|
using System;
|
||||||
|
using BasicDotnetTemplate.MainProject.Core.Database;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||||
|
using Microsoft.EntityFrameworkCore.Metadata;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace MainProject.Migrations
|
||||||
|
{
|
||||||
|
[DbContext(typeof(SqlServerContext))]
|
||||||
|
[Migration("20250316212343_AlterBaseUpdateTimeDeletionTimeNullable")]
|
||||||
|
partial class AlterBaseUpdateTimeDeletionTimeNullable
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||||
|
{
|
||||||
|
#pragma warning disable 612, 618
|
||||||
|
modelBuilder
|
||||||
|
.HasAnnotation("ProductVersion", "9.0.2")
|
||||||
|
.HasAnnotation("Relational:MaxIdentifierLength", 128);
|
||||||
|
|
||||||
|
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
|
||||||
|
|
||||||
|
modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.Role", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||||
|
|
||||||
|
b.Property<DateTime>("CreationTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("CreationUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("DeletionTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("DeletionUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<string>("Guid")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(45)
|
||||||
|
.HasColumnType("nvarchar(45)");
|
||||||
|
|
||||||
|
b.Property<bool>("IsDeleted")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<string>("Name")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(100)
|
||||||
|
.HasColumnType("nvarchar(100)");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("UpdateTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("UpdateUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex(new[] { "IsDeleted", "Guid" }, "IX_IsDeleted_Guid")
|
||||||
|
.HasFilter("[IsDeleted] = 0");
|
||||||
|
|
||||||
|
b.ToTable("Roles");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.User", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||||
|
|
||||||
|
b.Property<DateTime>("CreationTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("CreationUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("DeletionTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("DeletionUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<string>("Email")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(200)
|
||||||
|
.HasColumnType("nvarchar(200)");
|
||||||
|
|
||||||
|
b.Property<string>("FirstName")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(200)
|
||||||
|
.HasColumnType("nvarchar(200)");
|
||||||
|
|
||||||
|
b.Property<string>("Guid")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(45)
|
||||||
|
.HasColumnType("nvarchar(45)");
|
||||||
|
|
||||||
|
b.Property<bool>("IsDeleted")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<bool>("IsTestUser")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<string>("LastName")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(200)
|
||||||
|
.HasColumnType("nvarchar(200)");
|
||||||
|
|
||||||
|
b.Property<string>("Password")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<string>("PasswordHash")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<string>("PasswordSalt")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<int>("RoleId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("UpdateTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("UpdateUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("RoleId");
|
||||||
|
|
||||||
|
b.HasIndex(new[] { "Email" }, "IX_Email");
|
||||||
|
|
||||||
|
b.HasIndex(new[] { "IsDeleted", "Guid" }, "IX_IsDeleted_Guid")
|
||||||
|
.HasFilter("[IsDeleted] = 0");
|
||||||
|
|
||||||
|
b.ToTable("Users");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.User", b =>
|
||||||
|
{
|
||||||
|
b.HasOne("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.Role", "Role")
|
||||||
|
.WithMany()
|
||||||
|
.HasForeignKey("RoleId")
|
||||||
|
.OnDelete(DeleteBehavior.Cascade)
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
b.Navigation("Role");
|
||||||
|
});
|
||||||
|
#pragma warning restore 612, 618
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,91 @@
|
|||||||
|
using System;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace MainProject.Migrations
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
public partial class AlterBaseUpdateTimeDeletionTimeNullable : Migration
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.AlterColumn<DateTime>(
|
||||||
|
name: "UpdateTime",
|
||||||
|
table: "Users",
|
||||||
|
type: "datetime2",
|
||||||
|
nullable: true,
|
||||||
|
oldClrType: typeof(DateTime),
|
||||||
|
oldType: "datetime2");
|
||||||
|
|
||||||
|
migrationBuilder.AlterColumn<DateTime>(
|
||||||
|
name: "DeletionTime",
|
||||||
|
table: "Users",
|
||||||
|
type: "datetime2",
|
||||||
|
nullable: true,
|
||||||
|
oldClrType: typeof(DateTime),
|
||||||
|
oldType: "datetime2");
|
||||||
|
|
||||||
|
migrationBuilder.AlterColumn<DateTime>(
|
||||||
|
name: "UpdateTime",
|
||||||
|
table: "Roles",
|
||||||
|
type: "datetime2",
|
||||||
|
nullable: true,
|
||||||
|
oldClrType: typeof(DateTime),
|
||||||
|
oldType: "datetime2");
|
||||||
|
|
||||||
|
migrationBuilder.AlterColumn<DateTime>(
|
||||||
|
name: "DeletionTime",
|
||||||
|
table: "Roles",
|
||||||
|
type: "datetime2",
|
||||||
|
nullable: true,
|
||||||
|
oldClrType: typeof(DateTime),
|
||||||
|
oldType: "datetime2");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.AlterColumn<DateTime>(
|
||||||
|
name: "UpdateTime",
|
||||||
|
table: "Users",
|
||||||
|
type: "datetime2",
|
||||||
|
nullable: false,
|
||||||
|
defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified),
|
||||||
|
oldClrType: typeof(DateTime),
|
||||||
|
oldType: "datetime2",
|
||||||
|
oldNullable: true);
|
||||||
|
|
||||||
|
migrationBuilder.AlterColumn<DateTime>(
|
||||||
|
name: "DeletionTime",
|
||||||
|
table: "Users",
|
||||||
|
type: "datetime2",
|
||||||
|
nullable: false,
|
||||||
|
defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified),
|
||||||
|
oldClrType: typeof(DateTime),
|
||||||
|
oldType: "datetime2",
|
||||||
|
oldNullable: true);
|
||||||
|
|
||||||
|
migrationBuilder.AlterColumn<DateTime>(
|
||||||
|
name: "UpdateTime",
|
||||||
|
table: "Roles",
|
||||||
|
type: "datetime2",
|
||||||
|
nullable: false,
|
||||||
|
defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified),
|
||||||
|
oldClrType: typeof(DateTime),
|
||||||
|
oldType: "datetime2",
|
||||||
|
oldNullable: true);
|
||||||
|
|
||||||
|
migrationBuilder.AlterColumn<DateTime>(
|
||||||
|
name: "DeletionTime",
|
||||||
|
table: "Roles",
|
||||||
|
type: "datetime2",
|
||||||
|
nullable: false,
|
||||||
|
defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified),
|
||||||
|
oldClrType: typeof(DateTime),
|
||||||
|
oldType: "datetime2",
|
||||||
|
oldNullable: true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
170
MainProject/Migrations/20250316212722_AlterTableRoleAddedIsNotEditable.Designer.cs
generated
Normal file
170
MainProject/Migrations/20250316212722_AlterTableRoleAddedIsNotEditable.Designer.cs
generated
Normal file
@@ -0,0 +1,170 @@
|
|||||||
|
// <auto-generated />
|
||||||
|
using System;
|
||||||
|
using BasicDotnetTemplate.MainProject.Core.Database;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||||
|
using Microsoft.EntityFrameworkCore.Metadata;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace MainProject.Migrations
|
||||||
|
{
|
||||||
|
[DbContext(typeof(SqlServerContext))]
|
||||||
|
[Migration("20250316212722_AlterTableRoleAddedIsNotEditable")]
|
||||||
|
partial class AlterTableRoleAddedIsNotEditable
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||||
|
{
|
||||||
|
#pragma warning disable 612, 618
|
||||||
|
modelBuilder
|
||||||
|
.HasAnnotation("ProductVersion", "9.0.2")
|
||||||
|
.HasAnnotation("Relational:MaxIdentifierLength", 128);
|
||||||
|
|
||||||
|
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
|
||||||
|
|
||||||
|
modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.Role", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||||
|
|
||||||
|
b.Property<DateTime>("CreationTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("CreationUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("DeletionTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("DeletionUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<string>("Guid")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(45)
|
||||||
|
.HasColumnType("nvarchar(45)");
|
||||||
|
|
||||||
|
b.Property<bool>("IsDeleted")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<bool>("IsNotEditable")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<string>("Name")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(100)
|
||||||
|
.HasColumnType("nvarchar(100)");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("UpdateTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("UpdateUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex(new[] { "IsDeleted", "Guid" }, "IX_IsDeleted_Guid")
|
||||||
|
.HasFilter("[IsDeleted] = 0");
|
||||||
|
|
||||||
|
b.ToTable("Roles");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.User", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||||
|
|
||||||
|
b.Property<DateTime>("CreationTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("CreationUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("DeletionTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("DeletionUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<string>("Email")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(200)
|
||||||
|
.HasColumnType("nvarchar(200)");
|
||||||
|
|
||||||
|
b.Property<string>("FirstName")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(200)
|
||||||
|
.HasColumnType("nvarchar(200)");
|
||||||
|
|
||||||
|
b.Property<string>("Guid")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(45)
|
||||||
|
.HasColumnType("nvarchar(45)");
|
||||||
|
|
||||||
|
b.Property<bool>("IsDeleted")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<bool>("IsTestUser")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<string>("LastName")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(200)
|
||||||
|
.HasColumnType("nvarchar(200)");
|
||||||
|
|
||||||
|
b.Property<string>("Password")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<string>("PasswordHash")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<string>("PasswordSalt")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<int>("RoleId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("UpdateTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("UpdateUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("RoleId");
|
||||||
|
|
||||||
|
b.HasIndex(new[] { "Email" }, "IX_Email");
|
||||||
|
|
||||||
|
b.HasIndex(new[] { "IsDeleted", "Guid" }, "IX_IsDeleted_Guid")
|
||||||
|
.HasFilter("[IsDeleted] = 0");
|
||||||
|
|
||||||
|
b.ToTable("Users");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.User", b =>
|
||||||
|
{
|
||||||
|
b.HasOne("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.Role", "Role")
|
||||||
|
.WithMany()
|
||||||
|
.HasForeignKey("RoleId")
|
||||||
|
.OnDelete(DeleteBehavior.Cascade)
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
b.Navigation("Role");
|
||||||
|
});
|
||||||
|
#pragma warning restore 612, 618
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace MainProject.Migrations
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
public partial class AlterTableRoleAddedIsNotEditable : Migration
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.AddColumn<bool>(
|
||||||
|
name: "IsNotEditable",
|
||||||
|
table: "Roles",
|
||||||
|
type: "bit",
|
||||||
|
nullable: false,
|
||||||
|
defaultValue: false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.DropColumn(
|
||||||
|
name: "IsNotEditable",
|
||||||
|
table: "Roles");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
543
MainProject/Migrations/20250426183010_AddingPermissionsTables.Designer.cs
generated
Normal file
543
MainProject/Migrations/20250426183010_AddingPermissionsTables.Designer.cs
generated
Normal file
@@ -0,0 +1,543 @@
|
|||||||
|
// <auto-generated />
|
||||||
|
using System;
|
||||||
|
using BasicDotnetTemplate.MainProject.Core.Database;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||||
|
using Microsoft.EntityFrameworkCore.Metadata;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace MainProject.Migrations
|
||||||
|
{
|
||||||
|
[DbContext(typeof(SqlServerContext))]
|
||||||
|
[Migration("20250426183010_AddingPermissionsTables")]
|
||||||
|
partial class AddingPermissionsTables
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||||
|
{
|
||||||
|
#pragma warning disable 612, 618
|
||||||
|
modelBuilder
|
||||||
|
.HasAnnotation("ProductVersion", "9.0.2")
|
||||||
|
.HasAnnotation("Relational:MaxIdentifierLength", 128);
|
||||||
|
|
||||||
|
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
|
||||||
|
|
||||||
|
modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.PermissionModule", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||||
|
|
||||||
|
b.Property<DateTime>("CreationTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("CreationUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("DeletionTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("DeletionUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<bool>("Enabled")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<string>("Guid")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(45)
|
||||||
|
.HasColumnType("nvarchar(45)");
|
||||||
|
|
||||||
|
b.Property<bool>("IsDeleted")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<string>("Name")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(100)
|
||||||
|
.HasColumnType("nvarchar(100)");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("UpdateTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("UpdateUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex(new[] { "Enabled" }, "IX_Enabled")
|
||||||
|
.HasFilter("[Enabled] = 1");
|
||||||
|
|
||||||
|
b.HasIndex(new[] { "IsDeleted" }, "IX_IsDeleted")
|
||||||
|
.HasFilter("[IsDeleted] = 0");
|
||||||
|
|
||||||
|
b.HasIndex(new[] { "IsDeleted", "Name", "Enabled" }, "IX_IsDeleted_Name_Enabled")
|
||||||
|
.HasFilter("[IsDeleted] = 0");
|
||||||
|
|
||||||
|
b.ToTable("PermissionModules");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.PermissionOperation", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||||
|
|
||||||
|
b.Property<DateTime>("CreationTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("CreationUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("DeletionTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("DeletionUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<string>("Guid")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(45)
|
||||||
|
.HasColumnType("nvarchar(45)");
|
||||||
|
|
||||||
|
b.Property<bool>("IsDeleted")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<string>("Name")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(100)
|
||||||
|
.HasColumnType("nvarchar(100)");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("UpdateTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("UpdateUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex(new[] { "IsDeleted", "Name" }, "IX_IsDeleted_Name");
|
||||||
|
|
||||||
|
b.ToTable("PermissionOperations");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.PermissionSystem", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||||
|
|
||||||
|
b.Property<DateTime>("CreationTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("CreationUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("DeletionTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("DeletionUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<bool>("Enabled")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<string>("Guid")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(45)
|
||||||
|
.HasColumnType("nvarchar(45)");
|
||||||
|
|
||||||
|
b.Property<bool>("IsDeleted")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<string>("Name")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(100)
|
||||||
|
.HasColumnType("nvarchar(100)");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("UpdateTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("UpdateUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex(new[] { "Enabled" }, "IX_Enabled")
|
||||||
|
.HasFilter("[Enabled] = 1");
|
||||||
|
|
||||||
|
b.HasIndex(new[] { "IsDeleted" }, "IX_IsDeleted")
|
||||||
|
.HasFilter("[IsDeleted] = 0");
|
||||||
|
|
||||||
|
b.HasIndex(new[] { "IsDeleted", "Name", "Enabled" }, "IX_IsDeleted_Name_Enabled")
|
||||||
|
.HasFilter("[IsDeleted] = 0");
|
||||||
|
|
||||||
|
b.ToTable("PermissionSystems");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.PermissionSystemModule", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||||
|
|
||||||
|
b.Property<DateTime>("CreationTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("CreationUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("DeletionTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("DeletionUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<bool>("Enabled")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<string>("Guid")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(45)
|
||||||
|
.HasColumnType("nvarchar(45)");
|
||||||
|
|
||||||
|
b.Property<bool>("IsDeleted")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<int>("PermissionModuleId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<int>("PermissionSystemId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("UpdateTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("UpdateUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("PermissionModuleId");
|
||||||
|
|
||||||
|
b.HasIndex("PermissionSystemId");
|
||||||
|
|
||||||
|
b.ToTable("PermissionSystemModules");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.PermissionSystemModuleOperation", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||||
|
|
||||||
|
b.Property<DateTime>("CreationTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("CreationUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("DeletionTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("DeletionUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<bool>("Enabled")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<string>("Guid")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(45)
|
||||||
|
.HasColumnType("nvarchar(45)");
|
||||||
|
|
||||||
|
b.Property<bool>("IsDeleted")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<int>("PermissionOperationId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<int>("PermissionSystemModuleId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("UpdateTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("UpdateUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("PermissionOperationId");
|
||||||
|
|
||||||
|
b.HasIndex("PermissionSystemModuleId");
|
||||||
|
|
||||||
|
b.HasIndex(new[] { "IsDeleted", "Enabled", "Guid" }, "IX_IsDeleted_Enabled_Guid");
|
||||||
|
|
||||||
|
b.ToTable("PermissionSystemModuleOperations");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.Role", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||||
|
|
||||||
|
b.Property<DateTime>("CreationTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("CreationUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("DeletionTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("DeletionUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<string>("Guid")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(45)
|
||||||
|
.HasColumnType("nvarchar(45)");
|
||||||
|
|
||||||
|
b.Property<bool>("IsDeleted")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<bool>("IsNotEditable")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<string>("Name")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(100)
|
||||||
|
.HasColumnType("nvarchar(100)");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("UpdateTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("UpdateUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex(new[] { "IsDeleted", "Guid" }, "IX_IsDeleted_Guid")
|
||||||
|
.HasFilter("[IsDeleted] = 0");
|
||||||
|
|
||||||
|
b.ToTable("Roles");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.RolePermissionSystemModuleOperation", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||||
|
|
||||||
|
b.Property<bool>("Active")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<DateTime>("CreationTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("CreationUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("DeletionTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("DeletionUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<string>("Guid")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(45)
|
||||||
|
.HasColumnType("nvarchar(45)");
|
||||||
|
|
||||||
|
b.Property<bool>("IsDeleted")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<int>("PermissionSystemModuleOperationId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<int>("RoleId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("UpdateTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("UpdateUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("PermissionSystemModuleOperationId");
|
||||||
|
|
||||||
|
b.HasIndex("RoleId");
|
||||||
|
|
||||||
|
b.ToTable("RolePermissionSystemModuleOperations");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.User", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||||
|
|
||||||
|
b.Property<DateTime>("CreationTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("CreationUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("DeletionTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("DeletionUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<string>("Email")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(200)
|
||||||
|
.HasColumnType("nvarchar(200)");
|
||||||
|
|
||||||
|
b.Property<string>("FirstName")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(200)
|
||||||
|
.HasColumnType("nvarchar(200)");
|
||||||
|
|
||||||
|
b.Property<string>("Guid")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(45)
|
||||||
|
.HasColumnType("nvarchar(45)");
|
||||||
|
|
||||||
|
b.Property<bool>("IsDeleted")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<bool>("IsTestUser")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<string>("LastName")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(200)
|
||||||
|
.HasColumnType("nvarchar(200)");
|
||||||
|
|
||||||
|
b.Property<string>("Password")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<string>("PasswordHash")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<string>("PasswordSalt")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<int>("RoleId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("UpdateTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("UpdateUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("RoleId");
|
||||||
|
|
||||||
|
b.HasIndex(new[] { "Email" }, "IX_Email");
|
||||||
|
|
||||||
|
b.HasIndex(new[] { "IsDeleted", "Guid" }, "IX_IsDeleted_Guid")
|
||||||
|
.HasFilter("[IsDeleted] = 0");
|
||||||
|
|
||||||
|
b.ToTable("Users");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.PermissionSystemModule", b =>
|
||||||
|
{
|
||||||
|
b.HasOne("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.PermissionModule", "PermissionModule")
|
||||||
|
.WithMany()
|
||||||
|
.HasForeignKey("PermissionModuleId")
|
||||||
|
.OnDelete(DeleteBehavior.Cascade)
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
b.HasOne("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.PermissionSystem", "PermissionSystem")
|
||||||
|
.WithMany()
|
||||||
|
.HasForeignKey("PermissionSystemId")
|
||||||
|
.OnDelete(DeleteBehavior.Cascade)
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
b.Navigation("PermissionModule");
|
||||||
|
|
||||||
|
b.Navigation("PermissionSystem");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.PermissionSystemModuleOperation", b =>
|
||||||
|
{
|
||||||
|
b.HasOne("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.PermissionOperation", "PermissionOperation")
|
||||||
|
.WithMany()
|
||||||
|
.HasForeignKey("PermissionOperationId")
|
||||||
|
.OnDelete(DeleteBehavior.Cascade)
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
b.HasOne("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.PermissionSystemModule", "PermissionSystemModule")
|
||||||
|
.WithMany()
|
||||||
|
.HasForeignKey("PermissionSystemModuleId")
|
||||||
|
.OnDelete(DeleteBehavior.Cascade)
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
b.Navigation("PermissionOperation");
|
||||||
|
|
||||||
|
b.Navigation("PermissionSystemModule");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.RolePermissionSystemModuleOperation", b =>
|
||||||
|
{
|
||||||
|
b.HasOne("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.PermissionSystemModuleOperation", "PermissionSystemModuleOperation")
|
||||||
|
.WithMany()
|
||||||
|
.HasForeignKey("PermissionSystemModuleOperationId")
|
||||||
|
.OnDelete(DeleteBehavior.Cascade)
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
b.HasOne("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.Role", "Role")
|
||||||
|
.WithMany()
|
||||||
|
.HasForeignKey("RoleId")
|
||||||
|
.OnDelete(DeleteBehavior.Cascade)
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
b.Navigation("PermissionSystemModuleOperation");
|
||||||
|
|
||||||
|
b.Navigation("Role");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.User", b =>
|
||||||
|
{
|
||||||
|
b.HasOne("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.Role", "Role")
|
||||||
|
.WithMany()
|
||||||
|
.HasForeignKey("RoleId")
|
||||||
|
.OnDelete(DeleteBehavior.Cascade)
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
b.Navigation("Role");
|
||||||
|
});
|
||||||
|
#pragma warning restore 612, 618
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
283
MainProject/Migrations/20250426183010_AddingPermissionsTables.cs
Normal file
283
MainProject/Migrations/20250426183010_AddingPermissionsTables.cs
Normal file
@@ -0,0 +1,283 @@
|
|||||||
|
using System;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace MainProject.Migrations
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
public partial class AddingPermissionsTables : Migration
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.CreateTable(
|
||||||
|
name: "PermissionModules",
|
||||||
|
columns: table => new
|
||||||
|
{
|
||||||
|
Id = table.Column<int>(type: "int", nullable: false)
|
||||||
|
.Annotation("SqlServer:Identity", "1, 1"),
|
||||||
|
Name = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: false),
|
||||||
|
Enabled = table.Column<bool>(type: "bit", nullable: false),
|
||||||
|
Guid = table.Column<string>(type: "nvarchar(45)", maxLength: 45, nullable: false),
|
||||||
|
IsDeleted = table.Column<bool>(type: "bit", nullable: false),
|
||||||
|
CreationTime = table.Column<DateTime>(type: "datetime2", nullable: false),
|
||||||
|
CreationUserId = table.Column<int>(type: "int", nullable: true),
|
||||||
|
UpdateTime = table.Column<DateTime>(type: "datetime2", nullable: true),
|
||||||
|
UpdateUserId = table.Column<int>(type: "int", nullable: true),
|
||||||
|
DeletionTime = table.Column<DateTime>(type: "datetime2", nullable: true),
|
||||||
|
DeletionUserId = table.Column<int>(type: "int", nullable: true)
|
||||||
|
},
|
||||||
|
constraints: table =>
|
||||||
|
{
|
||||||
|
table.PrimaryKey("PK_PermissionModules", x => x.Id);
|
||||||
|
});
|
||||||
|
|
||||||
|
migrationBuilder.CreateTable(
|
||||||
|
name: "PermissionOperations",
|
||||||
|
columns: table => new
|
||||||
|
{
|
||||||
|
Id = table.Column<int>(type: "int", nullable: false)
|
||||||
|
.Annotation("SqlServer:Identity", "1, 1"),
|
||||||
|
Name = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: false),
|
||||||
|
Guid = table.Column<string>(type: "nvarchar(45)", maxLength: 45, nullable: false),
|
||||||
|
IsDeleted = table.Column<bool>(type: "bit", nullable: false),
|
||||||
|
CreationTime = table.Column<DateTime>(type: "datetime2", nullable: false),
|
||||||
|
CreationUserId = table.Column<int>(type: "int", nullable: true),
|
||||||
|
UpdateTime = table.Column<DateTime>(type: "datetime2", nullable: true),
|
||||||
|
UpdateUserId = table.Column<int>(type: "int", nullable: true),
|
||||||
|
DeletionTime = table.Column<DateTime>(type: "datetime2", nullable: true),
|
||||||
|
DeletionUserId = table.Column<int>(type: "int", nullable: true)
|
||||||
|
},
|
||||||
|
constraints: table =>
|
||||||
|
{
|
||||||
|
table.PrimaryKey("PK_PermissionOperations", x => x.Id);
|
||||||
|
});
|
||||||
|
|
||||||
|
migrationBuilder.CreateTable(
|
||||||
|
name: "PermissionSystems",
|
||||||
|
columns: table => new
|
||||||
|
{
|
||||||
|
Id = table.Column<int>(type: "int", nullable: false)
|
||||||
|
.Annotation("SqlServer:Identity", "1, 1"),
|
||||||
|
Name = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: false),
|
||||||
|
Enabled = table.Column<bool>(type: "bit", nullable: false),
|
||||||
|
Guid = table.Column<string>(type: "nvarchar(45)", maxLength: 45, nullable: false),
|
||||||
|
IsDeleted = table.Column<bool>(type: "bit", nullable: false),
|
||||||
|
CreationTime = table.Column<DateTime>(type: "datetime2", nullable: false),
|
||||||
|
CreationUserId = table.Column<int>(type: "int", nullable: true),
|
||||||
|
UpdateTime = table.Column<DateTime>(type: "datetime2", nullable: true),
|
||||||
|
UpdateUserId = table.Column<int>(type: "int", nullable: true),
|
||||||
|
DeletionTime = table.Column<DateTime>(type: "datetime2", nullable: true),
|
||||||
|
DeletionUserId = table.Column<int>(type: "int", nullable: true)
|
||||||
|
},
|
||||||
|
constraints: table =>
|
||||||
|
{
|
||||||
|
table.PrimaryKey("PK_PermissionSystems", x => x.Id);
|
||||||
|
});
|
||||||
|
|
||||||
|
migrationBuilder.CreateTable(
|
||||||
|
name: "PermissionSystemModules",
|
||||||
|
columns: table => new
|
||||||
|
{
|
||||||
|
Id = table.Column<int>(type: "int", nullable: false)
|
||||||
|
.Annotation("SqlServer:Identity", "1, 1"),
|
||||||
|
PermissionSystemId = table.Column<int>(type: "int", nullable: false),
|
||||||
|
PermissionModuleId = table.Column<int>(type: "int", nullable: false),
|
||||||
|
Enabled = table.Column<bool>(type: "bit", nullable: false),
|
||||||
|
Guid = table.Column<string>(type: "nvarchar(45)", maxLength: 45, nullable: false),
|
||||||
|
IsDeleted = table.Column<bool>(type: "bit", nullable: false),
|
||||||
|
CreationTime = table.Column<DateTime>(type: "datetime2", nullable: false),
|
||||||
|
CreationUserId = table.Column<int>(type: "int", nullable: true),
|
||||||
|
UpdateTime = table.Column<DateTime>(type: "datetime2", nullable: true),
|
||||||
|
UpdateUserId = table.Column<int>(type: "int", nullable: true),
|
||||||
|
DeletionTime = table.Column<DateTime>(type: "datetime2", nullable: true),
|
||||||
|
DeletionUserId = table.Column<int>(type: "int", nullable: true)
|
||||||
|
},
|
||||||
|
constraints: table =>
|
||||||
|
{
|
||||||
|
table.PrimaryKey("PK_PermissionSystemModules", x => x.Id);
|
||||||
|
table.ForeignKey(
|
||||||
|
name: "FK_PermissionSystemModules_PermissionModules_PermissionModuleId",
|
||||||
|
column: x => x.PermissionModuleId,
|
||||||
|
principalTable: "PermissionModules",
|
||||||
|
principalColumn: "Id",
|
||||||
|
onDelete: ReferentialAction.Cascade);
|
||||||
|
table.ForeignKey(
|
||||||
|
name: "FK_PermissionSystemModules_PermissionSystems_PermissionSystemId",
|
||||||
|
column: x => x.PermissionSystemId,
|
||||||
|
principalTable: "PermissionSystems",
|
||||||
|
principalColumn: "Id",
|
||||||
|
onDelete: ReferentialAction.Cascade);
|
||||||
|
});
|
||||||
|
|
||||||
|
migrationBuilder.CreateTable(
|
||||||
|
name: "PermissionSystemModuleOperations",
|
||||||
|
columns: table => new
|
||||||
|
{
|
||||||
|
Id = table.Column<int>(type: "int", nullable: false)
|
||||||
|
.Annotation("SqlServer:Identity", "1, 1"),
|
||||||
|
PermissionSystemModuleId = table.Column<int>(type: "int", nullable: false),
|
||||||
|
PermissionOperationId = table.Column<int>(type: "int", nullable: false),
|
||||||
|
Enabled = table.Column<bool>(type: "bit", nullable: false),
|
||||||
|
Guid = table.Column<string>(type: "nvarchar(45)", maxLength: 45, nullable: false),
|
||||||
|
IsDeleted = table.Column<bool>(type: "bit", nullable: false),
|
||||||
|
CreationTime = table.Column<DateTime>(type: "datetime2", nullable: false),
|
||||||
|
CreationUserId = table.Column<int>(type: "int", nullable: true),
|
||||||
|
UpdateTime = table.Column<DateTime>(type: "datetime2", nullable: true),
|
||||||
|
UpdateUserId = table.Column<int>(type: "int", nullable: true),
|
||||||
|
DeletionTime = table.Column<DateTime>(type: "datetime2", nullable: true),
|
||||||
|
DeletionUserId = table.Column<int>(type: "int", nullable: true)
|
||||||
|
},
|
||||||
|
constraints: table =>
|
||||||
|
{
|
||||||
|
table.PrimaryKey("PK_PermissionSystemModuleOperations", x => x.Id);
|
||||||
|
table.ForeignKey(
|
||||||
|
name: "FK_PermissionSystemModuleOperations_PermissionOperations_PermissionOperationId",
|
||||||
|
column: x => x.PermissionOperationId,
|
||||||
|
principalTable: "PermissionOperations",
|
||||||
|
principalColumn: "Id",
|
||||||
|
onDelete: ReferentialAction.Cascade);
|
||||||
|
table.ForeignKey(
|
||||||
|
name: "FK_PermissionSystemModuleOperations_PermissionSystemModules_PermissionSystemModuleId",
|
||||||
|
column: x => x.PermissionSystemModuleId,
|
||||||
|
principalTable: "PermissionSystemModules",
|
||||||
|
principalColumn: "Id",
|
||||||
|
onDelete: ReferentialAction.Cascade);
|
||||||
|
});
|
||||||
|
|
||||||
|
migrationBuilder.CreateTable(
|
||||||
|
name: "RolePermissionSystemModuleOperations",
|
||||||
|
columns: table => new
|
||||||
|
{
|
||||||
|
Id = table.Column<int>(type: "int", nullable: false)
|
||||||
|
.Annotation("SqlServer:Identity", "1, 1"),
|
||||||
|
RoleId = table.Column<int>(type: "int", nullable: false),
|
||||||
|
PermissionSystemModuleOperationId = table.Column<int>(type: "int", nullable: false),
|
||||||
|
Active = table.Column<bool>(type: "bit", nullable: false),
|
||||||
|
Guid = table.Column<string>(type: "nvarchar(45)", maxLength: 45, nullable: false),
|
||||||
|
IsDeleted = table.Column<bool>(type: "bit", nullable: false),
|
||||||
|
CreationTime = table.Column<DateTime>(type: "datetime2", nullable: false),
|
||||||
|
CreationUserId = table.Column<int>(type: "int", nullable: true),
|
||||||
|
UpdateTime = table.Column<DateTime>(type: "datetime2", nullable: true),
|
||||||
|
UpdateUserId = table.Column<int>(type: "int", nullable: true),
|
||||||
|
DeletionTime = table.Column<DateTime>(type: "datetime2", nullable: true),
|
||||||
|
DeletionUserId = table.Column<int>(type: "int", nullable: true)
|
||||||
|
},
|
||||||
|
constraints: table =>
|
||||||
|
{
|
||||||
|
table.PrimaryKey("PK_RolePermissionSystemModuleOperations", x => x.Id);
|
||||||
|
table.ForeignKey(
|
||||||
|
name: "FK_RolePermissionSystemModuleOperations_PermissionSystemModuleOperations_PermissionSystemModuleOperationId",
|
||||||
|
column: x => x.PermissionSystemModuleOperationId,
|
||||||
|
principalTable: "PermissionSystemModuleOperations",
|
||||||
|
principalColumn: "Id",
|
||||||
|
onDelete: ReferentialAction.Cascade);
|
||||||
|
table.ForeignKey(
|
||||||
|
name: "FK_RolePermissionSystemModuleOperations_Roles_RoleId",
|
||||||
|
column: x => x.RoleId,
|
||||||
|
principalTable: "Roles",
|
||||||
|
principalColumn: "Id",
|
||||||
|
onDelete: ReferentialAction.Cascade);
|
||||||
|
});
|
||||||
|
|
||||||
|
migrationBuilder.CreateIndex(
|
||||||
|
name: "IX_Enabled",
|
||||||
|
table: "PermissionModules",
|
||||||
|
column: "Enabled",
|
||||||
|
filter: "[Enabled] = 1");
|
||||||
|
|
||||||
|
migrationBuilder.CreateIndex(
|
||||||
|
name: "IX_IsDeleted",
|
||||||
|
table: "PermissionModules",
|
||||||
|
column: "IsDeleted",
|
||||||
|
filter: "[IsDeleted] = 0");
|
||||||
|
|
||||||
|
migrationBuilder.CreateIndex(
|
||||||
|
name: "IX_IsDeleted_Name_Enabled",
|
||||||
|
table: "PermissionModules",
|
||||||
|
columns: new[] { "IsDeleted", "Name", "Enabled" },
|
||||||
|
filter: "[IsDeleted] = 0");
|
||||||
|
|
||||||
|
migrationBuilder.CreateIndex(
|
||||||
|
name: "IX_IsDeleted_Name",
|
||||||
|
table: "PermissionOperations",
|
||||||
|
columns: new[] { "IsDeleted", "Name" });
|
||||||
|
|
||||||
|
migrationBuilder.CreateIndex(
|
||||||
|
name: "IX_IsDeleted_Enabled_Guid",
|
||||||
|
table: "PermissionSystemModuleOperations",
|
||||||
|
columns: new[] { "IsDeleted", "Enabled", "Guid" });
|
||||||
|
|
||||||
|
migrationBuilder.CreateIndex(
|
||||||
|
name: "IX_PermissionSystemModuleOperations_PermissionOperationId",
|
||||||
|
table: "PermissionSystemModuleOperations",
|
||||||
|
column: "PermissionOperationId");
|
||||||
|
|
||||||
|
migrationBuilder.CreateIndex(
|
||||||
|
name: "IX_PermissionSystemModuleOperations_PermissionSystemModuleId",
|
||||||
|
table: "PermissionSystemModuleOperations",
|
||||||
|
column: "PermissionSystemModuleId");
|
||||||
|
|
||||||
|
migrationBuilder.CreateIndex(
|
||||||
|
name: "IX_PermissionSystemModules_PermissionModuleId",
|
||||||
|
table: "PermissionSystemModules",
|
||||||
|
column: "PermissionModuleId");
|
||||||
|
|
||||||
|
migrationBuilder.CreateIndex(
|
||||||
|
name: "IX_PermissionSystemModules_PermissionSystemId",
|
||||||
|
table: "PermissionSystemModules",
|
||||||
|
column: "PermissionSystemId");
|
||||||
|
|
||||||
|
migrationBuilder.CreateIndex(
|
||||||
|
name: "IX_Enabled",
|
||||||
|
table: "PermissionSystems",
|
||||||
|
column: "Enabled",
|
||||||
|
filter: "[Enabled] = 1");
|
||||||
|
|
||||||
|
migrationBuilder.CreateIndex(
|
||||||
|
name: "IX_IsDeleted",
|
||||||
|
table: "PermissionSystems",
|
||||||
|
column: "IsDeleted",
|
||||||
|
filter: "[IsDeleted] = 0");
|
||||||
|
|
||||||
|
migrationBuilder.CreateIndex(
|
||||||
|
name: "IX_IsDeleted_Name_Enabled",
|
||||||
|
table: "PermissionSystems",
|
||||||
|
columns: new[] { "IsDeleted", "Name", "Enabled" },
|
||||||
|
filter: "[IsDeleted] = 0");
|
||||||
|
|
||||||
|
migrationBuilder.CreateIndex(
|
||||||
|
name: "IX_RolePermissionSystemModuleOperations_PermissionSystemModuleOperationId",
|
||||||
|
table: "RolePermissionSystemModuleOperations",
|
||||||
|
column: "PermissionSystemModuleOperationId");
|
||||||
|
|
||||||
|
migrationBuilder.CreateIndex(
|
||||||
|
name: "IX_RolePermissionSystemModuleOperations_RoleId",
|
||||||
|
table: "RolePermissionSystemModuleOperations",
|
||||||
|
column: "RoleId");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.DropTable(
|
||||||
|
name: "RolePermissionSystemModuleOperations");
|
||||||
|
|
||||||
|
migrationBuilder.DropTable(
|
||||||
|
name: "PermissionSystemModuleOperations");
|
||||||
|
|
||||||
|
migrationBuilder.DropTable(
|
||||||
|
name: "PermissionOperations");
|
||||||
|
|
||||||
|
migrationBuilder.DropTable(
|
||||||
|
name: "PermissionSystemModules");
|
||||||
|
|
||||||
|
migrationBuilder.DropTable(
|
||||||
|
name: "PermissionModules");
|
||||||
|
|
||||||
|
migrationBuilder.DropTable(
|
||||||
|
name: "PermissionSystems");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -17,11 +17,275 @@ namespace MainProject.Migrations
|
|||||||
{
|
{
|
||||||
#pragma warning disable 612, 618
|
#pragma warning disable 612, 618
|
||||||
modelBuilder
|
modelBuilder
|
||||||
.HasAnnotation("ProductVersion", "8.0.8")
|
.HasAnnotation("ProductVersion", "9.0.2")
|
||||||
.HasAnnotation("Relational:MaxIdentifierLength", 128);
|
.HasAnnotation("Relational:MaxIdentifierLength", 128);
|
||||||
|
|
||||||
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
|
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
|
||||||
|
|
||||||
|
modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.PermissionModule", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||||
|
|
||||||
|
b.Property<DateTime>("CreationTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("CreationUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("DeletionTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("DeletionUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<bool>("Enabled")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<string>("Guid")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(45)
|
||||||
|
.HasColumnType("nvarchar(45)");
|
||||||
|
|
||||||
|
b.Property<bool>("IsDeleted")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<string>("Name")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(100)
|
||||||
|
.HasColumnType("nvarchar(100)");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("UpdateTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("UpdateUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex(new[] { "Enabled" }, "IX_Enabled")
|
||||||
|
.HasFilter("[Enabled] = 1");
|
||||||
|
|
||||||
|
b.HasIndex(new[] { "IsDeleted" }, "IX_IsDeleted")
|
||||||
|
.HasFilter("[IsDeleted] = 0");
|
||||||
|
|
||||||
|
b.HasIndex(new[] { "IsDeleted", "Name", "Enabled" }, "IX_IsDeleted_Name_Enabled")
|
||||||
|
.HasFilter("[IsDeleted] = 0");
|
||||||
|
|
||||||
|
b.ToTable("PermissionModules");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.PermissionOperation", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||||
|
|
||||||
|
b.Property<DateTime>("CreationTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("CreationUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("DeletionTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("DeletionUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<string>("Guid")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(45)
|
||||||
|
.HasColumnType("nvarchar(45)");
|
||||||
|
|
||||||
|
b.Property<bool>("IsDeleted")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<string>("Name")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(100)
|
||||||
|
.HasColumnType("nvarchar(100)");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("UpdateTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("UpdateUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex(new[] { "IsDeleted", "Name" }, "IX_IsDeleted_Name");
|
||||||
|
|
||||||
|
b.ToTable("PermissionOperations");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.PermissionSystem", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||||
|
|
||||||
|
b.Property<DateTime>("CreationTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("CreationUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("DeletionTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("DeletionUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<bool>("Enabled")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<string>("Guid")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(45)
|
||||||
|
.HasColumnType("nvarchar(45)");
|
||||||
|
|
||||||
|
b.Property<bool>("IsDeleted")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<string>("Name")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(100)
|
||||||
|
.HasColumnType("nvarchar(100)");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("UpdateTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("UpdateUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex(new[] { "Enabled" }, "IX_Enabled")
|
||||||
|
.HasFilter("[Enabled] = 1");
|
||||||
|
|
||||||
|
b.HasIndex(new[] { "IsDeleted" }, "IX_IsDeleted")
|
||||||
|
.HasFilter("[IsDeleted] = 0");
|
||||||
|
|
||||||
|
b.HasIndex(new[] { "IsDeleted", "Name", "Enabled" }, "IX_IsDeleted_Name_Enabled")
|
||||||
|
.HasFilter("[IsDeleted] = 0");
|
||||||
|
|
||||||
|
b.ToTable("PermissionSystems");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.PermissionSystemModule", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||||
|
|
||||||
|
b.Property<DateTime>("CreationTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("CreationUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("DeletionTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("DeletionUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<bool>("Enabled")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<string>("Guid")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(45)
|
||||||
|
.HasColumnType("nvarchar(45)");
|
||||||
|
|
||||||
|
b.Property<bool>("IsDeleted")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<int>("PermissionModuleId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<int>("PermissionSystemId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("UpdateTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("UpdateUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("PermissionModuleId");
|
||||||
|
|
||||||
|
b.HasIndex("PermissionSystemId");
|
||||||
|
|
||||||
|
b.ToTable("PermissionSystemModules");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.PermissionSystemModuleOperation", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||||
|
|
||||||
|
b.Property<DateTime>("CreationTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("CreationUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("DeletionTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("DeletionUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<bool>("Enabled")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<string>("Guid")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(45)
|
||||||
|
.HasColumnType("nvarchar(45)");
|
||||||
|
|
||||||
|
b.Property<bool>("IsDeleted")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<int>("PermissionOperationId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<int>("PermissionSystemModuleId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("UpdateTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("UpdateUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("PermissionOperationId");
|
||||||
|
|
||||||
|
b.HasIndex("PermissionSystemModuleId");
|
||||||
|
|
||||||
|
b.HasIndex(new[] { "IsDeleted", "Enabled", "Guid" }, "IX_IsDeleted_Enabled_Guid");
|
||||||
|
|
||||||
|
b.ToTable("PermissionSystemModuleOperations");
|
||||||
|
});
|
||||||
|
|
||||||
modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.Role", b =>
|
modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.Role", b =>
|
||||||
{
|
{
|
||||||
b.Property<int>("Id")
|
b.Property<int>("Id")
|
||||||
@@ -33,28 +297,95 @@ namespace MainProject.Migrations
|
|||||||
b.Property<DateTime>("CreationTime")
|
b.Property<DateTime>("CreationTime")
|
||||||
.HasColumnType("datetime2");
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
b.Property<int>("CreationUserId")
|
b.Property<int?>("CreationUserId")
|
||||||
.HasColumnType("int");
|
.HasColumnType("int");
|
||||||
|
|
||||||
b.Property<DateTime>("DeletionTime")
|
b.Property<DateTime?>("DeletionTime")
|
||||||
.HasColumnType("datetime2");
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
b.Property<int>("DeletionUserId")
|
b.Property<int?>("DeletionUserId")
|
||||||
.HasColumnType("int");
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<string>("Guid")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(45)
|
||||||
|
.HasColumnType("nvarchar(45)");
|
||||||
|
|
||||||
|
b.Property<bool>("IsDeleted")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<bool>("IsNotEditable")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
b.Property<string>("Name")
|
b.Property<string>("Name")
|
||||||
.IsRequired()
|
.IsRequired()
|
||||||
.HasColumnType("nvarchar(max)");
|
.HasMaxLength(100)
|
||||||
|
.HasColumnType("nvarchar(100)");
|
||||||
|
|
||||||
b.Property<DateTime>("UpdateTime")
|
b.Property<DateTime?>("UpdateTime")
|
||||||
.HasColumnType("datetime2");
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
b.Property<int>("UpdateUserId")
|
b.Property<int?>("UpdateUserId")
|
||||||
.HasColumnType("int");
|
.HasColumnType("int");
|
||||||
|
|
||||||
b.HasKey("Id");
|
b.HasKey("Id");
|
||||||
|
|
||||||
b.ToTable("Role");
|
b.HasIndex(new[] { "IsDeleted", "Guid" }, "IX_IsDeleted_Guid")
|
||||||
|
.HasFilter("[IsDeleted] = 0");
|
||||||
|
|
||||||
|
b.ToTable("Roles");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.RolePermissionSystemModuleOperation", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||||
|
|
||||||
|
b.Property<bool>("Active")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<DateTime>("CreationTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("CreationUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("DeletionTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("DeletionUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<string>("Guid")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(45)
|
||||||
|
.HasColumnType("nvarchar(45)");
|
||||||
|
|
||||||
|
b.Property<bool>("IsDeleted")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<int>("PermissionSystemModuleOperationId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<int>("RoleId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("UpdateTime")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int?>("UpdateUserId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("PermissionSystemModuleOperationId");
|
||||||
|
|
||||||
|
b.HasIndex("RoleId");
|
||||||
|
|
||||||
|
b.ToTable("RolePermissionSystemModuleOperations");
|
||||||
});
|
});
|
||||||
|
|
||||||
modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.User", b =>
|
modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.User", b =>
|
||||||
@@ -68,24 +399,42 @@ namespace MainProject.Migrations
|
|||||||
b.Property<DateTime>("CreationTime")
|
b.Property<DateTime>("CreationTime")
|
||||||
.HasColumnType("datetime2");
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
b.Property<int>("CreationUserId")
|
b.Property<int?>("CreationUserId")
|
||||||
.HasColumnType("int");
|
.HasColumnType("int");
|
||||||
|
|
||||||
b.Property<DateTime>("DeletionTime")
|
b.Property<DateTime?>("DeletionTime")
|
||||||
.HasColumnType("datetime2");
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
b.Property<int>("DeletionUserId")
|
b.Property<int?>("DeletionUserId")
|
||||||
.HasColumnType("int");
|
.HasColumnType("int");
|
||||||
|
|
||||||
b.Property<string>("Email")
|
b.Property<string>("Email")
|
||||||
.IsRequired()
|
.IsRequired()
|
||||||
.HasColumnType("nvarchar(max)");
|
.HasMaxLength(200)
|
||||||
|
.HasColumnType("nvarchar(200)");
|
||||||
|
|
||||||
b.Property<string>("FirstName")
|
b.Property<string>("FirstName")
|
||||||
.IsRequired()
|
.IsRequired()
|
||||||
.HasColumnType("nvarchar(max)");
|
.HasMaxLength(200)
|
||||||
|
.HasColumnType("nvarchar(200)");
|
||||||
|
|
||||||
|
b.Property<string>("Guid")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(45)
|
||||||
|
.HasColumnType("nvarchar(45)");
|
||||||
|
|
||||||
|
b.Property<bool>("IsDeleted")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
|
b.Property<bool>("IsTestUser")
|
||||||
|
.HasColumnType("bit");
|
||||||
|
|
||||||
b.Property<string>("LastName")
|
b.Property<string>("LastName")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(200)
|
||||||
|
.HasColumnType("nvarchar(200)");
|
||||||
|
|
||||||
|
b.Property<string>("Password")
|
||||||
.IsRequired()
|
.IsRequired()
|
||||||
.HasColumnType("nvarchar(max)");
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
@@ -93,26 +442,88 @@ namespace MainProject.Migrations
|
|||||||
.IsRequired()
|
.IsRequired()
|
||||||
.HasColumnType("nvarchar(max)");
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<string>("PasswordSalt")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
b.Property<int>("RoleId")
|
b.Property<int>("RoleId")
|
||||||
.HasColumnType("int");
|
.HasColumnType("int");
|
||||||
|
|
||||||
b.Property<DateTime>("UpdateTime")
|
b.Property<DateTime?>("UpdateTime")
|
||||||
.HasColumnType("datetime2");
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
b.Property<int>("UpdateUserId")
|
b.Property<int?>("UpdateUserId")
|
||||||
.HasColumnType("int");
|
.HasColumnType("int");
|
||||||
|
|
||||||
b.Property<string>("Username")
|
|
||||||
.IsRequired()
|
|
||||||
.HasColumnType("nvarchar(max)");
|
|
||||||
|
|
||||||
b.HasKey("Id");
|
b.HasKey("Id");
|
||||||
|
|
||||||
b.HasIndex("RoleId");
|
b.HasIndex("RoleId");
|
||||||
|
|
||||||
|
b.HasIndex(new[] { "Email" }, "IX_Email");
|
||||||
|
|
||||||
|
b.HasIndex(new[] { "IsDeleted", "Guid" }, "IX_IsDeleted_Guid")
|
||||||
|
.HasFilter("[IsDeleted] = 0");
|
||||||
|
|
||||||
b.ToTable("Users");
|
b.ToTable("Users");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.PermissionSystemModule", b =>
|
||||||
|
{
|
||||||
|
b.HasOne("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.PermissionModule", "PermissionModule")
|
||||||
|
.WithMany()
|
||||||
|
.HasForeignKey("PermissionModuleId")
|
||||||
|
.OnDelete(DeleteBehavior.Cascade)
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
b.HasOne("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.PermissionSystem", "PermissionSystem")
|
||||||
|
.WithMany()
|
||||||
|
.HasForeignKey("PermissionSystemId")
|
||||||
|
.OnDelete(DeleteBehavior.Cascade)
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
b.Navigation("PermissionModule");
|
||||||
|
|
||||||
|
b.Navigation("PermissionSystem");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.PermissionSystemModuleOperation", b =>
|
||||||
|
{
|
||||||
|
b.HasOne("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.PermissionOperation", "PermissionOperation")
|
||||||
|
.WithMany()
|
||||||
|
.HasForeignKey("PermissionOperationId")
|
||||||
|
.OnDelete(DeleteBehavior.Cascade)
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
b.HasOne("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.PermissionSystemModule", "PermissionSystemModule")
|
||||||
|
.WithMany()
|
||||||
|
.HasForeignKey("PermissionSystemModuleId")
|
||||||
|
.OnDelete(DeleteBehavior.Cascade)
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
b.Navigation("PermissionOperation");
|
||||||
|
|
||||||
|
b.Navigation("PermissionSystemModule");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.RolePermissionSystemModuleOperation", b =>
|
||||||
|
{
|
||||||
|
b.HasOne("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.PermissionSystemModuleOperation", "PermissionSystemModuleOperation")
|
||||||
|
.WithMany()
|
||||||
|
.HasForeignKey("PermissionSystemModuleOperationId")
|
||||||
|
.OnDelete(DeleteBehavior.Cascade)
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
b.HasOne("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.Role", "Role")
|
||||||
|
.WithMany()
|
||||||
|
.HasForeignKey("RoleId")
|
||||||
|
.OnDelete(DeleteBehavior.Cascade)
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
b.Navigation("PermissionSystemModuleOperation");
|
||||||
|
|
||||||
|
b.Navigation("Role");
|
||||||
|
});
|
||||||
|
|
||||||
modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.User", b =>
|
modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.User", b =>
|
||||||
{
|
{
|
||||||
b.HasOne("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.Role", "Role")
|
b.HasOne("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.Role", "Role")
|
||||||
|
|||||||
11
MainProject/Models/Api/Common/Exceptions/CreateException.cs
Normal file
11
MainProject/Models/Api/Common/Exceptions/CreateException.cs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Models.Api.Common.Exceptions;
|
||||||
|
|
||||||
|
public class CreateException : Exception
|
||||||
|
{
|
||||||
|
public CreateException(string message, Exception innerException)
|
||||||
|
: base(message, innerException)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
11
MainProject/Models/Api/Common/Exceptions/UpdateException.cs
Normal file
11
MainProject/Models/Api/Common/Exceptions/UpdateException.cs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Models.Api.Common.Exceptions;
|
||||||
|
|
||||||
|
public class UpdateException : Exception
|
||||||
|
{
|
||||||
|
public UpdateException(string message, Exception innerException)
|
||||||
|
: base(message, innerException)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
14
MainProject/Models/Api/Common/Role/RoleDto.cs
Normal file
14
MainProject/Models/Api/Common/Role/RoleDto.cs
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
namespace BasicDotnetTemplate.MainProject.Models.Api.Common.Role;
|
||||||
|
|
||||||
|
public class RoleDto
|
||||||
|
{
|
||||||
|
#nullable enable
|
||||||
|
public string? Guid { get; set; }
|
||||||
|
public string? Name { get; set; }
|
||||||
|
public bool? IsNotEditable { get; set; }
|
||||||
|
#nullable disable
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
19
MainProject/Models/Api/Common/Role/UserRole.cs
Normal file
19
MainProject/Models/Api/Common/Role/UserRole.cs
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
using DatabaseSqlServer = BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Models.Api.Common.Role;
|
||||||
|
|
||||||
|
public class UserRole
|
||||||
|
{
|
||||||
|
#nullable enable
|
||||||
|
public string? Guid { get; set; }
|
||||||
|
public string? Name { get; set; }
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
public UserRole() {}
|
||||||
|
|
||||||
|
public UserRole(DatabaseSqlServer.Role role)
|
||||||
|
{
|
||||||
|
Guid = role.Guid;
|
||||||
|
Name = role.Name;
|
||||||
|
}
|
||||||
|
}
|
||||||
28
MainProject/Models/Api/Common/User/AuthenticatedUser.cs
Normal file
28
MainProject/Models/Api/Common/User/AuthenticatedUser.cs
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Common.Role;
|
||||||
|
using DatabaseSqlServer = BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Models.Api.Common.User;
|
||||||
|
|
||||||
|
public class AuthenticatedUser
|
||||||
|
{
|
||||||
|
#nullable enable
|
||||||
|
public string? Guid { get; set; }
|
||||||
|
public string? FirstName { get; set; }
|
||||||
|
public string? LastName { get; set; }
|
||||||
|
public string? Email { get; set; }
|
||||||
|
public UserRole? Role { get; set; }
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
public AuthenticatedUser(DatabaseSqlServer.User user)
|
||||||
|
{
|
||||||
|
Guid = user.Guid;
|
||||||
|
FirstName = user.FirstName;
|
||||||
|
LastName = user.LastName;
|
||||||
|
Email = user.Email;
|
||||||
|
Role = new UserRole();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
18
MainProject/Models/Api/Common/User/UserDto.cs
Normal file
18
MainProject/Models/Api/Common/User/UserDto.cs
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Common.Role;
|
||||||
|
using DatabaseSqlServer = BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Models.Api.Common.User;
|
||||||
|
|
||||||
|
public class UserDto
|
||||||
|
{
|
||||||
|
#nullable enable
|
||||||
|
public string? Guid { get; set; }
|
||||||
|
public string? FirstName { get; set; }
|
||||||
|
public string? LastName { get; set; }
|
||||||
|
public string? Email { get; set; }
|
||||||
|
#nullable disable
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
13
MainProject/Models/Api/Data/Auth/AuthenticateRequestData.cs
Normal file
13
MainProject/Models/Api/Data/Auth/AuthenticateRequestData.cs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
namespace BasicDotnetTemplate.MainProject.Models.Api.Data.Auth;
|
||||||
|
|
||||||
|
public class AuthenticateRequestData
|
||||||
|
{
|
||||||
|
#nullable enable
|
||||||
|
public string? Email { get; set; }
|
||||||
|
public string? Password { get; set; }
|
||||||
|
#nullable disable
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
namespace BasicDotnetTemplate.MainProject.Models.Api.Data.Role;
|
||||||
|
|
||||||
|
public class CreateRoleRequestData
|
||||||
|
{
|
||||||
|
public string Name { get; set; } = String.Empty;
|
||||||
|
public required bool IsNotEditable { get; set; }
|
||||||
|
|
||||||
|
}
|
||||||
15
MainProject/Models/Api/Data/User/CreateUserRequestData.cs
Normal file
15
MainProject/Models/Api/Data/User/CreateUserRequestData.cs
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
namespace BasicDotnetTemplate.MainProject.Models.Api.Data.User;
|
||||||
|
|
||||||
|
public class CreateUserRequestData
|
||||||
|
{
|
||||||
|
public string FirstName { get; set; } = String.Empty;
|
||||||
|
public string LastName { get; set; } = String.Empty;
|
||||||
|
public string Email { get; set; } = String.Empty;
|
||||||
|
public string Password { get; set; } = String.Empty;
|
||||||
|
public string? RoleGuid { get; set; }
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
14
MainProject/Models/Api/Request/Auth/AuthenticateRequest.cs
Normal file
14
MainProject/Models/Api/Request/Auth/AuthenticateRequest.cs
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Data.Auth;
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Models.Api.Request.Auth;
|
||||||
|
|
||||||
|
public class AuthenticateRequest
|
||||||
|
{
|
||||||
|
#nullable enable
|
||||||
|
public AuthenticateRequestData? Data { get; set; }
|
||||||
|
#nullable disable
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
10
MainProject/Models/Api/Request/Role/CreateRoleRequest.cs
Normal file
10
MainProject/Models/Api/Request/Role/CreateRoleRequest.cs
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Data.Role;
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Models.Api.Request.Role;
|
||||||
|
|
||||||
|
public class CreateRoleRequest
|
||||||
|
{
|
||||||
|
#nullable enable
|
||||||
|
public CreateRoleRequestData? Data { get; set; }
|
||||||
|
#nullable disable
|
||||||
|
}
|
||||||
14
MainProject/Models/Api/Request/User/CreateUserRequest.cs
Normal file
14
MainProject/Models/Api/Request/User/CreateUserRequest.cs
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Data.User;
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Models.Api.Request.User;
|
||||||
|
|
||||||
|
public class CreateUserRequest
|
||||||
|
{
|
||||||
|
#nullable enable
|
||||||
|
public CreateUserRequestData? Data { get; set; }
|
||||||
|
#nullable disable
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Common.User;
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Models.Api.Response.Auth;
|
||||||
|
|
||||||
|
public class AuthenticateResponse : BaseResponse<AuthenticatedUser>
|
||||||
|
{
|
||||||
|
public AuthenticateResponse(int status, string? message, AuthenticatedUser? data) : base(status, message, data) { }
|
||||||
|
}
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
namespace BasicDotnetTemplate.MainProject.Models.Api.Response;
|
namespace BasicDotnetTemplate.MainProject.Models.Api.Response;
|
||||||
|
|
||||||
public class BaseResponse
|
public class BaseResponse<T>
|
||||||
{
|
{
|
||||||
#nullable enable
|
#nullable enable
|
||||||
public BaseResponse(int status, string? message, dynamic? data)
|
public BaseResponse(int status, string? message, T? data)
|
||||||
{
|
{
|
||||||
this.Status = status;
|
this.Status = status;
|
||||||
this.Message = message;
|
this.Message = message;
|
||||||
|
|||||||
8
MainProject/Models/Api/Response/Role/GetRoleResponse.cs
Normal file
8
MainProject/Models/Api/Response/Role/GetRoleResponse.cs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Common.Role;
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Models.Api.Response.Role;
|
||||||
|
|
||||||
|
public class GetRoleResponse : BaseResponse<RoleDto>
|
||||||
|
{
|
||||||
|
public GetRoleResponse(int status, string? message, RoleDto? data) : base(status, message, data) { }
|
||||||
|
}
|
||||||
8
MainProject/Models/Api/Response/User/GetUserResponse.cs
Normal file
8
MainProject/Models/Api/Response/User/GetUserResponse.cs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Common.User;
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Models.Api.Response.User;
|
||||||
|
|
||||||
|
public class GetUserResponse : BaseResponse<UserDto>
|
||||||
|
{
|
||||||
|
public GetUserResponse(int status, string? message, UserDto? data) : base(status, message, data) { }
|
||||||
|
}
|
||||||
9
MainProject/Models/Common/OperationInfo.cs
Normal file
9
MainProject/Models/Common/OperationInfo.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
namespace BasicDotnetTemplate.MainProject.Models.Common;
|
||||||
|
|
||||||
|
public class OperationInfo
|
||||||
|
{
|
||||||
|
#nullable enable
|
||||||
|
public string? Operation { get; set; }
|
||||||
|
public List<string>? Roles {get; set; }
|
||||||
|
#nullable disable
|
||||||
|
}
|
||||||
9
MainProject/Models/Common/PermissionInfo.cs
Normal file
9
MainProject/Models/Common/PermissionInfo.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
namespace BasicDotnetTemplate.MainProject.Models.Common;
|
||||||
|
|
||||||
|
public class PermissionInfo
|
||||||
|
{
|
||||||
|
#nullable enable
|
||||||
|
public string? System { get; set; }
|
||||||
|
public List<RolePermissionModuleOperation>? RolePermissionModuleOperations {get; set; }
|
||||||
|
#nullable disable
|
||||||
|
}
|
||||||
8
MainProject/Models/Common/PermissionsFile.cs
Normal file
8
MainProject/Models/Common/PermissionsFile.cs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
namespace BasicDotnetTemplate.MainProject.Models.Common;
|
||||||
|
|
||||||
|
public class PermissionsFile
|
||||||
|
{
|
||||||
|
#nullable enable
|
||||||
|
public List<PermissionInfo>? PermissionInfos { get; set; }
|
||||||
|
#nullable disable
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
namespace BasicDotnetTemplate.MainProject.Models.Common;
|
||||||
|
|
||||||
|
public class RolePermissionModuleOperation
|
||||||
|
{
|
||||||
|
#nullable enable
|
||||||
|
public string? Module { get; set; }
|
||||||
|
public List<OperationInfo>? Operations { get; set; }
|
||||||
|
#nullable disable
|
||||||
|
}
|
||||||
@@ -1,16 +1,24 @@
|
|||||||
namespace BasicDotnetTemplate.MainProject.Models.Database.SqlServer
|
using System.ComponentModel.DataAnnotations;
|
||||||
{
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||||
|
|
||||||
public class Base
|
public class Base
|
||||||
{
|
{
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
|
[MaxLength(45)]
|
||||||
|
public string Guid { get; set; }
|
||||||
|
public bool IsDeleted { get; set; }
|
||||||
public DateTime CreationTime { get; set; }
|
public DateTime CreationTime { get; set; }
|
||||||
public int CreationUserId { get; set; }
|
#nullable enable
|
||||||
public DateTime UpdateTime { get; set; }
|
public int? CreationUserId { get; set; }
|
||||||
public int UpdateUserId { get; set; }
|
public DateTime? UpdateTime { get; set; }
|
||||||
public DateTime DeletionTime { get; set; }
|
public int? UpdateUserId { get; set; }
|
||||||
public int DeletionUserId { get; set; }
|
public DateTime? DeletionTime { get; set; }
|
||||||
}
|
public int? DeletionUserId { get; set; }
|
||||||
|
#nullable disable
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
11
MainProject/Models/Database/SqlServer/PermissionModule.cs
Normal file
11
MainProject/Models/Database/SqlServer/PermissionModule.cs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Models.Database.SqlServer
|
||||||
|
{
|
||||||
|
public class PermissionModule : Base
|
||||||
|
{
|
||||||
|
[MaxLength(100)]
|
||||||
|
public required string Name { get; set; }
|
||||||
|
public required bool Enabled { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
10
MainProject/Models/Database/SqlServer/PermissionOperation.cs
Normal file
10
MainProject/Models/Database/SqlServer/PermissionOperation.cs
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Models.Database.SqlServer
|
||||||
|
{
|
||||||
|
public class PermissionOperation : Base
|
||||||
|
{
|
||||||
|
[MaxLength(100)]
|
||||||
|
public required string Name { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
11
MainProject/Models/Database/SqlServer/PermissionSystem.cs
Normal file
11
MainProject/Models/Database/SqlServer/PermissionSystem.cs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Models.Database.SqlServer
|
||||||
|
{
|
||||||
|
public class PermissionSystem : Base
|
||||||
|
{
|
||||||
|
[MaxLength(100)]
|
||||||
|
public required string Name { get; set; }
|
||||||
|
public required bool Enabled { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Models.Database.SqlServer
|
||||||
|
{
|
||||||
|
public class PermissionSystemModule : Base
|
||||||
|
{
|
||||||
|
public required int PermissionSystemId { get; set; }
|
||||||
|
public required int PermissionModuleId { get; set; }
|
||||||
|
public required PermissionSystem PermissionSystem { get; set; }
|
||||||
|
public required PermissionModule PermissionModule { get; set; }
|
||||||
|
public required bool Enabled { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Models.Database.SqlServer
|
||||||
|
{
|
||||||
|
public class PermissionSystemModuleOperation : Base
|
||||||
|
{
|
||||||
|
public required int PermissionSystemModuleId { get; set; }
|
||||||
|
public required int PermissionOperationId { get; set; }
|
||||||
|
public required bool Enabled { get; set; }
|
||||||
|
public required PermissionSystemModule PermissionSystemModule { get; set; }
|
||||||
|
public required PermissionOperation PermissionOperation { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,10 +1,12 @@
|
|||||||
using System.Text.Json.Serialization;
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
namespace BasicDotnetTemplate.MainProject.Models.Database.SqlServer
|
namespace BasicDotnetTemplate.MainProject.Models.Database.SqlServer
|
||||||
{
|
{
|
||||||
public class Role : Base
|
public class Role : Base
|
||||||
{
|
{
|
||||||
|
[MaxLength(100)]
|
||||||
public required string Name { get; set; }
|
public required string Name { get; set; }
|
||||||
|
public required bool IsNotEditable { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Models.Database.SqlServer
|
||||||
|
{
|
||||||
|
public class RolePermissionSystemModuleOperation : Base
|
||||||
|
{
|
||||||
|
public required int RoleId { get; set; }
|
||||||
|
public required int PermissionSystemModuleOperationId { get; set; }
|
||||||
|
public required bool Active { get; set; }
|
||||||
|
public required Role Role { get; set; }
|
||||||
|
public required PermissionSystemModuleOperation PermissionSystemModuleOperation { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,19 +1,26 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||||
|
|
||||||
namespace BasicDotnetTemplate.MainProject.Models.Database.SqlServer
|
|
||||||
{
|
|
||||||
public class User : Base
|
public class User : Base
|
||||||
{
|
{
|
||||||
public required string Username { get; set; }
|
[MaxLength(200)]
|
||||||
public required string FirstName { get; set; }
|
public required string FirstName { get; set; }
|
||||||
|
[MaxLength(200)]
|
||||||
public required string LastName { get; set; }
|
public required string LastName { get; set; }
|
||||||
|
[MaxLength(200)]
|
||||||
public required string Email { get; set; }
|
public required string Email { get; set; }
|
||||||
|
public required string PasswordSalt { get; set; }
|
||||||
|
public required string PasswordHash { get; set; }
|
||||||
public required Role Role { get; set; }
|
public required Role Role { get; set; }
|
||||||
|
public required bool IsTestUser { get; set; }
|
||||||
|
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public required string PasswordHash { get; set; }
|
public required string Password { get; set; }
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ public class AppSettings
|
|||||||
public PrivateSettings? PrivateSettings { get; set; }
|
public PrivateSettings? PrivateSettings { get; set; }
|
||||||
public OpenApiSettings? OpenApiSettings { get; set; }
|
public OpenApiSettings? OpenApiSettings { get; set; }
|
||||||
public DatabaseSettings? DatabaseSettings { get; set; }
|
public DatabaseSettings? DatabaseSettings { get; set; }
|
||||||
|
public JwtSettings? JwtSettings { get; set; }
|
||||||
|
public EncryptionSettings? EncryptionSettings { get; set; }
|
||||||
|
public PermissionsSettings? PermissionsSettings { get; set; }
|
||||||
#nullable disable
|
#nullable disable
|
||||||
}
|
}
|
||||||
9
MainProject/Models/Settings/EncryptionSettings.cs
Normal file
9
MainProject/Models/Settings/EncryptionSettings.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
namespace BasicDotnetTemplate.MainProject.Models.Settings;
|
||||||
|
|
||||||
|
public class EncryptionSettings
|
||||||
|
{
|
||||||
|
#nullable enable
|
||||||
|
public string? Salt { get; set; }
|
||||||
|
public string? Pepper { get; set; }
|
||||||
|
#nullable disable
|
||||||
|
}
|
||||||
12
MainProject/Models/Settings/JwtSettings.cs
Normal file
12
MainProject/Models/Settings/JwtSettings.cs
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
namespace BasicDotnetTemplate.MainProject.Models.Settings;
|
||||||
|
|
||||||
|
public class JwtSettings
|
||||||
|
{
|
||||||
|
#nullable enable
|
||||||
|
public string? ValidAudience { get; set; }
|
||||||
|
public string? ValidIssuer { get; set; }
|
||||||
|
public string? Secret { get; set; }
|
||||||
|
public int? ExpiredAfterMinsOfInactivity { get; set; }
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
}
|
||||||
8
MainProject/Models/Settings/PermissionsSettings.cs
Normal file
8
MainProject/Models/Settings/PermissionsSettings.cs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
namespace BasicDotnetTemplate.MainProject.Models.Settings;
|
||||||
|
|
||||||
|
public class PermissionsSettings
|
||||||
|
{
|
||||||
|
#nullable enable
|
||||||
|
public string? FilePath { get; set; }
|
||||||
|
#nullable disable
|
||||||
|
}
|
||||||
@@ -44,6 +44,8 @@ internal static class Program
|
|||||||
ProgramUtils.AddDbContext(ref builder, appSettings);
|
ProgramUtils.AddDbContext(ref builder, appSettings);
|
||||||
WebApplication app = builder.Build();
|
WebApplication app = builder.Build();
|
||||||
ProgramUtils.AddMiddlewares(ref app);
|
ProgramUtils.AddMiddlewares(ref app);
|
||||||
|
ProgramUtils.CreateRoles(ref app);
|
||||||
|
ProgramUtils.CreatePermissions(ref app);
|
||||||
|
|
||||||
Logger.Info("[Program][Initialize] End building");
|
Logger.Info("[Program][Initialize] End building");
|
||||||
return app;
|
return app;
|
||||||
|
|||||||
49
MainProject/Services/AuthService.cs
Normal file
49
MainProject/Services/AuthService.cs
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Data.Auth;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Common.User;
|
||||||
|
using BasicDotnetTemplate.MainProject.Core.Database;
|
||||||
|
using BasicDotnetTemplate.MainProject.Utils;
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Services;
|
||||||
|
|
||||||
|
public interface IAuthService
|
||||||
|
{
|
||||||
|
Task<AuthenticatedUser?> AuthenticateAsync(AuthenticateRequestData data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class AuthService : BaseService, IAuthService
|
||||||
|
{
|
||||||
|
protected CryptUtils _cryptUtils;
|
||||||
|
protected readonly IUserService _userService;
|
||||||
|
|
||||||
|
public AuthService(
|
||||||
|
IHttpContextAccessor httpContextAccessor,
|
||||||
|
IConfiguration configuration,
|
||||||
|
SqlServerContext sqlServerContext,
|
||||||
|
IUserService userService
|
||||||
|
) : base(httpContextAccessor, configuration, sqlServerContext)
|
||||||
|
{
|
||||||
|
_cryptUtils = new CryptUtils(_appSettings);
|
||||||
|
_userService = userService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<AuthenticatedUser?> AuthenticateAsync(AuthenticateRequestData data)
|
||||||
|
{
|
||||||
|
AuthenticatedUser? authenticatedUser = null;
|
||||||
|
|
||||||
|
var decryptedUsername = _cryptUtils.Decrypt(data.Email ?? String.Empty);
|
||||||
|
var decryptedPassword = _cryptUtils.Decrypt(data.Password ?? String.Empty);
|
||||||
|
|
||||||
|
if (!String.IsNullOrEmpty(decryptedUsername) && !String.IsNullOrEmpty(decryptedPassword))
|
||||||
|
{
|
||||||
|
var user = await this._userService.GetUserByUsernameAndPassword(decryptedUsername, decryptedPassword);
|
||||||
|
if (user != null)
|
||||||
|
{
|
||||||
|
authenticatedUser = new AuthenticatedUser(user);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return authenticatedUser;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
43
MainProject/Services/BaseService.cs
Normal file
43
MainProject/Services/BaseService.cs
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
using BasicDotnetTemplate.MainProject.Core.Database;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Common.User;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Settings;
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Services;
|
||||||
|
|
||||||
|
public class BaseService
|
||||||
|
{
|
||||||
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||||
|
protected readonly IConfiguration _configuration;
|
||||||
|
protected readonly AppSettings _appSettings;
|
||||||
|
protected readonly SqlServerContext _sqlServerContext;
|
||||||
|
|
||||||
|
public BaseService(
|
||||||
|
IHttpContextAccessor httpContextAccessor,
|
||||||
|
IConfiguration configuration,
|
||||||
|
SqlServerContext sqlServerContext
|
||||||
|
)
|
||||||
|
{
|
||||||
|
_httpContextAccessor = httpContextAccessor;
|
||||||
|
_configuration = configuration;
|
||||||
|
_appSettings = new AppSettings();
|
||||||
|
_configuration.GetSection("AppSettings").Bind(_appSettings);
|
||||||
|
_sqlServerContext = sqlServerContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected int? GetCurrentUserId()
|
||||||
|
{
|
||||||
|
int? userId = null;
|
||||||
|
var user = this.GetCurrentUser();
|
||||||
|
if (user != null)
|
||||||
|
{
|
||||||
|
userId = this._sqlServerContext.Users.Where(x => !x.IsDeleted && x.Guid == user.Guid).FirstOrDefault()?.Id;
|
||||||
|
}
|
||||||
|
return userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected AuthenticatedUser? GetCurrentUser()
|
||||||
|
{
|
||||||
|
return _httpContextAccessor.HttpContext?.Items["User"] as AuthenticatedUser;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
41
MainProject/Services/JwtService.cs
Normal file
41
MainProject/Services/JwtService.cs
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
using Microsoft.IdentityModel.Tokens;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IdentityModel.Tokens.Jwt;
|
||||||
|
using System.Security.Claims;
|
||||||
|
using System.Text;
|
||||||
|
using BasicDotnetTemplate.MainProject.Core.Database;
|
||||||
|
using BasicDotnetTemplate.MainProject.Utils;
|
||||||
|
using DatabaseSqlServer = BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||||
|
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Services;
|
||||||
|
|
||||||
|
public interface IJwtService
|
||||||
|
{
|
||||||
|
string GenerateToken(string guid);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class JwtService : BaseService, IJwtService
|
||||||
|
{
|
||||||
|
private readonly JwtTokenUtils _jwtTokenUtils;
|
||||||
|
|
||||||
|
public JwtService(
|
||||||
|
IHttpContextAccessor httpContextAccessor,
|
||||||
|
IConfiguration configuration,
|
||||||
|
SqlServerContext sqlServerContext
|
||||||
|
) : base(httpContextAccessor, configuration, sqlServerContext)
|
||||||
|
{
|
||||||
|
_jwtTokenUtils = new JwtTokenUtils(_appSettings);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public string GenerateToken(string guid)
|
||||||
|
{
|
||||||
|
return _jwtTokenUtils.GenerateToken(guid);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
1278
MainProject/Services/PermissionService.cs
Normal file
1278
MainProject/Services/PermissionService.cs
Normal file
File diff suppressed because it is too large
Load Diff
184
MainProject/Services/RoleService.cs
Normal file
184
MainProject/Services/RoleService.cs
Normal file
@@ -0,0 +1,184 @@
|
|||||||
|
|
||||||
|
using System.Collections;
|
||||||
|
using BasicDotnetTemplate.MainProject.Core.Database;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Common.Exceptions;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Data.Role;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using BasicDotnetTemplate.MainProject.Utils;
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Services;
|
||||||
|
|
||||||
|
public interface IRoleService
|
||||||
|
{
|
||||||
|
Task<Role?> GetRoleByIdAsync(int id);
|
||||||
|
Task<Role?> GetRoleByGuidAsync(string guid);
|
||||||
|
Task<bool> CheckIfNameIsValid(string name, string? guid = "");
|
||||||
|
Task<Role?> CreateRoleAsync(CreateRoleRequestData data);
|
||||||
|
Task<Role?> UpdateRoleAsync(CreateRoleRequestData data, Role role);
|
||||||
|
Task<Role?> GetRoleForUser(string? guid);
|
||||||
|
Task<bool?> DeleteRoleAsync(Role role);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class RoleService : BaseService, IRoleService
|
||||||
|
{
|
||||||
|
private readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
|
||||||
|
private readonly CommonDbMethodsUtils _commonDbMethodsUtils;
|
||||||
|
|
||||||
|
public RoleService(
|
||||||
|
IHttpContextAccessor httpContextAccessor,
|
||||||
|
IConfiguration configuration,
|
||||||
|
SqlServerContext sqlServerContext
|
||||||
|
) : base(httpContextAccessor, configuration, sqlServerContext)
|
||||||
|
{
|
||||||
|
_commonDbMethodsUtils = new CommonDbMethodsUtils(sqlServerContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
private IQueryable<Role> GetRolesQueryable()
|
||||||
|
{
|
||||||
|
return _commonDbMethodsUtils.GetRolesQueryable();
|
||||||
|
}
|
||||||
|
private IQueryable<Role> GetRoleByNameQueryable(string name)
|
||||||
|
{
|
||||||
|
return _commonDbMethodsUtils.GetRoleByNameQueryable(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private Role CreateRoleData(CreateRoleRequestData data)
|
||||||
|
{
|
||||||
|
Role role = new()
|
||||||
|
{
|
||||||
|
CreationTime = DateTime.UtcNow,
|
||||||
|
CreationUserId = this.GetCurrentUserId(),
|
||||||
|
IsDeleted = false,
|
||||||
|
Guid = Guid.NewGuid().ToString(),
|
||||||
|
Name = data.Name,
|
||||||
|
IsNotEditable = data.IsNotEditable
|
||||||
|
};
|
||||||
|
|
||||||
|
return role;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public async Task<Role?> GetRoleByIdAsync(int id)
|
||||||
|
{
|
||||||
|
return await this.GetRolesQueryable().Where(x => x.Id == id).FirstOrDefaultAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Role?> GetRoleByGuidAsync(string guid)
|
||||||
|
{
|
||||||
|
return await this.GetRolesQueryable().Where(x => x.Guid == guid).FirstOrDefaultAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<bool> CheckIfNameIsValid(string name, string? guid = "")
|
||||||
|
{
|
||||||
|
var valid = false;
|
||||||
|
|
||||||
|
Role? role = await this.GetRoleByNameQueryable(name).FirstOrDefaultAsync();
|
||||||
|
if (role != null)
|
||||||
|
{
|
||||||
|
if (!String.IsNullOrEmpty(guid))
|
||||||
|
{
|
||||||
|
valid = role.Guid == guid && role.Name == name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
valid = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return valid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Role?> CreateRoleAsync(CreateRoleRequestData data)
|
||||||
|
{
|
||||||
|
Role? role = null;
|
||||||
|
|
||||||
|
using var transaction = await _sqlServerContext.Database.BeginTransactionAsync();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var tempRole = this.CreateRoleData(data);
|
||||||
|
await _sqlServerContext.Roles.AddAsync(tempRole);
|
||||||
|
await _sqlServerContext.SaveChangesAsync();
|
||||||
|
await transaction.CommitAsync();
|
||||||
|
role = tempRole;
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
await transaction.RollbackAsync();
|
||||||
|
Logger.Error(exception, $"[RoleService][CreateRoleAsync]");
|
||||||
|
throw new CreateException($"An error occurred while saving the role for transaction ID {transaction.TransactionId}.", exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
return role;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Role?> UpdateRoleAsync(CreateRoleRequestData data, Role role)
|
||||||
|
{
|
||||||
|
if (role.IsNotEditable)
|
||||||
|
return role;
|
||||||
|
|
||||||
|
using var transaction = await _sqlServerContext.Database.BeginTransactionAsync();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
role.Name = data.Name;
|
||||||
|
role.IsNotEditable = data.IsNotEditable;
|
||||||
|
role.UpdateTime = DateTime.UtcNow;
|
||||||
|
role.UpdateUserId = this.GetCurrentUserId();
|
||||||
|
|
||||||
|
_sqlServerContext.Roles.Update(role);
|
||||||
|
await _sqlServerContext.SaveChangesAsync();
|
||||||
|
await transaction.CommitAsync();
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
Logger.Error(exception, $"[RoleService][UpdateRoleAsync] | {transaction.TransactionId}");
|
||||||
|
await transaction.RollbackAsync();
|
||||||
|
throw new UpdateException($"An error occurred while updating the role for transaction ID {transaction.TransactionId}.", exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
return role;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Role?> GetRoleForUser(string? guid)
|
||||||
|
{
|
||||||
|
Role? role = null;
|
||||||
|
|
||||||
|
if (String.IsNullOrEmpty(guid))
|
||||||
|
{
|
||||||
|
role = await this.GetRoleByNameQueryable("Default").FirstOrDefaultAsync();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
role = await this.GetRoleByGuidAsync(guid);
|
||||||
|
}
|
||||||
|
|
||||||
|
return role;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<bool?> DeleteRoleAsync(Role role)
|
||||||
|
{
|
||||||
|
bool? deleted = false;
|
||||||
|
|
||||||
|
using (var transaction = _sqlServerContext.Database.BeginTransactionAsync())
|
||||||
|
{
|
||||||
|
role.IsDeleted = true;
|
||||||
|
role.DeletionTime = DateTime.UtcNow;
|
||||||
|
_sqlServerContext.Update(role);
|
||||||
|
await _sqlServerContext.SaveChangesAsync();
|
||||||
|
await (await transaction).CommitAsync();
|
||||||
|
deleted = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return deleted;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
149
MainProject/Services/UserService.cs
Normal file
149
MainProject/Services/UserService.cs
Normal file
@@ -0,0 +1,149 @@
|
|||||||
|
|
||||||
|
using System.Collections;
|
||||||
|
using BasicDotnetTemplate.MainProject.Core.Database;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Common.Exceptions;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Data.User;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Services;
|
||||||
|
|
||||||
|
public interface IUserService
|
||||||
|
{
|
||||||
|
Task<User?> GetUserByIdAsync(int id);
|
||||||
|
Task<User?> GetUserByGuidAsync(string guid);
|
||||||
|
Task<User?> GetUserByUsernameAndPassword(string email, string password);
|
||||||
|
Task<bool> CheckIfEmailIsValid(string email, string? guid = "");
|
||||||
|
Task<User?> CreateUserAsync(CreateUserRequestData data, Role role);
|
||||||
|
Task<bool?> DeleteUserAsync(User user);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class UserService : BaseService, IUserService
|
||||||
|
{
|
||||||
|
private readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
|
||||||
|
public UserService(
|
||||||
|
IHttpContextAccessor httpContextAccessor,
|
||||||
|
IConfiguration configuration,
|
||||||
|
SqlServerContext sqlServerContext
|
||||||
|
) : base(httpContextAccessor, configuration, sqlServerContext)
|
||||||
|
{ }
|
||||||
|
|
||||||
|
private IQueryable<User> GetUsersQueryable()
|
||||||
|
{
|
||||||
|
return this._sqlServerContext.Users.Where(x => !x.IsDeleted);
|
||||||
|
}
|
||||||
|
|
||||||
|
private IQueryable<User> GetUserByEmailQueryable(string email)
|
||||||
|
{
|
||||||
|
return this.GetUsersQueryable().Where(x =>
|
||||||
|
x.Email.ToString() == email.ToString()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private User CreateUserData(CreateUserRequestData data, Role role)
|
||||||
|
{
|
||||||
|
User user = new()
|
||||||
|
{
|
||||||
|
CreationTime = DateTime.UtcNow,
|
||||||
|
CreationUserId = this.GetCurrentUserId(),
|
||||||
|
IsDeleted = false,
|
||||||
|
Guid = Guid.NewGuid().ToString(),
|
||||||
|
FirstName = data.FirstName,
|
||||||
|
LastName = data.LastName,
|
||||||
|
Email = data.Email,
|
||||||
|
PasswordSalt = "",
|
||||||
|
PasswordHash = "",
|
||||||
|
Password = "",
|
||||||
|
Role = role,
|
||||||
|
IsTestUser = false
|
||||||
|
};
|
||||||
|
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public async Task<User?> GetUserByIdAsync(int id)
|
||||||
|
{
|
||||||
|
return await this.GetUsersQueryable().Where(x => x.Id == id).FirstOrDefaultAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<User?> GetUserByGuidAsync(string guid)
|
||||||
|
{
|
||||||
|
return await this.GetUsersQueryable().Where(x => x.Guid == guid).FirstOrDefaultAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<User?> GetUserByUsernameAndPassword(string email, string password)
|
||||||
|
{
|
||||||
|
User? user = await this.GetUserByEmailQueryable(email).FirstOrDefaultAsync();
|
||||||
|
if (user != null)
|
||||||
|
{
|
||||||
|
var encryptedPassword = user.PasswordHash;
|
||||||
|
}
|
||||||
|
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<bool> CheckIfEmailIsValid(string email, string? guid = "")
|
||||||
|
{
|
||||||
|
var valid = false;
|
||||||
|
|
||||||
|
User? user = await this.GetUserByEmailQueryable(email).FirstOrDefaultAsync();
|
||||||
|
if (user != null)
|
||||||
|
{
|
||||||
|
if (!String.IsNullOrEmpty(guid))
|
||||||
|
{
|
||||||
|
valid = user.Guid == guid && user.Email == email;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
valid = true;
|
||||||
|
}
|
||||||
|
return valid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<User?> CreateUserAsync(CreateUserRequestData data, Role role)
|
||||||
|
{
|
||||||
|
User? user;
|
||||||
|
|
||||||
|
using var transaction = await _sqlServerContext.Database.BeginTransactionAsync();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var tempUser = CreateUserData(data, role);
|
||||||
|
await _sqlServerContext.Users.AddAsync(tempUser);
|
||||||
|
await _sqlServerContext.SaveChangesAsync();
|
||||||
|
await transaction.CommitAsync();
|
||||||
|
user = tempUser;
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
await transaction.RollbackAsync();
|
||||||
|
Logger.Error(exception, $"[UserService][CreateUserAsync]");
|
||||||
|
throw new CreateException($"An error occurred while creating the user for transaction ID {transaction.TransactionId}.", exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<bool?> DeleteUserAsync(User user)
|
||||||
|
{
|
||||||
|
bool? deleted = false;
|
||||||
|
|
||||||
|
using (var transaction = _sqlServerContext.Database.BeginTransactionAsync())
|
||||||
|
{
|
||||||
|
user.IsDeleted = true;
|
||||||
|
user.DeletionTime = DateTime.UtcNow;
|
||||||
|
_sqlServerContext.Update(user);
|
||||||
|
await _sqlServerContext.SaveChangesAsync();
|
||||||
|
await (await transaction).CommitAsync();
|
||||||
|
deleted = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return deleted;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
32
MainProject/Utils/CommonDbMethodsUtils.cs
Normal file
32
MainProject/Utils/CommonDbMethodsUtils.cs
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
using System;
|
||||||
|
using System.Security.Cryptography;
|
||||||
|
using System.Text;
|
||||||
|
using BasicDotnetTemplate.MainProject.Core.Database;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Utils;
|
||||||
|
public class CommonDbMethodsUtils
|
||||||
|
{
|
||||||
|
private readonly SqlServerContext _sqlServerContext;
|
||||||
|
|
||||||
|
public CommonDbMethodsUtils(SqlServerContext sqlServerContext)
|
||||||
|
{
|
||||||
|
_sqlServerContext = sqlServerContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public IQueryable<Role> GetRolesQueryable()
|
||||||
|
{
|
||||||
|
return this._sqlServerContext.Roles.Where(x => !x.IsDeleted);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IQueryable<Role> GetRoleByNameQueryable(string name)
|
||||||
|
{
|
||||||
|
return this.GetRolesQueryable().Where(x =>
|
||||||
|
x.Name.ToString() == name.ToString()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
92
MainProject/Utils/CryptoUtils.cs
Normal file
92
MainProject/Utils/CryptoUtils.cs
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
using System;
|
||||||
|
using System.Security.Cryptography;
|
||||||
|
using System.Text;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Settings;
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Utils;
|
||||||
|
public class CryptUtils
|
||||||
|
{
|
||||||
|
private readonly string _secretKey;
|
||||||
|
private readonly string _pepper;
|
||||||
|
private const int _M = 16;
|
||||||
|
private const int _N = 32;
|
||||||
|
|
||||||
|
public CryptUtils(AppSettings appSettings)
|
||||||
|
{
|
||||||
|
_secretKey = appSettings.EncryptionSettings?.Salt ?? String.Empty;
|
||||||
|
_pepper = appSettings.EncryptionSettings?.Pepper ?? String.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Decrypt(string encryptedData)
|
||||||
|
{
|
||||||
|
var decrypted = String.Empty;
|
||||||
|
|
||||||
|
if (String.IsNullOrEmpty(this._secretKey) || this._secretKey.Length < _M)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Unable to proceed with decryption due to invalid settings");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!String.IsNullOrEmpty(encryptedData) && encryptedData.Length > _N)
|
||||||
|
{
|
||||||
|
var iv = encryptedData.Substring(0, _M);
|
||||||
|
|
||||||
|
var cipherText = encryptedData.Substring(_N);
|
||||||
|
var fullCipher = Convert.FromBase64String(cipherText);
|
||||||
|
|
||||||
|
using (var aes = Aes.Create())
|
||||||
|
{
|
||||||
|
aes.Key = Encoding.UTF8.GetBytes(this._secretKey);
|
||||||
|
aes.IV = Encoding.UTF8.GetBytes(iv);
|
||||||
|
|
||||||
|
using (var decryptor = aes.CreateDecryptor(aes.Key, aes.IV))
|
||||||
|
{
|
||||||
|
using (var msDecrypt = new MemoryStream(fullCipher))
|
||||||
|
{
|
||||||
|
using (var cryptoStream = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
|
||||||
|
{
|
||||||
|
using (var srDecrypt = new StreamReader(cryptoStream))
|
||||||
|
{
|
||||||
|
decrypted = srDecrypt.ReadToEnd();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return decrypted;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string GenerateSalt()
|
||||||
|
{
|
||||||
|
using var rng = RandomNumberGenerator.Create();
|
||||||
|
var byteSalt = new byte[16];
|
||||||
|
rng.GetBytes(byteSalt);
|
||||||
|
var salt = Convert.ToBase64String(byteSalt);
|
||||||
|
return salt;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string GeneratePassword(string password, string salt, int iteration)
|
||||||
|
{
|
||||||
|
string hashedPassword = password;
|
||||||
|
for(var i = 0; i <= iteration; i++)
|
||||||
|
{
|
||||||
|
var passwordSaltPepper = $"{hashedPassword}{salt}{this._pepper}";
|
||||||
|
var byteValue = Encoding.UTF8.GetBytes(passwordSaltPepper);
|
||||||
|
var byteHash = SHA256.HashData(byteValue);
|
||||||
|
hashedPassword = Convert.ToBase64String(byteHash);
|
||||||
|
}
|
||||||
|
|
||||||
|
return hashedPassword;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool VerifyPassword(string password, string salt, int iteration, string userPassword)
|
||||||
|
{
|
||||||
|
string hashedPassword = this.GeneratePassword(password, salt, iteration);
|
||||||
|
return hashedPassword.Equals(userPassword, StringComparison.OrdinalIgnoreCase);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
42
MainProject/Utils/FileUtils.cs
Normal file
42
MainProject/Utils/FileUtils.cs
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
using System.Text.Json;
|
||||||
|
using NLog;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Utils;
|
||||||
|
|
||||||
|
public static class FileUtils
|
||||||
|
{
|
||||||
|
private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
|
||||||
|
private static readonly JsonSerializerOptions jsonSerializerOptions = new()
|
||||||
|
{
|
||||||
|
PropertyNameCaseInsensitive = true
|
||||||
|
};
|
||||||
|
|
||||||
|
public static T? ConvertFileToObject<T>(string? filePath = "")
|
||||||
|
{
|
||||||
|
Logger.Info("[FileUtils][ReadJson] Reading file");
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(filePath))
|
||||||
|
{
|
||||||
|
throw new ArgumentException("filePath cannot be null or empty", nameof(filePath));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!File.Exists(filePath))
|
||||||
|
{
|
||||||
|
throw new FileNotFoundException("The specified file does not exists", filePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string fileContent = File.ReadAllText(filePath);
|
||||||
|
|
||||||
|
return JsonSerializer.Deserialize<T>(fileContent, jsonSerializerOptions);
|
||||||
|
}
|
||||||
|
catch (JsonException ex)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("Error during file deserialization", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
110
MainProject/Utils/JwtTokenUtils.cs
Normal file
110
MainProject/Utils/JwtTokenUtils.cs
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IdentityModel.Tokens.Jwt;
|
||||||
|
using System.Security.Cryptography;
|
||||||
|
using System.Security.Claims;
|
||||||
|
using System.Text;
|
||||||
|
using Microsoft.IdentityModel.Tokens;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Settings;
|
||||||
|
using DatabaseSqlServer = BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Utils;
|
||||||
|
|
||||||
|
public class JwtTokenUtils
|
||||||
|
{
|
||||||
|
private readonly string _jwtKey;
|
||||||
|
private readonly string _jwtIssuer;
|
||||||
|
private readonly string _jwtAudience;
|
||||||
|
private readonly int _expiration;
|
||||||
|
private readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
|
||||||
|
|
||||||
|
public JwtTokenUtils(AppSettings appSettings)
|
||||||
|
{
|
||||||
|
_jwtKey = appSettings?.JwtSettings?.Secret ?? String.Empty;
|
||||||
|
_jwtIssuer = appSettings?.JwtSettings?.ValidIssuer ?? String.Empty;
|
||||||
|
_jwtAudience = appSettings?.JwtSettings?.ValidAudience ?? String.Empty;
|
||||||
|
_expiration = appSettings?.JwtSettings?.ExpiredAfterMinsOfInactivity ?? 15;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string GenerateToken(string guid)
|
||||||
|
{
|
||||||
|
var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtKey));
|
||||||
|
var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);
|
||||||
|
|
||||||
|
var claims = new List<Claim>
|
||||||
|
{
|
||||||
|
new Claim(JwtRegisteredClaimNames.Sub, guid),
|
||||||
|
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
|
||||||
|
new Claim("guid", guid)
|
||||||
|
};
|
||||||
|
|
||||||
|
var token = new JwtSecurityToken(
|
||||||
|
_jwtIssuer,
|
||||||
|
_jwtAudience,
|
||||||
|
claims,
|
||||||
|
expires: DateTime.Now.AddMinutes(_expiration),
|
||||||
|
signingCredentials: credentials);
|
||||||
|
|
||||||
|
return new JwtSecurityTokenHandler().WriteToken(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public string? ValidateToken(string headerAuthorization)
|
||||||
|
{
|
||||||
|
string? token = null;
|
||||||
|
string? guid = null;
|
||||||
|
|
||||||
|
if (
|
||||||
|
String.IsNullOrEmpty(_jwtKey) ||
|
||||||
|
String.IsNullOrEmpty(_jwtIssuer) ||
|
||||||
|
String.IsNullOrEmpty(_jwtAudience)
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return guid;
|
||||||
|
}
|
||||||
|
|
||||||
|
string[] authorizations = headerAuthorization.Split(" ");
|
||||||
|
if (authorizations.Length == 2)
|
||||||
|
{
|
||||||
|
token = authorizations[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!String.IsNullOrEmpty(token))
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var tokenHandler = new JwtSecurityTokenHandler();
|
||||||
|
var key = Encoding.ASCII.GetBytes(_jwtKey);
|
||||||
|
tokenHandler.ValidateToken(token, new TokenValidationParameters
|
||||||
|
{
|
||||||
|
ValidateIssuerSigningKey = true,
|
||||||
|
IssuerSigningKey = new SymmetricSecurityKey(key),
|
||||||
|
ValidateIssuer = true,
|
||||||
|
ValidIssuer = _jwtIssuer,
|
||||||
|
ValidateAudience = true,
|
||||||
|
ValidAudience = _jwtAudience,
|
||||||
|
ValidateLifetime = true,
|
||||||
|
ClockSkew = TimeSpan.Zero
|
||||||
|
}, out SecurityToken validatedToken);
|
||||||
|
|
||||||
|
var jwtToken = (JwtSecurityToken)validatedToken;
|
||||||
|
|
||||||
|
if (jwtToken != null)
|
||||||
|
{
|
||||||
|
var claimedUserId = jwtToken.Claims.FirstOrDefault(c => c.Type == "guid");
|
||||||
|
if (claimedUserId != null && !String.IsNullOrEmpty(claimedUserId.Value))
|
||||||
|
{
|
||||||
|
guid = claimedUserId.Value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
Logger.Error(exception, $"[JwtTokenUtils][ValidateToken]");
|
||||||
|
return guid;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return guid;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user