mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-07-15 14:04:41 +08:00
同步OpenAuth.Core,单元测试使用WebApi配置文件
This commit is contained in:
parent
1ed50271bf
commit
24f8eaf8c0
@ -22,6 +22,7 @@
|
|||||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="3.1.2" />
|
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="3.1.2" />
|
||||||
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="2.0.4" />
|
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="2.0.4" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Http" Version="3.1.2" />
|
<PackageReference Include="Microsoft.Extensions.Http" Version="3.1.2" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Logging.Log4Net.AspNetCore" Version="3.1.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="3.1.2" />
|
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="3.1.2" />
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
|
||||||
<PackageReference Include="Moq" Version="4.13.1" />
|
<PackageReference Include="Moq" Version="4.13.1" />
|
||||||
|
@ -33,8 +33,7 @@ namespace OpenAuth.App
|
|||||||
UnitWork.Save();
|
UnitWork.Save();
|
||||||
|
|
||||||
//如果当前账号不是SYSTEM,则直接分配
|
//如果当前账号不是SYSTEM,则直接分配
|
||||||
var loginUser = _auth.GetCurrentUser();
|
if (loginContext.User.Account != Define.SYSTEM_USERNAME)
|
||||||
if (loginUser.User.Account != Define.SYSTEM_USERNAME)
|
|
||||||
{
|
{
|
||||||
_revelanceApp.Assign(new AssignReq
|
_revelanceApp.Assign(new AssignReq
|
||||||
{
|
{
|
||||||
|
@ -1,15 +1,14 @@
|
|||||||
using Autofac.Extensions.DependencyInjection;
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using Autofac.Extensions.DependencyInjection;
|
||||||
using Infrastructure;
|
using Infrastructure;
|
||||||
using Infrastructure.Cache;
|
|
||||||
using Infrastructure.Extensions.AutofacManager;
|
using Infrastructure.Extensions.AutofacManager;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Logging;
|
||||||
using Moq;
|
using Moq;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
using OpenAuth.App.SSO;
|
|
||||||
using OpenAuth.Repository;
|
using OpenAuth.Repository;
|
||||||
|
|
||||||
namespace OpenAuth.App.Test
|
namespace OpenAuth.App.Test
|
||||||
@ -24,31 +23,36 @@ namespace OpenAuth.App.Test
|
|||||||
var serviceCollection = GetService();
|
var serviceCollection = GetService();
|
||||||
serviceCollection.AddMemoryCache();
|
serviceCollection.AddMemoryCache();
|
||||||
serviceCollection.AddOptions();
|
serviceCollection.AddOptions();
|
||||||
serviceCollection.AddLogging();
|
//读取OpenAuth.WebApi的配置文件用于单元测试
|
||||||
|
var path = AppContext.BaseDirectory;
|
||||||
//模拟配置文件
|
int pos = path.IndexOf("OpenAuth.App");
|
||||||
var optionMock = new Mock<IOptions<AppSetting>>();
|
var basepath = Path.Combine(path.Substring(0,pos) ,"OpenAuth.WebApi");
|
||||||
optionMock.Setup(x => x.Value).Returns(new AppSetting { DbType = Define.DBTYPE_MYSQL});
|
IConfiguration config = new ConfigurationBuilder()
|
||||||
serviceCollection.AddScoped(x => optionMock.Object);
|
.SetBasePath(basepath)
|
||||||
|
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
||||||
|
.AddJsonFile("appsettings.Development.json", optional: true)
|
||||||
|
.AddEnvironmentVariables()
|
||||||
|
.Build();
|
||||||
|
Console.WriteLine($"单元测试数据库信息:{config.GetSection("AppSetting")["DbType"]}/{config.GetSection("ConnectionStrings")["OpenAuthDBContext"]}");
|
||||||
|
|
||||||
//模拟多租户id
|
//添加log4net
|
||||||
var configMock = new Mock<IConfiguration>();
|
serviceCollection.AddLogging(builder =>
|
||||||
configMock.Setup(x => x.GetSection("ConnectionStrings")[Define.TENANT_ID]).Returns("");
|
{
|
||||||
serviceCollection.AddScoped(x => configMock.Object);
|
builder.ClearProviders(); //去掉默认的日志
|
||||||
|
builder.AddConfiguration(config.GetSection("Logging")); //读取配置文件中的Logging配置
|
||||||
|
builder.AddLog4Net();
|
||||||
|
});
|
||||||
|
//注入OpenAuth.WebApi配置文件
|
||||||
|
serviceCollection.AddScoped(x => config);
|
||||||
|
|
||||||
|
//模拟HTTP请求
|
||||||
var httpContextAccessorMock = new Mock<IHttpContextAccessor>();
|
var httpContextAccessorMock = new Mock<IHttpContextAccessor>();
|
||||||
httpContextAccessorMock.Setup(x => x.HttpContext.Request.Query[Define.TOKEN_NAME]).Returns("tokentest");
|
httpContextAccessorMock.Setup(x => x.HttpContext.Request.Query[Define.TOKEN_NAME]).Returns("tokentest");
|
||||||
httpContextAccessorMock.Setup(x => x.HttpContext.Request.Query[Define.TENANT_ID]).Returns("OpenAuthDBContext");
|
httpContextAccessorMock.Setup(x => x.HttpContext.Request.Query[Define.TENANT_ID]).Returns("OpenAuthDBContext");
|
||||||
|
|
||||||
serviceCollection.AddScoped(x => httpContextAccessorMock.Object);
|
serviceCollection.AddScoped(x => httpContextAccessorMock.Object);
|
||||||
|
|
||||||
// 测试my sql
|
serviceCollection.AddDbContext<OpenAuthDBContext>();
|
||||||
serviceCollection.AddDbContext<OpenAuthDBContext>(options =>
|
|
||||||
options.UseMySql("server=127.0.0.1;user id=root;database=openauthdb;password=000000"));
|
|
||||||
|
|
||||||
// serviceCollection.AddDbContext<OpenAuthDBContext>(options =>
|
|
||||||
// options.UseSqlServer("Data Source=.;Initial Catalog=OpenAuthDB;User=sa;Password=000000;Integrated Security=True"));
|
|
||||||
|
|
||||||
var container = AutofacExt.InitForTest(serviceCollection);
|
var container = AutofacExt.InitForTest(serviceCollection);
|
||||||
_autofacServiceProvider = new AutofacServiceProvider(container);
|
_autofacServiceProvider = new AutofacServiceProvider(container);
|
||||||
AutofacContainerModule.ConfigServiceProvider(_autofacServiceProvider);
|
AutofacContainerModule.ConfigServiceProvider(_autofacServiceProvider);
|
||||||
|
@ -92,16 +92,7 @@ namespace OpenAuth.Mvc
|
|||||||
var connectionString = Configuration.GetConnectionString("OpenAuthDBContext");
|
var connectionString = Configuration.GetConnectionString("OpenAuthDBContext");
|
||||||
logger.LogInformation($"当前数据库类型:{dbType},连接字符串:{connectionString}");
|
logger.LogInformation($"当前数据库类型:{dbType},连接字符串:{connectionString}");
|
||||||
|
|
||||||
if (dbType == Define.DBTYPE_SQLSERVER)
|
services.AddDbContext<OpenAuthDBContext>();
|
||||||
{
|
|
||||||
services.AddDbContext<OpenAuthDBContext>(options =>
|
|
||||||
options.UseSqlServer(connectionString));
|
|
||||||
}
|
|
||||||
else //mysql
|
|
||||||
{
|
|
||||||
services.AddDbContext<OpenAuthDBContext>(options =>
|
|
||||||
options.UseMySql(connectionString));
|
|
||||||
}
|
|
||||||
|
|
||||||
services.AddHttpClient();
|
services.AddHttpClient();
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using Infrastructure;
|
using System;
|
||||||
|
using Infrastructure;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
@ -52,15 +53,19 @@ namespace OpenAuth.Repository
|
|||||||
}
|
}
|
||||||
|
|
||||||
//如果没有租户id,或租户用的是默认的OpenAuthDBContext,则不做任何调整
|
//如果没有租户id,或租户用的是默认的OpenAuthDBContext,则不做任何调整
|
||||||
if (string.IsNullOrEmpty(tenantId) || tenantId == "OpenAuthDBContext")
|
if (string.IsNullOrEmpty(tenantId))
|
||||||
{
|
{
|
||||||
return;
|
tenantId = "OpenAuthDBContext";
|
||||||
}
|
}
|
||||||
|
|
||||||
string connect = _configuration.GetConnectionString(tenantId);
|
string connect = _configuration.GetConnectionString(tenantId);
|
||||||
if (string.IsNullOrEmpty(connect)) return;
|
if (string.IsNullOrEmpty(connect))
|
||||||
|
{
|
||||||
|
throw new Exception($"未能找到租户{tenantId}对应的连接字符串信息");
|
||||||
|
}
|
||||||
|
|
||||||
var dbType =_appConfiguration.Value.DbType;
|
//这个地方如果用IOption,在单元测试的时候会获取不到AppSetting的值😅
|
||||||
|
var dbType = _configuration.GetSection("AppSetting")["DbType"];
|
||||||
if (dbType == Define.DBTYPE_SQLSERVER)
|
if (dbType == Define.DBTYPE_SQLSERVER)
|
||||||
{
|
{
|
||||||
optionsBuilder.UseSqlServer(connect);
|
optionsBuilder.UseSqlServer(connect);
|
||||||
|
@ -139,20 +139,12 @@ namespace OpenAuth.WebApi
|
|||||||
// }
|
// }
|
||||||
// services.AddCors(option=>option.AddPolicy("cors", policy =>
|
// services.AddCors(option=>option.AddPolicy("cors", policy =>
|
||||||
// policy.AllowAnyHeader().AllowAnyMethod().AllowCredentials().WithOrigins(origins)));
|
// policy.AllowAnyHeader().AllowAnyMethod().AllowCredentials().WithOrigins(origins)));
|
||||||
|
|
||||||
//在startup里面只能通过这种方式获取到appsettings里面的值,不能用IOptions😰
|
//在startup里面只能通过这种方式获取到appsettings里面的值,不能用IOptions😰
|
||||||
var dbType = ((ConfigurationSection)Configuration.GetSection("AppSetting:DbType")).Value;
|
var dbType = ((ConfigurationSection)Configuration.GetSection("AppSetting:DbType")).Value;
|
||||||
var connectionString = Configuration.GetConnectionString("OpenAuthDBContext");
|
var connectionString = Configuration.GetConnectionString("OpenAuthDBContext");
|
||||||
logger.LogInformation($"当前数据库类型:{dbType},连接字符串:{connectionString}");
|
logger.LogInformation($"当前数据库类型:{dbType},连接字符串:{connectionString}");
|
||||||
if (dbType == Define.DBTYPE_SQLSERVER)
|
services.AddDbContext<OpenAuthDBContext>();
|
||||||
{
|
|
||||||
services.AddDbContext<OpenAuthDBContext>(options =>
|
|
||||||
options.UseSqlServer(connectionString));
|
|
||||||
}
|
|
||||||
else //mysql
|
|
||||||
{
|
|
||||||
services.AddDbContext<OpenAuthDBContext>(options =>
|
|
||||||
options.UseMySql(connectionString));
|
|
||||||
}
|
|
||||||
|
|
||||||
services.AddHttpClient();
|
services.AddHttpClient();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user