test: 增加 Account 控制器单元测试

# Conflicts:
#	src/admin/Bootstrap.Admin/Controllers/Api/LoginController.cs
This commit is contained in:
Argo Zhang 2019-10-07 23:56:57 +08:00
parent f271b2402c
commit 9d2208fc65
No known key found for this signature in database
GPG Key ID: 152E398953DDF19F
9 changed files with 107 additions and 16 deletions

View File

@ -158,22 +158,6 @@ namespace Bootstrap.Admin.Controllers
return Redirect(originUrl);
}
/// <summary>
/// 创建登录用户信息
/// </summary>
/// <param name="ipLocator"></param>
/// <param name="context"></param>
/// <param name="loginUser"></param>
internal static void CreateLoginUser(IIPLocatorProvider ipLocator, HttpContext context, LoginUser loginUser)
{
loginUser.UserAgent = context.Request.Headers["User-Agent"];
var agent = new UserAgent(loginUser.UserAgent);
loginUser.Ip = context.Connection.RemoteIpAddress.ToIPv4String();
loginUser.City = ipLocator.Locate(loginUser.Ip);
loginUser.Browser = $"{agent.Browser?.Name} {agent.Browser?.Version}";
loginUser.OS = $"{agent.OS?.Name} {agent.OS?.Version}";
}
/// <summary>
/// Logout this instance.
/// </summary>

View File

@ -30,6 +30,7 @@ namespace Bootstrap.Admin.Query
/// <returns></returns>
public QueryData<DBLog> RetrieveData()
{
if (string.IsNullOrEmpty(Order)) Order = "LogTime";
var data = LogHelper.RetrieveDBLogs(this, OperateTimeStart, OperateTimeEnd, UserName);
var ret = new QueryData<DBLog>();
ret.total = data.TotalItems;

View File

@ -0,0 +1,10 @@
using Xunit;
namespace Bootstrap.Admin.Api.MySql
{
[Collection("MySqlContext")]
public class SQLTest : SqlServer.SQLTest
{
public SQLTest(SQLiteBAWebHost factory) : base(factory) { }
}
}

View File

@ -32,6 +32,19 @@ namespace Bootstrap.Admin.Api.SqlServer
Assert.Equal("", _token);
}
[Fact]
public async void Put_Ok()
{
var resq = await Client.PutAsync("?phone=", new StringContent(""));
var _token = await resq.Content.ReadAsStringAsync();
Assert.Equal("false", _token);
// UNDONE: 重构短信登陆后完善
//resq = await Client.PutAsync("?phone=", new StringContent(""));
//_token = await resq.Content.ReadAsStringAsync();
//Assert.Equal("true", _token);
}
[Fact]
public async void Option_Ok()
{

View File

@ -0,0 +1,18 @@
using Bootstrap.DataAccess;
using Longbow.Web.Mvc;
using Xunit;
namespace Bootstrap.Admin.Api.SqlServer
{
public class SQLTest : ControllerTest
{
public SQLTest(BAWebHost factory) : base(factory, "api/SQL") { }
[Fact]
public async void Get_Ok()
{
var resp = await Client.GetAsJsonAsync<QueryData<DBLog>>("?userName=Admin&OperateTimeStart=&OperateTimeEnd=");
Assert.NotNull(resp);
}
}
}

View File

@ -0,0 +1,10 @@
using Xunit;
namespace Bootstrap.Admin.Api.SQLite
{
[Collection("SQLiteContext")]
public class SQLTest : SqlServer.SQLTest
{
public SQLTest(SQLiteBAWebHost factory) : base(factory) { }
}
}

View File

@ -1,7 +1,10 @@
using Bootstrap.DataAccess;
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using Xunit;
namespace Bootstrap.Admin.Controllers.SqlServer
@ -105,5 +108,37 @@ namespace Bootstrap.Admin.Controllers.SqlServer
};
await Client.PostAsync("Lock", data);
}
[Theory]
[InlineData("Gitee")]
[InlineData("GitHub")]
[InlineData("WeChat")]
public async void OAuth_Ok(string url)
{
var client = Host.CreateClient();
var r = await client.GetAsync($"/Account/{url}");
Assert.True(r.IsSuccessStatusCode);
var content = await r.Content.ReadAsStringAsync();
Assert.Contains("登 录", content);
}
[Fact]
public void Mobile_Ok()
{
// UNDONE: Mobile 单元测试未完成
// 反射设置 SMSHelper 内部验证码保证 Validate 方法返回真
var validateCodeInstance = Activator.CreateInstance(Type.GetType("Bootstrap.DataAccess.SMSHelper+AutoExpireValidateCode, Bootstrap.DataAccess"), new object[] { "18910001000", "1234", TimeSpan.FromSeconds(10)});
var _poolInstance = typeof(SMSHelper).GetField("_pool", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
//_pool.AddOrUpdate(option.Phone, key => new AutoExpireValidateCode(option.Phone, result.Data, option.Expires), (key, v) => v.Reset(result.Data));
//var addMethod = _poolInstance.GetType().GetMethod("AddOrUpdate");
//addMethod.Invoke(_poolInstance, new object[] { "18910001000", validateCodeInstance, null });
//var client = Host.CreateClient();
//var r = await client.GetAsync($"/Account/Mobile?phone=18910001000&code=1234");
//Assert.True(r.IsSuccessStatusCode);
//var content = await r.Content.ReadAsStringAsync();
//Assert.Contains("登 录", content);
}
}
}

View File

@ -29,6 +29,7 @@ namespace Bootstrap.Admin.Controllers.SqlServer
[InlineData("Tasks", "任务管理")]
[InlineData("Mobile", "客户端测试")]
[InlineData("Analyse", "网站分析")]
[InlineData("SQL", "SQL日志")]
public async void View_Ok(string view, string text)
{
var r = await Client.GetAsync(view);

View File

@ -49,6 +49,25 @@
}
}
],
"GiteeHealthChecks": true,
"GiteeOptions": {
"Enabled": false
},
"GitHubOptions": {
"Enabled": false
},
"WeChatOptions": {
"Enabled": false
},
"SMSOptions": {
"CompanyCode": "<CompanyCode>",
"MD5Key": "MD5Key",
"Roles": [
"Administrators"
],
"HomePath": "/Admin/Profiles",
"App": "0"
},
"LongbowCache": {
"Enabled": false
}