test: 增加 Mobile 认证单元测试
This commit is contained in:
parent
dbaced9477
commit
6434cea61c
|
@ -2,7 +2,6 @@
|
|||
using Bootstrap.DataAccess;
|
||||
using Longbow.GiteeAuth;
|
||||
using Longbow.GitHubAuth;
|
||||
using Longbow.Web;
|
||||
using Longbow.WeChatAuth;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||
|
|
|
@ -52,11 +52,7 @@ namespace Bootstrap.Admin.Controllers.Api
|
|||
/// <returns></returns>
|
||||
[AllowAnonymous]
|
||||
[HttpPut]
|
||||
public async Task<bool> Put([FromServices]ISMSProvider provider, [FromQuery]string phone)
|
||||
{
|
||||
if (string.IsNullOrEmpty(phone)) return false;
|
||||
return await provider.SendCodeAsync(phone);
|
||||
}
|
||||
public async Task<bool> Put([FromServices]ISMSProvider provider, [FromQuery]string phone) => string.IsNullOrEmpty(phone) ? false : await provider.SendCodeAsync(phone);
|
||||
|
||||
/// <summary>
|
||||
/// 跨域握手协议
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
using Microsoft.AspNetCore.WebUtilities;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
|
@ -241,7 +240,7 @@ namespace Microsoft.Extensions.DependencyInjection
|
|||
/// <summary>
|
||||
/// 获得/设置 默认授权 App
|
||||
/// </summary>
|
||||
public string App { get; set; }
|
||||
public string App { get; set; } = "0";
|
||||
|
||||
/// <summary>
|
||||
/// 获得/设置 短信下发网关地址
|
||||
|
|
|
@ -10,6 +10,8 @@ using System.Net;
|
|||
using System.Net.Http;
|
||||
using UnitTest;
|
||||
using Xunit;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
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));
|
||||
}
|
||||
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();
|
||||
Assert.Equal("false", _token);
|
||||
|
||||
// UNDONE: 重构短信登陆后完善
|
||||
//resq = await Client.PutAsync("?phone=", new StringContent(""));
|
||||
//_token = await resq.Content.ReadAsStringAsync();
|
||||
//Assert.Equal("true", _token);
|
||||
resq = await Client.PutAsync("?phone=18910001000", new StringContent(""));
|
||||
_token = await resq.Content.ReadAsStringAsync();
|
||||
Assert.Equal("true", _token);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
|
@ -1,10 +1,8 @@
|
|||
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
|
||||
|
@ -123,22 +121,54 @@ namespace Bootstrap.Admin.Controllers.SqlServer
|
|||
}
|
||||
|
||||
[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 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 content = new MultipartFormDataContent
|
||||
{
|
||||
{ new StringContent("18910001000"), "phone" },
|
||||
{ new StringContent("1234"), "code" },
|
||||
{ new StringContent(antiToken), "__RequestVerificationToken" }
|
||||
};
|
||||
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();
|
||||
//var r = await client.GetAsync($"/Account/Mobile?phone=18910001000&code=1234");
|
||||
//Assert.True(r.IsSuccessStatusCode);
|
||||
//var content = await r.Content.ReadAsStringAsync();
|
||||
//Assert.Contains("登 录", content);
|
||||
[Fact]
|
||||
public async void Mobile_Fail()
|
||||
{
|
||||
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);
|
||||
|
||||
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