282 lines
9.3 KiB
C#
282 lines
9.3 KiB
C#
using System.Net;
|
|
using System.Net.Http.Json;
|
|
using System.Text.Json;
|
|
using Microsoft.AspNetCore.Authentication;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Mvc.Testing;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
|
using NSubstitute;
|
|
using Xunit;
|
|
using XiangYi.AdminApi;
|
|
using XiangYi.Application.DTOs.Requests;
|
|
using XiangYi.Application.DTOs.Responses;
|
|
using XiangYi.Application.Interfaces;
|
|
|
|
namespace XiangYi.Api.Tests.AdminApi;
|
|
|
|
/// <summary>
|
|
/// 后台用户管理控制器集成测试
|
|
/// </summary>
|
|
public class AdminUserControllerIntegrationTests : IClassFixture<WebApplicationFactory<IAdminApiMarker>>
|
|
{
|
|
private readonly WebApplicationFactory<IAdminApiMarker> _factory;
|
|
private readonly IAdminUserService _mockAdminUserService;
|
|
|
|
public AdminUserControllerIntegrationTests(WebApplicationFactory<IAdminApiMarker> factory)
|
|
{
|
|
_mockAdminUserService = Substitute.For<IAdminUserService>();
|
|
|
|
_factory = factory.WithWebHostBuilder(builder =>
|
|
{
|
|
builder.UseEnvironment("Testing");
|
|
builder.ConfigureServices(services =>
|
|
{
|
|
services.RemoveAll<IAdminUserService>();
|
|
services.AddSingleton(_mockAdminUserService);
|
|
|
|
services.AddAuthentication(options =>
|
|
{
|
|
options.DefaultAuthenticateScheme = AdminTestAuthHandler.AuthenticationScheme;
|
|
options.DefaultChallengeScheme = AdminTestAuthHandler.AuthenticationScheme;
|
|
})
|
|
.AddScheme<AuthenticationSchemeOptions, AdminTestAuthHandler>(
|
|
AdminTestAuthHandler.AuthenticationScheme, options => { });
|
|
});
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// 测试获取用户列表 - 未授权返回401
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task GetUserList_WithoutAuth_ReturnsUnauthorized()
|
|
{
|
|
// Arrange
|
|
var client = _factory.CreateClient();
|
|
|
|
// Act
|
|
var response = await client.GetAsync("/api/admin/users");
|
|
|
|
// Assert
|
|
Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 测试获取用户列表 - 授权后成功
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task GetUserList_WithAuth_ReturnsSuccess()
|
|
{
|
|
// Arrange
|
|
var expectedResult = new PagedResult<AdminUserListDto>
|
|
{
|
|
Items = new List<AdminUserListDto>
|
|
{
|
|
new AdminUserListDto
|
|
{
|
|
UserId = 1,
|
|
XiangQinNo = "123456",
|
|
Nickname = "测试用户",
|
|
Status = 1,
|
|
StatusText = "正常",
|
|
IsMember = false,
|
|
MemberLevel = 0
|
|
}
|
|
},
|
|
Total = 1,
|
|
PageIndex = 1,
|
|
PageSize = 20
|
|
};
|
|
|
|
_mockAdminUserService.GetUserListAsync(Arg.Any<AdminUserQueryRequest>())
|
|
.Returns(Task.FromResult(expectedResult));
|
|
|
|
var client = _factory.CreateClient();
|
|
client.DefaultRequestHeaders.Add("Authorization", "Bearer admin-token-1");
|
|
|
|
// Act
|
|
var response = await client.GetAsync("/api/admin/users?pageIndex=1&pageSize=20");
|
|
|
|
// Assert
|
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
|
|
|
var content = await response.Content.ReadAsStringAsync();
|
|
var result = JsonSerializer.Deserialize<ApiResponse<PagedResult<AdminUserListDto>>>(content,
|
|
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Equal(0, result.Code);
|
|
Assert.NotNull(result.Data);
|
|
Assert.Single(result.Data.Items);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 测试获取用户详情 - 成功
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task GetUserDetail_WithAuth_ReturnsSuccess()
|
|
{
|
|
// Arrange
|
|
var expectedResult = new AdminUserDetailDto
|
|
{
|
|
UserId = 1,
|
|
XiangQinNo = "123456",
|
|
Nickname = "测试用户",
|
|
Status = 1,
|
|
StatusText = "正常",
|
|
IsMember = false,
|
|
MemberLevel = 0,
|
|
Profile = new AdminUserProfileDto
|
|
{
|
|
ProfileId = 1,
|
|
Relationship = 1,
|
|
RelationshipText = "父亲",
|
|
Surname = "李"
|
|
}
|
|
};
|
|
|
|
_mockAdminUserService.GetUserDetailAsync(1)
|
|
.Returns(Task.FromResult(expectedResult));
|
|
|
|
var client = _factory.CreateClient();
|
|
client.DefaultRequestHeaders.Add("Authorization", "Bearer admin-token-1");
|
|
|
|
// Act
|
|
var response = await client.GetAsync("/api/admin/users/1");
|
|
|
|
// Assert
|
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
|
|
|
var content = await response.Content.ReadAsStringAsync();
|
|
var result = JsonSerializer.Deserialize<ApiResponse<AdminUserDetailDto>>(content,
|
|
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Equal(0, result.Code);
|
|
Assert.NotNull(result.Data);
|
|
Assert.Equal(expectedResult.UserId, result.Data.UserId);
|
|
Assert.NotNull(result.Data.Profile);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 测试更新用户状态 - 成功
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task UpdateUserStatus_WithAuth_ReturnsSuccess()
|
|
{
|
|
// Arrange
|
|
_mockAdminUserService.UpdateUserStatusAsync(1, 2, 1)
|
|
.Returns(Task.FromResult(true));
|
|
|
|
var client = _factory.CreateClient();
|
|
client.DefaultRequestHeaders.Add("Authorization", "Bearer admin-token-1");
|
|
|
|
var request = new UpdateUserStatusRequest { Status = 2 }; // 禁用
|
|
|
|
// Act
|
|
var response = await client.PutAsJsonAsync("/api/admin/users/1/status", request);
|
|
|
|
// Assert
|
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
|
|
|
var content = await response.Content.ReadAsStringAsync();
|
|
var result = JsonSerializer.Deserialize<ApiResponse>(content,
|
|
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Equal(0, result.Code);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 测试获取用户统计 - 成功
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task GetUserStatistics_WithAuth_ReturnsSuccess()
|
|
{
|
|
// Arrange
|
|
var expectedResult = new AdminUserStatisticsDto
|
|
{
|
|
TotalUsers = 1000,
|
|
TodayNewUsers = 50,
|
|
WeekNewUsers = 200,
|
|
MonthNewUsers = 800,
|
|
TotalMembers = 100,
|
|
TodayNewMembers = 5,
|
|
RealNameUsers = 80,
|
|
ProfileCompletedUsers = 900,
|
|
PendingAuditProfiles = 20,
|
|
TodayActiveUsers = 300,
|
|
DisabledUsers = 10
|
|
};
|
|
|
|
_mockAdminUserService.GetUserStatisticsAsync()
|
|
.Returns(Task.FromResult(expectedResult));
|
|
|
|
var client = _factory.CreateClient();
|
|
client.DefaultRequestHeaders.Add("Authorization", "Bearer admin-token-1");
|
|
|
|
// Act
|
|
var response = await client.GetAsync("/api/admin/users/statistics");
|
|
|
|
// Assert
|
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
|
|
|
var content = await response.Content.ReadAsStringAsync();
|
|
var result = JsonSerializer.Deserialize<ApiResponse<AdminUserStatisticsDto>>(content,
|
|
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Equal(0, result.Code);
|
|
Assert.NotNull(result.Data);
|
|
Assert.Equal(expectedResult.TotalUsers, result.Data.TotalUsers);
|
|
Assert.Equal(expectedResult.TodayNewUsers, result.Data.TodayNewUsers);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 测试用户列表搜索 - 按关键词搜索
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task GetUserList_WithKeyword_ReturnsFilteredResults()
|
|
{
|
|
// Arrange
|
|
var expectedResult = new PagedResult<AdminUserListDto>
|
|
{
|
|
Items = new List<AdminUserListDto>
|
|
{
|
|
new AdminUserListDto
|
|
{
|
|
UserId = 1,
|
|
XiangQinNo = "123456",
|
|
Nickname = "李家长",
|
|
Status = 1
|
|
}
|
|
},
|
|
Total = 1,
|
|
PageIndex = 1,
|
|
PageSize = 20
|
|
};
|
|
|
|
_mockAdminUserService.GetUserListAsync(Arg.Is<AdminUserQueryRequest>(r => r.Keyword == "李"))
|
|
.Returns(Task.FromResult(expectedResult));
|
|
|
|
var client = _factory.CreateClient();
|
|
client.DefaultRequestHeaders.Add("Authorization", "Bearer admin-token-1");
|
|
|
|
// Act
|
|
var response = await client.GetAsync("/api/admin/users?keyword=李");
|
|
|
|
// Assert
|
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
|
|
|
var content = await response.Content.ReadAsStringAsync();
|
|
var result = JsonSerializer.Deserialize<ApiResponse<PagedResult<AdminUserListDto>>>(content,
|
|
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Equal(0, result.Code);
|
|
Assert.NotNull(result.Data);
|
|
Assert.Single(result.Data.Items);
|
|
}
|
|
}
|