功能:增加平台账号管理与管理员保护

This commit is contained in:
yml2213
2026-08-13 12:52:50 +08:00
parent 8c47b961e1
commit 2b3a1b111b
8 changed files with 167 additions and 10 deletions
+22
View File
@@ -202,3 +202,25 @@ func TestAuthServiceChangePasswordVerifiesCurrentPassword(t *testing.T) {
t.Fatal("old password should no longer match")
}
}
func TestUserServiceCannotDeleteOrDisableLastAdmin(t *testing.T) {
db := newServiceTestDB(t)
svc := NewUserService(db, nil)
admin, err := svc.Create("only-admin", "password123", "唯一管理员", model.RoleAdmin, 0)
if err != nil {
t.Fatalf("create admin: %v", err)
}
if err := svc.UpdateStatus(admin.ID, 0); err == nil {
t.Fatal("should not disable the last enabled admin")
}
other, err := svc.Create("other-admin", "password123", "另一管理员", model.RoleAdmin, 0)
if err != nil {
t.Fatalf("create second admin: %v", err)
}
if err := svc.Delete(other.ID, admin.ID); err != nil {
t.Fatalf("delete non-current admin: %v", err)
}
if err := svc.Delete(admin.ID, other.ID); err == nil {
t.Fatal("should not delete the last enabled admin")
}
}