61 lines
1.8 KiB
Go
61 lines
1.8 KiB
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"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())
|
|
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())
|
|
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)
|
|
}
|
|
}
|
|
}
|