Release / update-version (push) Has been cancelled
Release / build-frontend (push) Has been cancelled
Release / release (push) Has been cancelled
Release / sync-version-file (push) Has been cancelled
CI / shell (push) Canceled after 0s
CI / test (push) Canceled after 0s
CI / frontend (push) Canceled after 0s
CI / golangci-lint (push) Canceled after 0s
Security Scan / backend-security (push) Canceled after 0s
Security Scan / frontend-security (push) Canceled after 0s
54 lines
1.8 KiB
Go
54 lines
1.8 KiB
Go
package routes
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/handler"
|
|
adminhandler "github.com/Wei-Shaw/sub2api/internal/handler/admin"
|
|
servermiddleware "github.com/Wei-Shaw/sub2api/internal/server/middleware"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestIngressRejectAdminRoutesRequireAdminAuthentication(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
router := gin.New()
|
|
handlers := &handler.Handlers{Admin: &handler.AdminHandlers{Ops: adminhandler.NewOpsHandler(nil)}}
|
|
adminAuth := servermiddleware.AdminAuthMiddleware(func(c *gin.Context) {
|
|
if c.GetHeader("Authorization") == "" {
|
|
servermiddleware.AbortWithError(c, http.StatusUnauthorized, "UNAUTHORIZED", "Authorization required")
|
|
return
|
|
}
|
|
servermiddleware.AbortWithError(c, http.StatusForbidden, "FORBIDDEN", "Admin access required")
|
|
})
|
|
auditLog := servermiddleware.AuditLogMiddleware(func(c *gin.Context) { c.Next() })
|
|
stepUp := servermiddleware.StepUpAuthMiddleware(func(c *gin.Context) { c.Next() })
|
|
RegisterAdminRoutes(router.Group("/api/v1"), handlers, adminAuth, auditLog, stepUp, nil, nil)
|
|
|
|
for _, path := range []string{
|
|
"/api/v1/admin/ops/ingress-rejections",
|
|
"/api/v1/admin/ops/ingress-rejections/health",
|
|
} {
|
|
for _, tc := range []struct {
|
|
name string
|
|
auth string
|
|
wantStatus int
|
|
}{
|
|
{name: "unauthenticated", wantStatus: http.StatusUnauthorized},
|
|
{name: "non-admin", auth: "Bearer user-token", wantStatus: http.StatusForbidden},
|
|
} {
|
|
t.Run(path+"/"+tc.name, func(t *testing.T) {
|
|
recorder := httptest.NewRecorder()
|
|
request := httptest.NewRequest(http.MethodGet, path, nil)
|
|
if tc.auth != "" {
|
|
request.Header.Set("Authorization", tc.auth)
|
|
}
|
|
router.ServeHTTP(recorder, request)
|
|
require.Equal(t, tc.wantStatus, recorder.Code)
|
|
})
|
|
}
|
|
}
|
|
}
|