diff --git a/OpenAuth.App/OpenAuth.App.csproj b/OpenAuth.App/OpenAuth.App.csproj
index 91ef110f..8b7a37d4 100644
--- a/OpenAuth.App/OpenAuth.App.csproj
+++ b/OpenAuth.App/OpenAuth.App.csproj
@@ -22,6 +22,7 @@
+
diff --git a/OpenAuth.App/OrgManagerApp.cs b/OpenAuth.App/OrgManagerApp.cs
index a55d20ad..aee88341 100644
--- a/OpenAuth.App/OrgManagerApp.cs
+++ b/OpenAuth.App/OrgManagerApp.cs
@@ -33,8 +33,7 @@ namespace OpenAuth.App
UnitWork.Save();
//如果当前账号不是SYSTEM,则直接分配
- var loginUser = _auth.GetCurrentUser();
- if (loginUser.User.Account != Define.SYSTEM_USERNAME)
+ if (loginContext.User.Account != Define.SYSTEM_USERNAME)
{
_revelanceApp.Assign(new AssignReq
{
diff --git a/OpenAuth.App/Test/TestBase.cs b/OpenAuth.App/Test/TestBase.cs
index d36f7b26..077f66de 100644
--- a/OpenAuth.App/Test/TestBase.cs
+++ b/OpenAuth.App/Test/TestBase.cs
@@ -1,15 +1,14 @@
-using Autofac.Extensions.DependencyInjection;
+using System;
+using System.IO;
+using Autofac.Extensions.DependencyInjection;
using Infrastructure;
-using Infrastructure.Cache;
using Infrastructure.Extensions.AutofacManager;
using Microsoft.AspNetCore.Http;
-using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
-using Microsoft.Extensions.Options;
+using Microsoft.Extensions.Logging;
using Moq;
using NUnit.Framework;
-using OpenAuth.App.SSO;
using OpenAuth.Repository;
namespace OpenAuth.App.Test
@@ -24,31 +23,36 @@ namespace OpenAuth.App.Test
var serviceCollection = GetService();
serviceCollection.AddMemoryCache();
serviceCollection.AddOptions();
- serviceCollection.AddLogging();
-
- //模拟配置文件
- var optionMock = new Mock>();
- optionMock.Setup(x => x.Value).Returns(new AppSetting { DbType = Define.DBTYPE_MYSQL});
- serviceCollection.AddScoped(x => optionMock.Object);
+ //读取OpenAuth.WebApi的配置文件用于单元测试
+ var path = AppContext.BaseDirectory;
+ int pos = path.IndexOf("OpenAuth.App");
+ var basepath = Path.Combine(path.Substring(0,pos) ,"OpenAuth.WebApi");
+ IConfiguration config = new ConfigurationBuilder()
+ .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
- var configMock = new Mock();
- configMock.Setup(x => x.GetSection("ConnectionStrings")[Define.TENANT_ID]).Returns("");
- serviceCollection.AddScoped(x => configMock.Object);
+ //添加log4net
+ serviceCollection.AddLogging(builder =>
+ {
+ builder.ClearProviders(); //去掉默认的日志
+ builder.AddConfiguration(config.GetSection("Logging")); //读取配置文件中的Logging配置
+ builder.AddLog4Net();
+ });
+ //注入OpenAuth.WebApi配置文件
+ serviceCollection.AddScoped(x => config);
+ //模拟HTTP请求
var httpContextAccessorMock = new Mock();
httpContextAccessorMock.Setup(x => x.HttpContext.Request.Query[Define.TOKEN_NAME]).Returns("tokentest");
httpContextAccessorMock.Setup(x => x.HttpContext.Request.Query[Define.TENANT_ID]).Returns("OpenAuthDBContext");
-
serviceCollection.AddScoped(x => httpContextAccessorMock.Object);
-
- // 测试my sql
- serviceCollection.AddDbContext(options =>
- options.UseMySql("server=127.0.0.1;user id=root;database=openauthdb;password=000000"));
-
-// serviceCollection.AddDbContext(options =>
-// options.UseSqlServer("Data Source=.;Initial Catalog=OpenAuthDB;User=sa;Password=000000;Integrated Security=True"));
-
+
+ serviceCollection.AddDbContext();
+
var container = AutofacExt.InitForTest(serviceCollection);
_autofacServiceProvider = new AutofacServiceProvider(container);
AutofacContainerModule.ConfigServiceProvider(_autofacServiceProvider);
diff --git a/OpenAuth.Mvc/Startup.cs b/OpenAuth.Mvc/Startup.cs
index 4a6a20f6..b4ba0d14 100644
--- a/OpenAuth.Mvc/Startup.cs
+++ b/OpenAuth.Mvc/Startup.cs
@@ -92,16 +92,7 @@ namespace OpenAuth.Mvc
var connectionString = Configuration.GetConnectionString("OpenAuthDBContext");
logger.LogInformation($"当前数据库类型:{dbType},连接字符串:{connectionString}");
- if (dbType == Define.DBTYPE_SQLSERVER)
- {
- services.AddDbContext(options =>
- options.UseSqlServer(connectionString));
- }
- else //mysql
- {
- services.AddDbContext(options =>
- options.UseMySql(connectionString));
- }
+ services.AddDbContext();
services.AddHttpClient();
diff --git a/OpenAuth.Repository/OpenAuthDBContext.cs b/OpenAuth.Repository/OpenAuthDBContext.cs
index 28a589a3..e7969b30 100644
--- a/OpenAuth.Repository/OpenAuthDBContext.cs
+++ b/OpenAuth.Repository/OpenAuthDBContext.cs
@@ -1,4 +1,5 @@
-using Infrastructure;
+using System;
+using Infrastructure;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
@@ -52,15 +53,19 @@ namespace OpenAuth.Repository
}
//如果没有租户id,或租户用的是默认的OpenAuthDBContext,则不做任何调整
- if (string.IsNullOrEmpty(tenantId) || tenantId == "OpenAuthDBContext")
+ if (string.IsNullOrEmpty(tenantId))
{
- return;
+ tenantId = "OpenAuthDBContext";
}
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)
{
optionsBuilder.UseSqlServer(connect);
diff --git a/OpenAuth.WebApi/Startup.cs b/OpenAuth.WebApi/Startup.cs
index 23f0c3fd..7db5c6c9 100644
--- a/OpenAuth.WebApi/Startup.cs
+++ b/OpenAuth.WebApi/Startup.cs
@@ -139,20 +139,12 @@ namespace OpenAuth.WebApi
// }
// services.AddCors(option=>option.AddPolicy("cors", policy =>
// policy.AllowAnyHeader().AllowAnyMethod().AllowCredentials().WithOrigins(origins)));
+
//在startup里面只能通过这种方式获取到appsettings里面的值,不能用IOptions😰
var dbType = ((ConfigurationSection)Configuration.GetSection("AppSetting:DbType")).Value;
var connectionString = Configuration.GetConnectionString("OpenAuthDBContext");
logger.LogInformation($"当前数据库类型:{dbType},连接字符串:{connectionString}");
- if (dbType == Define.DBTYPE_SQLSERVER)
- {
- services.AddDbContext(options =>
- options.UseSqlServer(connectionString));
- }
- else //mysql
- {
- services.AddDbContext(options =>
- options.UseMySql(connectionString));
- }
+ services.AddDbContext();
services.AddHttpClient();