138 lines
5.4 KiB
C#
138 lines
5.4 KiB
C#
using System.Net;
|
|
using System.Net.Http.Headers;
|
|
using System.Net.Http.Json;
|
|
using CampusErrand.Data;
|
|
using CampusErrand.Models;
|
|
using CampusErrand.Models.Dtos;
|
|
using FsCheck;
|
|
using FsCheck.Xunit;
|
|
using Microsoft.AspNetCore.Mvc.Testing;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace CampusErrand.Tests;
|
|
|
|
/// <summary>
|
|
/// Property 9: 订单大厅仅展示未接取订单
|
|
/// 对任意订单集合,订单大厅接口应仅返回 Status=Pending 的订单。
|
|
/// **Feature: login-and-homepage, Property 9: 订单大厅仅展示未接取订单**
|
|
///
|
|
/// </summary>
|
|
public class OrderHallFilterPropertyTests : IDisposable
|
|
{
|
|
private const string JwtSecret = "YourSuperSecretKeyForJwtTokenGeneration_AtLeast32Chars!";
|
|
private const string JwtIssuer = "CampusErrand";
|
|
private const string JwtAudience = "CampusErrandApp";
|
|
|
|
private readonly List<WebApplicationFactory<Program>> _factories = [];
|
|
|
|
private WebApplicationFactory<Program> CreateFactory(string dbName)
|
|
{
|
|
var factory = new WebApplicationFactory<Program>()
|
|
.WithWebHostBuilder(builder =>
|
|
{
|
|
builder.ConfigureServices(services =>
|
|
{
|
|
var efDescriptors = services
|
|
.Where(d =>
|
|
d.ServiceType.FullName?.Contains("EntityFrameworkCore") == true
|
|
|| d.ServiceType == typeof(DbContextOptions<AppDbContext>)
|
|
|| d.ServiceType == typeof(DbContextOptions)
|
|
|| d.ImplementationType?.FullName?.Contains("EntityFrameworkCore") == true)
|
|
.ToList();
|
|
foreach (var d in efDescriptors) services.Remove(d);
|
|
|
|
services.AddDbContext<AppDbContext>(options =>
|
|
options.UseInMemoryDatabase(dbName));
|
|
});
|
|
});
|
|
_factories.Add(factory);
|
|
return factory;
|
|
}
|
|
|
|
private static string GenerateUserToken(int userId = 1)
|
|
{
|
|
var key = new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(
|
|
System.Text.Encoding.UTF8.GetBytes(JwtSecret));
|
|
var credentials = new Microsoft.IdentityModel.Tokens.SigningCredentials(
|
|
key, Microsoft.IdentityModel.Tokens.SecurityAlgorithms.HmacSha256);
|
|
var claims = new[]
|
|
{
|
|
new System.Security.Claims.Claim(System.Security.Claims.ClaimTypes.NameIdentifier, userId.ToString()),
|
|
new System.Security.Claims.Claim(System.Security.Claims.ClaimTypes.Role, "User")
|
|
};
|
|
var token = new System.IdentityModel.Tokens.Jwt.JwtSecurityToken(
|
|
issuer: JwtIssuer, audience: JwtAudience, claims: claims,
|
|
expires: DateTime.UtcNow.AddHours(1), signingCredentials: credentials);
|
|
return new System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler().WriteToken(token);
|
|
}
|
|
|
|
private static readonly OrderStatus[] AllStatuses =
|
|
[OrderStatus.Pending, OrderStatus.InProgress, OrderStatus.WaitConfirm,
|
|
OrderStatus.Completed, OrderStatus.Cancelled, OrderStatus.Appealing];
|
|
|
|
/// <summary>
|
|
/// 属性:订单大厅接口仅返回 Status=Pending 的订单
|
|
/// </summary>
|
|
[Property(MaxTest = 20)]
|
|
public bool 订单大厅仅返回待接单状态订单(PositiveInt seed)
|
|
{
|
|
var dbName = $"hall_filter_{Guid.NewGuid()}";
|
|
using var factory = CreateFactory(dbName);
|
|
|
|
// 插入不同状态的订单
|
|
int expectedPendingCount;
|
|
using (var scope = factory.Services.CreateScope())
|
|
{
|
|
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
|
var rng = new Random(seed.Get);
|
|
var count = rng.Next(1, 10);
|
|
var pendingCount = 0;
|
|
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
var status = AllStatuses[rng.Next(AllStatuses.Length)];
|
|
if (status == OrderStatus.Pending) pendingCount++;
|
|
|
|
db.Orders.Add(new Order
|
|
{
|
|
OrderNo = $"ORD{Guid.NewGuid():N}"[..20],
|
|
OwnerId = 99, // 非当前用户
|
|
OrderType = OrderType.Pickup,
|
|
Status = status,
|
|
ItemName = $"物品{i}",
|
|
DeliveryLocation = "测试地点",
|
|
Phone = "13800138000",
|
|
Commission = 5.0m,
|
|
TotalAmount = 5.0m,
|
|
CreatedAt = DateTime.UtcNow
|
|
});
|
|
}
|
|
db.SaveChanges();
|
|
expectedPendingCount = pendingCount;
|
|
}
|
|
|
|
// 调用订单大厅接口
|
|
var client = factory.CreateClient();
|
|
client.DefaultRequestHeaders.Authorization =
|
|
new AuthenticationHeaderValue("Bearer", GenerateUserToken());
|
|
|
|
var response = client.GetAsync("/api/orders/hall").Result;
|
|
if (response.StatusCode != HttpStatusCode.OK) return false;
|
|
|
|
var orders = response.Content.ReadFromJsonAsync<List<OrderHallItemResponse>>().Result!;
|
|
|
|
// 验证:所有返回的订单状态都是 Pending
|
|
if (orders.Any(o => o.Status != "Pending")) return false;
|
|
|
|
// 验证:返回数量等于 Pending 状态的订单数
|
|
return orders.Count == expectedPendingCount;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
foreach (var f in _factories) f.Dispose();
|
|
_factories.Clear();
|
|
}
|
|
}
|