refactor: 修复单元测试

This commit is contained in:
Argo-Tianyi 2021-07-02 22:05:00 +08:00
parent b12d75f2cd
commit a7c1789eb2
26 changed files with 294 additions and 188 deletions

View File

@ -1,4 +1,5 @@
using System.Net.Http;
using System.Net.Http.Json;
using Xunit;
using static Bootstrap.Admin.Controllers.Api.AnalyseController;
@ -11,11 +12,11 @@ namespace Bootstrap.Admin.Api
[Fact]
public async void Get_Ok()
{
var cates = await Client.GetAsJsonAsync<AnalyseData>("?logType=LoginUsers");
var cates = await Client.GetFromJsonAsync<AnalyseData>("?logType=LoginUsers");
Assert.NotNull(cates);
cates = await Client.GetAsJsonAsync<AnalyseData>("?logType=log");
cates = await Client.GetFromJsonAsync<AnalyseData>("?logType=log");
Assert.NotNull(cates);
cates = await Client.GetAsJsonAsync<AnalyseData>("?logType=trace");
cates = await Client.GetFromJsonAsync<AnalyseData>("?logType=trace");
Assert.NotNull(cates);
}
}

View File

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Json;
using Xunit;
namespace Bootstrap.Admin.Api
@ -14,7 +15,7 @@ namespace Bootstrap.Admin.Api
public async void Get_Ok()
{
var rid = RoleHelper.Retrieves().Where(r => r.RoleName == "Administrators").First().Id;
var cates = await Client.GetAsJsonAsync<IEnumerable<App>>(rid);
var cates = await Client.GetFromJsonAsync<IEnumerable<App>>(rid);
Assert.NotEmpty(cates);
}
}

View File

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Json;
using Xunit;
namespace Bootstrap.Admin.Api
@ -13,21 +14,21 @@ namespace Bootstrap.Admin.Api
[Fact]
public async void DictCategorys_Ok()
{
var cates = await Client.GetAsJsonAsync<IEnumerable<string>>("RetrieveDictCategorys");
var cates = await Client.GetFromJsonAsync<IEnumerable<string>>("RetrieveDictCategorys");
Assert.NotEmpty(cates);
}
[Fact]
public async void Menus_Ok()
{
var cates = await Client.GetAsJsonAsync<IEnumerable<string>>("RetrieveMenus");
var cates = await Client.GetFromJsonAsync<IEnumerable<string>>("RetrieveMenus");
Assert.NotEmpty(cates);
}
[Fact]
public async void ParentMenus_Ok()
{
var cates = await Client.GetAsJsonAsync<IEnumerable<string>>("RetrieveParentMenus");
var cates = await Client.GetFromJsonAsync<IEnumerable<string>>("RetrieveParentMenus");
Assert.NotEmpty(cates);
}
@ -35,11 +36,11 @@ namespace Bootstrap.Admin.Api
public async void ValidateMenuBySubMenu_Ok()
{
var id = MenuHelper.RetrieveAllMenus("Admin").First(m => m.Name == "个人中心").Id;
var cates = await Client.GetAsJsonAsync<bool>($"ValidateMenuBySubMenu/{id}");
var cates = await Client.GetFromJsonAsync<bool>($"ValidateMenuBySubMenu/{id}");
Assert.False(cates);
id = MenuHelper.RetrieveAllMenus("Admin").First(m => m.Name == "后台管理").Id;
cates = await Client.GetAsJsonAsync<bool>($"ValidateMenuBySubMenu/{id}");
cates = await Client.GetFromJsonAsync<bool>($"ValidateMenuBySubMenu/{id}");
Assert.True(cates);
}
@ -47,11 +48,11 @@ namespace Bootstrap.Admin.Api
public async void ValidateParentMenuById_Ok()
{
var id = MenuHelper.RetrieveAllMenus("Admin").First(m => m.Name == "个人中心").Id;
var cates = await Client.GetAsJsonAsync<bool>($"ValidateParentMenuById/{id}");
var cates = await Client.GetFromJsonAsync<bool>($"ValidateParentMenuById/{id}");
Assert.True(cates);
var subId = MenuHelper.RetrieveAllMenus("Admin").First(m => m.ParentId == id).Id;
cates = await Client.GetAsJsonAsync<bool>($"ValidateParentMenuById/{subId}");
cates = await Client.GetFromJsonAsync<bool>($"ValidateParentMenuById/{subId}");
Assert.False(cates);
}
}

View File

@ -3,7 +3,7 @@ using Bootstrap.Security;
using Longbow.Web.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Json;
using Xunit;
namespace Bootstrap.Admin.Api
@ -25,7 +25,7 @@ namespace Bootstrap.Admin.Api
public async void Get_Ok(string query, string order)
{
// 菜单 系统菜单 系统使用条件
var qd = await Client.GetAsJsonAsync<QueryData<BootstrapDict>>($"?sort={query}&order={order}&offset=0&limit=20&category=%E8%8F%9C%E5%8D%95&name=%E7%B3%BB%E7%BB%9F%E8%8F%9C%E5%8D%95&define=0&_=1547608210979");
var qd = await Client.GetFromJsonAsync<QueryData<BootstrapDict>>($"?sort={query}&order={order}&offset=0&limit=20&category=%E8%8F%9C%E5%8D%95&name=%E7%B3%BB%E7%BB%9F%E8%8F%9C%E5%8D%95&define=0&_=1547608210979");
Assert.Single(qd.rows);
}
@ -36,18 +36,20 @@ namespace Bootstrap.Admin.Api
public async void Search_Ok(string search)
{
// 菜单 系统菜单 系统使用条件
var qd = await Client.GetAsJsonAsync<QueryData<BootstrapDict>>($"?search={search}&sort=&order=&offset=0&limit=20&category=&name=&define=0&_=1547608210979");
var qd = await Client.GetFromJsonAsync<QueryData<BootstrapDict>>($"?search={search}&sort=&order=&offset=0&limit=20&category=&name=&define=0&_=1547608210979");
Assert.NotEmpty(qd.rows);
}
[Fact]
public async void PostAndDelete_Ok()
{
var ret = await Client.PostAsJsonAsync<BootstrapDict, bool>("", new BootstrapDict() { Name = "UnitTest-Dict", Category = "UnitTest-Category", Code = "0", Define = 0 });
Assert.True(ret);
var ret = await Client.PostAsJsonAsync<BootstrapDict>("", new BootstrapDict() { Name = "UnitTest-Dict", Category = "UnitTest-Category", Code = "0", Define = 0 });
Assert.True(ret.IsSuccessStatusCode);
var ids = DictHelper.RetrieveDicts().Where(d => d.Name == "UnitTest-Dict").Select(d => d.Id);
Assert.True(await Client.DeleteAsJsonAsync<IEnumerable<string>, bool>("", ids));
var resp = await Client.DeleteAsJsonAsync<IEnumerable<string>>("", ids);
var ret1 = await resp.Content.ReadFromJsonAsync<bool>();
Assert.True(ret1);
}
}
}

View File

@ -4,6 +4,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Json;
using Xunit;
using static Bootstrap.Admin.Controllers.Api.ExceptionsController;
@ -22,7 +23,7 @@ namespace Bootstrap.Admin.Api
// 菜单 系统菜单 系统使用条件
var query = "?sort=LogTime&order=desc&offset=0&limit=20&StartTime=&EndTime=&_=1547610349796";
var qd = await Client.GetAsJsonAsync<QueryData<Exceptions>>(query);
var qd = await Client.GetFromJsonAsync<QueryData<Exceptions>>(query);
Assert.NotEmpty(qd.rows);
// clean
@ -32,13 +33,15 @@ namespace Bootstrap.Admin.Api
[Fact]
public async void Post_Ok()
{
var files = await Client.PostAsJsonAsync<string, IEnumerable<string>>(string.Empty, "");
var ret = await Client.PostAsJsonAsync<string>(string.Empty, "");
var files = await ret.Content.ReadFromJsonAsync<IEnumerable<string>>();
Assert.NotNull(files);
var fileName = files.FirstOrDefault();
if (!string.IsNullOrEmpty(fileName))
{
var resp = await Client.PutAsJsonAsync<ExceptionFileQuery, string>("", new ExceptionFileQuery() { FileName = fileName });
ret = await Client.PutAsJsonAsync<ExceptionFileQuery>("", new ExceptionFileQuery() { FileName = fileName });
var resp = await ret.Content.ReadAsStringAsync();
Assert.NotNull(resp);
}

View File

@ -1,6 +1,7 @@
using Bootstrap.Admin.Controllers.Api;
using System;
using System.Net.Http;
using System.Net.Http.Json;
using System.Reflection;
using System.Threading.Tasks;
using Xunit;
@ -14,28 +15,28 @@ namespace Bootstrap.Admin.Api
[Fact]
public async void Issues_Ok()
{
var cates = await Client.GetAsJsonAsync<object>("Issues");
var cates = await Client.GetFromJsonAsync<object>("Issues");
Assert.NotNull(cates);
}
[Fact]
public async void Pulls_Ok()
{
var cates = await Client.GetAsJsonAsync<object>("Pulls");
var cates = await Client.GetFromJsonAsync<object>("Pulls");
Assert.NotNull(cates);
}
[Fact]
public async void Releases_Ok()
{
var cates = await Client.GetAsJsonAsync<object>("Releases");
var cates = await Client.GetFromJsonAsync<object>("Releases");
Assert.NotNull(cates);
}
[Fact]
public async void Builds_Ok()
{
var cates = await Client.GetAsJsonAsync<object>("Builds");
var cates = await Client.GetFromJsonAsync<object>("Builds");
Assert.NotNull(cates);
}

View File

@ -2,7 +2,7 @@
using Longbow.Web.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Json;
using Xunit;
namespace Bootstrap.Admin.Api
@ -16,9 +16,9 @@ namespace Bootstrap.Admin.Api
{
// 菜单 系统菜单 系统使用条件
var query = "?sort=GroupName&order=asc&offset=0&limit=20&groupName=Admin&description=%E7%B3%BB%E7%BB%9F%E9%BB%98%E8%AE%A4%E7%BB%84&_=1547614230481";
var qd = await Client.GetAsJsonAsync<QueryData<Group>>(query);
await Client.GetFromJsonAsync<QueryData<Group>>(query);
query = "?sort=GroupName&order=desc&offset=0&limit=20&groupName=Admin&description=%E7%B3%BB%E7%BB%9F%E9%BB%98%E8%AE%A4%E7%BB%84&_=1547614230481";
qd = await Client.GetAsJsonAsync<QueryData<Group>>(query);
var qd = await Client.GetFromJsonAsync<QueryData<Group>>(query);
Assert.Single(qd.rows);
}
@ -27,30 +27,34 @@ namespace Bootstrap.Admin.Api
[InlineData("系统默认")]
public async void Search_Ok(string search)
{
var qd = await Client.GetAsJsonAsync<QueryData<Group>>($"?search={search}&sort=&order=&offset=0&limit=20&category=&name=&define=0&_=1547608210979");
var qd = await Client.GetFromJsonAsync<QueryData<Group>>($"?search={search}&sort=&order=&offset=0&limit=20&category=&name=&define=0&_=1547608210979");
Assert.NotEmpty(qd.rows);
}
[Fact]
public async void PostAndDelete_Ok()
{
var ret = await Client.PostAsJsonAsync<Group, bool>("", new Group() { GroupCode = "002", GroupName = "UnitTest-Group", Description = "UnitTest-Desc" });
Assert.True(ret);
var ret = await Client.PostAsJsonAsync<Group>("", new Group() { GroupCode = "002", GroupName = "UnitTest-Group", Description = "UnitTest-Desc" });
Assert.True(ret.IsSuccessStatusCode);
var ids = GroupHelper.Retrieves().Where(d => d.GroupName == "UnitTest-Group").Select(d => d.Id);
Assert.True(await Client.DeleteAsJsonAsync<IEnumerable<string>, bool>("", ids));
var resp = await Client.DeleteAsJsonAsync<IEnumerable<string>>("", ids);
var ret1 = await resp.Content.ReadFromJsonAsync<bool>();
Assert.True(ret1);
}
[Fact]
public async void PostById_Ok()
{
var uid = UserHelper.Retrieves().Where(u => u.UserName == "Admin").First().Id;
var ret = await Client.PostAsJsonAsync<string, IEnumerable<Group>>($"{uid}?type=user", string.Empty);
Assert.NotEmpty(ret);
var ret = await Client.PostAsJsonAsync<string>($"{uid}?type=user", string.Empty);
var groups = await ret.Content.ReadFromJsonAsync<IEnumerable<Group>>();
Assert.NotEmpty(groups);
var rid = RoleHelper.Retrieves().Where(r => r.RoleName == "Administrators").First().Id;
ret = await Client.PostAsJsonAsync<string, IEnumerable<Group>>($"{rid}?type=role", string.Empty);
Assert.NotEmpty(ret);
ret = await Client.PostAsJsonAsync<string>($"{rid}?type=role", string.Empty);
groups = await ret.Content.ReadFromJsonAsync<IEnumerable<Group>>();
Assert.NotEmpty(groups);
}
[Fact]
@ -58,12 +62,12 @@ namespace Bootstrap.Admin.Api
{
var ids = GroupHelper.Retrieves().Select(g => g.Id);
var uid = UserHelper.Retrieves().Where(u => u.UserName == "Admin").First().Id;
var ret = await Client.PutAsJsonAsync<IEnumerable<string>, bool>($"{uid}?type=user", ids);
Assert.True(ret);
var ret = await Client.PutAsJsonAsync<IEnumerable<string>>($"{uid}?type=user", ids);
Assert.True(ret.IsSuccessStatusCode);
var rid = RoleHelper.Retrieves().Where(r => r.RoleName == "Administrators").First().Id;
ret = await Client.PutAsJsonAsync<IEnumerable<string>, bool>($"{rid}?type=role", ids);
Assert.True(ret);
ret = await Client.PutAsJsonAsync<IEnumerable<string>>($"{rid}?type=role", ids);
Assert.True(ret.IsSuccessStatusCode);
}
}
}

View File

@ -9,15 +9,15 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Hosting;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text.Json;
using UnitTest;
using Xunit;
namespace Bootstrap.Admin.Api
{
public class HealthCheckTest : IClassFixture<HealthCheckWebHost<HealthCheckStartup>>
{
private HttpClient client;
private readonly HttpClient client;
public HealthCheckTest(HealthCheckWebHost<HealthCheckStartup> factory)
{
@ -27,7 +27,7 @@ namespace Bootstrap.Admin.Api
[Fact]
public async void Get_Ok()
{
var cates = await client.GetAsJsonAsync<object>("/Healths");
var cates = await client.GetFromJsonAsync<object>("/Healths");
Assert.NotNull(cates);
}

View File

@ -1,6 +1,7 @@
using Bootstrap.Security;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Json;
using Xunit;
namespace Bootstrap.Admin.Api
@ -12,42 +13,48 @@ namespace Bootstrap.Admin.Api
[Fact]
public async void RetrieveDicts_Ok()
{
var ret = await Client.PostAsJsonAsync<string, IEnumerable<BootstrapDict>>("RetrieveDicts", "");
var req = await Client.PostAsJsonAsync<string>("RetrieveDicts", "");
var ret = await req.Content.ReadFromJsonAsync<IEnumerable<BootstrapDict>>();
Assert.NotEmpty(ret);
}
[Fact]
public async void RetrieveRolesByUrl_Ok()
{
var ret = await Client.PostAsJsonAsync<string, IEnumerable<string>>("RetrieveRolesByUrl", "~/Admin/Index");
var req = await Client.PostAsJsonAsync<string>("RetrieveRolesByUrl", "~/Admin/Index");
var ret = await req.Content.ReadFromJsonAsync<IEnumerable<string>>();
Assert.NotEmpty(ret);
}
[Fact]
public async void RetrieveRolesByUserName_Ok()
{
var ret = await Client.PostAsJsonAsync<string, IEnumerable<string>>("RetrieveRolesByUserName", "Admin");
var req = await Client.PostAsJsonAsync<string>("RetrieveRolesByUserName", "Admin");
var ret = await req.Content.ReadFromJsonAsync<IEnumerable<string>>();
Assert.NotEmpty(ret);
}
[Fact]
public async void RetrieveUserByUserName_Ok()
{
var ret = await Client.PostAsJsonAsync<string, BootstrapUser>("RetrieveUserByUserName", "Admin");
var req = await Client.PostAsJsonAsync<string>("RetrieveUserByUserName", "Admin");
var ret = await req.Content.ReadFromJsonAsync<BootstrapUser>();
Assert.Equal("Admin", ret.UserName);
}
[Fact]
public async void RetrieveAppMenus_Ok()
{
var ret = await Client.PostAsJsonAsync<AppMenuOption, IEnumerable<BootstrapMenu>>("RetrieveAppMenus", new AppMenuOption() { AppId = "Demo", UserName = "Admin", Url = "~/Admin/Index" });
var req = await Client.PostAsJsonAsync<AppMenuOption>("RetrieveAppMenus", new AppMenuOption() { AppId = "Demo", UserName = "Admin", Url = "~/Admin/Index" });
var ret = await req.Content.ReadFromJsonAsync<IEnumerable<BootstrapMenu>>();
Assert.NotEmpty(ret);
}
[Fact]
public async void Healths_Ok()
{
var ret = await Client.PostAsJsonAsync<string, bool>("Healths", "UnitTest");
var req = await Client.PostAsJsonAsync<string>("Healths", "UnitTest");
var ret = await req.Content.ReadFromJsonAsync<bool>();
Assert.False(ret);
}
}

View File

@ -2,6 +2,7 @@
using Longbow.Web.Mvc;
using Longbow.Web.SMS;
using System.Net.Http;
using System.Net.Http.Json;
using Xunit;
namespace Bootstrap.Admin.Api
@ -13,7 +14,7 @@ namespace Bootstrap.Admin.Api
[Fact]
public async void Login_Get()
{
var users = await Client.GetAsJsonAsync<QueryData<LoginUser>>("?sort=LoginTime&order=&offset=0&limit=20&startTime=&endTime=&loginIp=&_=1560933256621");
var users = await Client.GetFromJsonAsync<QueryData<LoginUser>>("?sort=LoginTime&order=&offset=0&limit=20&startTime=&endTime=&loginIp=&_=1560933256621");
Assert.NotEmpty(users.rows);
}
@ -51,7 +52,7 @@ namespace Bootstrap.Admin.Api
public async void Option_Ok()
{
var req = new HttpRequestMessage(HttpMethod.Options, "");
var resp = await Client.SendAsync(req);
await Client.SendAsync(req);
}
}
}

View File

@ -2,6 +2,7 @@
using Longbow.Web.Mvc;
using System;
using System.Net.Http;
using System.Net.Http.Json;
using Xunit;
namespace Bootstrap.Admin.Api
@ -18,7 +19,7 @@ namespace Bootstrap.Admin.Api
// 菜单 系统菜单 系统使用条件
var query = "?sort=LogTime&order=desc&offset=0&limit=20&operateType=&OperateTimeStart=&OperateTimeEnd=&_=1547617573596";
var qd = await Client.GetAsJsonAsync<QueryData<Log>>(query);
var qd = await Client.GetFromJsonAsync<QueryData<Log>>(query);
Assert.NotEmpty(qd.rows);
// clean
@ -29,8 +30,9 @@ namespace Bootstrap.Admin.Api
public async void Post_Ok()
{
Client.DefaultRequestHeaders.Add("user-agent", "UnitTest");
var resp = await Client.PostAsJsonAsync<Log, bool>("", new Log() { CRUD = "UnitTest", RequestUrl = "~/UnitTest" });
Assert.True(resp);
var resp = await Client.PostAsJsonAsync<Log>("", new Log() { CRUD = "UnitTest", RequestUrl = "~/UnitTest" });
var ret = await resp.Content.ReadFromJsonAsync<bool>();
Assert.True(ret);
// clean
DbManager.Create().Execute("delete from Logs where CRUD = @0", "UnitTest");

View File

@ -4,6 +4,7 @@ using Longbow.Web.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Json;
using Xunit;
namespace Bootstrap.Admin.Api
@ -29,7 +30,7 @@ namespace Bootstrap.Admin.Api
[InlineData("Application", "desc")]
public async void Get_Ok(string query, string order)
{
var qd = await Client.GetAsJsonAsync<QueryData<object>>($"?sort={query}&order={order}&offset=0&limit=100&parentName=%E6%B5%8B%E8%AF%95%E9%A1%B5%E9%9D%A2&name=%E5%85%B3%E4%BA%8E&category=1&isresource=0&appId=Demo&_=1558235377255");
var qd = await Client.GetFromJsonAsync<QueryData<object>>($"?sort={query}&order={order}&offset=0&limit=100&parentName=%E6%B5%8B%E8%AF%95%E9%A1%B5%E9%9D%A2&name=%E5%85%B3%E4%BA%8E&category=1&isresource=0&appId=Demo&_=1558235377255");
Assert.Single(qd.rows);
}
@ -39,18 +40,21 @@ namespace Bootstrap.Admin.Api
public async void Search_Ok(string search)
{
// 菜单 系统菜单 系统使用条件
var qd = await Client.GetAsJsonAsync<QueryData<object>>($"?search={search}&sort=&order=&offset=0&limit=20&category=&name=&define=0&_=1547608210979");
var qd = await Client.GetFromJsonAsync<QueryData<object>>($"?search={search}&sort=&order=&offset=0&limit=20&category=&name=&define=0&_=1547608210979");
Assert.NotEmpty(qd.rows);
}
[Fact]
public async void PostAndDelete_Ok()
{
var ret = await Client.PostAsJsonAsync<BootstrapMenu, bool>("", new BootstrapMenu() { Name = "UnitTest-Menu", Application = "0", Category = "0", ParentId = "0", Url = "#", Target = "_self", IsResource = 0 });
var req = await Client.PostAsJsonAsync<BootstrapMenu>("", new BootstrapMenu() { Name = "UnitTest-Menu", Application = "0", Category = "0", ParentId = "0", Url = "#", Target = "_self", IsResource = 0 });
var ret = await req.Content.ReadFromJsonAsync<bool>();
Assert.True(ret);
var ids = MenuHelper.RetrieveAllMenus("Admin").Where(d => d.Name == "UnitTest-Menu").Select(d => d.Id);
Assert.True(await Client.DeleteAsJsonAsync<IEnumerable<string>, bool>("", ids));
var resp = await Client.DeleteAsJsonAsync<IEnumerable<string>>("", ids);
var ret1 = await resp.Content.ReadFromJsonAsync<bool>();
Assert.True(ret1);
}
@ -58,11 +62,13 @@ namespace Bootstrap.Admin.Api
public async void PostById_Ok()
{
var uid = UserHelper.Retrieves().Where(u => u.UserName == "Admin").First().Id;
var ret = await Client.PostAsJsonAsync<string, IEnumerable<object>>($"{uid}?type=user", string.Empty);
var req = await Client.PostAsJsonAsync<string>($"{uid}?type=user", string.Empty);
var ret = await req.Content.ReadFromJsonAsync<IEnumerable<object>>();
Assert.NotEmpty(ret);
var rid = RoleHelper.Retrieves().Where(r => r.RoleName == "Administrators").First().Id;
ret = await Client.PostAsJsonAsync<string, IEnumerable<object>>($"{rid}?type=role", string.Empty);
req = await Client.PostAsJsonAsync<string>($"{rid}?type=role", string.Empty);
ret = await req.Content.ReadFromJsonAsync<IEnumerable<object>>();
Assert.NotEmpty(ret);
}
@ -71,7 +77,8 @@ namespace Bootstrap.Admin.Api
{
var ids = MenuHelper.RetrieveAllMenus("Admin").Select(g => g.Id);
var rid = RoleHelper.Retrieves().Where(r => r.RoleName == "Administrators").First().Id;
var ret = await Client.PutAsJsonAsync<IEnumerable<string>, bool>($"{rid}", ids);
var req = await Client.PutAsJsonAsync<IEnumerable<string>>($"{rid}", ids);
var ret = await req.Content.ReadFromJsonAsync<bool>();
Assert.True(ret);
}

View File

@ -2,6 +2,7 @@
using Bootstrap.DataAccess;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Json;
using Xunit;
namespace Bootstrap.Admin.Api
@ -17,14 +18,14 @@ namespace Bootstrap.Admin.Api
[InlineData("trash")]
public async void Get_Ok(string action)
{
var resp = await Client.GetAsJsonAsync<IEnumerable<Message>>(action);
var resp = await Client.GetFromJsonAsync<IEnumerable<Message>>(action);
Assert.NotNull(resp);
}
[Fact]
public async void GetCount_Ok()
{
var resp = await Client.GetAsJsonAsync<MessageCountModel>();
var resp = await Client.GetFromJsonAsync<MessageCountModel>("");
Assert.NotNull(resp);
}
}

View File

@ -1,7 +1,7 @@
using Bootstrap.DataAccess;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Json;
using Xunit;
namespace Bootstrap.Admin.Api
@ -13,9 +13,9 @@ namespace Bootstrap.Admin.Api
[Fact]
public async void Get_Ok()
{
var nusr = InsertNewUser();
InsertNewUser();
var resp = await Client.GetAsJsonAsync<IEnumerable<object>>();
var resp = await Client.GetFromJsonAsync<IEnumerable<object>>("");
Assert.NotEmpty(resp);
// 删除新用户
@ -30,8 +30,9 @@ namespace Bootstrap.Admin.Api
// Approve
nusr.UserStatus = UserStates.ApproveUser;
var resp = await Client.PutAsJsonAsync<User, bool>("", nusr);
Assert.True(resp);
var resp = await Client.PutAsJsonAsync<User>("", nusr);
var ret = await resp.Content.ReadFromJsonAsync<bool>();
Assert.True(ret);
// 删除新用户
UserHelper.Delete(new string[] { nusr.Id });
@ -39,14 +40,15 @@ namespace Bootstrap.Admin.Api
// Reject
nusr = InsertNewUser();
nusr.UserStatus = UserStates.RejectUser;
resp = await Client.PutAsJsonAsync<User, bool>("", nusr);
Assert.True(resp);
resp = await Client.PutAsJsonAsync<User>("", nusr);
ret = await resp.Content.ReadFromJsonAsync<bool>();
Assert.True(ret);
// 删除新用户
DeleteUnitTestUser();
}
private User InsertNewUser()
private static User InsertNewUser()
{
// 插入新用户
var nusr = new User() { UserName = "UnitTest_New", DisplayName = "UnitTest", Password = "1", Description = "UnitTest" };
@ -54,7 +56,7 @@ namespace Bootstrap.Admin.Api
return nusr;
}
private void DeleteUnitTestUser()
private static void DeleteUnitTestUser()
{
var ids = UserHelper.RetrieveNewUsers().Where(u => u.UserName == "UnitTest_New").Select(u => u.Id);
UserHelper.Delete(ids);

View File

@ -1,4 +1,5 @@
using System.Net.Http;
using System.Net.Http.Json;
using Xunit;
namespace Bootstrap.Admin.Api
@ -10,7 +11,7 @@ namespace Bootstrap.Admin.Api
[Fact]
public async void Get_Ok()
{
var resp = await Client.GetAsJsonAsync<object>();
var resp = await Client.GetFromJsonAsync<object>("");
Assert.NotNull(resp);
}
}

View File

@ -1,7 +1,7 @@
using Longbow.Web;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Json;
using Xunit;
namespace Bootstrap.Admin.Api
@ -13,14 +13,14 @@ namespace Bootstrap.Admin.Api
[Fact]
public async void Get_Ok()
{
var users = await Client.GetAsJsonAsync<IEnumerable<OnlineUser>>();
var users = await Client.GetFromJsonAsync<IEnumerable<OnlineUser>>("");
Assert.Single(users);
}
[Fact]
public async void GetById_Ok()
{
var urls = await Client.GetAsJsonAsync<IEnumerable<KeyValuePair<DateTime, string>>>("UnitTest");
var urls = await Client.GetFromJsonAsync<IEnumerable<KeyValuePair<DateTime, string>>>("UnitTest");
Assert.Empty(urls);
}
@ -28,11 +28,12 @@ namespace Bootstrap.Admin.Api
public async void Put_Ok()
{
// 三次 Put 请求后返回真
var ret = await Client.PutAsJsonAsync<string, bool>("", "");
ret = await Client.PutAsJsonAsync<string, bool>("", "");
ret = await Client.PutAsJsonAsync<string, bool>("", "");
ret = await Client.PutAsJsonAsync<string, bool>("", "");
Assert.True(ret);
await Client.PutAsJsonAsync<string>("", "");
await Client.PutAsJsonAsync<string>("", "");
await Client.PutAsJsonAsync<string>("", "");
var ret = await Client.PutAsJsonAsync<string>("", "");
var req = await ret.Content.ReadFromJsonAsync<bool>();
Assert.True(req);
}
}
}

View File

@ -3,7 +3,7 @@ using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using UnitTest;
using System.Net.Http.Json;
using Xunit;
namespace Bootstrap.Admin.Api
@ -19,8 +19,9 @@ namespace Bootstrap.Admin.Api
// change theme
usr.Css = "blue.css";
usr.UserStatus = UserStates.ChangeTheme;
var resp = await Client.PutAsJsonAsync<User, bool>("", usr);
Assert.True(resp);
var resp = await Client.PutAsJsonAsync<User>("", usr);
var ret = await resp.Content.ReadFromJsonAsync<bool>();
Assert.True(ret);
}
[Fact]
@ -31,8 +32,9 @@ namespace Bootstrap.Admin.Api
usr.UserStatus = UserStates.ChangePassword;
usr.NewPassword = "123789";
usr.Password = "123789";
var resp = await Client.PutAsJsonAsync<User, bool>("", usr);
Assert.True(resp);
var resp = await Client.PutAsJsonAsync<User>("", usr);
var ret = await resp.Content.ReadFromJsonAsync<bool>();
Assert.True(ret);
}
[Fact]
@ -42,8 +44,9 @@ namespace Bootstrap.Admin.Api
// change displayname
usr.UserStatus = UserStates.ChangeDisplayName;
usr.DisplayName = "Administrator";
var resp = await Client.PutAsJsonAsync<User, bool>("", usr);
Assert.True(resp);
var resp = await Client.PutAsJsonAsync<User>("", usr);
var ret = await resp.Content.ReadFromJsonAsync<bool>();
Assert.True(ret);
}
[Fact]
@ -53,8 +56,9 @@ namespace Bootstrap.Admin.Api
// change app
usr.App = "UnitTest";
usr.UserStatus = UserStates.SaveApp;
var resp = await Client.PutAsJsonAsync<User, bool>("", usr);
Assert.True(resp);
var resp = await Client.PutAsJsonAsync<User>("", usr);
var ret = await resp.Content.ReadFromJsonAsync<bool>();
Assert.True(ret);
}
[Fact]
@ -63,8 +67,8 @@ namespace Bootstrap.Admin.Api
var iconFile = TestHelper.RetrievePath(string.Format("..{0}src{0}admin{0}Bootstrap.Admin{0}wwwroot{0}images{0}logo.jpg", Path.DirectorySeparatorChar));
var adminFile = TestHelper.RetrievePath(string.Format("..{0}src{0}admin{0}Bootstrap.Admin{0}wwwroot{0}images{0}uploader{0}Admin.jpg", Path.DirectorySeparatorChar));
var fi = new FileInfo(iconFile);
string fileName = fi.Name;
byte[] fileContents = File.ReadAllBytes(fi.FullName);
var fileName = fi.Name;
var fileContents = File.ReadAllBytes(fi.FullName);
var loginContent = new MultipartFormDataContent();
var byteArrayContent = new ByteArrayContent(fileContents);
@ -78,8 +82,8 @@ namespace Bootstrap.Admin.Api
// delete file
var delContent = new StringContent("key=Admin.jpg");
delContent.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
req = await Client.PostAsync("del", delContent);
req = await Client.PostAsync("Delete", delContent);
await Client.PostAsync("del", delContent);
await Client.PostAsync("Delete", delContent);
Assert.False(File.Exists(adminFile));
}
}

View File

@ -1,6 +1,6 @@
using Bootstrap.DataAccess;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Json;
using Xunit;
namespace Bootstrap.Admin.Api
@ -12,9 +12,9 @@ namespace Bootstrap.Admin.Api
[Fact]
public async void Get_Ok()
{
var resp = await Client.GetAsJsonAsync<bool>("?userName=Admin");
var resp = await Client.GetFromJsonAsync<bool>("?userName=Admin");
Assert.False(resp);
resp = await Client.GetAsJsonAsync<bool>("?userName=Admin1");
resp = await Client.GetFromJsonAsync<bool>("?userName=Admin1");
Assert.True(resp);
}
@ -23,10 +23,12 @@ namespace Bootstrap.Admin.Api
{
// register new user
var nusr = new User() { UserName = "U_Register", DisplayName = "UnitTest", Password = "1", Description = "UnitTest" };
var resp = await Client.PostAsJsonAsync<User, bool>("", nusr);
Assert.True(resp);
resp = await Client.GetAsJsonAsync<bool>($"?userName={nusr.UserName}");
Assert.False(resp);
var resp = await Client.PostAsJsonAsync<User>("", nusr);
var ret = await resp.Content.ReadFromJsonAsync<bool>();
Assert.True(ret);
ret = await Client.GetFromJsonAsync<bool>($"?userName={nusr.UserName}");
Assert.False(ret);
UserHelper.Delete(nusr.RetrieveNewUsers().Where(u => u.UserName == nusr.UserName).Select(u => u.Id));
}
@ -34,21 +36,24 @@ namespace Bootstrap.Admin.Api
public async void Put_Ok()
{
var user = new ResetUser() { DisplayName = "UnitTest", UserName = "UnitTest", Reason = "UnitTest" };
var resp = await Client.PutAsJsonAsync<ResetUser, bool>("", user);
Assert.True(resp);
var resp = await Client.PutAsJsonAsync<ResetUser>("", user);
var ret = await resp.Content.ReadFromJsonAsync<bool>();
Assert.True(ret);
}
[Fact]
public async void Put_UserName()
{
var user = new User() { Password = "1" };
var resp = await Client.PutAsJsonAsync<User, bool>("UnitTest", user);
Assert.False(resp);
var resp = await Client.PutAsJsonAsync<User>("UnitTest", user);
var ret = await resp.Content.ReadFromJsonAsync<bool>();
Assert.False(ret);
// 重置Admin密码
await Client.PutAsJsonAsync<ResetUser, bool>("", new ResetUser { UserName = "Admin", DisplayName = "Administrator", Reason = "UnitTest" });
resp = await Client.PutAsJsonAsync<User, bool>("Admin", new User() { Password = "123789" });
Assert.True(resp);
await Client.PutAsJsonAsync<ResetUser>("", new ResetUser { UserName = "Admin", DisplayName = "Administrator", Reason = "UnitTest" });
resp = await Client.PutAsJsonAsync<User>("Admin", new User() { Password = "123789" });
ret = await resp.Content.ReadFromJsonAsync<bool>();
Assert.True(ret);
}
}
}

View File

@ -3,6 +3,7 @@ using Longbow.Web.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Json;
using Xunit;
namespace Bootstrap.Admin.Api
@ -17,7 +18,7 @@ namespace Bootstrap.Admin.Api
public async void Get_Ok(string query, string order)
{
// 菜单 系统菜单 系统使用条件
var qd = await Client.GetAsJsonAsync<QueryData<Role>>($"?sort={query}&order={order}&offset=0&limit=20&roleName=Administrators&description=%E7%B3%BB%E7%BB%9F%E7%AE%A1%E7%90%86%E5%91%98&_=1547625202230");
var qd = await Client.GetFromJsonAsync<QueryData<Role>>($"?sort={query}&order={order}&offset=0&limit=20&roleName=Administrators&description=%E7%B3%BB%E7%BB%9F%E7%AE%A1%E7%90%86%E5%91%98&_=1547625202230");
Assert.Single(qd.rows);
}
@ -27,18 +28,21 @@ namespace Bootstrap.Admin.Api
public async void Search_Ok(string search)
{
// 菜单 系统菜单 系统使用条件
var qd = await Client.GetAsJsonAsync<QueryData<Role>>($"?search={search}&sort=&order=&offset=0&limit=20&category=&name=&define=0&_=1547608210979");
var qd = await Client.GetFromJsonAsync<QueryData<Role>>($"?search={search}&sort=&order=&offset=0&limit=20&category=&name=&define=0&_=1547608210979");
Assert.NotEmpty(qd.rows);
}
[Fact]
public async void PostAndDelete_Ok()
{
var ret = await Client.PostAsJsonAsync<Role, bool>("", new Role() { RoleName = "UnitTest-Role", Description = "UnitTest-Desc" });
var resp = await Client.PostAsJsonAsync<Role>("", new Role() { RoleName = "UnitTest-Role", Description = "UnitTest-Desc" });
var ret = await resp.Content.ReadFromJsonAsync<bool>();
Assert.True(ret);
var ids = RoleHelper.Retrieves().Where(d => d.RoleName == "UnitTest-Role").Select(d => d.Id);
Assert.True(await Client.DeleteAsJsonAsync<IEnumerable<string>, bool>("", ids));
resp = await Client.DeleteAsJsonAsync<IEnumerable<string>>("", ids);
var ret1 = await resp.Content.ReadFromJsonAsync<bool>();
Assert.True(ret1);
}
[Fact]
@ -48,14 +52,15 @@ namespace Bootstrap.Admin.Api
var gid = GroupHelper.Retrieves().Where(g => g.GroupName == "Admin").First().Id;
var mid = MenuHelper.RetrieveAllMenus("Admin").Where(m => m.Url == "~/Admin/Index").First().Id;
var ret = await Client.PostAsJsonAsync<string, IEnumerable<object>>($"{uid}?type=user", string.Empty);
var req = await Client.PostAsJsonAsync<string>($"{uid}?type=user", string.Empty);
var ret = await req.Content.ReadFromJsonAsync<IEnumerable<object>>();
Assert.NotEmpty(ret);
ret = await Client.PostAsJsonAsync<string, IEnumerable<object>>($"{gid}?type=group", string.Empty);
Assert.NotEmpty(ret);
req = await Client.PostAsJsonAsync<string>($"{gid}?type=group", string.Empty);
ret = await req.Content.ReadFromJsonAsync<IEnumerable<object>>(); Assert.NotEmpty(ret);
ret = await Client.PostAsJsonAsync<string, IEnumerable<object>>($"{mid}?type=menu", string.Empty);
Assert.NotEmpty(ret);
req = await Client.PostAsJsonAsync<string>($"{mid}?type=menu", string.Empty);
ret = await req.Content.ReadFromJsonAsync<IEnumerable<object>>(); Assert.NotEmpty(ret);
}
[Fact]
@ -66,17 +71,18 @@ namespace Bootstrap.Admin.Api
var mid = MenuHelper.RetrieveAllMenus("Admin").Where(m => m.Url == "~/Admin/Index").First().Id;
var ids = RoleHelper.Retrieves().Select(r => r.Id);
var ret = await Client.PutAsJsonAsync<IEnumerable<string>, bool>($"{uid}?type=user", ids);
var req = await Client.PutAsJsonAsync<IEnumerable<string>>($"{uid}?type=user", ids);
var ret = await req.Content.ReadFromJsonAsync<bool>();
Assert.True(ret);
ret = await Client.PutAsJsonAsync<IEnumerable<string>, bool>($"{gid}?type=group", ids);
Assert.True(ret);
req = await Client.PutAsJsonAsync<IEnumerable<string>>($"{gid}?type=group", ids);
ret = await req.Content.ReadFromJsonAsync<bool>(); Assert.True(ret);
ret = await Client.PutAsJsonAsync<IEnumerable<string>, bool>($"{mid}?type=menu", ids);
Assert.True(ret);
req = await Client.PutAsJsonAsync<IEnumerable<string>>($"{mid}?type=menu", ids);
ret = await req.Content.ReadFromJsonAsync<bool>(); Assert.True(ret);
ret = await Client.PutAsJsonAsync<IEnumerable<string>, bool>($"{mid}?type=app", ids);
Assert.True(ret);
req = await Client.PutAsJsonAsync<IEnumerable<string>>($"{mid}?type=app", ids);
ret = await req.Content.ReadFromJsonAsync<bool>(); Assert.True(ret);
}
}
}

View File

@ -1,6 +1,7 @@
using Bootstrap.DataAccess;
using Longbow.Web.Mvc;
using System.Net.Http;
using System.Net.Http.Json;
using Xunit;
namespace Bootstrap.Admin.Api
@ -12,7 +13,7 @@ namespace Bootstrap.Admin.Api
[Fact]
public async void Get_Ok()
{
var resp = await Client.GetAsJsonAsync<QueryData<DBLog>>("?userName=Admin&OperateTimeStart=&OperateTimeEnd=");
var resp = await Client.GetFromJsonAsync<QueryData<DBLog>>("?userName=Admin&OperateTimeStart=&OperateTimeEnd=");
Assert.NotNull(resp);
}
}

View File

@ -5,6 +5,7 @@ using Longbow.Cache;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Json;
using Xunit;
namespace Bootstrap.Admin.Api
@ -16,14 +17,14 @@ namespace Bootstrap.Admin.Api
[Fact]
public async void Get_Ok()
{
var resp = await Client.GetAsJsonAsync<IEnumerable<CacheCorsItem>>();
var resp = await Client.GetFromJsonAsync<IEnumerable<CacheCorsItem>>("");
Assert.NotNull(resp);
}
[Fact]
public async void GetByKey_Ok()
{
var resp = await Client.GetAsJsonAsync<QueryAppOption>("Demo");
var resp = await Client.GetFromJsonAsync<QueryAppOption>("Demo");
Assert.NotNull(resp);
}
@ -40,30 +41,33 @@ namespace Bootstrap.Admin.Api
AppFooter = "网站页脚"
};
var resp = await Client.PutAsJsonAsync<QueryAppOption, bool>("", data);
Assert.True(resp);
var resp = await Client.PutAsJsonAsync<QueryAppOption>("", data);
var ret = await resp.Content.ReadFromJsonAsync<bool>();
Assert.True(ret);
// Check
var op = await Client.GetAsJsonAsync<QueryAppOption>(data.AppCode);
var op = await Client.GetFromJsonAsync<QueryAppOption>(data.AppCode);
Assert.Equal(data.AppTitle, op.AppTitle);
// update
data.AppId = "edit";
data.AppUrl = "http://UnitTest";
resp = await Client.PutAsJsonAsync<QueryAppOption, bool>("", data);
Assert.True(resp);
resp = await Client.PutAsJsonAsync<QueryAppOption>("", data);
ret = await resp.Content.ReadFromJsonAsync<bool>();
Assert.True(ret);
op = await Client.GetAsJsonAsync<QueryAppOption>(data.AppCode);
op = await Client.GetFromJsonAsync<QueryAppOption>(data.AppCode);
Assert.Equal(data.AppUrl, op.AppUrl);
// 删除
resp = await Client.DeleteAsJsonAsync<BootstrapDict, bool>("AppPath", new BootstrapDict()
resp = await Client.DeleteAsJsonAsync<BootstrapDict>("AppPath", new BootstrapDict()
{
Category = data.AppName,
Name = data.AppName,
Code = data.AppCode
});
Assert.True(resp);
var ret1 = await resp.Content.ReadFromJsonAsync<bool>();
Assert.True(ret1);
}
[Fact]
@ -77,10 +81,11 @@ namespace Bootstrap.Admin.Api
Assert.True(DictHelper.Save(new BootstrapDict() { Category = "UnitTest-Settings", Name = "UnitTest", Code = "0", Define = 0 }));
// 调用 Settings webapi
var resp = await Client.PostAsJsonAsync<IEnumerable<BootstrapDict>, bool>("", new BootstrapDict[]{
var resp = await Client.PostAsJsonAsync<IEnumerable<BootstrapDict>>("", new BootstrapDict[]{
new BootstrapDict() { Category = "UnitTest-Settings", Name = "UnitTest", Code = "UnitTest" }
});
Assert.True(resp);
var ret = await resp.Content.ReadFromJsonAsync<bool>();
Assert.True(ret);
// 由于 SaveUISettings 函数保护功能,上一步保存成功,但是未更改 Code 值
var code = DictHelper.RetrieveDicts().FirstOrDefault(d => d.Category == "UnitTest-Settings").Code;
@ -95,11 +100,13 @@ namespace Bootstrap.Admin.Api
public async void Post_Id_Ok()
{
// Demo
var resp = await Client.PostAsJsonAsync<BootstrapDict, bool>("Demo", new BootstrapDict() { Name = "UnitTest", Code = "0" });
Assert.False(resp);
var resp = await Client.PostAsJsonAsync<BootstrapDict>("Demo", new BootstrapDict() { Name = "UnitTest", Code = "0" });
var ret = await resp.Content.ReadFromJsonAsync<bool>();
Assert.False(ret);
resp = await Client.PostAsJsonAsync<BootstrapDict, bool>("Demo", new BootstrapDict() { Name = "123789", Code = "0" });
Assert.True(resp);
resp = await Client.PostAsJsonAsync<BootstrapDict>("Demo", new BootstrapDict() { Name = "123789", Code = "0" });
ret = await resp.Content.ReadFromJsonAsync<bool>();
Assert.True(ret);
}
internal class CacheCorsItem : ICacheCorsItem

View File

@ -1,6 +1,7 @@
using Bootstrap.DataAccess;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Json;
using Xunit;
namespace Bootstrap.Admin.Api
@ -12,11 +13,11 @@ namespace Bootstrap.Admin.Api
[Fact]
public async void Get_Ok()
{
var resp = await Client.GetAsJsonAsync<IEnumerable<Task>>();
var resp = await Client.GetFromJsonAsync<IEnumerable<Task>>("");
Assert.NotNull(resp);
// receive log
var recv = await Client.GetAsJsonAsync<bool>("/api/TasksLog?name=周期任务");
var recv = await Client.GetFromJsonAsync<bool>("/api/TasksLog?name=周期任务");
Assert.True(recv);
// for test SignalRManager.SendTaskLog
@ -26,12 +27,14 @@ namespace Bootstrap.Admin.Api
[Fact]
public async void Put_Ok()
{
var resp = await Client.PutAsJsonAsync<string, bool>("/api/Tasks/SQL日志?operType=pause", "");
Assert.True(resp);
var resp = await Client.PutAsJsonAsync<string>("/api/Tasks/SQL日志?operType=pause", "");
var ret = await resp.Content.ReadFromJsonAsync<bool>();
Assert.True(ret);
// receive log
var recv = await Client.PutAsJsonAsync<string, bool>("/api/Tasks/SQL日志?operType=run", "");
Assert.True(recv);
var recv = await Client.PutAsJsonAsync<string>("/api/Tasks/SQL日志?operType=run", "");
ret = await recv.Content.ReadFromJsonAsync<bool>();
Assert.True(ret);
}
[Fact]
@ -40,23 +43,27 @@ namespace Bootstrap.Admin.Api
var widget = new TaskWidget();
// widget Cron 表达式为 ”“
var resp = await Client.PostAsJsonAsync<TaskWidget, bool>("/api/Tasks", widget);
Assert.False(resp);
var resp = await Client.PostAsJsonAsync<TaskWidget>("/api/Tasks", widget);
var ret = await resp.Content.ReadFromJsonAsync<bool>();
Assert.False(ret);
// task executor 不合法
widget.CronExpression = Longbow.Tasks.Cron.Secondly(5);
widget.TaskExecutorName = "UnitTest-Widget";
resp = await Client.PostAsJsonAsync<TaskWidget, bool>("/api/Tasks", widget);
Assert.False(resp);
resp = await Client.PostAsJsonAsync<TaskWidget>("/api/Tasks", widget);
ret = await resp.Content.ReadFromJsonAsync<bool>();
Assert.False(ret);
widget.TaskExecutorName = "Bootstrap.Admin.DefaultTaskExecutor";
widget.Name = "UnitTest-Task";
resp = await Client.PostAsJsonAsync<TaskWidget, bool>("/api/Tasks", widget);
Assert.True(resp);
resp = await Client.PostAsJsonAsync<TaskWidget>("/api/Tasks", widget);
ret = await resp.Content.ReadFromJsonAsync<bool>();
Assert.True(ret);
// Delete
resp = await Client.DeleteAsJsonAsync<IEnumerable<string>, bool>("/api/Tasks", new string[] { widget.Name });
Assert.True(resp);
resp = await Client.DeleteAsJsonAsync<IEnumerable<string>>("/api/Tasks", new string[] { widget.Name });
ret = await resp.Content.ReadFromJsonAsync<bool>();
Assert.True(ret);
}
[Fact]
@ -70,7 +77,7 @@ namespace Bootstrap.Admin.Api
[Collection("SystemModel")]
public class TasksSystemModelTest
{
private HttpClient client;
private readonly HttpClient client;
public TasksSystemModelTest(BASystemModelWebHost factory)
{
@ -80,24 +87,30 @@ namespace Bootstrap.Admin.Api
[Fact]
public async void Post_Ok()
{
var widget = new TaskWidget();
widget.CronExpression = Longbow.Tasks.Cron.Secondly(5);
widget.Name = "单次任务";
widget.TaskExecutorName = "Bootstrap.Admin.DefaultTaskExecutor";
var widget = new TaskWidget
{
CronExpression = Longbow.Tasks.Cron.Secondly(5),
Name = "单次任务",
TaskExecutorName = "Bootstrap.Admin.DefaultTaskExecutor"
};
// 演示模式下禁止移除系统内置任务
var resp = await client.PostAsJsonAsync<TaskWidget, bool>("/api/Tasks", widget);
Assert.False(resp);
var resp = await client.PostAsJsonAsync<TaskWidget>("/api/Tasks", widget);
var ret = await resp.Content.ReadFromJsonAsync<bool>();
Assert.False(ret);
resp = await client.DeleteAsJsonAsync<IEnumerable<string>, bool>("/api/Tasks", new string[] { widget.Name });
Assert.False(resp);
resp = await client.DeleteAsJsonAsync<IEnumerable<string>>("/api/Tasks", new string[] { widget.Name });
ret = await resp.Content.ReadFromJsonAsync<bool>();
Assert.False(ret);
widget.Name = "Test-Widget";
resp = await client.PostAsJsonAsync<TaskWidget, bool>("/api/Tasks", widget);
Assert.True(resp);
resp = await client.PostAsJsonAsync<TaskWidget>("/api/Tasks", widget);
ret = await resp.Content.ReadFromJsonAsync<bool>();
Assert.True(ret);
resp = await client.DeleteAsJsonAsync<IEnumerable<string>, bool>("/api/Tasks", new string[] { widget.Name });
Assert.True(resp);
resp = await client.DeleteAsJsonAsync<IEnumerable<string>>("/api/Tasks", new string[] { widget.Name });
ret = await resp.Content.ReadFromJsonAsync<bool>();
Assert.True(ret);
}
}
}

View File

@ -3,6 +3,7 @@ using Longbow.Web;
using Longbow.Web.Mvc;
using System;
using System.Net.Http;
using System.Net.Http.Json;
using Xunit;
namespace Bootstrap.Admin.Api
@ -19,7 +20,7 @@ namespace Bootstrap.Admin.Api
// 菜单 系统菜单 系统使用条件
var query = "?sort=LogTime&order=desc&offset=0&limit=20&operateType=&OperateTimeStart=&OperateTimeEnd=&AccessIP=&_=1547617573596";
var qd = await Client.GetAsJsonAsync<QueryData<Trace>>(query);
var qd = await Client.GetFromJsonAsync<QueryData<Trace>>(query);
Assert.NotEmpty(qd.rows);
// clean
@ -39,8 +40,9 @@ namespace Bootstrap.Admin.Api
OS = "UniTest",
UserAgent = "UniTest"
};
var result = await Client.PostAsJsonAsync<OnlineUser, bool>("", onlineUser);
Assert.True(result);
var result = await Client.PostAsJsonAsync<OnlineUser>("", onlineUser);
var ret = await result.Content.ReadFromJsonAsync<bool>();
Assert.True(ret);
}
}
}

View File

@ -5,6 +5,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Json;
using Xunit;
namespace Bootstrap.Admin.Api
@ -35,7 +36,7 @@ namespace Bootstrap.Admin.Api
public async void Get_Ok(string query, string order)
{
// 菜单 系统菜单 系统使用条件
var qd = await Client.GetAsJsonAsync<QueryData<object>>($"?sort={query}&order={order}&offset=0&limit=20&name=Admin&displayName=Administrator&_=1547628247338");
var qd = await Client.GetFromJsonAsync<QueryData<object>>($"?sort={query}&order={order}&offset=0&limit=20&name=Admin&displayName=Administrator&_=1547628247338");
Assert.Single(qd.rows);
}
@ -46,7 +47,7 @@ namespace Bootstrap.Admin.Api
public async void Search_Ok(string search)
{
// 菜单 系统菜单 系统使用条件
var qd = await Client.GetAsJsonAsync<QueryData<object>>($"?search={search}&sort=&order=&offset=0&limit=20&category=&name=&define=0&_=1547608210979");
var qd = await Client.GetFromJsonAsync<QueryData<object>>($"?search={search}&sort=&order=&offset=0&limit=20&category=&name=&define=0&_=1547608210979");
Assert.NotEmpty(qd.rows);
}
@ -56,15 +57,19 @@ namespace Bootstrap.Admin.Api
UserHelper.Delete(UserHelper.Retrieves().Where(usr => usr.UserName == "UnitTest_Delete").Select(usr => usr.Id));
var nusr = new User { UserName = "UnitTest_Delete", Password = "1", DisplayName = "DisplayName", ApprovedBy = "System", ApprovedTime = DateTime.Now, Description = "Desc", Icon = "default.jpg" };
var resp = await Client.PostAsJsonAsync<User, bool>("", nusr);
Assert.True(resp);
var resp = await Client.PostAsJsonAsync<User>("", nusr);
var ret = await resp.Content.ReadFromJsonAsync<bool>();
Assert.True(ret);
nusr.Id = UserHelper.Retrieves().First(u => u.UserName == nusr.UserName).Id;
resp = await Client.PostAsJsonAsync<User, bool>("", nusr);
Assert.True(resp);
resp = await Client.PostAsJsonAsync<User>("", nusr);
ret = await resp.Content.ReadFromJsonAsync<bool>();
Assert.True(ret);
var ids = UserHelper.Retrieves().Where(d => d.UserName == nusr.UserName).Select(d => d.Id);
Assert.True(await Client.DeleteAsJsonAsync<IEnumerable<string>, bool>("", ids));
var resp1 = await Client.DeleteAsJsonAsync<IEnumerable<string>>("", ids);
var ret1 = await resp1.Content.ReadFromJsonAsync<bool>();
Assert.True(ret1);
}
[Fact]
@ -72,11 +77,13 @@ namespace Bootstrap.Admin.Api
{
var rid = RoleHelper.Retrieves().Where(r => r.RoleName == "Administrators").First().Id;
var ret = await Client.PostAsJsonAsync<string, IEnumerable<object>>($"{rid}?type=role", string.Empty);
var resp = await Client.PostAsJsonAsync<string>($"{rid}?type=role", string.Empty);
var ret = await resp.Content.ReadFromJsonAsync<IEnumerable<object>>();
Assert.NotNull(ret);
var gid = GroupHelper.Retrieves().Where(r => r.GroupName == "Admin").First().Id;
ret = await Client.PostAsJsonAsync<string, IEnumerable<object>>($"{gid}?type=group", string.Empty);
resp = await Client.PostAsJsonAsync<string>($"{gid}?type=group", string.Empty);
ret = await resp.Content.ReadFromJsonAsync<IEnumerable<object>>();
Assert.NotNull(ret);
// 创建用户
@ -87,7 +94,8 @@ namespace Bootstrap.Admin.Api
UserHelper.ForgotPassword(new ResetUser() { DisplayName = nusr.DisplayName, Reason = "UnitTest", ResetTime = DateTime.Now, UserName = nusr.UserName });
// 重置操作
ret = await Client.PostAsJsonAsync<string, IEnumerable<object>>($"{nusr.UserName}?type=reset", string.Empty);
resp = await Client.PostAsJsonAsync<string>($"{nusr.UserName}?type=reset", string.Empty);
ret = await resp.Content.ReadFromJsonAsync<IEnumerable<object>>();
Assert.NotNull(ret);
UserHelper.Delete(UserHelper.Retrieves().Where(usr => usr.UserName == nusr.UserName).Select(usr => usr.Id));
@ -98,11 +106,13 @@ namespace Bootstrap.Admin.Api
{
var ids = UserHelper.Retrieves().Where(u => u.UserName == "Admin").Select(u => u.Id);
var gid = GroupHelper.Retrieves().Where(r => r.GroupName == "Admin").First().Id;
var ret = await Client.PutAsJsonAsync<IEnumerable<string>, bool>($"{gid}?type=group", ids);
var resp = await Client.PutAsJsonAsync<IEnumerable<string>>($"{gid}?type=group", ids);
var ret = await resp.Content.ReadFromJsonAsync<bool>();
Assert.True(ret);
var rid = RoleHelper.Retrieves().Where(r => r.RoleName == "Administrators").First().Id;
ret = await Client.PutAsJsonAsync<IEnumerable<string>, bool>($"{rid}?type=role", ids);
resp = await Client.PutAsJsonAsync<IEnumerable<string>>($"{rid}?type=role", ids);
ret = await resp.Content.ReadFromJsonAsync<bool>();
Assert.True(ret);
}
}

View File

@ -1,4 +1,7 @@
using System.Net.Http;
using Microsoft.Extensions.DependencyInjection;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.Json;
using System.Threading.Tasks;
namespace Bootstrap.Admin
@ -34,5 +37,25 @@ namespace Bootstrap.Admin
};
await client.PostAsync("/Account/Login", content);
}
/// <summary>
///
/// </summary>
/// <typeparam name="TValue"></typeparam>
/// <param name="client"></param>
/// <param name="requestUri"></param>
/// <param name="value"></param>
/// <returns></returns>
public static async Task<HttpResponseMessage> DeleteAsJsonAsync<TValue>(this HttpClient client, string requestUri, TValue value)
{
var options = new JsonSerializerOptions().AddDefaultConverters();
var req = new HttpRequestMessage(HttpMethod.Delete, requestUri)
{
Content = new StringContent(JsonSerializer.Serialize(value, options))
};
req.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
return await client.SendAsync(req);
}
}
}

View File

@ -186,11 +186,11 @@ namespace Bootstrap.DataAccess
using var client = new HttpClient();
// 日本东京
var locator = await client.GetAsJsonAsync<BaiDuIPLocator>($"{ipUri}207.148.111.94");
var locator = await client.GetFromJsonAsync<BaiDuIPLocator>($"{ipUri}207.148.111.94");
Assert.NotEqual(0, locator.Status);
// 四川成都
locator = await client.GetAsJsonAsync<BaiDuIPLocator>($"{ipUri}182.148.123.196");
locator = await client.GetFromJsonAsync<BaiDuIPLocator>($"{ipUri}182.148.123.196");
Assert.Equal(0, locator.Status);
}
@ -201,11 +201,11 @@ namespace Bootstrap.DataAccess
// 日本东京
using var client = new HttpClient();
var locator = await client.GetAsJsonAsync<JuheIPLocator>($"{ipUri}207.148.111.94");
var locator = await client.GetFromJsonAsync<JuheIPLocator>($"{ipUri}207.148.111.94");
Assert.Contains(new int[] { 0, 10012 }, c => c == locator.Error_Code);
// 四川成都
locator = await client.GetAsJsonAsync<JuheIPLocator>($"{ipUri}182.148.123.196");
locator = await client.GetFromJsonAsync<JuheIPLocator>($"{ipUri}182.148.123.196");
Assert.Contains(new int[] { 0, 10012 }, c => c == locator.Error_Code);
}
@ -222,7 +222,7 @@ namespace Bootstrap.DataAccess
Assert.Equal("0", locator.Status);
// 四川成都
locator = await client.GetAsJsonAsync<BaiduIP138Locator>($"{ipUri}182.148.123.196");
locator = await client.GetFromJsonAsync<BaiduIP138Locator>($"{ipUri}182.148.123.196");
Assert.Equal("0", locator.Status);
}