172 lines
6.8 KiB
C#
172 lines
6.8 KiB
C#
using System.Net;
|
|
using System.Net.Http.Headers;
|
|
using System.Net.Http.Json;
|
|
using CampusErrand.Data;
|
|
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 3: 订单支付金额计算
|
|
/// 对任意包含商品总金额和跑腿佣金的订单(万能帮、代购、美食街类型),
|
|
/// 支付总金额应等于商品总金额加上跑腿佣金。
|
|
/// **Feature: login-and-homepage, Property 3: 订单支付金额计算**
|
|
///
|
|
/// </summary>
|
|
public class OrderTotalAmountPropertyTests : 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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 属性:万能帮订单的支付总金额 = 商品总金额 + 跑腿佣金
|
|
/// </summary>
|
|
[Property(MaxTest = 20)]
|
|
public bool 万能帮订单支付总金额等于商品金额加佣金(PositiveInt goodsSeed, PositiveInt commSeed)
|
|
{
|
|
var goodsAmount = Math.Round(1.0m + (goodsSeed.Get % 500) * 0.1m, 1);
|
|
var commission = Math.Round(1.0m + (commSeed.Get % 100) * 0.1m, 1);
|
|
|
|
var dbName = $"total_help_{Guid.NewGuid()}";
|
|
using var factory = CreateFactory(dbName);
|
|
var client = factory.CreateClient();
|
|
client.DefaultRequestHeaders.Authorization =
|
|
new AuthenticationHeaderValue("Bearer", GenerateUserToken());
|
|
|
|
var request = new CreateOrderRequest
|
|
{
|
|
OrderType = "Help",
|
|
ItemName = "帮忙买东西",
|
|
DeliveryLocation = "宿舍楼",
|
|
Phone = "13800138000",
|
|
Commission = commission,
|
|
GoodsAmount = goodsAmount
|
|
};
|
|
|
|
var response = client.PostAsJsonAsync("/api/orders", request).Result;
|
|
if (response.StatusCode != HttpStatusCode.Created) return false;
|
|
|
|
var order = response.Content.ReadFromJsonAsync<OrderResponse>().Result!;
|
|
return order.TotalAmount == goodsAmount + commission;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 属性:代购订单的支付总金额 = 商品总金额 + 跑腿佣金
|
|
/// </summary>
|
|
[Property(MaxTest = 20)]
|
|
public bool 代购订单支付总金额等于商品金额加佣金(PositiveInt goodsSeed, PositiveInt commSeed)
|
|
{
|
|
var goodsAmount = Math.Round(1.0m + (goodsSeed.Get % 500) * 0.1m, 1);
|
|
var commission = Math.Round(1.0m + (commSeed.Get % 100) * 0.1m, 1);
|
|
|
|
var dbName = $"total_purchase_{Guid.NewGuid()}";
|
|
using var factory = CreateFactory(dbName);
|
|
var client = factory.CreateClient();
|
|
client.DefaultRequestHeaders.Authorization =
|
|
new AuthenticationHeaderValue("Bearer", GenerateUserToken());
|
|
|
|
var request = new CreateOrderRequest
|
|
{
|
|
OrderType = "Purchase",
|
|
ItemName = "代购物品",
|
|
PickupLocation = "超市",
|
|
DeliveryLocation = "宿舍楼",
|
|
Phone = "13800138000",
|
|
Commission = commission,
|
|
GoodsAmount = goodsAmount
|
|
};
|
|
|
|
var response = client.PostAsJsonAsync("/api/orders", request).Result;
|
|
if (response.StatusCode != HttpStatusCode.Created) return false;
|
|
|
|
var order = response.Content.ReadFromJsonAsync<OrderResponse>().Result!;
|
|
return order.TotalAmount == goodsAmount + commission;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 属性:代取订单的支付总金额 = 跑腿佣金(纯佣金类)
|
|
/// </summary>
|
|
[Property(MaxTest = 20)]
|
|
public bool 代取订单支付总金额等于佣金(PositiveInt commSeed)
|
|
{
|
|
var commission = Math.Round(1.0m + (commSeed.Get % 100) * 0.1m, 1);
|
|
|
|
var dbName = $"total_pickup_{Guid.NewGuid()}";
|
|
using var factory = CreateFactory(dbName);
|
|
var client = factory.CreateClient();
|
|
client.DefaultRequestHeaders.Authorization =
|
|
new AuthenticationHeaderValue("Bearer", GenerateUserToken());
|
|
|
|
var request = new CreateOrderRequest
|
|
{
|
|
OrderType = "Pickup",
|
|
ItemName = "代取快递",
|
|
PickupLocation = "菜鸟驿站",
|
|
DeliveryLocation = "宿舍楼",
|
|
Phone = "13800138000",
|
|
Commission = commission
|
|
};
|
|
|
|
var response = client.PostAsJsonAsync("/api/orders", request).Result;
|
|
if (response.StatusCode != HttpStatusCode.Created) return false;
|
|
|
|
var order = response.Content.ReadFromJsonAsync<OrderResponse>().Result!;
|
|
return order.TotalAmount == commission;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
foreach (var f in _factories) f.Dispose();
|
|
_factories.Clear();
|
|
}
|
|
}
|