fix(tenant): 收紧双站运营提号与录单边界
This commit is contained in:
@@ -9,6 +9,9 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/database"
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/platformscope"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -20,11 +23,66 @@ func TestAdminPermissionCacheKeyIncludesTenant(t *testing.T) {
|
||||
if got := adminPermissionCacheKey(ctx, 7); got != "admin:perms:7:tenant:101" {
|
||||
t.Fatalf("tenant permission cache key = %q", got)
|
||||
}
|
||||
platformCtx := platformscope.WithScope(context.Background())
|
||||
if got := adminPermissionCacheKey(platformCtx, 7); got != "admin:perms:7:platform" {
|
||||
t.Fatalf("platform permission cache key = %q", got)
|
||||
}
|
||||
if got := adminPermissionCacheKey(context.Background(), 7); got != "admin:perms:7" {
|
||||
t.Fatalf("legacy permission cache key = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequirePlatformSuperAdmin(t *testing.T) {
|
||||
db := database.NewTestDB()
|
||||
roles := []model.Role{
|
||||
{Code: "super_admin", Name: "超级管理员"},
|
||||
{Code: "finance", Name: "财务"},
|
||||
}
|
||||
if err := db.Create(&roles).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
admins := []model.AdminUser{
|
||||
{Username: "platform-root", PasswordHash: "x", Status: "active"},
|
||||
{Username: "tenant-finance", PasswordHash: "x", Status: "active"},
|
||||
}
|
||||
if err := db.Create(&admins).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Create(&[]model.AdminUserRole{
|
||||
{AdminID: admins[0].ID, RoleID: roles[0].ID, TenantID: 0},
|
||||
{AdminID: admins[1].ID, RoleID: roles[1].ID, TenantID: 101},
|
||||
}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
engine := gin.New()
|
||||
engine.Use(func(c *gin.Context) {
|
||||
if c.GetHeader("X-Test-Admin") == "1" {
|
||||
c.Set(ContextAdminID, admins[0].ID)
|
||||
} else {
|
||||
c.Set(ContextAdminID, admins[1].ID)
|
||||
}
|
||||
c.Next()
|
||||
})
|
||||
engine.GET("/probe", RequirePlatformSuperAdmin(db), func(c *gin.Context) {
|
||||
c.Status(http.StatusNoContent)
|
||||
})
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/probe", nil)
|
||||
req.Header.Set("X-Test-Admin", "1")
|
||||
rec := httptest.NewRecorder()
|
||||
engine.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusNoContent {
|
||||
t.Fatalf("super admin status = %d, want %d", rec.Code, http.StatusNoContent)
|
||||
}
|
||||
|
||||
rec = httptest.NewRecorder()
|
||||
engine.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/probe", nil))
|
||||
if rec.Code != http.StatusForbidden {
|
||||
t.Fatalf("tenant admin status = %d, want %d", rec.Code, http.StatusForbidden)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequireAnyPermission(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
|
||||
Reference in New Issue
Block a user