280 lines
9.8 KiB
C#
280 lines
9.8 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 AdminOrderControllerIntegrationTests : IClassFixture<WebApplicationFactory<IAdminApiMarker>>
|
|
{
|
|
private readonly WebApplicationFactory<IAdminApiMarker> _factory;
|
|
private readonly IAdminOrderService _mockAdminOrderService;
|
|
|
|
public AdminOrderControllerIntegrationTests(WebApplicationFactory<IAdminApiMarker> factory)
|
|
{
|
|
_mockAdminOrderService = Substitute.For<IAdminOrderService>();
|
|
|
|
_factory = factory.WithWebHostBuilder(builder =>
|
|
{
|
|
builder.UseEnvironment("Testing");
|
|
builder.ConfigureServices(services =>
|
|
{
|
|
services.RemoveAll<IAdminOrderService>();
|
|
services.AddSingleton(_mockAdminOrderService);
|
|
|
|
services.AddAuthentication(options =>
|
|
{
|
|
options.DefaultAuthenticateScheme = AdminTestAuthHandler.AuthenticationScheme;
|
|
options.DefaultChallengeScheme = AdminTestAuthHandler.AuthenticationScheme;
|
|
})
|
|
.AddScheme<AuthenticationSchemeOptions, AdminTestAuthHandler>(
|
|
AdminTestAuthHandler.AuthenticationScheme, options => { });
|
|
});
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// 测试获取订单列表 - 未授权返回401
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task GetOrderList_WithoutAuth_ReturnsUnauthorized()
|
|
{
|
|
var client = _factory.CreateClient();
|
|
var response = await client.GetAsync("/api/admin/orders");
|
|
Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 测试获取订单列表 - 授权后成功
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task GetOrderList_WithAuth_ReturnsSuccess()
|
|
{
|
|
var expectedResult = new PagedResult<AdminOrderListDto>
|
|
{
|
|
Items = new List<AdminOrderListDto>
|
|
{
|
|
new AdminOrderListDto
|
|
{
|
|
OrderId = 1,
|
|
OrderNo = "ORD202312310001",
|
|
UserId = 100,
|
|
XiangQinNo = "123456",
|
|
Nickname = "测试用户",
|
|
OrderType = 1,
|
|
OrderTypeText = "会员",
|
|
ProductName = "不限时会员",
|
|
Amount = 1299,
|
|
PayAmount = 1299,
|
|
Status = 2,
|
|
StatusText = "已支付",
|
|
PayTime = DateTime.Now.AddHours(-1),
|
|
CreateTime = DateTime.Now.AddHours(-2)
|
|
}
|
|
},
|
|
Total = 1,
|
|
PageIndex = 1,
|
|
PageSize = 20
|
|
};
|
|
|
|
_mockAdminOrderService.GetOrderListAsync(Arg.Any<AdminOrderQueryRequest>())
|
|
.Returns(Task.FromResult(expectedResult));
|
|
|
|
var client = _factory.CreateClient();
|
|
client.DefaultRequestHeaders.Add("Authorization", "Bearer admin-token-1");
|
|
|
|
var response = await client.GetAsync("/api/admin/orders");
|
|
|
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
|
|
|
var content = await response.Content.ReadAsStringAsync();
|
|
var result = JsonSerializer.Deserialize<ApiResponse<PagedResult<AdminOrderListDto>>>(content,
|
|
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Equal(0, result.Code);
|
|
Assert.NotNull(result.Data);
|
|
Assert.Single(result.Data.Items);
|
|
Assert.Equal("ORD202312310001", result.Data.Items[0].OrderNo);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 测试获取订单详情 - 成功
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task GetOrderDetail_WithAuth_ReturnsSuccess()
|
|
{
|
|
var expectedResult = new AdminOrderDetailDto
|
|
{
|
|
OrderId = 1,
|
|
OrderNo = "ORD202312310001",
|
|
UserId = 100,
|
|
XiangQinNo = "123456",
|
|
Nickname = "测试用户",
|
|
OrderType = 1,
|
|
OrderTypeText = "会员",
|
|
ProductName = "不限时会员",
|
|
Amount = 1299,
|
|
PayAmount = 1299,
|
|
Status = 2,
|
|
StatusText = "已支付",
|
|
PayType = 1,
|
|
PayTypeText = "微信支付",
|
|
TransactionId = "wx123456789"
|
|
};
|
|
|
|
_mockAdminOrderService.GetOrderDetailAsync(1)
|
|
.Returns(Task.FromResult(expectedResult));
|
|
|
|
var client = _factory.CreateClient();
|
|
client.DefaultRequestHeaders.Add("Authorization", "Bearer admin-token-1");
|
|
|
|
var response = await client.GetAsync("/api/admin/orders/1");
|
|
|
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
|
|
|
var content = await response.Content.ReadAsStringAsync();
|
|
var result = JsonSerializer.Deserialize<ApiResponse<AdminOrderDetailDto>>(content,
|
|
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Equal(0, result.Code);
|
|
Assert.NotNull(result.Data);
|
|
Assert.Equal(expectedResult.OrderId, result.Data.OrderId);
|
|
Assert.Equal(expectedResult.OrderNo, result.Data.OrderNo);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 测试订单退款 - 成功
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task RefundOrder_WithAuth_ReturnsSuccess()
|
|
{
|
|
var expectedResult = new AdminOrderRefundResponse
|
|
{
|
|
Success = true,
|
|
Message = "退款成功",
|
|
RefundId = "refund123456"
|
|
};
|
|
|
|
_mockAdminOrderService.RefundOrderAsync(1, Arg.Any<AdminOrderRefundRequest>(), 1)
|
|
.Returns(Task.FromResult(expectedResult));
|
|
|
|
var client = _factory.CreateClient();
|
|
client.DefaultRequestHeaders.Add("Authorization", "Bearer admin-token-1");
|
|
|
|
var request = new AdminOrderRefundRequest { Reason = "用户申请退款" };
|
|
|
|
var response = await client.PostAsJsonAsync("/api/admin/orders/1/refund", request);
|
|
|
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
|
|
|
var content = await response.Content.ReadAsStringAsync();
|
|
var result = JsonSerializer.Deserialize<ApiResponse<AdminOrderRefundResponse>>(content,
|
|
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Equal(0, result.Code);
|
|
Assert.NotNull(result.Data);
|
|
Assert.True(result.Data.Success);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 测试获取订单统计 - 成功
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task GetOrderStatistics_WithAuth_ReturnsSuccess()
|
|
{
|
|
var expectedResult = new AdminOrderStatisticsDto
|
|
{
|
|
TotalOrders = 1000,
|
|
TodayOrders = 50,
|
|
WeekOrders = 200,
|
|
MonthOrders = 800,
|
|
PaidOrders = 900,
|
|
TotalIncome = 500000,
|
|
TodayIncome = 25000,
|
|
WeekIncome = 100000,
|
|
MonthIncome = 400000,
|
|
RefundedOrders = 10,
|
|
RefundedAmount = 5000
|
|
};
|
|
|
|
_mockAdminOrderService.GetOrderStatisticsAsync()
|
|
.Returns(Task.FromResult(expectedResult));
|
|
|
|
var client = _factory.CreateClient();
|
|
client.DefaultRequestHeaders.Add("Authorization", "Bearer admin-token-1");
|
|
|
|
var response = await client.GetAsync("/api/admin/orders/statistics");
|
|
|
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
|
|
|
var content = await response.Content.ReadAsStringAsync();
|
|
var result = JsonSerializer.Deserialize<ApiResponse<AdminOrderStatisticsDto>>(content,
|
|
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Equal(0, result.Code);
|
|
Assert.NotNull(result.Data);
|
|
Assert.Equal(expectedResult.TotalOrders, result.Data.TotalOrders);
|
|
Assert.Equal(expectedResult.TotalIncome, result.Data.TotalIncome);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 测试订单列表按状态筛选 - 成功
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task GetOrderList_WithStatusFilter_ReturnsFilteredResults()
|
|
{
|
|
var expectedResult = new PagedResult<AdminOrderListDto>
|
|
{
|
|
Items = new List<AdminOrderListDto>
|
|
{
|
|
new AdminOrderListDto
|
|
{
|
|
OrderId = 1,
|
|
OrderNo = "ORD202312310001",
|
|
Status = 2,
|
|
StatusText = "已支付"
|
|
}
|
|
},
|
|
Total = 1,
|
|
PageIndex = 1,
|
|
PageSize = 20
|
|
};
|
|
|
|
_mockAdminOrderService.GetOrderListAsync(Arg.Is<AdminOrderQueryRequest>(r => r.Status == 2))
|
|
.Returns(Task.FromResult(expectedResult));
|
|
|
|
var client = _factory.CreateClient();
|
|
client.DefaultRequestHeaders.Add("Authorization", "Bearer admin-token-1");
|
|
|
|
var response = await client.GetAsync("/api/admin/orders?status=2");
|
|
|
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
|
|
|
var content = await response.Content.ReadAsStringAsync();
|
|
var result = JsonSerializer.Deserialize<ApiResponse<PagedResult<AdminOrderListDto>>>(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(2, item.Status));
|
|
}
|
|
}
|