feat(platform-ops): 增加平台只读统计接口与超管入口
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"hfb_sys/backend/internal/database"
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/platformscope"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
)
|
||||
|
||||
@@ -14,6 +15,50 @@ func adminTenantContext(tenantID uint64) context.Context {
|
||||
return tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: tenantID})
|
||||
}
|
||||
|
||||
func TestPlatformOpsOnlyAllowsSuperAdmin(t *testing.T) {
|
||||
db := database.NewTestDB()
|
||||
roles := []model.Role{
|
||||
{Code: "super_admin", Name: "超级管理员"},
|
||||
{Code: "cs", Name: "客服"},
|
||||
}
|
||||
if err := db.Create(&roles).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tenantAdmin := model.AdminUser{Username: "tenant-cs-platform", PasswordHash: "x", Status: "active", TokenVersion: 1}
|
||||
superAdmin := model.AdminUser{Username: "root-platform", PasswordHash: "x", Status: "active", TokenVersion: 1}
|
||||
if err := db.Create(&tenantAdmin).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Create(&superAdmin).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, assignment := range []model.AdminUserRole{
|
||||
{AdminID: tenantAdmin.ID, RoleID: roles[1].ID, TenantID: 101},
|
||||
{AdminID: superAdmin.ID, RoleID: roles[0].ID, TenantID: 0},
|
||||
} {
|
||||
if err := db.Create(&assignment).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
repo := NewRepository(db, nil, nil)
|
||||
ctx := platformscope.WithScope(context.Background())
|
||||
if _, err := repo.FindActiveForToken(ctx, tenantAdmin.ID, 1); !errors.Is(err, ErrAdminNotFound) {
|
||||
t.Fatalf("tenant admin platform token error = %v, want ErrAdminNotFound", err)
|
||||
}
|
||||
if _, err := repo.FindActiveForToken(ctx, superAdmin.ID, 1); err != nil {
|
||||
t.Fatalf("super admin platform token: %v", err)
|
||||
}
|
||||
dto, err := repo.FindByID(ctx, superAdmin.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(dto.Roles) != 1 || dto.Roles[0].Code != "super_admin" ||
|
||||
len(dto.Permissions) != 1 || dto.Permissions[0] != "*" {
|
||||
t.Fatalf("platform super admin = %+v", dto)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdminAccessAndPermissionsAreScopedByTenant(t *testing.T) {
|
||||
db := database.NewTestDB()
|
||||
roles := []model.Role{
|
||||
|
||||
Reference in New Issue
Block a user