From 6434cea61c8b3804412f5657dc6e387014503ea1 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Tue, 8 Oct 2019 19:38:17 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E5=A2=9E=E5=8A=A0=20Mobile=20=E8=AE=A4?= =?UTF-8?q?=E8=AF=81=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/AccountController.cs | 1 - .../Controllers/Api/LoginController.cs | 6 +- .../Extensions/SMSExtensions.cs | 3 +- test/UnitTest/BAWebHost.cs | 35 +++++++++++ .../Api/SQLServer/LoginTest.cs | 7 +-- .../Controllers/SQLServer/AccountTest.cs | 60 ++++++++++++++----- 6 files changed, 85 insertions(+), 27 deletions(-) diff --git a/src/admin/Bootstrap.Admin/Controllers/AccountController.cs b/src/admin/Bootstrap.Admin/Controllers/AccountController.cs index badd20d0..4e35fd22 100644 --- a/src/admin/Bootstrap.Admin/Controllers/AccountController.cs +++ b/src/admin/Bootstrap.Admin/Controllers/AccountController.cs @@ -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; diff --git a/src/admin/Bootstrap.Admin/Controllers/Api/LoginController.cs b/src/admin/Bootstrap.Admin/Controllers/Api/LoginController.cs index 4976e1bd..4be219ce 100644 --- a/src/admin/Bootstrap.Admin/Controllers/Api/LoginController.cs +++ b/src/admin/Bootstrap.Admin/Controllers/Api/LoginController.cs @@ -52,11 +52,7 @@ namespace Bootstrap.Admin.Controllers.Api /// [AllowAnonymous] [HttpPut] - public async Task Put([FromServices]ISMSProvider provider, [FromQuery]string phone) - { - if (string.IsNullOrEmpty(phone)) return false; - return await provider.SendCodeAsync(phone); - } + public async Task Put([FromServices]ISMSProvider provider, [FromQuery]string phone) => string.IsNullOrEmpty(phone) ? false : await provider.SendCodeAsync(phone); /// /// 跨域握手协议 diff --git a/src/admin/Bootstrap.Admin/Extensions/SMSExtensions.cs b/src/admin/Bootstrap.Admin/Extensions/SMSExtensions.cs index 33b31aac..331dc57a 100644 --- a/src/admin/Bootstrap.Admin/Extensions/SMSExtensions.cs +++ b/src/admin/Bootstrap.Admin/Extensions/SMSExtensions.cs @@ -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 /// /// 获得/设置 默认授权 App /// - public string App { get; set; } + public string App { get; set; } = "0"; /// /// 获得/设置 短信下发网关地址 diff --git a/test/UnitTest/BAWebHost.cs b/test/UnitTest/BAWebHost.cs index fafbc137..f8aa41c7 100644 --- a/test/UnitTest/BAWebHost.cs +++ b/test/UnitTest/BAWebHost.cs @@ -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(); + }); + } + + + /// + /// 手机号登陆帮助类 + /// + class DefaultSMSProvider : ISMSProvider + { + /// + /// 获得 短信配置信息 + /// + public SMSOptions Option { get; protected set; } = new SMSOptions(); + + /// + /// 下发验证码方法 + /// + /// + /// + public Task SendCodeAsync(string phoneNumber) => Task.FromResult(true); + + /// + /// 验证验证码方法 + /// + /// 手机号 + /// 验证码 + /// + public bool Validate(string phone, string code) => code == "1234"; } } } diff --git a/test/UnitTest/Bootstrap.Admin/Api/SQLServer/LoginTest.cs b/test/UnitTest/Bootstrap.Admin/Api/SQLServer/LoginTest.cs index df7d7e2d..e8b6dbd8 100644 --- a/test/UnitTest/Bootstrap.Admin/Api/SQLServer/LoginTest.cs +++ b/test/UnitTest/Bootstrap.Admin/Api/SQLServer/LoginTest.cs @@ -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] diff --git a/test/UnitTest/Bootstrap.Admin/Controllers/SQLServer/AccountTest.cs b/test/UnitTest/Bootstrap.Admin/Controllers/SQLServer/AccountTest.cs index 1ebe11f9..e682c273 100644 --- a/test/UnitTest/Bootstrap.Admin/Controllers/SQLServer/AccountTest.cs +++ b/test/UnitTest/Bootstrap.Admin/Controllers/SQLServer/AccountTest.cs @@ -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 = ""); + 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 = ""); + 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); } } }