fix(server): devices 唯一键改 (user_id,uuid) —— 同机换账号不再 403 死结(F3,#27)
device_id 按安装持久、跨账号复用;旧全局 UNIQUE(uuid) 使同机第二账号注册永远 403 → ConnectNode 判 DEVICE_NOT_REGISTERED,提示的「重新登录」无法自救。 - migration 21(sqlite/mysql):UNIQUE(uuid)→UNIQUE(user_id,uuid);platform 放行 linux(normalizePlatform 早已接受,旧 CHECK/ENUM 会拒)。SQLite 表重建用 rename→重建→复制→drop 次序,单事务内不触发 sessions 的级联清空(FK ON)。 - 查找全部收口为按 (user,uuid) 作用域(重复 uuid 跨用户后全局查询歧义): findDeviceByUserUUIDTx / FindByUserUUID;Register 删跨用户 Forbidden 分支; Delete/ForceLogout/Rename 对他人设备返回 404(不可见);SessionActive 删 「非本人 fail-safe」分支,dev==nil→false 语义不变。 - 测试:SQLite 真迁移库 F3 回归(两账号同 uuid 各自成行/同用户重复拒/linux 入库/ sessions 重建后级联仍成立)+ MySQL 集成测试 schema 同步与双账号用例。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -29,8 +29,8 @@ func TestSQLiteMigrateUpDown(t *testing.T) {
|
||||
if dirty {
|
||||
t.Fatalf("schema dirty after MigrateUp")
|
||||
}
|
||||
if v != 20 {
|
||||
t.Errorf("version = %d, want 20", v)
|
||||
if v != 21 {
|
||||
t.Errorf("version = %d, want 21", v)
|
||||
}
|
||||
|
||||
// 2. Core tables exist.
|
||||
|
||||
@@ -146,6 +146,69 @@ func TestSQLite_SessionHasActiveSession(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestSQLite_DevicesUserScopedUUID:F3 回归(migration 21)——同一物理设备的
|
||||
// device uuid 在两个账号下各自成行(UNIQUE(user_id,uuid)),同用户重复注册仍被
|
||||
// 唯一键拒绝;linux 平台可入库(CHECK 已放行);sessions 表在重建后 FK 仍指向新
|
||||
// devices(级联删除成立)。
|
||||
func TestSQLite_DevicesUserScopedUUID(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
db := openSQLite(t)
|
||||
|
||||
mkUser := func(u, email string) int64 {
|
||||
res, err := db.ExecContext(ctx,
|
||||
`INSERT INTO users (uuid, email, pw_hash, dp_uuid) VALUES (?, ?, 'h', ?)`,
|
||||
u, email, "dp-"+u)
|
||||
if err != nil {
|
||||
t.Fatalf("user %s: %v", u, err)
|
||||
}
|
||||
id, _ := res.LastInsertId()
|
||||
return id
|
||||
}
|
||||
userA := mkUser("u-a", "a@x.c")
|
||||
userB := mkUser("u-b", "b@x.c")
|
||||
|
||||
// 同一 device uuid,两个账号各自成行(旧全局 UNIQUE(uuid) 下第二条会失败)。
|
||||
if _, err := db.ExecContext(ctx,
|
||||
`INSERT INTO devices (uuid, user_id, name, platform) VALUES ('shared-dev', ?, 'Mac', 'macos')`, userA); err != nil {
|
||||
t.Fatalf("register A: %v", err)
|
||||
}
|
||||
res, err := db.ExecContext(ctx,
|
||||
`INSERT INTO devices (uuid, user_id, name, platform) VALUES ('shared-dev', ?, 'Mac', 'macos')`, userB)
|
||||
if err != nil {
|
||||
t.Fatalf("register B (same uuid, other user) must succeed: %v", err)
|
||||
}
|
||||
devB, _ := res.LastInsertId()
|
||||
|
||||
// 同用户重复注册仍被 UNIQUE(user_id,uuid) 拒绝。
|
||||
if _, err := db.ExecContext(ctx,
|
||||
`INSERT INTO devices (uuid, user_id, name, platform) VALUES ('shared-dev', ?, 'Mac2', 'macos')`, userA); err == nil {
|
||||
t.Fatalf("duplicate (user,uuid) must be rejected")
|
||||
}
|
||||
|
||||
// linux 平台可入库(migration 21 顺手放行,normalizePlatform 早已接受)。
|
||||
if _, err := db.ExecContext(ctx,
|
||||
`INSERT INTO devices (uuid, user_id, name, platform) VALUES ('linux-dev', ?, 'NUC', 'linux')`, userA); err != nil {
|
||||
t.Fatalf("linux platform must be accepted: %v", err)
|
||||
}
|
||||
|
||||
// sessions FK 重建后仍指向新 devices:删 B 的设备,B 的会话级联消失。
|
||||
ss := sessions.NewStore(db)
|
||||
if err := ss.Create(ctx, userB, devB, "jti-b", "", ""); err != nil {
|
||||
t.Fatalf("session B: %v", err)
|
||||
}
|
||||
if _, err := db.ExecContext(ctx, `DELETE FROM devices WHERE id=?`, devB); err != nil {
|
||||
t.Fatalf("delete devB: %v", err)
|
||||
}
|
||||
var n int
|
||||
if err := db.QueryRowContext(ctx,
|
||||
`SELECT COUNT(1) FROM sessions WHERE device_id=?`, devB).Scan(&n); err != nil {
|
||||
t.Fatalf("count sessions: %v", err)
|
||||
}
|
||||
if n != 0 {
|
||||
t.Errorf("sessions must cascade on device delete after rebuild, got %d rows", n)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSQLite_NodeAccumulateUsage(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
db := openSQLite(t)
|
||||
|
||||
Reference in New Issue
Block a user