update:添加测试内容

This commit is contained in:
2021-05-19 10:55:25 +08:00
parent 38a4f32fa2
commit 2a4c45128a

View File

@@ -7,10 +7,14 @@ using Xunit.Abstractions;
using Newtonsoft.Json; using Newtonsoft.Json;
using System.Threading.Tasks; using System.Threading.Tasks;
using Ewide.Core; using Ewide.Core;
using System.Net.Http.Headers;
using Ewide.Core.Service;
using System.Text;
using Ewide.Core.Util;
namespace Ewide.Test namespace Ewide.Test
{ {
public class AuthTest:IClassFixture<CustomWebApplicationFactory<Startup>> public class AuthTest : IClassFixture<CustomWebApplicationFactory<Startup>>
{ {
private readonly CustomWebApplicationFactory<Startup> _factory; private readonly CustomWebApplicationFactory<Startup> _factory;
private readonly HttpClient _client; private readonly HttpClient _client;
@@ -20,26 +24,66 @@ namespace Ewide.Test
_factory = factory; _factory = factory;
_client = _factory.CreateClient(new WebApplicationFactoryClientOptions _client = _factory.CreateClient(new WebApplicationFactoryClientOptions
{ {
AllowAutoRedirect = false AllowAutoRedirect = false,
}); ; });
_client.DefaultRequestHeaders.Add("User-Agent", "xUnit");
_output = output; _output = output;
} }
[Fact] [Fact]
public async Task SuperAdmin_Login() public async Task Get_NotNull_UserLoginInfo()
{ {
var loginUrl = "/login"; await GetAccessToken("superAdmin", "123456");
var res = await _client.PostAsync(loginUrl, new StringContent( var response = await _client.GetAsync("/getLoginUser");
JsonConvert.SerializeObject( _output.WriteLine($"login status code {response.StatusCode}");
new { var body = await response.Content.ReadAsStringAsync();
Account = "superAdmin",
Password = "123456" }
)
));
_output.WriteLine($"login status code {res.StatusCode}");
var body = await res.Content.ReadAsStringAsync();
_output.WriteLine($"body {body}"); _output.WriteLine($"body {body}");
Assert.True(JsonConvert.DeserializeObject<XnRestfulResult<object>>(body).Success) ; var result = JsonConvert.DeserializeObject<XnRestfulResult<LoginOutput>>(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<XnRestfulResult<string>>(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<XnRestfulResult<string>>(body);
if (!result.Success)
{
throw new ArgumentException("»ñÈ¡AccessTokenʧ°Ü");
}
SetToken(result.Data);
} }
} }
} }