mirror of
https://github.com/grafana/grafana.git
synced 2024-11-25 18:30:41 -06:00
df25e9197e
* Add auth checks and test * Check user is authorized to view rule and add tests * Change naming * Update Swagger params * Update auth test and swagger gen * Update swagger gen * Change response to GettableExtendedRuleNode * openapi3-gen * Update tests with refactors models pkg
65 lines
1.4 KiB
Go
65 lines
1.4 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/go-openapi/loads"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
acmock "github.com/grafana/grafana/pkg/services/accesscontrol/mock"
|
|
)
|
|
|
|
func TestAuthorize(t *testing.T) {
|
|
json, err := os.ReadFile(filepath.Join("tooling", "spec.json"))
|
|
require.NoError(t, err)
|
|
swaggerSpec, err := loads.Analyzed(json, "")
|
|
require.NoError(t, err)
|
|
|
|
paths := make(map[string][]string)
|
|
|
|
for p, item := range swaggerSpec.Spec().Paths.Paths {
|
|
var methods []string
|
|
|
|
if item.Get != nil {
|
|
methods = append(methods, http.MethodGet)
|
|
}
|
|
if item.Put != nil {
|
|
methods = append(methods, http.MethodPut)
|
|
}
|
|
if item.Post != nil {
|
|
methods = append(methods, http.MethodPost)
|
|
}
|
|
if item.Delete != nil {
|
|
methods = append(methods, http.MethodDelete)
|
|
}
|
|
if item.Patch != nil {
|
|
methods = append(methods, http.MethodPatch)
|
|
}
|
|
paths[p] = methods
|
|
}
|
|
require.Len(t, paths, 59)
|
|
|
|
ac := acmock.New()
|
|
api := &API{AccessControl: ac}
|
|
|
|
t.Run("should not panic on known routes", func(t *testing.T) {
|
|
for path, methods := range paths {
|
|
path := swaggerSpec.Spec().BasePath + path
|
|
for _, method := range methods {
|
|
require.NotPanics(t, func() {
|
|
api.authorize(method, path)
|
|
})
|
|
}
|
|
}
|
|
})
|
|
|
|
t.Run("should panic if route is unknown", func(t *testing.T) {
|
|
require.Panics(t, func() {
|
|
api.authorize("test", "test")
|
|
})
|
|
})
|
|
}
|