Owensmallwood/pubdash get public dashboard definition (#50269)

* When getting a public dashboard, backend returns a response structured the same as when you get a regular dashboard

* Updates backend tests for getting public dashboard

* Frontend can load the public dashboard based on the pubdash uid provided

* adds frontend test to make sure public dashboard doesnt render toolbar and submenu

* sorts imports
pull/50276/head
owensmallwood 3 years ago committed by GitHub
parent 31630edf0c
commit e7d6a58037
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 22
      pkg/api/dashboard_public.go
  2. 23
      pkg/api/dashboard_public_test.go
  3. 4
      public/app/core/services/backend_srv.ts
  4. 16
      public/app/features/dashboard/containers/DashboardPage.test.tsx
  5. 9
      public/app/features/dashboard/services/DashboardLoaderSrv.ts
  6. 8
      public/app/features/dashboard/state/initDashboard.ts

@ -4,6 +4,7 @@ import (
"errors" "errors"
"net/http" "net/http"
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/api/response" "github.com/grafana/grafana/pkg/api/response"
"github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/dashboards" "github.com/grafana/grafana/pkg/services/dashboards"
@ -16,7 +17,26 @@ func (hs *HTTPServer) GetPublicDashboard(c *models.ReqContext) response.Response
if err != nil { if err != nil {
return handleDashboardErr(http.StatusInternalServerError, "Failed to get public dashboard", err) return handleDashboardErr(http.StatusInternalServerError, "Failed to get public dashboard", err)
} }
return response.JSON(http.StatusOK, dash)
meta := dtos.DashboardMeta{
Slug: dash.Slug,
Type: models.DashTypeDB,
CanStar: false,
CanSave: false,
CanEdit: false,
CanAdmin: false,
CanDelete: false,
Created: dash.Created,
Updated: dash.Updated,
Version: dash.Version,
IsFolder: false,
FolderId: dash.FolderId,
IsPublic: dash.IsPublic,
}
dto := dtos.DashboardFullWithMeta{Meta: meta, Dashboard: dash.Data}
return response.JSON(http.StatusOK, dto)
} }
// gets public dashboard configuration for dashboard // gets public dashboard configuration for dashboard

@ -12,6 +12,8 @@ import (
"github.com/stretchr/testify/mock" "github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/dashboards" "github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/services/featuremgmt" "github.com/grafana/grafana/pkg/services/featuremgmt"
@ -44,6 +46,9 @@ func TestAPIGetPublicDashboard(t *testing.T) {
assert.Equal(t, http.StatusNotFound, response.Code) assert.Equal(t, http.StatusNotFound, response.Code)
}) })
dashboardUid := "dashboard-abcd1234"
pubdashUid := "pubdash-abcd1234"
testCases := []struct { testCases := []struct {
name string name string
uid string uid string
@ -53,16 +58,19 @@ func TestAPIGetPublicDashboard(t *testing.T) {
}{ }{
{ {
name: "It gets a public dashboard", name: "It gets a public dashboard",
uid: "pubdash-abcd1234", uid: pubdashUid,
expectedHttpResponse: http.StatusOK, expectedHttpResponse: http.StatusOK,
publicDashboardResult: &models.Dashboard{ publicDashboardResult: &models.Dashboard{
Uid: "dashboard-abcd1234", Data: simplejson.NewFromAny(map[string]interface{}{
"Uid": dashboardUid,
}),
IsPublic: true,
}, },
publicDashboardErr: nil, publicDashboardErr: nil,
}, },
{ {
name: "It should return 404 if isPublicDashboard is false", name: "It should return 404 if isPublicDashboard is false",
uid: "pubdash-abcd1234", uid: pubdashUid,
expectedHttpResponse: http.StatusNotFound, expectedHttpResponse: http.StatusNotFound,
publicDashboardResult: nil, publicDashboardResult: nil,
publicDashboardErr: models.ErrPublicDashboardNotFound, publicDashboardErr: models.ErrPublicDashboardNotFound,
@ -89,10 +97,15 @@ func TestAPIGetPublicDashboard(t *testing.T) {
assert.Equal(t, test.expectedHttpResponse, response.Code) assert.Equal(t, test.expectedHttpResponse, response.Code)
if test.publicDashboardErr == nil { if test.publicDashboardErr == nil {
var dashResp models.Dashboard var dashResp dtos.DashboardFullWithMeta
err := json.Unmarshal(response.Body.Bytes(), &dashResp) err := json.Unmarshal(response.Body.Bytes(), &dashResp)
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, test.publicDashboardResult.Uid, dashResp.Uid)
assert.Equal(t, dashboardUid, dashResp.Dashboard.Get("Uid").MustString())
assert.Equal(t, true, dashResp.Meta.IsPublic)
assert.Equal(t, false, dashResp.Meta.CanEdit)
assert.Equal(t, false, dashResp.Meta.CanDelete)
assert.Equal(t, false, dashResp.Meta.CanSave)
} else { } else {
var errResp struct { var errResp struct {
Error string `json:"error"` Error string `json:"error"`

@ -429,6 +429,10 @@ export class BackendSrv implements BackendService {
return this.get<DashboardDTO>(`/api/dashboards/uid/${uid}`); return this.get<DashboardDTO>(`/api/dashboards/uid/${uid}`);
} }
getPublicDashboardByUid(uid: string) {
return this.get<DashboardDTO>(`/api/public/dashboards/${uid}`);
}
getFolderByUid(uid: string) { getFolderByUid(uid: string) {
return this.get<FolderDTO>(`/api/folders/${uid}`); return this.get<FolderDTO>(`/api/folders/${uid}`);
} }

@ -302,4 +302,20 @@ describe('DashboardPage', () => {
expect(screen.queryAllByLabelText(selectors.pages.Dashboard.SubMenu.submenu)).toHaveLength(0); expect(screen.queryAllByLabelText(selectors.pages.Dashboard.SubMenu.submenu)).toHaveLength(0);
}); });
}); });
dashboardPageScenario('When dashboard is public', (ctx) => {
ctx.setup(() => {
locationService.partial({ kiosk: false });
ctx.mount({
queryParams: {},
dashboard: getTestDashboard(),
});
ctx.rerender({ dashboard: ctx.dashboard, isPublic: true });
});
it('should not render page toolbar and submenu', () => {
expect(screen.queryAllByTestId(selectors.pages.Dashboard.DashNav.navV2)).toHaveLength(0);
expect(screen.queryAllByLabelText(selectors.pages.Dashboard.SubMenu.submenu)).toHaveLength(0);
});
});
}); });

@ -41,6 +41,15 @@ export class DashboardLoaderSrv {
}); });
} else if (type === 'ds') { } else if (type === 'ds') {
promise = this._loadFromDatasource(slug); // explore dashboards as code promise = this._loadFromDatasource(slug); // explore dashboards as code
} else if (type === 'public') {
promise = backendSrv
.getPublicDashboardByUid(uid)
.then((result: any) => {
return result;
})
.catch(() => {
return this._dashboardLoadFailed('Public Dashboard Not found', true);
});
} else { } else {
promise = backendSrv promise = backendSrv
.getDashboardByUid(uid) .getDashboardByUid(uid)

@ -61,13 +61,7 @@ async function fetchDashboard(
return dashDTO; return dashDTO;
} }
case DashboardRoutes.Public: { case DashboardRoutes.Public: {
const dashDTO: DashboardDTO = await dashboardLoaderSrv.loadDashboard(args.urlType, args.urlSlug, args.urlUid); return await dashboardLoaderSrv.loadDashboard('public', args.urlSlug, args.urlUid);
// Make sure new endpoint to fetch dashboard DTO sets these as false
dashDTO.meta.canEdit = false;
dashDTO.meta.canMakeEditable = false;
dashDTO.meta.isPublic = true;
return dashDTO;
} }
case DashboardRoutes.Normal: { case DashboardRoutes.Normal: {
const dashDTO: DashboardDTO = await dashboardLoaderSrv.loadDashboard(args.urlType, args.urlSlug, args.urlUid); const dashDTO: DashboardDTO = await dashboardLoaderSrv.loadDashboard(args.urlType, args.urlSlug, args.urlUid);

Loading…
Cancel
Save