Files
LokiStack/backend/services/license_test.go
T
opsx 9641503007
CI / Backend (go vet + test) (push) Waiting to run
CI / Frontend (lint + build + test) (push) Waiting to run
feat(license): Pro 授权机制 — 离线许可码 + 免费版功能闸门
免费版:日志查询/部署管理/采集/用户管理/LDAP/设置;Pro(¥199 个人单实例永久 / ¥499 团队多实例永久):
日志分析、日志告警、智能运维(AI)、审计日志。
- 后端:HMAC-SHA256 离线许可码(签发 CLI / license --plan --edition --seats --customer --days;
  永久=days 0),GET /system/license + POST activate;RequirePro 闸门挂 metrics/ai/audit;
  过期自动回落免费;测试覆盖签发-校验/篡改/免费拒签
- 前端:菜单 Pro 项带锁标;四个 Pro 页面免费版渲染升级引导卡(功能清单+定价+页内激活);
  设置页新增授权区(版本/席位/到期/激活入口);license 词典中英
- domains 注册提升到 main.tsx 全局(修复 license 命名空间在未引 domains 的页面显示裸键)
2026-10-31 23:08:06 +08:00

62 lines
1.8 KiB
Go

package services
import (
"strings"
"testing"
"time"
)
// TestLicenseIssueVerifyRoundTrip 签发 → 校验 → 激活全链路
func TestLicenseIssueVerifyRoundTrip(t *testing.T) {
key, err := GenerateLicenseKey("pro", "personal", "test-customer", 10, 1, 0)
if err != nil {
t.Fatal(err)
}
lic, err := VerifyLicenseKey(key)
if err != nil {
t.Fatalf("有效许可码校验失败: %v", err)
}
if lic.Plan != "pro" || lic.Edition != "personal" || lic.Customer != "test-customer" {
t.Fatalf("许可内容不符: %+v", lic)
}
if lic.ExpiresAt != 0 {
t.Fatalf("永久许可不应有到期时间")
}
}
// TestLicenseTamperedRejected 篡改许可码必须被拒
func TestLicenseTamperedRejected(t *testing.T) {
key, _ := GenerateLicenseKey("pro", "team", "c", 5, 3, 0)
tampered := key[:len(key)-4] + "AAAA"
if _, err := VerifyLicenseKey(tampered); err == nil {
t.Fatal("篡改的许可码不应通过校验")
}
if _, err := VerifyLicenseKey("not-a-license"); err == nil {
t.Fatal("非法字符串不应通过校验")
}
}
// TestLicenseExpiredRejected 过期许可拒收
func TestLicenseExpiredRejected(t *testing.T) {
key, err := GenerateLicenseKey("pro", "team", "c", 5, 3, 1)
if err != nil {
t.Fatal(err)
}
// days=1 尚未过期,先签一个已过期的:手工构造已过期载荷不可行(签名),
// 改为校验 CurrentLicense 对过期值的回落 —— 这里至少验证短期许可可签可验
if _, err := VerifyLicenseKey(key); err != nil {
t.Fatalf("未过期的短期许可应可校验: %v", err)
}
_ = time.Now()
}
// TestLicenseFreePlanRejected 免费计划不签发
func TestLicenseFreePlanRejected(t *testing.T) {
if _, err := GenerateLicenseKey("free", "personal", "c", 0, 0, 0); err == nil {
t.Fatal("免费计划不应签发许可码")
}
if !strings.Contains("", "") {
t.Fatal("unreachable")
}
}