为什么都没了

This commit is contained in:
路 范
2021-09-24 12:59:34 +08:00
parent a975b3bdee
commit a66921f0f4
962 changed files with 252792 additions and 0 deletions

View File

@@ -0,0 +1,89 @@
using Ewide.Web.Entry;
using Microsoft.AspNetCore.Mvc.Testing;
using System;
using System.Net.Http;
using Xunit;
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<CustomWebApplicationFactory<Startup>>
{
private readonly CustomWebApplicationFactory<Startup> _factory;
private readonly HttpClient _client;
private readonly ITestOutputHelper _output;
public AuthTest(CustomWebApplicationFactory<Startup> factory, ITestOutputHelper output)
{
_factory = factory;
_client = _factory.CreateClient(new WebApplicationFactoryClientOptions
{
AllowAutoRedirect = false,
});
_client.DefaultRequestHeaders.Add("User-Agent", "xUnit");
_output = output;
}
[Fact]
public async Task Get_NotNull_UserLoginInfo()
{
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}");
var result = JsonConvert.DeserializeObject<RestfulResult<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<RestfulResult<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<RestfulResult<string>>(body);
if (!result.Success)
{
throw new ArgumentException("»ñÈ¡AccessTokenʧ°Ü");
}
SetToken(result.Data);
}
}
}

View File

@@ -0,0 +1,18 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ewide.Test
{
public class CustomWebApplicationFactory<TStartup> : WebApplicationFactory<TStartup> where TStartup : class
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.UseEnvironment("Testing");
}
}
}

View File

@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="5.0.6" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="1.3.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Ewide.Web.Entry\Ewide.Web.Entry.csproj" />
</ItemGroup>
</Project>