Adding role creation during startup + minor fixes in tests
This commit is contained in:
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 = "Email",
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,7 @@ using Microsoft.EntityFrameworkCore;
|
||||
using Moq;
|
||||
using BasicDotnetTemplate.MainProject.Core.Database;
|
||||
using BasicDotnetTemplate.MainProject.Services;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
|
||||
namespace BasicDotnetTemplate.MainProject.Tests;
|
||||
@@ -43,33 +44,42 @@ public static class TestUtils
|
||||
.Build();
|
||||
}
|
||||
|
||||
public static string GetSqlConnectionString(IConfiguration configuration)
|
||||
{
|
||||
AppSettings _appSettings = new AppSettings();
|
||||
configuration.GetSection("AppSettings").Bind(_appSettings);
|
||||
return _appSettings.DatabaseSettings?.SqlServerConnectionString ?? String.Empty;
|
||||
}
|
||||
|
||||
public static AuthService CreateAuthService()
|
||||
{
|
||||
IConfiguration configuration = CreateConfiguration();
|
||||
var optionsBuilder = new DbContextOptionsBuilder<SqlServerContext>();
|
||||
optionsBuilder.UseSqlServer("test");
|
||||
optionsBuilder.UseSqlServer(GetSqlConnectionString(configuration));
|
||||
SqlServerContext sqlServerContext = new SqlServerContext(optionsBuilder.Options);
|
||||
var userServiceMock = new Mock<IUserService>();
|
||||
return new AuthService(configuration, sqlServerContext, userServiceMock.Object);
|
||||
var httpContextAccessor = new Mock<IHttpContextAccessor>();
|
||||
return new AuthService(httpContextAccessor.Object, configuration, sqlServerContext, userServiceMock.Object);
|
||||
}
|
||||
|
||||
public static UserService CreateUserService()
|
||||
{
|
||||
IConfiguration configuration = CreateConfiguration();
|
||||
var optionsBuilder = new DbContextOptionsBuilder<SqlServerContext>();
|
||||
optionsBuilder.UseSqlServer("test");
|
||||
optionsBuilder.UseSqlServer(GetSqlConnectionString(configuration));
|
||||
SqlServerContext sqlServerContext = new SqlServerContext(optionsBuilder.Options);
|
||||
return new UserService(configuration, sqlServerContext);
|
||||
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("test");
|
||||
optionsBuilder.UseSqlServer(GetSqlConnectionString(configuration));
|
||||
SqlServerContext sqlServerContext = new SqlServerContext(optionsBuilder.Options);
|
||||
var userServiceMock = new Mock<IUserService>();
|
||||
return new JwtService(configuration, sqlServerContext, userServiceMock.Object);
|
||||
var httpContextAccessor = new Mock<IHttpContextAccessor>();
|
||||
return new JwtService(httpContextAccessor.Object, configuration, sqlServerContext);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user