diff --git a/BootstrapAdmin.sln b/BootstrapAdmin.sln index 24020a88..e1c23122 100644 --- a/BootstrapAdmin.sln +++ b/BootstrapAdmin.sln @@ -54,7 +54,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "MySQL", "MySQL", "{084E2E94 DatabaseScripts\MySQL\install.sql = DatabaseScripts\MySQL\install.sql EndProjectSection EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UniTest", "UnitTest\UniTest.csproj", "{CFE75C48-F9D5-403A-8419-D07939BBD769}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTest", "UnitTest\UnitTest.csproj", "{CFE75C48-F9D5-403A-8419-D07939BBD769}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/UnitTest/Bootstrap.Admin/Api/LoginTest.cs b/UnitTest/Bootstrap.Admin/Api/LoginTest.cs new file mode 100644 index 00000000..ea7db33b --- /dev/null +++ b/UnitTest/Bootstrap.Admin/Api/LoginTest.cs @@ -0,0 +1,23 @@ +using System.Net.Http; +using Xunit; + +namespace Bootstrap.Admin +{ + public class LoginTest : IClassFixture + { + private HttpClient _client; + + public LoginTest(BAWebHost factory) + { + _client = factory.CreateClient(); + } + + [Fact] + public async void Login_Ok() + { + var resq = await _client.PostAsJsonAsync("/api/Login", new { userName = "Admin", password = "123789" }); + var _token = await resq.Content.ReadAsStringAsync(); + Assert.NotNull(_token); + } + } +} diff --git a/UnitTest/Bootstrap.Admin/BAWebHost.cs b/UnitTest/Bootstrap.Admin/BAWebHost.cs new file mode 100644 index 00000000..87390906 --- /dev/null +++ b/UnitTest/Bootstrap.Admin/BAWebHost.cs @@ -0,0 +1,20 @@ +using Microsoft.AspNetCore.Mvc.Testing; +using UnitTest; + +namespace Bootstrap.Admin +{ + /// + /// + /// + public class BAWebHost : WebApplicationFactory + { + /// + /// + /// + public BAWebHost() + { + // Copy license + TestHelper.CopyLicense(); + } + } +} diff --git a/UnitTest/Bootstrap.Admin/Controller/AccountTest.cs b/UnitTest/Bootstrap.Admin/Controller/AccountTest.cs new file mode 100644 index 00000000..c01afd8e --- /dev/null +++ b/UnitTest/Bootstrap.Admin/Controller/AccountTest.cs @@ -0,0 +1,68 @@ +using System.Net.Http; +using Xunit; + +namespace Bootstrap.Admin +{ + public class AccountTest : IClassFixture + { + private HttpClient _client; + + public AccountTest(BAWebHost factory) + { + _client = factory.CreateClient(); + } + + [Fact] + public async void Login_Fail() + { + // login + var r = await _client.GetAsync("/Account/Login"); + Assert.True(r.IsSuccessStatusCode); + var content = await r.Content.ReadAsStringAsync(); + Assert.Contains("登 陆", content); + } + + [Fact] + public async void Login_Admin() + { + // login + var r = await _client.GetAsync("/Account/Login"); + Assert.True(r.IsSuccessStatusCode); + var view = await r.Content.ReadAsStringAsync(); + var tokenTag = ""); + var antiToken = view.Substring(0, index); + + var content = new MultipartFormDataContent(); + content.Add(new StringContent("Admin"), "userName"); + content.Add(new StringContent("123789"), "password"); + content.Add(new StringContent("true"), "remember"); + content.Add(new StringContent(antiToken), "__RequestVerificationToken"); + r = await _client.PostAsync("/Account/Login", content); + var resp = await r.Content.ReadAsStringAsync(); + Assert.Contains("注销", resp); + } + + [Fact] + public async void Logout_Ok() + { + // logout + var r = await _client.GetAsync("/Account/Logout"); + Assert.True(r.IsSuccessStatusCode); + var content = await r.Content.ReadAsStringAsync(); + Assert.Contains("登 陆", content); + } + + [Fact] + public async void AccessDenied_Ok() + { + // logout + var r = await _client.GetAsync("/Account/AccessDenied"); + Assert.True(r.IsSuccessStatusCode); + var content = await r.Content.ReadAsStringAsync(); + Assert.Contains("您无权访问本页面请联系网站管理员授权后再查看", content); + } + } +} diff --git a/UnitTest/Bootstrap.Admin/Controller/HomeTest.cs b/UnitTest/Bootstrap.Admin/Controller/HomeTest.cs new file mode 100644 index 00000000..e643ca38 --- /dev/null +++ b/UnitTest/Bootstrap.Admin/Controller/HomeTest.cs @@ -0,0 +1,38 @@ +using System.Net.Http; +using Xunit; + +namespace Bootstrap.Admin +{ + public class HomeTest : IClassFixture + { + private HttpClient _client; + + public HomeTest(BAWebHost factory) + { + _client = factory.CreateClient(); + } + + [Theory] + [InlineData(0)] + [InlineData(404)] + [InlineData(500)] + public async void Error_Ok(int errorCode) + { + var r = await _client.GetAsync($"/Home/Error/{errorCode}"); + Assert.True(r.IsSuccessStatusCode); + var content = await r.Content.ReadAsStringAsync(); + if (errorCode == 0) + { + Assert.Contains("未处理服务器内部错误", content); + } + else if (errorCode == 404) + { + Assert.Contains("请求资源未找到", content); + } + else + { + Assert.Contains("服务器内部错误", content); + } + } + } +} diff --git a/UnitTest/License/Longbow.lic b/UnitTest/License/Longbow.lic new file mode 100644 index 00000000..8d0114d1 --- /dev/null +++ b/UnitTest/License/Longbow.lic @@ -0,0 +1,3 @@ + + tH0NNQQxF3DJiiPpEWlKmsCMOhp/ijYoPnWvpjAH9Nfph06YjvCg6SBgs0SLGKrp+pHKey0Lf/wN94bR7/TYcsHrX20WvxvhBPmk2REJxLyqn6M+fAQAX++XZsQH9ywhgZaM2maGN+cXO+y+5aXGvUYKKOL6dxCIRBSNtAl21swA + \ No newline at end of file diff --git a/UnitTest/TestHelper.cs b/UnitTest/TestHelper.cs index 7c55244d..18edf5fe 100644 --- a/UnitTest/TestHelper.cs +++ b/UnitTest/TestHelper.cs @@ -1,7 +1,7 @@ using System; using System.IO; -namespace UniTest +namespace UnitTest { public static class TestHelper { @@ -26,5 +26,16 @@ namespace UniTest var soluFolder = RetrieveSolutionPath(); return Path.Combine(soluFolder, folder); } + + /// + /// + /// + public static void CopyLicense() + { + var licFile = RetrievePath($"UnitTest{Path.DirectorySeparatorChar}License{Path.DirectorySeparatorChar}Longbow.lic"); + + var targetFile = Path.Combine(AppContext.BaseDirectory, "Longbow.lic"); + if (!File.Exists(targetFile)) File.Copy(licFile, targetFile, true); + } } } diff --git a/UnitTest/UniTest.csproj b/UnitTest/UnitTest.csproj similarity index 76% rename from UnitTest/UniTest.csproj rename to UnitTest/UnitTest.csproj index 182f1333..f9c16f68 100644 --- a/UnitTest/UniTest.csproj +++ b/UnitTest/UnitTest.csproj @@ -6,6 +6,8 @@ + + @@ -16,6 +18,7 @@ +