test: 增加 Mobile 认证单元测试
This commit is contained in:
parent
dbaced9477
commit
6434cea61c
|
@ -2,7 +2,6 @@
|
||||||
using Bootstrap.DataAccess;
|
using Bootstrap.DataAccess;
|
||||||
using Longbow.GiteeAuth;
|
using Longbow.GiteeAuth;
|
||||||
using Longbow.GitHubAuth;
|
using Longbow.GitHubAuth;
|
||||||
using Longbow.Web;
|
|
||||||
using Longbow.WeChatAuth;
|
using Longbow.WeChatAuth;
|
||||||
using Microsoft.AspNetCore.Authentication;
|
using Microsoft.AspNetCore.Authentication;
|
||||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||||
|
|
|
@ -52,11 +52,7 @@ namespace Bootstrap.Admin.Controllers.Api
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[AllowAnonymous]
|
[AllowAnonymous]
|
||||||
[HttpPut]
|
[HttpPut]
|
||||||
public async Task<bool> Put([FromServices]ISMSProvider provider, [FromQuery]string phone)
|
public async Task<bool> Put([FromServices]ISMSProvider provider, [FromQuery]string phone) => string.IsNullOrEmpty(phone) ? false : await provider.SendCodeAsync(phone);
|
||||||
{
|
|
||||||
if (string.IsNullOrEmpty(phone)) return false;
|
|
||||||
return await provider.SendCodeAsync(phone);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 跨域握手协议
|
/// 跨域握手协议
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
using Microsoft.AspNetCore.WebUtilities;
|
using Microsoft.AspNetCore.WebUtilities;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.Options;
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
@ -241,7 +240,7 @@ namespace Microsoft.Extensions.DependencyInjection
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获得/设置 默认授权 App
|
/// 获得/设置 默认授权 App
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string App { get; set; }
|
public string App { get; set; } = "0";
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获得/设置 短信下发网关地址
|
/// 获得/设置 短信下发网关地址
|
||||||
|
|
|
@ -10,6 +10,8 @@ using System.Net;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using UnitTest;
|
using UnitTest;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Bootstrap.Admin
|
namespace Bootstrap.Admin
|
||||||
{
|
{
|
||||||
|
@ -119,6 +121,39 @@ namespace Bootstrap.Admin
|
||||||
builder.ConfigureAppConfiguration(app => app.AddJsonFile(TestHelper.RetrievePath($"UnitTest{Path.DirectorySeparatorChar}appsettings.appveyor.json"), false, true));
|
builder.ConfigureAppConfiguration(app => app.AddJsonFile(TestHelper.RetrievePath($"UnitTest{Path.DirectorySeparatorChar}appsettings.appveyor.json"), false, true));
|
||||||
}
|
}
|
||||||
TestHelper.ConfigureWebHost(builder);
|
TestHelper.ConfigureWebHost(builder);
|
||||||
|
|
||||||
|
// 替换 SMS 服务
|
||||||
|
builder.ConfigureServices(services =>
|
||||||
|
{
|
||||||
|
services.AddTransient<ISMSProvider, DefaultSMSProvider>();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 手机号登陆帮助类
|
||||||
|
/// </summary>
|
||||||
|
class DefaultSMSProvider : ISMSProvider
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 获得 短信配置信息
|
||||||
|
/// </summary>
|
||||||
|
public SMSOptions Option { get; protected set; } = new SMSOptions();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 下发验证码方法
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="phoneNumber"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public Task<bool> SendCodeAsync(string phoneNumber) => Task.FromResult(true);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 验证验证码方法
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="phone">手机号</param>
|
||||||
|
/// <param name="code">验证码</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public bool Validate(string phone, string code) => code == "1234";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,10 +39,9 @@ namespace Bootstrap.Admin.Api.SqlServer
|
||||||
var _token = await resq.Content.ReadAsStringAsync();
|
var _token = await resq.Content.ReadAsStringAsync();
|
||||||
Assert.Equal("false", _token);
|
Assert.Equal("false", _token);
|
||||||
|
|
||||||
// UNDONE: 重构短信登陆后完善
|
resq = await Client.PutAsync("?phone=18910001000", new StringContent(""));
|
||||||
//resq = await Client.PutAsync("?phone=", new StringContent(""));
|
_token = await resq.Content.ReadAsStringAsync();
|
||||||
//_token = await resq.Content.ReadAsStringAsync();
|
Assert.Equal("true", _token);
|
||||||
//Assert.Equal("true", _token);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|
|
@ -1,10 +1,8 @@
|
||||||
using Bootstrap.DataAccess;
|
using Bootstrap.DataAccess;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Concurrent;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Reflection;
|
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace Bootstrap.Admin.Controllers.SqlServer
|
namespace Bootstrap.Admin.Controllers.SqlServer
|
||||||
|
@ -123,22 +121,54 @@ namespace Bootstrap.Admin.Controllers.SqlServer
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Mobile_Ok()
|
public async void Mobile_Ok()
|
||||||
{
|
{
|
||||||
// UNDONE: Mobile 单元测试未完成
|
using (var db = DbManager.Create()) db.Execute("delete from Users where UserName = @0", "18910001000");
|
||||||
|
var client = Host.CreateClient();
|
||||||
|
var 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);
|
||||||
|
|
||||||
// 反射设置 SMSHelper 内部验证码保证 Validate 方法返回真
|
var content = new MultipartFormDataContent
|
||||||
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);
|
{ new StringContent("18910001000"), "phone" },
|
||||||
//_pool.AddOrUpdate(option.Phone, key => new AutoExpireValidateCode(option.Phone, result.Data, option.Expires), (key, v) => v.Reset(result.Data));
|
{ new StringContent("1234"), "code" },
|
||||||
//var addMethod = _poolInstance.GetType().GetMethod("AddOrUpdate");
|
{ new StringContent(antiToken), "__RequestVerificationToken" }
|
||||||
//addMethod.Invoke(_poolInstance, new object[] { "18910001000", validateCodeInstance, null });
|
};
|
||||||
|
var m = await client.PostAsync("/Account/Mobile", content);
|
||||||
|
Assert.True(m.IsSuccessStatusCode);
|
||||||
|
var payload = await r.Content.ReadAsStringAsync();
|
||||||
|
Assert.Contains("登 录", payload);
|
||||||
|
}
|
||||||
|
|
||||||
//var client = Host.CreateClient();
|
[Fact]
|
||||||
//var r = await client.GetAsync($"/Account/Mobile?phone=18910001000&code=1234");
|
public async void Mobile_Fail()
|
||||||
//Assert.True(r.IsSuccessStatusCode);
|
{
|
||||||
//var content = await r.Content.ReadAsStringAsync();
|
using (var db = DbManager.Create()) db.Execute("delete from Users where UserName = @0", "18910001000");
|
||||||
//Assert.Contains("登 录", content);
|
|
||||||
|
var client = Host.CreateClient();
|
||||||
|
var 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 content = new MultipartFormDataContent
|
||||||
|
{
|
||||||
|
{ new StringContent("18910001000"), "phone" },
|
||||||
|
{ new StringContent("1000"), "code" },
|
||||||
|
{ new StringContent(antiToken), "__RequestVerificationToken" }
|
||||||
|
};
|
||||||
|
var m = await client.PostAsync("/Account/Mobile?AppId=0", content);
|
||||||
|
Assert.True(m.IsSuccessStatusCode);
|
||||||
|
var payload = await r.Content.ReadAsStringAsync();
|
||||||
|
Assert.Contains("登 录", payload);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue