From 2a4c45128ab0d5ffe48a6b093469f1c0bc513a59 Mon Sep 17 00:00:00 2001 From: zhangqi <2794379662@qq.com> Date: Wed, 19 May 2021 10:55:25 +0800 Subject: [PATCH] =?UTF-8?q?update:=E6=B7=BB=E5=8A=A0=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Api/Ewide.Test/AuthTest.cs | 76 ++++++++++++++++++++++++++++++-------- 1 file changed, 60 insertions(+), 16 deletions(-) diff --git a/Api/Ewide.Test/AuthTest.cs b/Api/Ewide.Test/AuthTest.cs index 6236d82..0b9de36 100644 --- a/Api/Ewide.Test/AuthTest.cs +++ b/Api/Ewide.Test/AuthTest.cs @@ -7,10 +7,14 @@ using Xunit.Abstractions; using Newtonsoft.Json; using System.Threading.Tasks; using Ewide.Core; +using System.Net.Http.Headers; +using Ewide.Core.Service; +using System.Text; +using Ewide.Core.Util; namespace Ewide.Test { - public class AuthTest:IClassFixture> + public class AuthTest : IClassFixture> { private readonly CustomWebApplicationFactory _factory; private readonly HttpClient _client; @@ -20,26 +24,66 @@ namespace Ewide.Test _factory = factory; _client = _factory.CreateClient(new WebApplicationFactoryClientOptions { - AllowAutoRedirect = false - }); ; + AllowAutoRedirect = false, + }); + _client.DefaultRequestHeaders.Add("User-Agent", "xUnit"); _output = output; } - [Fact] - public async Task SuperAdmin_Login() + public async Task Get_NotNull_UserLoginInfo() { - var loginUrl = "/login"; - var res = await _client.PostAsync(loginUrl, new StringContent( - JsonConvert.SerializeObject( - new { - Account = "superAdmin", - Password = "123456" } - ) - )); - _output.WriteLine($"login status code {res.StatusCode}"); - var body = await res.Content.ReadAsStringAsync(); + await GetAccessToken("superAdmin", "123456"); + var response = await _client.GetAsync("/getLoginUser"); + _output.WriteLine($"login status code {response.StatusCode}"); + var body = await response.Content.ReadAsStringAsync(); _output.WriteLine($"body {body}"); - Assert.True(JsonConvert.DeserializeObject>(body).Success) ; + var result = JsonConvert.DeserializeObject>(body); + Assert.NotNull(result.Data); + } + [Theory] + [InlineData("superAdmin","123456",200)] + [InlineData("superAdmin","12345",500)] + public async Task Get_RightCode_LoginResult(string account,string password,int code) + { + var response = await _client.PostAsync("/login", new StringContent( + JsonConvert.SerializeObject( + new LoginInput + { + Account = account, + Password = RSAHandler.RSAEncrypt(password) + } + ) + , Encoding.UTF8, "application/json")); + var body = await response.Content.ReadAsStringAsync(); + _output.WriteLine($"body {body}"); + var result = JsonConvert.DeserializeObject>(body); + Assert.Equal(code, result.Code); + } + + + private void SetToken(string token) + { + _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); + } + private async Task GetAccessToken(string account,string password) + { + var response = await _client.PostAsync("/login", new StringContent( + JsonConvert.SerializeObject( + new LoginInput + { + Account = account, + Password = RSAHandler.RSAEncrypt(password) + } + ) + , Encoding.UTF8, "application/json")); + var body = await response.Content.ReadAsStringAsync(); + _output.WriteLine($"body {body}"); + var result = JsonConvert.DeserializeObject>(body); + if (!result.Success) + { + throw new ArgumentException("»ñÈ¡AccessTokenʧ°Ü"); + } + SetToken(result.Data); } } }