feat(server): 设备上限登录闸(超限非硬拒登,返回 device_limit 信号)#16
启用设备数量限制的服务端部分。登录照常成功签发 token(非硬拒登),但若账户活跃 设备数超套餐上限,登录响应带 device_limit 信号,客户端据此弹「移除设备」页。 - devices/store.go:CountActiveDevices(last_seen 近 staleWindow)+ PruneStaleDevices (删超期僵尸行,免费版重装 churn 自愈) - devices/service.go:staleWindow=30d;DeviceLimitStatus + CheckDeviceLimit (best-effort prune → ResolvePlan → 活跃 count > cap 即 Over,附活跃设备列表) - auth:DeviceRegistrar 加 CheckDeviceLimit;recordLogin 回传 *DeviceLimit; LoginOutcome.DeviceLimit;Login 透传;handler tokenPairResponse.device_limit(omitempty) - main.go:authDeviceRegistrar 适配 devices.CheckDeviceLimit → auth.DeviceLimit - 测试:auth 登录透传超限信号(仍签发 token);devices within/over/prune-stale 连接侧 backstop(服务端硬拦)本轮从简未做,作为后续硬化(登录闸为客户端可信信号)。 DB 无 schema 变更。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -265,6 +265,55 @@ func TestDeviceLimitEnforced(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestCheckDeviceLimit covers the login-gate helper: within-cap, over-cap (active
|
||||
// devices exceed the plan cap), and stale-prune (churned rows dropped + excluded).
|
||||
func TestCheckDeviceLimit(t *testing.T) {
|
||||
db := setupMySQL(t)
|
||||
svc := devices.NewService(devices.NewStore(db), nil)
|
||||
ctx := context.Background()
|
||||
userID := createUser(t, db, "cap@example.com", "active") // free plan, cap 1
|
||||
|
||||
// 1 device → within cap.
|
||||
d1 := newUUID(t, db)
|
||||
if _, _, e := svc.RegisterIfAbsent(ctx, devices.RegisterInput{UserID: userID, DeviceUUID: d1, Name: "P1", Platform: "android"}); e != nil {
|
||||
t.Fatalf("register d1: %v", e)
|
||||
}
|
||||
st, apiErr := svc.CheckDeviceLimit(ctx, userID)
|
||||
if apiErr != nil {
|
||||
t.Fatalf("CheckDeviceLimit: %v", apiErr)
|
||||
}
|
||||
if st.Over || st.MaxDevices != 1 {
|
||||
t.Fatalf("1 device should be within cap 1, got %+v", st)
|
||||
}
|
||||
|
||||
// 2nd device (MaxDevices:0 bypasses the register-time cap) → over cap.
|
||||
d2 := newUUID(t, db)
|
||||
if _, _, e := svc.RegisterIfAbsent(ctx, devices.RegisterInput{UserID: userID, DeviceUUID: d2, Name: "P2", Platform: "ios"}); e != nil {
|
||||
t.Fatalf("register d2: %v", e)
|
||||
}
|
||||
st, _ = svc.CheckDeviceLimit(ctx, userID)
|
||||
if !st.Over || len(st.Devices) != 2 {
|
||||
t.Fatalf("2 devices should exceed cap 1, got %+v", st)
|
||||
}
|
||||
|
||||
// Age d1 past staleWindow → pruned + excluded → back within cap.
|
||||
if _, err := db.Exec(`UPDATE devices SET last_seen=? WHERE uuid=?`,
|
||||
time.Now().UTC().Add(-40*24*time.Hour), d1); err != nil {
|
||||
t.Fatalf("age d1: %v", err)
|
||||
}
|
||||
st, _ = svc.CheckDeviceLimit(ctx, userID)
|
||||
if st.Over {
|
||||
t.Fatalf("stale device should be pruned/excluded → within cap, got %+v", st)
|
||||
}
|
||||
var n int
|
||||
if err := db.QueryRow(`SELECT COUNT(1) FROM devices WHERE uuid=?`, d1).Scan(&n); err != nil {
|
||||
t.Fatalf("count d1: %v", err)
|
||||
}
|
||||
if n != 0 {
|
||||
t.Errorf("stale device should be pruned, still present")
|
||||
}
|
||||
}
|
||||
|
||||
// TestDeleteOthersDevice verifies ownership enforcement and not-found handling.
|
||||
func TestDeleteOthersDevice(t *testing.T) {
|
||||
db := setupMySQL(t)
|
||||
|
||||
Reference in New Issue
Block a user