100 lines
3.1 KiB
Go
100 lines
3.1 KiB
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"hfb_sys/backend/internal/database"
|
|
"hfb_sys/backend/internal/model"
|
|
"hfb_sys/backend/internal/sitecode"
|
|
"hfb_sys/backend/internal/tenancy"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func TestBlockTenantLegacyDistributionAdmin(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
engine := gin.New()
|
|
engine.Use(BlockTenantLegacyDistributionAdmin(nil))
|
|
engine.GET("/api/admin/distribution/sites", func(c *gin.Context) {
|
|
c.Status(http.StatusNoContent)
|
|
})
|
|
engine.GET("/api/admin/invitations", func(c *gin.Context) {
|
|
c.Status(http.StatusNoContent)
|
|
})
|
|
engine.GET("/api/distribution/sites/1", func(c *gin.Context) {
|
|
c.Status(http.StatusNoContent)
|
|
})
|
|
|
|
tenantMiddleware := func(c *gin.Context) {
|
|
ctx := tenancy.WithScope(c.Request.Context(), tenancy.Scope{TenantID: 2, TenantCode: "jieyou"})
|
|
ctx = sitecode.WithTenantScope(ctx)
|
|
c.Request = c.Request.WithContext(ctx)
|
|
c.Next()
|
|
}
|
|
tenantEngine := gin.New()
|
|
tenantEngine.Use(tenantMiddleware, BlockTenantLegacyDistributionAdmin(nil))
|
|
tenantEngine.GET("/api/admin/distribution/sites", func(c *gin.Context) {
|
|
c.Status(http.StatusNoContent)
|
|
})
|
|
|
|
rec := httptest.NewRecorder()
|
|
engine.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/api/admin/distribution/sites", nil))
|
|
if rec.Code != http.StatusNoContent {
|
|
t.Fatalf("legacy request status = %d, want 204", rec.Code)
|
|
}
|
|
|
|
rec = httptest.NewRecorder()
|
|
tenantEngine.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/api/admin/distribution/sites", nil))
|
|
if rec.Code != http.StatusForbidden {
|
|
t.Fatalf("tenant legacy admin status = %d, want 403", rec.Code)
|
|
}
|
|
|
|
for _, path := range []string{"/api/admin/invitations", "/api/distribution/sites/1"} {
|
|
rec = httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, path, nil)
|
|
engine.ServeHTTP(rec, req)
|
|
if rec.Code != http.StatusNoContent {
|
|
t.Fatalf("%s status = %d, want 204", path, rec.Code)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestBlockTenantLegacyDistributionAdminAllowsPlatformSuperAdmin(t *testing.T) {
|
|
db := database.NewTestDB()
|
|
role := model.Role{Code: "super_admin", Name: "超级管理员"}
|
|
if err := db.Create(&role).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
admin := model.AdminUser{Username: "legacy-dist-root", PasswordHash: "x", Status: "active"}
|
|
if err := db.Create(&admin).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.Create(&model.AdminUserRole{
|
|
AdminID: admin.ID,
|
|
RoleID: role.ID,
|
|
TenantID: 0,
|
|
}).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
gin.SetMode(gin.TestMode)
|
|
engine := gin.New()
|
|
engine.Use(func(c *gin.Context) {
|
|
ctx := tenancy.WithScope(c.Request.Context(), tenancy.Scope{TenantID: 1, TenantCode: "dachui"})
|
|
c.Request = c.Request.WithContext(sitecode.WithTenantScope(ctx))
|
|
c.Set(ContextAdminID, admin.ID)
|
|
c.Next()
|
|
}, BlockTenantLegacyDistributionAdmin(db))
|
|
engine.GET("/api/admin/distribution/sites", func(c *gin.Context) {
|
|
c.Status(http.StatusNoContent)
|
|
})
|
|
|
|
rec := httptest.NewRecorder()
|
|
engine.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/api/admin/distribution/sites", nil))
|
|
if rec.Code != http.StatusNoContent {
|
|
t.Fatalf("platform super admin status = %d, want 204, body=%s", rec.Code, rec.Body.String())
|
|
}
|
|
}
|