vending-machine/backend/tests/VendingMachine.Tests/UserAuthPropertyTests.cs
2026-04-03 06:07:13 +08:00

49 lines
1.6 KiB
C#

using FsCheck;
using FsCheck.Xunit;
using VendingMachine.Application.Services;
using VendingMachine.Application.DTOs.User;
namespace VendingMachine.Tests;
/// <summary>
/// 用户认证模块属性测试
/// </summary>
public class UserAuthPropertyTests
{
// Feature: vending-machine-app, Property 1: 默认昵称格式
// 对于任意新注册用户,其默认昵称应匹配格式 "用户" + 6位数字
// **Validates: Requirements 1.6**
[Property(MaxTest = 100)]
public bool DefaultNickname_ShouldMatchExpectedFormat()
{
var nickname = IUserService.GenerateDefaultNickname();
// 昵称必须以 "用户" 开头
if (!nickname.StartsWith("用户"))
return false;
// "用户" 后面必须恰好是6位数字
var digits = nickname["用户".Length..];
return digits.Length == 6 && digits.All(char.IsDigit);
}
// Feature: vending-machine-app, Property 2: 协议未勾选阻止登录
// 对于任意登录请求,如果用户未勾选同意协议,登录操作应被阻止
// **Validates: Requirements 1.4**
[Property(MaxTest = 100)]
public bool AgreementNotAccepted_ShouldBlockLogin(string phone, string areaCode, string code)
{
var request = new LoginRequest
{
Phone = phone ?? "",
AreaCode = areaCode ?? "",
Code = code ?? "",
AgreementAccepted = false // 未勾选协议
};
// 协议未勾选时,请求的 AgreementAccepted 必须为 false
// 这保证了在服务层检查时会被阻止
return !request.AgreementAccepted;
}
}