334 lines
12 KiB
C#
334 lines
12 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 AdminReportControllerIntegrationTests : IClassFixture<WebApplicationFactory<IAdminApiMarker>>
|
|
{
|
|
private readonly WebApplicationFactory<IAdminApiMarker> _factory;
|
|
private readonly IAdminReportService _mockAdminReportService;
|
|
|
|
public AdminReportControllerIntegrationTests(WebApplicationFactory<IAdminApiMarker> factory)
|
|
{
|
|
_mockAdminReportService = Substitute.For<IAdminReportService>();
|
|
|
|
_factory = factory.WithWebHostBuilder(builder =>
|
|
{
|
|
builder.UseEnvironment("Testing");
|
|
builder.ConfigureServices(services =>
|
|
{
|
|
services.RemoveAll<IAdminReportService>();
|
|
services.AddSingleton(_mockAdminReportService);
|
|
|
|
services.AddAuthentication(options =>
|
|
{
|
|
options.DefaultAuthenticateScheme = AdminTestAuthHandler.AuthenticationScheme;
|
|
options.DefaultChallengeScheme = AdminTestAuthHandler.AuthenticationScheme;
|
|
})
|
|
.AddScheme<AuthenticationSchemeOptions, AdminTestAuthHandler>(
|
|
AdminTestAuthHandler.AuthenticationScheme, options => { });
|
|
});
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// 测试获取举报列表 - 未授权返回401
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task GetReportList_WithoutAuth_ReturnsUnauthorized()
|
|
{
|
|
var client = _factory.CreateClient();
|
|
var response = await client.GetAsync("/api/admin/reports");
|
|
Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 测试获取举报列表 - 授权后成功
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task GetReportList_WithAuth_ReturnsSuccess()
|
|
{
|
|
var expectedResult = new PagedResult<AdminReportListDto>
|
|
{
|
|
Items = new List<AdminReportListDto>
|
|
{
|
|
new AdminReportListDto
|
|
{
|
|
ReportId = 1,
|
|
ReporterId = 100,
|
|
ReporterXiangQinNo = "123456",
|
|
ReporterNickname = "举报人",
|
|
ReportedUserId = 200,
|
|
ReportedUserXiangQinNo = "654321",
|
|
ReportedUserNickname = "被举报人",
|
|
ReportType = 1,
|
|
ReportTypeText = "用户",
|
|
Reason = "虚假资料",
|
|
Status = 0,
|
|
StatusText = "待处理",
|
|
CreateTime = DateTime.Now.AddHours(-1)
|
|
}
|
|
},
|
|
Total = 1,
|
|
PageIndex = 1,
|
|
PageSize = 20
|
|
};
|
|
|
|
_mockAdminReportService.GetReportListAsync(Arg.Any<AdminReportQueryRequest>())
|
|
.Returns(Task.FromResult(expectedResult));
|
|
|
|
var client = _factory.CreateClient();
|
|
client.DefaultRequestHeaders.Add("Authorization", "Bearer admin-token-1");
|
|
|
|
var response = await client.GetAsync("/api/admin/reports");
|
|
|
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
|
|
|
var content = await response.Content.ReadAsStringAsync();
|
|
var result = JsonSerializer.Deserialize<ApiResponse<PagedResult<AdminReportListDto>>>(content,
|
|
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Equal(0, result.Code);
|
|
Assert.NotNull(result.Data);
|
|
Assert.Single(result.Data.Items);
|
|
Assert.Equal("待处理", result.Data.Items[0].StatusText);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 测试获取举报详情 - 成功
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task GetReportDetail_WithAuth_ReturnsSuccess()
|
|
{
|
|
var expectedResult = new AdminReportDetailDto
|
|
{
|
|
ReportId = 1,
|
|
Reporter = new AdminReportUserDto
|
|
{
|
|
UserId = 100,
|
|
XiangQinNo = "123456",
|
|
Nickname = "举报人",
|
|
Status = 1,
|
|
StatusText = "正常"
|
|
},
|
|
ReportedUser = new AdminReportUserDto
|
|
{
|
|
UserId = 200,
|
|
XiangQinNo = "654321",
|
|
Nickname = "被举报人",
|
|
Status = 1,
|
|
StatusText = "正常"
|
|
},
|
|
ReportType = 1,
|
|
ReportTypeText = "用户",
|
|
Reason = "虚假资料",
|
|
Description = "该用户资料与实际不符",
|
|
EvidenceUrls = new List<string> { "https://example.com/evidence1.jpg" },
|
|
Status = 0,
|
|
StatusText = "待处理",
|
|
CreateTime = DateTime.Now.AddHours(-1)
|
|
};
|
|
|
|
_mockAdminReportService.GetReportDetailAsync(1)
|
|
.Returns(Task.FromResult<AdminReportDetailDto?>(expectedResult));
|
|
|
|
var client = _factory.CreateClient();
|
|
client.DefaultRequestHeaders.Add("Authorization", "Bearer admin-token-1");
|
|
|
|
var response = await client.GetAsync("/api/admin/reports/1");
|
|
|
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
|
|
|
var content = await response.Content.ReadAsStringAsync();
|
|
var result = JsonSerializer.Deserialize<ApiResponse<AdminReportDetailDto>>(content,
|
|
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Equal(0, result.Code);
|
|
Assert.NotNull(result.Data);
|
|
Assert.Equal(expectedResult.ReportId, result.Data.ReportId);
|
|
Assert.NotNull(result.Data.Reporter);
|
|
Assert.NotNull(result.Data.ReportedUser);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 测试处理举报 - 成功
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task HandleReport_WithAuth_ReturnsSuccess()
|
|
{
|
|
_mockAdminReportService.HandleReportAsync(1, 1, Arg.Any<AdminHandleReportRequest>())
|
|
.Returns(Task.FromResult(true));
|
|
|
|
var client = _factory.CreateClient();
|
|
client.DefaultRequestHeaders.Add("Authorization", "Bearer admin-token-1");
|
|
|
|
var request = new AdminHandleReportRequest
|
|
{
|
|
HandleResult = "已核实,对被举报人进行警告处理",
|
|
IsPunish = true,
|
|
PunishmentType = 1,
|
|
IsWarnReporter = false
|
|
};
|
|
|
|
var response = await client.PostAsJsonAsync("/api/admin/reports/1/handle", request);
|
|
|
|
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 IgnoreReport_WithAuth_ReturnsSuccess()
|
|
{
|
|
_mockAdminReportService.IgnoreReportAsync(1, 1, "证据不足")
|
|
.Returns(Task.FromResult(true));
|
|
|
|
var client = _factory.CreateClient();
|
|
client.DefaultRequestHeaders.Add("Authorization", "Bearer admin-token-1");
|
|
|
|
var response = await client.PostAsJsonAsync("/api/admin/reports/1/ignore", "证据不足");
|
|
|
|
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 HandleReport_WithMutePunishment_ReturnsSuccess()
|
|
{
|
|
_mockAdminReportService.HandleReportAsync(1, 1, Arg.Any<AdminHandleReportRequest>())
|
|
.Returns(Task.FromResult(true));
|
|
|
|
var client = _factory.CreateClient();
|
|
client.DefaultRequestHeaders.Add("Authorization", "Bearer admin-token-1");
|
|
|
|
var request = new AdminHandleReportRequest
|
|
{
|
|
HandleResult = "已核实,对被举报人进行禁言处理",
|
|
IsPunish = true,
|
|
PunishmentType = 2,
|
|
PunishmentDays = 7,
|
|
IsWarnReporter = false
|
|
};
|
|
|
|
var response = await client.PostAsJsonAsync("/api/admin/reports/1/handle", request);
|
|
|
|
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 HandleReport_WithReporterWarning_ReturnsSuccess()
|
|
{
|
|
_mockAdminReportService.HandleReportAsync(1, 1, Arg.Any<AdminHandleReportRequest>())
|
|
.Returns(Task.FromResult(true));
|
|
|
|
var client = _factory.CreateClient();
|
|
client.DefaultRequestHeaders.Add("Authorization", "Bearer admin-token-1");
|
|
|
|
var request = new AdminHandleReportRequest
|
|
{
|
|
HandleResult = "经核实为恶意举报",
|
|
IsPunish = false,
|
|
IsWarnReporter = true,
|
|
ReporterWarningReason = "恶意举报,请勿滥用举报功能"
|
|
};
|
|
|
|
var response = await client.PostAsJsonAsync("/api/admin/reports/1/handle", request);
|
|
|
|
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 GetReportList_WithStatusFilter_ReturnsFilteredResults()
|
|
{
|
|
var expectedResult = new PagedResult<AdminReportListDto>
|
|
{
|
|
Items = new List<AdminReportListDto>
|
|
{
|
|
new AdminReportListDto
|
|
{
|
|
ReportId = 1,
|
|
Status = 0,
|
|
StatusText = "待处理"
|
|
}
|
|
},
|
|
Total = 1,
|
|
PageIndex = 1,
|
|
PageSize = 20
|
|
};
|
|
|
|
_mockAdminReportService.GetReportListAsync(Arg.Is<AdminReportQueryRequest>(r => r.Status == 0))
|
|
.Returns(Task.FromResult(expectedResult));
|
|
|
|
var client = _factory.CreateClient();
|
|
client.DefaultRequestHeaders.Add("Authorization", "Bearer admin-token-1");
|
|
|
|
var response = await client.GetAsync("/api/admin/reports?status=0");
|
|
|
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
|
|
|
var content = await response.Content.ReadAsStringAsync();
|
|
var result = JsonSerializer.Deserialize<ApiResponse<PagedResult<AdminReportListDto>>>(content,
|
|
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Equal(0, result.Code);
|
|
Assert.NotNull(result.Data);
|
|
Assert.All(result.Data.Items, item => Assert.Equal(0, item.Status));
|
|
}
|
|
}
|