system_permission_test.go 748 B

12345678910111213141516171819202122
  1. package admin
  2. import (
  3. "github.com/Wei-Shaw/sub2api/internal/server/middleware"
  4. "github.com/Wei-Shaw/sub2api/internal/service"
  5. "github.com/gin-gonic/gin"
  6. "github.com/stretchr/testify/require"
  7. "net/http"
  8. "net/http/httptest"
  9. "testing"
  10. )
  11. func TestSystemMutationsRejectOrdinaryAdminBeforeSideEffects(t *testing.T) {
  12. for _, method := range []gin.HandlerFunc{(&SystemHandler{}).PerformUpdate, (&SystemHandler{}).Rollback, (&SystemHandler{}).RestartService} {
  13. r := gin.New()
  14. r.Use(func(c *gin.Context) { c.Set(string(middleware.ContextKeyUserRole), service.RoleAdmin) })
  15. r.POST("/mutation", method)
  16. w := httptest.NewRecorder()
  17. r.ServeHTTP(w, httptest.NewRequest(http.MethodPost, "/mutation", nil))
  18. require.Equal(t, 403, w.Code)
  19. }
  20. }