Roles #21
@@ -11,6 +11,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<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="Moq" Version="4.20.72" />
|
||||
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
|
||||
|
||||
@@ -12,6 +12,8 @@ public class RoleService_Tests
|
||||
{
|
||||
private static Role? _expectedRole = ModelsInit.CreateRole();
|
||||
private static Role? _role;
|
||||
private static RoleService _roleService = TestUtils.CreateRoleService();
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public void Inizialize()
|
||||
@@ -41,10 +43,10 @@ public class RoleService_Tests
|
||||
try
|
||||
{
|
||||
var expectedRole = ModelsInit.CreateRole();
|
||||
var roleService = TestUtils.CreateRoleService();
|
||||
if (roleService != null)
|
||||
|
||||
if (_roleService != null)
|
||||
{
|
||||
var valid = await roleService.CheckIfNameIsValid(expectedRole.Name);
|
||||
var valid = await _roleService.CheckIfNameIsValid(expectedRole.Name);
|
||||
Assert.IsTrue(valid);
|
||||
}
|
||||
else
|
||||
@@ -64,7 +66,6 @@ public class RoleService_Tests
|
||||
{
|
||||
try
|
||||
{
|
||||
var _roleService = TestUtils.CreateRoleService();
|
||||
CreateRoleRequestData data = new CreateRoleRequestData()
|
||||
{
|
||||
Name = _expectedRole?.Name ?? String.Empty,
|
||||
@@ -98,10 +99,9 @@ public class RoleService_Tests
|
||||
try
|
||||
{
|
||||
var expectedRole = ModelsInit.CreateRole();
|
||||
var roleService = TestUtils.CreateRoleService();
|
||||
if (roleService != null)
|
||||
if (_roleService != null)
|
||||
{
|
||||
var valid = await roleService.CheckIfNameIsValid(expectedRole.Name, _role?.Guid ?? String.Empty);
|
||||
var valid = await _roleService.CheckIfNameIsValid(expectedRole.Name, _role?.Guid ?? String.Empty);
|
||||
Assert.IsTrue(valid);
|
||||
}
|
||||
else
|
||||
@@ -122,10 +122,10 @@ public class RoleService_Tests
|
||||
try
|
||||
{
|
||||
var expectedRole = ModelsInit.CreateRole();
|
||||
var roleService = TestUtils.CreateRoleService();
|
||||
if (roleService != null)
|
||||
|
||||
if (_roleService != null)
|
||||
{
|
||||
var valid = await roleService.CheckIfNameIsValid(expectedRole.Name);
|
||||
var valid = await _roleService.CheckIfNameIsValid(expectedRole.Name);
|
||||
Assert.IsFalse(valid);
|
||||
}
|
||||
else
|
||||
@@ -145,7 +145,6 @@ public class RoleService_Tests
|
||||
{
|
||||
try
|
||||
{
|
||||
var _roleService = TestUtils.CreateRoleService();
|
||||
if (_roleService != null)
|
||||
{
|
||||
var role = await _roleService.GetRoleByIdAsync(_role?.Id ?? 0);
|
||||
@@ -169,7 +168,6 @@ public class RoleService_Tests
|
||||
{
|
||||
try
|
||||
{
|
||||
var _roleService = TestUtils.CreateRoleService();
|
||||
if (_roleService != null)
|
||||
{
|
||||
var role = await _roleService.GetRoleByGuidAsync(_role?.Guid ?? String.Empty);
|
||||
@@ -193,7 +191,6 @@ public class RoleService_Tests
|
||||
{
|
||||
try
|
||||
{
|
||||
var _roleService = TestUtils.CreateRoleService();
|
||||
if (_roleService != null)
|
||||
{
|
||||
var role = await _roleService.GetRoleByGuidAsync(_role?.Guid ?? String.Empty);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using BasicDotnetTemplate.MainProject.Services;
|
||||
using BasicDotnetTemplate.MainProject.Models.Api.Data.User;
|
||||
using BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
|
||||
|
||||
@@ -10,7 +11,7 @@ namespace BasicDotnetTemplate.MainProject.Tests;
|
||||
public class UserService_Tests
|
||||
{
|
||||
private static User _user = ModelsInit.CreateUser();
|
||||
private static Role _role = ModelsInit.CreateRole();
|
||||
private static UserService _userService = TestUtils.CreateUserService();
|
||||
|
||||
[TestMethod]
|
||||
public void Inizialize()
|
||||
@@ -34,46 +35,15 @@ public class UserService_Tests
|
||||
}
|
||||
}
|
||||
|
||||
// [TestInitialize]
|
||||
// public void Setup()
|
||||
// {
|
||||
// _expectedUser = ModelsInit.CreateUser();
|
||||
// _expectedRole = ModelsInit.CreateRole();
|
||||
// _roleService = TestUtils.CreateRoleService();
|
||||
// _userService = TestUtils.CreateUserService();
|
||||
// }
|
||||
|
||||
// [TestMethod]
|
||||
// public void Inizialize()
|
||||
// {
|
||||
// try
|
||||
// {
|
||||
// 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}");
|
||||
// }
|
||||
// }
|
||||
|
||||
[TestMethod]
|
||||
public async Task GetUserByUsernameAndPassword_Null()
|
||||
{
|
||||
try
|
||||
{
|
||||
var userService = TestUtils.CreateUserService();
|
||||
var testString = "test";
|
||||
if (userService != null)
|
||||
if (_userService != null)
|
||||
{
|
||||
var user = await userService.GetUserByUsernameAndPassword(testString, testString);
|
||||
var user = await _userService.GetUserByUsernameAndPassword(testString, testString);
|
||||
Assert.IsTrue(user == null);
|
||||
}
|
||||
else
|
||||
@@ -88,41 +58,14 @@ public class UserService_Tests
|
||||
}
|
||||
}
|
||||
|
||||
// // TODO
|
||||
// // [TestMethod]
|
||||
// public async Task GetUserByUsernameAndPassword_Success()
|
||||
// {
|
||||
// try
|
||||
// {
|
||||
// var testEmail = "test@email.it";
|
||||
// var testPassword = "password";
|
||||
// if (_userService != null)
|
||||
// {
|
||||
// var user = await _userService.GetUserByUsernameAndPassword(testEmail, testPassword);
|
||||
// Assert.IsTrue(user != null);
|
||||
// Assert.IsTrue(user.Email == testEmail);
|
||||
// }
|
||||
// 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
|
||||
{
|
||||
var userService = TestUtils.CreateUserService();
|
||||
if (userService != null)
|
||||
if (_userService != null)
|
||||
{
|
||||
var valid = await userService.CheckIfEmailIsValid(_user.Email ?? String.Empty);
|
||||
var valid = await _userService.CheckIfEmailIsValid(_user.Email ?? String.Empty);
|
||||
Assert.IsTrue(valid);
|
||||
}
|
||||
else
|
||||
@@ -142,7 +85,7 @@ public class UserService_Tests
|
||||
{
|
||||
try
|
||||
{
|
||||
var userService = TestUtils.CreateUserService();
|
||||
|
||||
var expectedUser = ModelsInit.CreateUser();
|
||||
|
||||
CreateUserRequestData data = new CreateUserRequestData()
|
||||
@@ -159,7 +102,7 @@ public class UserService_Tests
|
||||
Guid = expectedUser.Role?.Guid ?? String.Empty
|
||||
};
|
||||
|
||||
var user = await userService.CreateUserAsync(data, role);
|
||||
var user = await _userService.CreateUserAsync(data, role);
|
||||
Assert.IsInstanceOfType(user, typeof(User));
|
||||
Assert.IsNotNull(user);
|
||||
Assert.IsTrue(expectedUser.FirstName == user.FirstName);
|
||||
@@ -182,10 +125,10 @@ public class UserService_Tests
|
||||
{
|
||||
try
|
||||
{
|
||||
var userService = TestUtils.CreateUserService();
|
||||
if (userService != null)
|
||||
|
||||
if (_userService != null)
|
||||
{
|
||||
var valid = await userService.CheckIfEmailIsValid(_user.Email ?? String.Empty, _user.Guid ?? String.Empty);
|
||||
var valid = await _userService.CheckIfEmailIsValid(_user.Email ?? String.Empty, _user.Guid ?? String.Empty);
|
||||
Assert.IsTrue(valid);
|
||||
}
|
||||
else
|
||||
@@ -205,10 +148,10 @@ public class UserService_Tests
|
||||
{
|
||||
try
|
||||
{
|
||||
var userService = TestUtils.CreateUserService();
|
||||
if (userService != null)
|
||||
|
||||
if (_userService != null)
|
||||
{
|
||||
var valid = await userService.CheckIfEmailIsValid(_user.Email ?? String.Empty);
|
||||
var valid = await _userService.CheckIfEmailIsValid(_user.Email ?? String.Empty);
|
||||
Assert.IsFalse(valid);
|
||||
}
|
||||
else
|
||||
@@ -228,10 +171,10 @@ public class UserService_Tests
|
||||
{
|
||||
try
|
||||
{
|
||||
var userService = TestUtils.CreateUserService();
|
||||
if (userService != null)
|
||||
|
||||
if (_userService != null)
|
||||
{
|
||||
var user = await userService.GetUserByIdAsync(_user.Id);
|
||||
var user = await _userService.GetUserByIdAsync(_user.Id);
|
||||
Assert.IsNotNull(user);
|
||||
Assert.IsTrue(user.Id == _user?.Id);
|
||||
}
|
||||
@@ -252,10 +195,10 @@ public class UserService_Tests
|
||||
{
|
||||
try
|
||||
{
|
||||
var userService = TestUtils.CreateUserService();
|
||||
if (userService != null)
|
||||
|
||||
if (_userService != null)
|
||||
{
|
||||
var user = await userService.GetUserByGuidAsync(_user.Guid ?? String.Empty);
|
||||
var user = await _userService.GetUserByGuidAsync(_user.Guid ?? String.Empty);
|
||||
Assert.IsNotNull(user);
|
||||
Assert.IsTrue(user.Guid == _user?.Guid);
|
||||
}
|
||||
@@ -276,12 +219,12 @@ public class UserService_Tests
|
||||
{
|
||||
try
|
||||
{
|
||||
var userService = TestUtils.CreateUserService();
|
||||
if (userService != null)
|
||||
|
||||
if (_userService != null)
|
||||
{
|
||||
var user = await userService.GetUserByGuidAsync(_user.Guid ?? String.Empty);
|
||||
var user = await _userService.GetUserByGuidAsync(_user.Guid ?? String.Empty);
|
||||
Assert.IsNotNull(user);
|
||||
var deleted = await userService.DeleteUserAsync(user);
|
||||
var deleted = await _userService.DeleteUserAsync(user);
|
||||
Assert.IsTrue(deleted);
|
||||
}
|
||||
else
|
||||
@@ -297,17 +240,6 @@ public class UserService_Tests
|
||||
}
|
||||
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public static async Task CleanupAsync()
|
||||
{
|
||||
var roleService = TestUtils.CreateRoleService();
|
||||
var role = await roleService.GetRoleByGuidAsync(_role.Guid ?? String.Empty);
|
||||
Assert.IsNotNull(role);
|
||||
var deleted = await roleService.DeleteRoleAsync(role);
|
||||
Assert.IsTrue(deleted);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ using Moq;
|
||||
using BasicDotnetTemplate.MainProject.Core.Database;
|
||||
using BasicDotnetTemplate.MainProject.Services;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
|
||||
namespace BasicDotnetTemplate.MainProject.Tests;
|
||||
@@ -51,12 +52,24 @@ public static class TestUtils
|
||||
return _appSettings.DatabaseSettings?.SqlServerConnectionString ?? String.Empty;
|
||||
}
|
||||
|
||||
public static SqlServerContext CreateInMemorySqlContext()
|
||||
{
|
||||
var options = new DbContextOptionsBuilder<SqlServerContext>()
|
||||
.UseSqlite("DataSource=:memory:") // Database in-memory
|
||||
.Options;
|
||||
|
||||
var context = new SqlServerContext(options);
|
||||
context.Database.OpenConnection();
|
||||
context.Database.EnsureCreated();
|
||||
return context;
|
||||
}
|
||||
|
||||
public static AuthService CreateAuthService()
|
||||
{
|
||||
IConfiguration configuration = CreateConfiguration();
|
||||
var optionsBuilder = new DbContextOptionsBuilder<SqlServerContext>();
|
||||
optionsBuilder.UseSqlServer(GetSqlConnectionString(configuration));
|
||||
SqlServerContext sqlServerContext = new SqlServerContext(optionsBuilder.Options);
|
||||
SqlServerContext sqlServerContext = CreateInMemorySqlContext();
|
||||
var userServiceMock = new Mock<IUserService>();
|
||||
var httpContextAccessor = new Mock<IHttpContextAccessor>();
|
||||
return new AuthService(httpContextAccessor.Object, configuration, sqlServerContext, userServiceMock.Object);
|
||||
@@ -65,9 +78,7 @@ public static class TestUtils
|
||||
public static UserService CreateUserService()
|
||||
{
|
||||
IConfiguration configuration = CreateConfiguration();
|
||||
var optionsBuilder = new DbContextOptionsBuilder<SqlServerContext>();
|
||||
optionsBuilder.UseSqlServer(GetSqlConnectionString(configuration));
|
||||
SqlServerContext sqlServerContext = new SqlServerContext(optionsBuilder.Options);
|
||||
SqlServerContext sqlServerContext = CreateInMemorySqlContext();
|
||||
var httpContextAccessor = new Mock<IHttpContextAccessor>();
|
||||
return new UserService(httpContextAccessor.Object, configuration, sqlServerContext);
|
||||
}
|
||||
@@ -77,7 +88,7 @@ public static class TestUtils
|
||||
IConfiguration configuration = CreateConfiguration();
|
||||
var optionsBuilder = new DbContextOptionsBuilder<SqlServerContext>();
|
||||
optionsBuilder.UseSqlServer(GetSqlConnectionString(configuration));
|
||||
SqlServerContext sqlServerContext = new SqlServerContext(optionsBuilder.Options);
|
||||
SqlServerContext sqlServerContext = CreateInMemorySqlContext();
|
||||
var httpContextAccessor = new Mock<IHttpContextAccessor>();
|
||||
return new JwtService(httpContextAccessor.Object, configuration, sqlServerContext);
|
||||
}
|
||||
@@ -87,7 +98,7 @@ public static class TestUtils
|
||||
IConfiguration configuration = CreateConfiguration();
|
||||
var optionsBuilder = new DbContextOptionsBuilder<SqlServerContext>();
|
||||
optionsBuilder.UseSqlServer(GetSqlConnectionString(configuration));
|
||||
SqlServerContext sqlServerContext = new SqlServerContext(optionsBuilder.Options);
|
||||
SqlServerContext sqlServerContext = CreateInMemorySqlContext();
|
||||
var httpContextAccessor = new Mock<IHttpContextAccessor>();
|
||||
return new RoleService(httpContextAccessor.Object, configuration, sqlServerContext);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user