package provisioning import ( "context" "net/http" "testing" "github.com/stretchr/testify/require" apierrors "k8s.io/apimachinery/pkg/api/errors" "github.com/grafana/grafana/pkg/util/testutil" ) func TestIntegrationProvisioning_SettingsAuthorization(t *testing.T) { testutil.SkipIntegrationTestInShortMode(t) helper := runGrafana(t) ctx := context.Background() t.Run("viewer can GET settings", func(t *testing.T) { var statusCode int result := helper.ViewerREST.Get(). Namespace("default"). Resource("settings"). Do(ctx).StatusCode(&statusCode) require.NoError(t, result.Error(), "viewer should be able to GET settings") require.Equal(t, http.StatusOK, statusCode, "should return 200 OK") }) t.Run("editor can GET settings", func(t *testing.T) { var statusCode int result := helper.EditorREST.Get(). Namespace("default"). Resource("settings"). Do(ctx).StatusCode(&statusCode) require.NoError(t, result.Error(), "editor should be able to GET settings") require.Equal(t, http.StatusOK, statusCode, "should return 200 OK") }) t.Run("admin can GET settings", func(t *testing.T) { var statusCode int result := helper.AdminREST.Get(). Namespace("default"). Resource("settings"). Do(ctx).StatusCode(&statusCode) require.NoError(t, result.Error(), "admin should be able to GET settings") require.Equal(t, http.StatusOK, statusCode, "should return 200 OK") }) } func TestIntegrationProvisioning_StatsAuthorization(t *testing.T) { testutil.SkipIntegrationTestInShortMode(t) helper := runGrafana(t) ctx := context.Background() // Create a repository to ensure stats endpoint has data const repo = "stats-auth-test" helper.CreateRepo(t, TestRepo{ Name: repo, Target: "folder", Copies: map[string]string{}, ExpectedDashboards: 0, ExpectedFolders: 1, }) t.Run("admin can GET stats", func(t *testing.T) { var statusCode int result := helper.AdminREST.Get(). Namespace("default"). Resource("stats"). Do(ctx).StatusCode(&statusCode) require.NoError(t, result.Error(), "admin should be able to GET stats") require.Equal(t, http.StatusOK, statusCode, "should return 200 OK") }) t.Run("editor cannot GET stats", func(t *testing.T) { var statusCode int result := helper.EditorREST.Get(). Namespace("default"). Resource("stats"). Do(ctx).StatusCode(&statusCode) require.Error(t, result.Error(), "editor should not be able to GET stats") require.Equal(t, http.StatusForbidden, statusCode, "should return 403 Forbidden") require.True(t, apierrors.IsForbidden(result.Error()), "error should be forbidden") }) t.Run("viewer cannot GET stats", func(t *testing.T) { var statusCode int result := helper.ViewerREST.Get(). Namespace("default"). Resource("stats"). Do(ctx).StatusCode(&statusCode) require.Error(t, result.Error(), "viewer should not be able to GET stats") require.Equal(t, http.StatusForbidden, statusCode, "should return 403 Forbidden") require.True(t, apierrors.IsForbidden(result.Error()), "error should be forbidden") }) }