BootstrapAdmin/test/UnitTest/Bootstrap.Admin/Controllers/SQLServer/AccountTest.cs

145 lines
5.6 KiB
C#
Raw Normal View History

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
{
public class AccountTest : ControllerTest
{
public AccountTest(BAWebHost factory) : base(factory, "Account") { }
[Fact]
public async void SystemMode_Test()
{
var dict = DictHelper.RetrieveDicts().FirstOrDefault(d => d.Category == "系统设置" && d.Name == "演示系统");
dict.Code = "1";
DictHelper.Save(dict);
var r = await Client.GetAsync("Login");
2019-06-01 16:57:33 +08:00
// 恢复保护模式
var db = DbManager.Create();
db.Execute("Update Dicts Set Code = @0 Where Id = @1", "0", dict.Id);
Assert.Equal(HttpStatusCode.OK, r.StatusCode);
2019-06-01 16:57:33 +08:00
var source = await r.Content.ReadAsStringAsync();
Assert.Contains("演示系统", source);
}
[Fact]
public async void Login_Fail()
{
var client = Host.CreateClient();
var r = await client.GetAsync("/Account/Login");
Assert.True(r.IsSuccessStatusCode);
var content = await r.Content.ReadAsStringAsync();
Assert.Contains("登 录", content);
r = await client.GetAsync("/Account/Login");
var view = await r.Content.ReadAsStringAsync();
var tokenTag = "<input name=\"__RequestVerificationToken\" type=\"hidden\" value=\"";
var index = view.IndexOf(tokenTag);
view = view.Substring(index + tokenTag.Length);
index = view.IndexOf("\" /></form>");
var antiToken = view.Substring(0, index);
var loginContent = new MultipartFormDataContent
{
{ new StringContent("Admin"), "userName" },
{ new StringContent("1"), "password" },
{ new StringContent("true"), "remember" },
{ new StringContent(antiToken), "__RequestVerificationToken" }
};
var req = await client.PostAsync("/Account/Login", loginContent);
Assert.Equal(HttpStatusCode.OK, req.StatusCode);
}
[Fact]
public async void Login_Admin()
{
var resp = await Client.GetAsync("Login");
var context = await resp.Content.ReadAsStringAsync();
Assert.Contains("注销", context);
}
[Fact]
public async void Logout_Ok()
{
var client = Host.CreateClient();
var r = await client.GetAsync("/Account/Logout");
Assert.True(r.IsSuccessStatusCode);
var content = await r.Content.ReadAsStringAsync();
Assert.Contains("登 录", content);
}
[Fact]
public async void AccessDenied_Ok()
{
// logout
var r = await Client.GetAsync("AccessDenied");
Assert.True(r.IsSuccessStatusCode);
var content = await r.Content.ReadAsStringAsync();
Assert.Contains("服务器拒绝处理您的请求", content);
}
[Fact]
public async void Lock_Ok()
{
var r = await Client.GetAsync("Lock");
Assert.True(r.IsSuccessStatusCode);
var content = await r.Content.ReadAsStringAsync();
Assert.Contains("系统锁屏", content);
2019-08-21 15:37:44 +08:00
// 第二次调用 跳转到登录页面
r = await Client.GetAsync("Lock");
Assert.True(r.IsSuccessStatusCode);
content = await r.Content.ReadAsStringAsync();
Assert.Contains("登 录", content);
// 调用Post
var data = new MultipartFormDataContent
{
{ new StringContent("Admin"), "userName" },
{ new StringContent("123789"), "password" }
};
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);
}
}
}