Adding tests
This commit is contained in:
@@ -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<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()
|
||||
{
|
||||
|
||||
@@ -39,7 +39,6 @@ public class RootController_Test
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.InnerException);
|
||||
Assert.Fail($"An exception was thrown: {ex}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,7 +69,6 @@ public class Program_Tests
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.InnerException);
|
||||
Assert.Fail($"An exception was thrown: {ex}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
{
|
||||
|
||||
@@ -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()
|
||||
{
|
||||
|
||||
@@ -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<SqlServerContext>()
|
||||
@@ -79,8 +84,6 @@ public static class TestUtils
|
||||
public static BaseService CreateBaseService()
|
||||
{
|
||||
IConfiguration configuration = CreateConfiguration();
|
||||
var optionsBuilder = new DbContextOptionsBuilder<SqlServerContext>();
|
||||
optionsBuilder.UseSqlServer(GetSqlConnectionString(configuration));
|
||||
SqlServerContext sqlServerContext = CreateInMemorySqlContext();
|
||||
var httpContextAccessor = new Mock<IHttpContextAccessor>();
|
||||
return new BaseService(httpContextAccessor.Object, configuration, sqlServerContext);
|
||||
@@ -89,8 +92,6 @@ public static class TestUtils
|
||||
public static AuthService CreateAuthService()
|
||||
{
|
||||
IConfiguration configuration = CreateConfiguration();
|
||||
var optionsBuilder = new DbContextOptionsBuilder<SqlServerContext>();
|
||||
optionsBuilder.UseSqlServer(GetSqlConnectionString(configuration));
|
||||
SqlServerContext sqlServerContext = CreateInMemorySqlContext();
|
||||
var userServiceMock = new Mock<IUserService>();
|
||||
var httpContextAccessor = new Mock<IHttpContextAccessor>();
|
||||
@@ -105,6 +106,16 @@ public static class TestUtils
|
||||
return new UserService(httpContextAccessor.Object, configuration, sqlServerContext);
|
||||
}
|
||||
|
||||
public static UserService CreateUserServiceException()
|
||||
{
|
||||
IConfiguration configuration = CreateConfiguration();
|
||||
var optionsBuilder = new DbContextOptionsBuilder<SqlServerContext>();
|
||||
optionsBuilder.UseSqlServer(GetFakeConnectionString());
|
||||
SqlServerContext sqlServerContext = new SqlServerContext(optionsBuilder.Options);
|
||||
var httpContextAccessor = new Mock<IHttpContextAccessor>();
|
||||
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<IHttpContextAccessor>();
|
||||
return new RoleService(httpContextAccessor.Object, configuration, sqlServerContext);
|
||||
}
|
||||
|
||||
public static RoleService CreateRoleServiceException()
|
||||
{
|
||||
IConfiguration configuration = CreateConfiguration();
|
||||
var optionsBuilder = new DbContextOptionsBuilder<SqlServerContext>();
|
||||
optionsBuilder.UseSqlServer(GetFakeConnectionString());
|
||||
SqlServerContext sqlServerContext = new SqlServerContext(optionsBuilder.Options);
|
||||
var httpContextAccessor = new Mock<IHttpContextAccessor>();
|
||||
return new RoleService(httpContextAccessor.Object, configuration, sqlServerContext);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -151,8 +151,6 @@ public class CryptoUtils_Tests
|
||||
AppSettings appSettings = ProgramUtils.AddConfiguration(ref builder, System.AppDomain.CurrentDomain.BaseDirectory + "/JsonData");
|
||||
CryptUtils cryptoUtils = new CryptUtils(appSettings);
|
||||
var verified = cryptoUtils.VerifyPassword(password, salt, 0, hashedPassword);
|
||||
Console.WriteLine(cryptoUtils.GeneratePassword(password, salt, 0));
|
||||
|
||||
Assert.IsTrue(verified);
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -116,6 +116,9 @@ public class RoleService : BaseService, IRoleService
|
||||
|
||||
public async Task<Role?> UpdateRoleAsync(CreateRoleRequestData data, Role role)
|
||||
{
|
||||
if (role.IsNotEditable)
|
||||
return role;
|
||||
|
||||
using var transaction = await _sqlServerContext.Database.BeginTransactionAsync();
|
||||
|
||||
try
|
||||
|
||||
@@ -104,12 +104,13 @@ public class UserService : BaseService, IUserService
|
||||
|
||||
public async Task<User?> 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();
|
||||
|
||||
Reference in New Issue
Block a user