diff --git a/Api/Ewide.Test/AuthTest.cs b/Api/Ewide.Test/AuthTest.cs new file mode 100644 index 0000000..0b9de36 --- /dev/null +++ b/Api/Ewide.Test/AuthTest.cs @@ -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> + { + private readonly CustomWebApplicationFactory _factory; + private readonly HttpClient _client; + private readonly ITestOutputHelper _output; + public AuthTest(CustomWebApplicationFactory 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>(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); + } + } +} diff --git a/Api/Ewide.Test/CustomWebApplicationFactory.cs b/Api/Ewide.Test/CustomWebApplicationFactory.cs new file mode 100644 index 0000000..f71226b --- /dev/null +++ b/Api/Ewide.Test/CustomWebApplicationFactory.cs @@ -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 : WebApplicationFactory where TStartup : class + { + protected override void ConfigureWebHost(IWebHostBuilder builder) + { + builder.UseEnvironment("Testing"); + } + } +} diff --git a/Api/Ewide.Test/Ewide.Test.csproj b/Api/Ewide.Test/Ewide.Test.csproj new file mode 100644 index 0000000..7e1a428 --- /dev/null +++ b/Api/Ewide.Test/Ewide.Test.csproj @@ -0,0 +1,28 @@ + + + + net5.0 + + false + + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + diff --git a/Api/Ewide.sln b/Api/Ewide.sln index f167482..c48a622 100644 --- a/Api/Ewide.sln +++ b/Api/Ewide.sln @@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ewide.Database.Migrations", EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ewide.Web.Entry", "Ewide.Web.Entry\Ewide.Web.Entry.csproj", "{9826E365-EEE9-4721-A738-B02AB64D47E5}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ewide.Test", "Ewide.Test\Ewide.Test.csproj", "{DECE4796-6B13-4607-9C27-C1FE093D4DC8}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -45,6 +47,10 @@ Global {9826E365-EEE9-4721-A738-B02AB64D47E5}.Debug|Any CPU.Build.0 = Debug|Any CPU {9826E365-EEE9-4721-A738-B02AB64D47E5}.Release|Any CPU.ActiveCfg = Release|Any CPU {9826E365-EEE9-4721-A738-B02AB64D47E5}.Release|Any CPU.Build.0 = Release|Any CPU + {DECE4796-6B13-4607-9C27-C1FE093D4DC8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DECE4796-6B13-4607-9C27-C1FE093D4DC8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DECE4796-6B13-4607-9C27-C1FE093D4DC8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DECE4796-6B13-4607-9C27-C1FE093D4DC8}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE