Sprint 5 #28
@@ -2,7 +2,7 @@ using BasicDotnetTemplate.MainProject.Services;
|
|||||||
using BasicDotnetTemplate.MainProject.Models.Api.Data.User;
|
using BasicDotnetTemplate.MainProject.Models.Api.Data.User;
|
||||||
using BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
using BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Common.Exceptions;
|
||||||
|
|
||||||
|
|
||||||
namespace BasicDotnetTemplate.MainProject.Tests;
|
namespace BasicDotnetTemplate.MainProject.Tests;
|
||||||
@@ -24,6 +24,11 @@ public class PermissionService_Tests
|
|||||||
Enabled = false
|
Enabled = false
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private static PermissionOperation _permissionOperation = new PermissionOperation()
|
||||||
|
{
|
||||||
|
Name = _name + "-OPERATION"
|
||||||
|
};
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void Inizialize()
|
public void Inizialize()
|
||||||
{
|
{
|
||||||
@@ -125,12 +130,13 @@ public class PermissionService_Tests
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var user = await exceptionPermissionService.CreatePermissionSystemAsync(_permissionSystem.Name, true);
|
var permission = await exceptionPermissionService.CreatePermissionSystemAsync(_permissionSystem.Name, true);
|
||||||
Assert.Fail($"Expected exception instead of response: {user?.Guid}");
|
Assert.Fail($"Expected exception instead of response: {permission?.Guid}");
|
||||||
}
|
}
|
||||||
catch (Exception exception)
|
catch (Exception exception)
|
||||||
{
|
{
|
||||||
Assert.IsInstanceOfType(exception, typeof(Exception));
|
Assert.IsInstanceOfType(exception, typeof(Exception));
|
||||||
|
Assert.IsInstanceOfType(exception, typeof(CreateException));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -289,18 +295,21 @@ public class PermissionService_Tests
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|
||||||
var exceptionPermissionService = TestUtils.CreatePermissionServiceException();
|
var exceptionPermissionService = TestUtils.CreatePermissionServiceException();
|
||||||
|
|
||||||
if (exceptionPermissionService != null)
|
if (exceptionPermissionService != null)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var user = await exceptionPermissionService.CreatePermissionModuleAsync(_permissionModule.Name, true);
|
var permission = await exceptionPermissionService.CreatePermissionModuleAsync(_permissionModule.Name, true);
|
||||||
Assert.Fail($"Expected exception instead of response: {user?.Guid}");
|
Assert.Fail($"Expected exception instead of response: {permission?.Guid}");
|
||||||
}
|
}
|
||||||
catch (Exception exception)
|
catch (Exception exception)
|
||||||
{
|
{
|
||||||
|
Console.WriteLine(exception);
|
||||||
Assert.IsInstanceOfType(exception, typeof(Exception));
|
Assert.IsInstanceOfType(exception, typeof(Exception));
|
||||||
|
Assert.IsInstanceOfType(exception, typeof(CreateException));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -386,7 +395,155 @@ public class PermissionService_Tests
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
#region "PermissionOperation"
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task GetPermissionOperationByGuidAsync_Null()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
|
||||||
|
if (_permissionService != null)
|
||||||
|
{
|
||||||
|
var permission = await _permissionService.GetPermissionOperationByGuidAsync(Guid.NewGuid().ToString());
|
||||||
|
Assert.IsTrue(permission == null);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.Fail($"PermissionService is null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.InnerException);
|
||||||
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task GetPermissionOperationByNameAsync_Null()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
|
||||||
|
if (_permissionService != null)
|
||||||
|
{
|
||||||
|
var permission = await _permissionService.GetPermissionOperationByNameAsync(Guid.NewGuid().ToString());
|
||||||
|
Assert.IsTrue(permission == null);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.Fail($"PermissionService is null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.InnerException);
|
||||||
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task CreatePermissionOperationAsync_Success()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var permission = await _permissionService.CreatePermissionOperationAsync(_permissionOperation.Name);
|
||||||
|
Assert.IsInstanceOfType(permission, typeof(PermissionOperation));
|
||||||
|
Assert.IsNotNull(permission);
|
||||||
|
Assert.IsTrue(permission.Name == _permissionOperation.Name);
|
||||||
|
_permissionOperation = permission;
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.InnerException);
|
||||||
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task CreatePermissionOperationAsync_Exception()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var exceptionPermissionService = TestUtils.CreatePermissionServiceException();
|
||||||
|
|
||||||
|
if (exceptionPermissionService != null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var permission = await exceptionPermissionService.CreatePermissionOperationAsync(_permissionOperation.Name);
|
||||||
|
Assert.Fail($"Expected exception instead of response: {permission?.Guid}");
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
Assert.IsInstanceOfType(exception, typeof(Exception));
|
||||||
|
Assert.IsInstanceOfType(exception, typeof(CreateException));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.Fail($"PermissionService is null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task GetPermissionOperationByGuidAsync_Success()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
|
||||||
|
if (_permissionService != null)
|
||||||
|
{
|
||||||
|
var permission = await _permissionService.GetPermissionOperationByGuidAsync(_permissionOperation.Guid);
|
||||||
|
Assert.IsNotNull(permission);
|
||||||
|
Assert.IsInstanceOfType(permission, typeof(PermissionOperation));
|
||||||
|
Assert.IsTrue(permission.Name == _permissionOperation.Name);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.Fail($"PermissionService is null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.InnerException);
|
||||||
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task GetPermissionOperationByNameAsync_Success()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
|
||||||
|
if (_permissionService != null)
|
||||||
|
{
|
||||||
|
var permission = await _permissionService.GetPermissionOperationByNameAsync(_permissionOperation.Name);
|
||||||
|
Assert.IsNotNull(permission);
|
||||||
|
Assert.IsInstanceOfType(permission, typeof(PermissionOperation));
|
||||||
|
Assert.IsTrue(permission.Guid == _permissionOperation.Guid);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.Fail($"PermissionService is null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.InnerException);
|
||||||
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -424,6 +581,22 @@ public class PermissionService_Tests
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task DeletePermissionOperationAsync()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var deleted = await _permissionService.DeletePermissionOperationAsync(_permissionOperation);
|
||||||
|
Assert.IsTrue(deleted);
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.InnerException);
|
||||||
|
Assert.Fail($"An exception was thrown: {ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -118,6 +118,7 @@ public class RoleService_Tests
|
|||||||
catch (Exception exception)
|
catch (Exception exception)
|
||||||
{
|
{
|
||||||
Assert.IsInstanceOfType(exception, typeof(Exception));
|
Assert.IsInstanceOfType(exception, typeof(Exception));
|
||||||
|
Assert.IsInstanceOfType(exception, typeof(CreateException));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ using BasicDotnetTemplate.MainProject.Services;
|
|||||||
using BasicDotnetTemplate.MainProject.Models.Api.Data.User;
|
using BasicDotnetTemplate.MainProject.Models.Api.Data.User;
|
||||||
using BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
using BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Common.Exceptions;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -152,6 +153,7 @@ public class UserService_Tests
|
|||||||
catch (Exception exception)
|
catch (Exception exception)
|
||||||
{
|
{
|
||||||
Assert.IsInstanceOfType(exception, typeof(Exception));
|
Assert.IsInstanceOfType(exception, typeof(Exception));
|
||||||
|
Assert.IsInstanceOfType(exception, typeof(CreateException));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|||||||
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -69,11 +69,16 @@ public static class TestUtils
|
|||||||
return "Server=127.0.0.1;Initial Catalog=MyFakeDatabase;User Id=MyFakeUser;Password='MyFakePassword';MultipleActiveResultSets=True;Encrypt=True;TrustServerCertificate=True;Connection Timeout=30";
|
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 SqlServerContext CreateInMemorySqlContext()
|
public static DbContextOptions<SqlServerContext> CreateInMemorySqlContextOptions()
|
||||||
{
|
{
|
||||||
var options = new DbContextOptionsBuilder<SqlServerContext>()
|
return new DbContextOptionsBuilder<SqlServerContext>()
|
||||||
.UseSqlite("DataSource=:memory:") // Database in-memory
|
.UseSqlite("DataSource=:memory:") // Database in-memory
|
||||||
.Options;
|
.Options;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static SqlServerContext CreateInMemorySqlContext()
|
||||||
|
{
|
||||||
|
var options = CreateInMemorySqlContextOptions();
|
||||||
|
|
||||||
var context = new SqlServerContext(options);
|
var context = new SqlServerContext(options);
|
||||||
context.Database.OpenConnection();
|
context.Database.OpenConnection();
|
||||||
@@ -108,10 +113,9 @@ public static class TestUtils
|
|||||||
|
|
||||||
public static UserService CreateUserServiceException()
|
public static UserService CreateUserServiceException()
|
||||||
{
|
{
|
||||||
|
var sqlServerContext = new ExceptionSqlServerContext();
|
||||||
|
sqlServerContext.ThrowExceptionOnSave = true;
|
||||||
IConfiguration configuration = CreateConfiguration();
|
IConfiguration configuration = CreateConfiguration();
|
||||||
var optionsBuilder = new DbContextOptionsBuilder<SqlServerContext>();
|
|
||||||
optionsBuilder.UseSqlServer(GetFakeConnectionString());
|
|
||||||
SqlServerContext sqlServerContext = new SqlServerContext(optionsBuilder.Options);
|
|
||||||
var httpContextAccessor = new Mock<IHttpContextAccessor>();
|
var httpContextAccessor = new Mock<IHttpContextAccessor>();
|
||||||
return new UserService(httpContextAccessor.Object, configuration, sqlServerContext);
|
return new UserService(httpContextAccessor.Object, configuration, sqlServerContext);
|
||||||
}
|
}
|
||||||
@@ -138,10 +142,9 @@ public static class TestUtils
|
|||||||
|
|
||||||
public static RoleService CreateRoleServiceException()
|
public static RoleService CreateRoleServiceException()
|
||||||
{
|
{
|
||||||
|
var sqlServerContext = new ExceptionSqlServerContext();
|
||||||
|
sqlServerContext.ThrowExceptionOnSave = true;
|
||||||
IConfiguration configuration = CreateConfiguration();
|
IConfiguration configuration = CreateConfiguration();
|
||||||
var optionsBuilder = new DbContextOptionsBuilder<SqlServerContext>();
|
|
||||||
optionsBuilder.UseSqlServer(GetFakeConnectionString());
|
|
||||||
SqlServerContext sqlServerContext = new SqlServerContext(optionsBuilder.Options);
|
|
||||||
var httpContextAccessor = new Mock<IHttpContextAccessor>();
|
var httpContextAccessor = new Mock<IHttpContextAccessor>();
|
||||||
return new RoleService(httpContextAccessor.Object, configuration, sqlServerContext);
|
return new RoleService(httpContextAccessor.Object, configuration, sqlServerContext);
|
||||||
}
|
}
|
||||||
@@ -156,10 +159,9 @@ public static class TestUtils
|
|||||||
|
|
||||||
public static PermissionService CreatePermissionServiceException()
|
public static PermissionService CreatePermissionServiceException()
|
||||||
{
|
{
|
||||||
|
var sqlServerContext = new ExceptionSqlServerContext();
|
||||||
|
sqlServerContext.ThrowExceptionOnSave = true;
|
||||||
IConfiguration configuration = CreateConfiguration();
|
IConfiguration configuration = CreateConfiguration();
|
||||||
var optionsBuilder = new DbContextOptionsBuilder<SqlServerContext>();
|
|
||||||
optionsBuilder.UseSqlServer(GetFakeConnectionString());
|
|
||||||
SqlServerContext sqlServerContext = new SqlServerContext(optionsBuilder.Options);
|
|
||||||
var httpContextAccessor = new Mock<IHttpContextAccessor>();
|
var httpContextAccessor = new Mock<IHttpContextAccessor>();
|
||||||
return new PermissionService(httpContextAccessor.Object, configuration, sqlServerContext);
|
return new PermissionService(httpContextAccessor.Object, configuration, sqlServerContext);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -78,7 +78,6 @@ public class UserService : BaseService, IUserService
|
|||||||
if (user != null)
|
if (user != null)
|
||||||
{
|
{
|
||||||
var encryptedPassword = user.PasswordHash;
|
var encryptedPassword = user.PasswordHash;
|
||||||
Console.WriteLine(encryptedPassword);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return user;
|
return user;
|
||||||
|
|||||||
Reference in New Issue
Block a user