Files
grafana/apps/quotas/pkg/app/app_test.go
T
owensmallwood a3daf0e39d Unified storage: Add quotas app to apiserver (#114425)
* initial generation

* went through doc to add new resource

* added dummy kind so grafana will run

* added dummy handler and custom route

* fix app name

* gets custom route working - still a dummy route

* adds groupOverride to manifest

* adds quotas to grpc client and server

* WIP - trying to get api recognized - not working

* Gets route working

* fixes group and resource vars

* expects group and resource as separate params

* set content-type header on response

* removes Quotas kind and regens

* Update grafana-app-sdk to v0.48.5

* Update codegen

* updates manifest

* formatting

* updates grafana-app-sdk version to 0.48.5

* regen ResourceClient mocks

* adds tests

* remove commented code

* uncomment go mod tidy

* fix tests and make update workspace

* adds quotas app to codeowners

* formatting

* make gen-apps

* deletes temp file

* fix generated folder code

* make gofmt

* make gen-go

* make update-workspace

* add COPY apps/quotas to Dockerfile

* fix test mock

* fixes undefined NewFolderStatus()

* make gen-apps, and add func for NewFolderStatus

* make gen-apps again

* make update-workspace

* regen folder_object_gen.go

* gofmt

* fix linting

* apps/folder make update-workspace

* make gen-apps

* make gen-apps

* fixes enterprise_imports.go

* go get testcontainers

* adds feature toggle

* make update-workspace

* fix go mod

* fix another client mock

---------

Co-authored-by: Steve Simpson <steve@grafana.com>
2025-12-09 09:40:34 -06:00

72 lines
2.3 KiB
Go

package app
import (
"context"
"net/http/httptest"
"net/url"
"testing"
"github.com/grafana/grafana-app-sdk/app"
"github.com/grafana/grafana/pkg/storage/unified/resource"
"github.com/grafana/grafana/pkg/storage/unified/resourcepb"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
func TestGetQuota(t *testing.T) {
t.Run("will return error when resource param is missing", func(t *testing.T) {
clientMock := resource.NewMockResourceClient(t)
handler := NewQuotasHandler(&QuotasAppConfig{
ResourceClient: clientMock,
})
url, err := url.Parse("http://localhost:3000/apis/quotas.grafana.app/v0alpha1/namespaces/stacks-1/usage?group=dashboard.grafana.app")
require.NoError(t, err)
req := &app.CustomRouteRequest{
URL: url,
Method: "GET",
}
recorder := &httptest.ResponseRecorder{}
err = handler.GetQuota(context.Background(), recorder, req)
require.Error(t, err)
})
t.Run("will return error when group param is missing", func(t *testing.T) {
clientMock := resource.NewMockResourceClient(t)
handler := NewQuotasHandler(&QuotasAppConfig{
ResourceClient: clientMock,
})
url, err := url.Parse("http://localhost:3000/apis/quotas.grafana.app/v0alpha1/namespaces/stacks-1/usage?resource=dashboards")
require.NoError(t, err)
req := &app.CustomRouteRequest{
URL: url,
Method: "GET",
}
recorder := &httptest.ResponseRecorder{}
err = handler.GetQuota(context.Background(), recorder, req)
require.Error(t, err)
})
t.Run("will return quotas response when params are valid", func(t *testing.T) {
clientMock := resource.NewMockResourceClient(t)
clientMock.On("GetQuotaUsage", mock.Anything, mock.Anything, mock.Anything).Return(&resourcepb.QuotaUsageResponse{
Error: nil,
Usage: 1,
Limit: 2,
}, nil)
handler := NewQuotasHandler(&QuotasAppConfig{
ResourceClient: clientMock,
})
url, err := url.Parse("http://localhost:3000/apis/quotas.grafana.app/v0alpha1/namespaces/stacks-1/usage?group=dashboard.grafana.app&resource=dashboards")
require.NoError(t, err)
req := &app.CustomRouteRequest{
URL: url,
Method: "GET",
}
recorder := &httptest.ResponseRecorder{}
err = handler.GetQuota(context.Background(), recorder, req)
require.NoError(t, err)
require.Equal(t, 200, recorder.Code)
})
}