135 lines
5.3 KiB
C#
135 lines
5.3 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 CampusErrand.Services;
|
|
using FsCheck;
|
|
using FsCheck.Xunit;
|
|
using Microsoft.AspNetCore.Mvc.Testing;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace CampusErrand.Tests;
|
|
|
|
/// <summary>
|
|
/// Property 5: Banner 列表排序与过滤
|
|
/// 对任意 Banner 数据集合,前端获取接口应仅返回 IsEnabled=true 的记录,且按 SortOrder 升序排列。
|
|
/// **Feature: login-and-homepage, Property 5: Banner 列表排序与过滤**
|
|
///
|
|
/// </summary>
|
|
public class BannerListPropertyTests : IDisposable
|
|
{
|
|
private const string JwtSecret = "YourSuperSecretKeyForJwtTokenGeneration_AtLeast32Chars!";
|
|
private const string JwtIssuer = "CampusErrand";
|
|
private const string JwtAudience = "CampusErrandApp";
|
|
|
|
private readonly List<WebApplicationFactory<Program>> _factories = [];
|
|
|
|
/// <summary>
|
|
/// 创建使用内存数据库的测试工厂
|
|
/// </summary>
|
|
private WebApplicationFactory<Program> CreateFactory(string dbName)
|
|
{
|
|
var factory = new WebApplicationFactory<Program>()
|
|
.WithWebHostBuilder(builder =>
|
|
{
|
|
builder.ConfigureServices(services =>
|
|
{
|
|
// 移除 EF Core 相关服务
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 生成管理员 JWT
|
|
/// </summary>
|
|
private static string GenerateAdminToken()
|
|
{
|
|
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, "1"),
|
|
new System.Security.Claims.Claim(System.Security.Claims.ClaimTypes.Role, "Admin")
|
|
};
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 属性:前端 Banner 接口仅返回已启用的记录,且按 SortOrder 升序排列
|
|
/// </summary>
|
|
[Property(MaxTest = 20)]
|
|
public bool 前端Banner接口仅返回启用记录且按排序权重升序(NonEmptyArray<byte> sortOrders, NonEmptyArray<bool> enabledFlags)
|
|
{
|
|
var dbName = $"banner_list_{Guid.NewGuid()}";
|
|
using var factory = CreateFactory(dbName);
|
|
|
|
// 准备测试数据:通过管理员接口插入 Banner
|
|
using (var scope = factory.Services.CreateScope())
|
|
{
|
|
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
|
var count = Math.Min(sortOrders.Get.Length, enabledFlags.Get.Length);
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
db.Banners.Add(new Banner
|
|
{
|
|
ImageUrl = $"https://img.example.com/{i}.png",
|
|
LinkType = LinkType.External,
|
|
LinkUrl = $"https://example.com/{i}",
|
|
SortOrder = sortOrders.Get[i],
|
|
IsEnabled = enabledFlags.Get[i],
|
|
CreatedAt = DateTime.UtcNow
|
|
});
|
|
}
|
|
db.SaveChanges();
|
|
}
|
|
|
|
// 调用前端接口
|
|
var client = factory.CreateClient();
|
|
var response = client.GetAsync("/api/banners").Result;
|
|
if (response.StatusCode != HttpStatusCode.OK) return false;
|
|
|
|
var banners = response.Content.ReadFromJsonAsync<List<BannerResponse>>().Result!;
|
|
|
|
// 验证:所有返回的 Banner 都是启用的
|
|
if (banners.Any(b => !b.IsEnabled)) return false;
|
|
|
|
// 验证:按 SortOrder 升序排列
|
|
for (int i = 1; i < banners.Count; i++)
|
|
{
|
|
if (banners[i].SortOrder < banners[i - 1].SortOrder) return false;
|
|
}
|
|
|
|
// 验证:返回数量等于启用的 Banner 数量
|
|
var count2 = Math.Min(sortOrders.Get.Length, enabledFlags.Get.Length);
|
|
var expectedEnabledCount = enabledFlags.Get.Take(count2).Count(f => f);
|
|
return banners.Count == expectedEnabledCount;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
foreach (var f in _factories) f.Dispose();
|
|
_factories.Clear();
|
|
}
|
|
}
|