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
This commit is contained in:
owensmallwood 2022-06-06 16:19:34 -06:00 committed by GitHub
parent 31630edf0c
commit e7d6a58037
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 69 additions and 13 deletions

View File

@ -4,6 +4,7 @@ import (
"errors"
"net/http"
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/api/response"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/dashboards"
@ -16,7 +17,26 @@ func (hs *HTTPServer) GetPublicDashboard(c *models.ReqContext) response.Response
if err != nil {
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

View File

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

View File

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

View File

@ -302,4 +302,20 @@ describe('DashboardPage', () => {
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);
});
});
});

View File

@ -41,6 +41,15 @@ export class DashboardLoaderSrv {
});
} else if (type === 'ds') {
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 {
promise = backendSrv
.getDashboardByUid(uid)

View File

@ -61,13 +61,7 @@ async function fetchDashboard(
return dashDTO;
}
case DashboardRoutes.Public: {
const dashDTO: DashboardDTO = await dashboardLoaderSrv.loadDashboard(args.urlType, 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;
return await dashboardLoaderSrv.loadDashboard('public', args.urlSlug, args.urlUid);
}
case DashboardRoutes.Normal: {
const dashDTO: DashboardDTO = await dashboardLoaderSrv.loadDashboard(args.urlType, args.urlSlug, args.urlUid);