Files
jiu/backend/internal/middleware/license_guard_test.go
T
wangjia 13fc9b7aaf test(backend): 授权系统端到端验证测试 (21H)
util/license_key_test.go:
- TestLicenseKeyRoundTrip: 签发→验签完整流程
- TestVerifyLicenseToken_TamperedPayload: 签名被篡改后被拒绝
- TestVerifyLicenseToken_WrongKey: 用错误公钥验签失败
- TestVerifyLicenseToken_InvalidFormat: 格式非法被拒绝

middleware/license_guard_test.go:
- TestCalcLicensePhase: 覆盖全部 phase 边界
  (perpetual→normal / future→normal / <7d→grace /
   7-15d→readonly / >15d→locked)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-10 07:10:15 +08:00

40 lines
1.0 KiB
Go

package middleware
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestCalcLicensePhase(t *testing.T) {
now := time.Now()
// nil = perpetual → normal
assert.Equal(t, PhaseNormal, CalcLicensePhase(nil))
// future expiry → normal
future := now.Add(10 * 24 * time.Hour)
assert.Equal(t, PhaseNormal, CalcLicensePhase(&future))
// just expired (1h ago) → grace
grace := now.Add(-1 * time.Hour)
assert.Equal(t, PhaseGrace, CalcLicensePhase(&grace))
// expired 6 days ago → grace (boundary)
grace6d := now.Add(-6 * 24 * time.Hour)
assert.Equal(t, PhaseGrace, CalcLicensePhase(&grace6d))
// expired 8 days ago → readonly
readonly := now.Add(-8 * 24 * time.Hour)
assert.Equal(t, PhaseReadOnly, CalcLicensePhase(&readonly))
// expired 14 days ago → readonly (boundary)
readonly14d := now.Add(-14 * 24 * time.Hour)
assert.Equal(t, PhaseReadOnly, CalcLicensePhase(&readonly14d))
// expired 16 days ago → locked
locked := now.Add(-16 * 24 * time.Hour)
assert.Equal(t, PhaseLocked, CalcLicensePhase(&locked))
}