单元测试:增加Bootstrap.Admin工程Controller单元测试
This commit is contained in:
parent
f020a3b683
commit
ff3a23dc2e
|
@ -54,7 +54,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "MySQL", "MySQL", "{084E2E94
|
||||||
DatabaseScripts\MySQL\install.sql = DatabaseScripts\MySQL\install.sql
|
DatabaseScripts\MySQL\install.sql = DatabaseScripts\MySQL\install.sql
|
||||||
EndProjectSection
|
EndProjectSection
|
||||||
EndProject
|
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
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
using System.Net.Http;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Bootstrap.Admin
|
||||||
|
{
|
||||||
|
public class LoginTest : IClassFixture<BAWebHost>
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,20 @@
|
||||||
|
using Microsoft.AspNetCore.Mvc.Testing;
|
||||||
|
using UnitTest;
|
||||||
|
|
||||||
|
namespace Bootstrap.Admin
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
public class BAWebHost : WebApplicationFactory<Startup>
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
public BAWebHost()
|
||||||
|
{
|
||||||
|
// Copy license
|
||||||
|
TestHelper.CopyLicense();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,68 @@
|
||||||
|
using System.Net.Http;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Bootstrap.Admin
|
||||||
|
{
|
||||||
|
public class AccountTest : IClassFixture<BAWebHost>
|
||||||
|
{
|
||||||
|
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 = "<input name=\"__RequestVerificationToken\" type=\"hidden\" value=\"";
|
||||||
|
var index = view.IndexOf(tokenTag);
|
||||||
|
view = view.Substring(index + tokenTag.Length);
|
||||||
|
index = view.IndexOf("\" /></form>");
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,38 @@
|
||||||
|
using System.Net.Http;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Bootstrap.Admin
|
||||||
|
{
|
||||||
|
public class HomeTest : IClassFixture<BAWebHost>
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,3 @@
|
||||||
|
<lgb>
|
||||||
|
<token>tH0NNQQxF3DJiiPpEWlKmsCMOhp/ijYoPnWvpjAH9Nfph06YjvCg6SBgs0SLGKrp+pHKey0Lf/wN94bR7/TYcsHrX20WvxvhBPmk2REJxLyqn6M+fAQAX++XZsQH9ywhgZaM2maGN+cXO+y+5aXGvUYKKOL6dxCIRBSNtAl21swA</token>
|
||||||
|
</lgb>
|
|
@ -1,7 +1,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
|
||||||
namespace UniTest
|
namespace UnitTest
|
||||||
{
|
{
|
||||||
public static class TestHelper
|
public static class TestHelper
|
||||||
{
|
{
|
||||||
|
@ -26,5 +26,16 @@ namespace UniTest
|
||||||
var soluFolder = RetrieveSolutionPath();
|
var soluFolder = RetrieveSolutionPath();
|
||||||
return Path.Combine(soluFolder, folder);
|
return Path.Combine(soluFolder, folder);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,8 @@
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.2.0" />
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="2.2.0" />
|
||||||
<PackageReference Include="Microsoft.Data.Sqlite" Version="2.2.1" />
|
<PackageReference Include="Microsoft.Data.Sqlite" Version="2.2.1" />
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
|
||||||
<PackageReference Include="xunit" Version="2.4.1" />
|
<PackageReference Include="xunit" Version="2.4.1" />
|
||||||
|
@ -16,6 +18,7 @@
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\Bootstrap.Admin\Bootstrap.Admin.csproj" />
|
||||||
<ProjectReference Include="..\Bootstrap.DataAccess\Bootstrap.DataAccess.csproj" />
|
<ProjectReference Include="..\Bootstrap.DataAccess\Bootstrap.DataAccess.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
Loading…
Reference in New Issue