51 lines
1.5 KiB
Go
51 lines
1.5 KiB
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"hfb_sys/backend/internal/platformscope"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func TestPlatformOpsContextAndReadOnlyGuard(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
engine := gin.New()
|
|
engine.Use(PlatformOpsContext("ops.localhost"), PlatformOpsReadOnlyGuard())
|
|
engine.GET("/probe", func(c *gin.Context) {
|
|
c.JSON(http.StatusOK, gin.H{"platform": platformscope.IsScope(c.Request.Context())})
|
|
})
|
|
engine.GET("/api/admin/platform/overview", func(c *gin.Context) {
|
|
c.Status(http.StatusNoContent)
|
|
})
|
|
engine.GET("/api/admin/users", func(c *gin.Context) {
|
|
c.Status(http.StatusNoContent)
|
|
})
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/probe", nil)
|
|
req.Host = "ops.localhost:5189"
|
|
rec := httptest.NewRecorder()
|
|
engine.ServeHTTP(rec, req)
|
|
if rec.Code != http.StatusOK || rec.Body.String() != `{"platform":true}` {
|
|
t.Fatalf("platform context response = %d %s", rec.Code, rec.Body.String())
|
|
}
|
|
|
|
req = httptest.NewRequest(http.MethodGet, "/api/admin/platform/overview", nil)
|
|
req.Host = "ops.localhost:5189"
|
|
rec = httptest.NewRecorder()
|
|
engine.ServeHTTP(rec, req)
|
|
if rec.Code != http.StatusNoContent {
|
|
t.Fatalf("platform overview status = %d", rec.Code)
|
|
}
|
|
|
|
req = httptest.NewRequest(http.MethodGet, "/api/admin/users", nil)
|
|
req.Host = "ops.localhost:5189"
|
|
rec = httptest.NewRecorder()
|
|
engine.ServeHTTP(rec, req)
|
|
if rec.Code != http.StatusForbidden {
|
|
t.Fatalf("platform non-readonly route status = %d, want 403", rec.Code)
|
|
}
|
|
}
|