diff --git a/MainProject.Tests/Controllers/RoleController_Tests.cs b/MainProject.Tests/Controllers/RoleController_Tests.cs index 50ecba3..7bfd290 100644 --- a/MainProject.Tests/Controllers/RoleController_Tests.cs +++ b/MainProject.Tests/Controllers/RoleController_Tests.cs @@ -805,6 +805,52 @@ public class RoleController_Tests } } + [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())).ReturnsAsync(role); + _roleServiceMock?.Setup(s => s.CheckIfNameIsValid(It.IsAny(), It.IsAny())).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)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() { diff --git a/MainProject.Tests/Controllers/RootController_Tests.cs b/MainProject.Tests/Controllers/RootController_Tests.cs index 199c7f6..05ae28f 100644 --- a/MainProject.Tests/Controllers/RootController_Tests.cs +++ b/MainProject.Tests/Controllers/RootController_Tests.cs @@ -39,7 +39,6 @@ public class RootController_Test } catch (Exception ex) { - Console.WriteLine(ex.InnerException); Assert.Fail($"An exception was thrown: {ex}"); } } diff --git a/MainProject.Tests/Controllers/UserController_Tests.cs b/MainProject.Tests/Controllers/UserController_Tests.cs index 5f35a07..1e4c415 100644 --- a/MainProject.Tests/Controllers/UserController_Tests.cs +++ b/MainProject.Tests/Controllers/UserController_Tests.cs @@ -232,7 +232,7 @@ public class UserController_Tests } [TestMethod] - public async Task CreateUserAsync_Should_Return_200_When_Successful() + public async Task CreateUserAsync_Success() { if (_userController == null) { diff --git a/MainProject.Tests/Controllers/VersionController_Tests.cs b/MainProject.Tests/Controllers/VersionController_Tests.cs index 7f38ef7..b19c318 100644 --- a/MainProject.Tests/Controllers/VersionController_Tests.cs +++ b/MainProject.Tests/Controllers/VersionController_Tests.cs @@ -68,7 +68,6 @@ public class VersionController_Tests } catch (Exception ex) { - Console.WriteLine(ex.InnerException); Assert.Fail($"An exception was thrown: {ex}"); } } @@ -80,7 +79,6 @@ public class VersionController_Tests try { - Console.WriteLine(System.AppDomain.CurrentDomain.BaseDirectory); var configuration = TestUtils.CreateEmptyConfiguration(System.AppDomain.CurrentDomain.BaseDirectory + "/JsonData", "emptyAppsettings.json"); VersionController versionController = new VersionController(configuration); var result = versionController.GetVersion(); @@ -97,7 +95,6 @@ public class VersionController_Tests } catch (Exception ex) { - Console.WriteLine(ex.InnerException); Assert.Fail($"An exception was thrown: {ex}"); } } diff --git a/MainProject.Tests/Program_Tests.cs b/MainProject.Tests/Program_Tests.cs index 93588db..f485a02 100644 --- a/MainProject.Tests/Program_Tests.cs +++ b/MainProject.Tests/Program_Tests.cs @@ -69,7 +69,6 @@ public class Program_Tests } catch (Exception ex) { - Console.WriteLine(ex.InnerException); Assert.Fail($"An exception was thrown: {ex}"); } } diff --git a/MainProject.Tests/Services/RoleService_Tests.cs b/MainProject.Tests/Services/RoleService_Tests.cs index dfd172e..027012e 100644 --- a/MainProject.Tests/Services/RoleService_Tests.cs +++ b/MainProject.Tests/Services/RoleService_Tests.cs @@ -62,7 +62,7 @@ public class RoleService_Tests } [TestMethod] - public async Task CreateRoleData() + public async Task CreateRoleAsync_Success() { try { @@ -93,6 +93,42 @@ public class RoleService_Tests } } + [TestMethod] + public async Task CreateRoleAsync_Exception() + { + try + { + CreateRoleRequestData data = new CreateRoleRequestData() + { + Name = "Exception", + IsNotEditable = false + }; + + var exceptionRoleService = TestUtils.CreateRoleServiceException(); + + if (exceptionRoleService != null) + { + try + { + var role = await exceptionRoleService.CreateRoleAsync(data); + Assert.Fail($"Expected exception instead of response: {role?.Guid}"); + } + catch (Exception exception) + { + Assert.IsInstanceOfType(exception, typeof(Exception)); + } + } + else + { + Assert.Fail($"RoleService is null"); + } + } + catch (Exception ex) + { + Assert.Fail($"An exception was thrown: {ex}"); + } + } + [TestMethod] public async Task CheckIfNameIsValid_NameCurrentRole() { @@ -261,6 +297,111 @@ public class RoleService_Tests } } + [TestMethod] + public async Task UpdateRoleAsync_Success() + { + try + { + CreateRoleRequestData data = new CreateRoleRequestData() + { + Name = "ChangedRoleName", + IsNotEditable = false + }; + + if (_roleService != null) + { + Assert.IsNotNull(_role); + var role = await _roleService.UpdateRoleAsync(data, _role!); + Assert.IsInstanceOfType(role, typeof(Role)); + Assert.IsNotNull(role); + Assert.IsTrue(data.Name == role.Name); + Assert.IsTrue(data.IsNotEditable == role.IsNotEditable); + _role = role; + } + else + { + Assert.Fail($"RoleService is null"); + } + } + catch (Exception ex) + { + Console.WriteLine(ex.InnerException); + Assert.Fail($"An exception was thrown: {ex}"); + } + } + + [TestMethod] + public async Task UpdateRoleAsync_NotEditable() + { + try + { + CreateRoleRequestData createRoleData = new CreateRoleRequestData() + { + Name = "NotEditableRole", + IsNotEditable = true + }; + + + if (_roleService != null) + { + var role = await _roleService.CreateRoleAsync(createRoleData); + Assert.IsNotNull(role); + + CreateRoleRequestData updateRoleData = new CreateRoleRequestData() + { + Name = "TryingToEditRole", + IsNotEditable = false + }; + + var roleUpdatedRole = await _roleService.UpdateRoleAsync(updateRoleData, role!); + Assert.IsInstanceOfType(roleUpdatedRole, typeof(Role)); + Assert.IsNotNull(roleUpdatedRole); + Assert.IsTrue(roleUpdatedRole.Name == createRoleData.Name); + Assert.IsTrue(roleUpdatedRole.IsNotEditable == createRoleData.IsNotEditable); + } + else + { + Assert.Fail($"RoleService is null"); + } + } + catch (Exception ex) + { + Console.WriteLine(ex.InnerException); + Assert.Fail($"An exception was thrown: {ex}"); + } + } + + [TestMethod] + public async Task UpdateRoleAsync_Exception() + { + try + { + CreateRoleRequestData data = new CreateRoleRequestData() + { + Name = "Exception", + IsNotEditable = false + }; + + var exceptionRoleService = TestUtils.CreateRoleServiceException(); + + if (exceptionRoleService != null) + { + Assert.IsNotNull(_role); + var role = await exceptionRoleService.UpdateRoleAsync(data, _role!); + Assert.Fail($"Expected exception instead of response: {role?.Guid}"); + + } + else + { + Assert.Fail($"RoleService is null"); + } + } + catch (Exception ex) + { + Assert.IsInstanceOfType(ex, typeof(Exception)); + } + } + [TestMethod] public async Task DeleteRoleAsync() { diff --git a/MainProject.Tests/Services/UserService_Tests.cs b/MainProject.Tests/Services/UserService_Tests.cs index 3af26f7..74b0381 100644 --- a/MainProject.Tests/Services/UserService_Tests.cs +++ b/MainProject.Tests/Services/UserService_Tests.cs @@ -119,6 +119,52 @@ public class UserService_Tests } } + [TestMethod] + public async Task CreateUserAsync_Exception() + { + try + { + var expectedUser = ModelsInit.CreateUser(); + + CreateUserRequestData data = new CreateUserRequestData() + { + FirstName = expectedUser.FirstName ?? String.Empty, + LastName = expectedUser.LastName ?? String.Empty, + Email = expectedUser.Email ?? String.Empty + }; + + Role role = new() + { + Name = expectedUser.Role?.Name ?? String.Empty, + IsNotEditable = expectedUser.Role?.IsNotEditable ?? false, + Guid = expectedUser.Role?.Guid ?? String.Empty + }; + + var exceptionUserService = TestUtils.CreateUserServiceException(); + + if (exceptionUserService != null) + { + try + { + var user = await exceptionUserService.CreateUserAsync(data, role); + Assert.Fail($"Expected exception instead of response: {user?.Guid}"); + } + catch (Exception exception) + { + Assert.IsInstanceOfType(exception, typeof(Exception)); + } + } + else + { + Assert.Fail($"UserService is null"); + } + } + catch (Exception ex) + { + Assert.Fail($"An exception was thrown: {ex}"); + } + } + [TestMethod] public async Task CheckIfEmailIsValid_EmailCurrentUser() { diff --git a/MainProject.Tests/TestsUtils/TestUtils.cs b/MainProject.Tests/TestsUtils/TestUtils.cs index 099777c..aa21c64 100644 --- a/MainProject.Tests/TestsUtils/TestUtils.cs +++ b/MainProject.Tests/TestsUtils/TestUtils.cs @@ -64,6 +64,11 @@ public static class TestUtils 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 SqlServerContext CreateInMemorySqlContext() { var options = new DbContextOptionsBuilder() @@ -79,8 +84,6 @@ public static class TestUtils public static BaseService CreateBaseService() { IConfiguration configuration = CreateConfiguration(); - var optionsBuilder = new DbContextOptionsBuilder(); - optionsBuilder.UseSqlServer(GetSqlConnectionString(configuration)); SqlServerContext sqlServerContext = CreateInMemorySqlContext(); var httpContextAccessor = new Mock(); return new BaseService(httpContextAccessor.Object, configuration, sqlServerContext); @@ -89,8 +92,6 @@ public static class TestUtils public static AuthService CreateAuthService() { IConfiguration configuration = CreateConfiguration(); - var optionsBuilder = new DbContextOptionsBuilder(); - optionsBuilder.UseSqlServer(GetSqlConnectionString(configuration)); SqlServerContext sqlServerContext = CreateInMemorySqlContext(); var userServiceMock = new Mock(); var httpContextAccessor = new Mock(); @@ -105,6 +106,16 @@ public static class TestUtils return new UserService(httpContextAccessor.Object, configuration, sqlServerContext); } + public static UserService CreateUserServiceException() + { + IConfiguration configuration = CreateConfiguration(); + var optionsBuilder = new DbContextOptionsBuilder(); + optionsBuilder.UseSqlServer(GetFakeConnectionString()); + SqlServerContext sqlServerContext = new SqlServerContext(optionsBuilder.Options); + var httpContextAccessor = new Mock(); + return new UserService(httpContextAccessor.Object, configuration, sqlServerContext); + } + public static JwtService CreateJwtService() { IConfiguration configuration = CreateConfiguration(); @@ -124,6 +135,16 @@ public static class TestUtils var httpContextAccessor = new Mock(); return new RoleService(httpContextAccessor.Object, configuration, sqlServerContext); } + + public static RoleService CreateRoleServiceException() + { + IConfiguration configuration = CreateConfiguration(); + var optionsBuilder = new DbContextOptionsBuilder(); + optionsBuilder.UseSqlServer(GetFakeConnectionString()); + SqlServerContext sqlServerContext = new SqlServerContext(optionsBuilder.Options); + var httpContextAccessor = new Mock(); + return new RoleService(httpContextAccessor.Object, configuration, sqlServerContext); + } } diff --git a/MainProject.Tests/Utils/CryptoUtils_Tests.cs b/MainProject.Tests/Utils/CryptoUtils_Tests.cs index 4d2a7ab..c054658 100644 --- a/MainProject.Tests/Utils/CryptoUtils_Tests.cs +++ b/MainProject.Tests/Utils/CryptoUtils_Tests.cs @@ -151,8 +151,6 @@ public class CryptoUtils_Tests AppSettings appSettings = ProgramUtils.AddConfiguration(ref builder, System.AppDomain.CurrentDomain.BaseDirectory + "/JsonData"); CryptUtils cryptoUtils = new CryptUtils(appSettings); var verified = cryptoUtils.VerifyPassword(password, salt, 0, hashedPassword); - Console.WriteLine(cryptoUtils.GeneratePassword(password, salt, 0)); - Assert.IsTrue(verified); } catch (Exception ex) diff --git a/MainProject.Tests/Utils/ProgramUtils_Tests.cs b/MainProject.Tests/Utils/ProgramUtils_Tests.cs index 261236e..f6de5b9 100644 --- a/MainProject.Tests/Utils/ProgramUtils_Tests.cs +++ b/MainProject.Tests/Utils/ProgramUtils_Tests.cs @@ -299,7 +299,6 @@ public class ProgramUtils_Tests ProgramUtils.AddDbContext(ref builder, realAppSettings); var areEquals = expectedDbSettings.SqlServerConnectionString == realAppSettings.DatabaseSettings?.SqlServerConnectionString; - Console.WriteLine(realAppSettings.DatabaseSettings?.SqlServerConnectionString); Assert.IsTrue(areEquals); } catch (Exception ex) diff --git a/MainProject/Controllers/RoleController.cs b/MainProject/Controllers/RoleController.cs index e2f64be..7aad3c5 100644 --- a/MainProject/Controllers/RoleController.cs +++ b/MainProject/Controllers/RoleController.cs @@ -146,6 +146,11 @@ namespace BasicDotnetTemplate.MainProject.Controllers 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) diff --git a/MainProject/Services/RoleService.cs b/MainProject/Services/RoleService.cs index 713469f..703a939 100644 --- a/MainProject/Services/RoleService.cs +++ b/MainProject/Services/RoleService.cs @@ -116,6 +116,9 @@ public class RoleService : BaseService, IRoleService public async Task UpdateRoleAsync(CreateRoleRequestData data, Role role) { + if (role.IsNotEditable) + return role; + using var transaction = await _sqlServerContext.Database.BeginTransactionAsync(); try diff --git a/MainProject/Services/UserService.cs b/MainProject/Services/UserService.cs index ba471ab..f4be4e2 100644 --- a/MainProject/Services/UserService.cs +++ b/MainProject/Services/UserService.cs @@ -104,12 +104,13 @@ public class UserService : BaseService, IUserService public async Task CreateUserAsync(CreateUserRequestData data, Role role) { + User? user; + using var transaction = await _sqlServerContext.Database.BeginTransactionAsync(); - User? user; - var tempUser = CreateUserData(data, role); try { + var tempUser = CreateUserData(data, role); await _sqlServerContext.Users.AddAsync(tempUser); await _sqlServerContext.SaveChangesAsync(); await transaction.CommitAsync();