From 963ae4ef8785813df1ac2a5d40b12f6ef0a39608 Mon Sep 17 00:00:00 2001 From: Sofia Papagiannaki <1632407+papagian@users.noreply.github.com> Date: Tue, 11 Jan 2022 18:39:53 +0200 Subject: [PATCH] API: Fix storing dashboard with static UID (#43861) * API: Fix storing dashboard with static UID --- pkg/api/dashboard.go | 6 ++-- .../api/dashboards/api_dashboards_test.go | 28 ++++++++++++++++--- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/pkg/api/dashboard.go b/pkg/api/dashboard.go index f0e6e6e505c..915a345e237 100644 --- a/pkg/api/dashboard.go +++ b/pkg/api/dashboard.go @@ -323,13 +323,13 @@ func (hs *HTTPServer) postDashboard(c *models.ReqContext, cmd models.SaveDashboa if dash.Id != 0 { data, err := svc.GetProvisionedDashboardDataByDashboardID(dash.Id) if err != nil { - return response.Error(500, "Error while checking if dashboard is provisioned", err) + return response.Error(500, "Error while checking if dashboard is provisioned using ID", err) } provisioningData = data } else if dash.Uid != "" { data, err := svc.GetProvisionedDashboardDataByDashboardUID(dash.OrgId, dash.Uid) - if err != nil && !errors.Is(err, models.ErrProvisionedDashboardNotFound) { - return response.Error(500, "Error while checking if dashboard is provisioned", err) + if err != nil && (!errors.Is(err, models.ErrProvisionedDashboardNotFound) && !errors.Is(err, models.ErrDashboardNotFound)) { + return response.Error(500, "Error while checking if dashboard is provisioned using UID", err) } provisioningData = data } diff --git a/pkg/tests/api/dashboards/api_dashboards_test.go b/pkg/tests/api/dashboards/api_dashboards_test.go index 18687c5598f..9d0aa581e42 100644 --- a/pkg/tests/api/dashboards/api_dashboards_test.go +++ b/pkg/tests/api/dashboards/api_dashboards_test.go @@ -97,7 +97,7 @@ func createUser(t *testing.T, store *sqlstore.SQLStore, cmd models.CreateUserCom return u.Id } -func TestProvisionioningDashboards(t *testing.T) { +func TestUpdatingProvisionionedDashboards(t *testing.T) { // Setup Grafana and its Database dir, path := testinfra.CreateGrafDir(t, testinfra.GrafanaOpts{ DisableAnonymous: true, @@ -161,14 +161,31 @@ providers: testCases := []struct { desc string dashboardData string + expStatus int + expErrReason string }{ { desc: "when updating provisioned dashboard using ID it should fail", dashboardData: fmt.Sprintf(`{"title":"just testing", "id": %d, "version": 1}`, dashboardID), + expStatus: http.StatusBadRequest, + expErrReason: models.ErrDashboardCannotSaveProvisionedDashboard.Reason, }, { - desc: "when updating provisioned dashboard using UID is should fail", + desc: "when updating provisioned dashboard using UID it should fail", dashboardData: fmt.Sprintf(`{"title":"just testing", "uid": %q, "version": 1}`, dashboardUID), + expStatus: http.StatusBadRequest, + expErrReason: models.ErrDashboardCannotSaveProvisionedDashboard.Reason, + }, + { + desc: "when updating dashboard using unknown ID, it should fail", + dashboardData: `{"title":"just testing", "id": 42, "version": 1}`, + expStatus: http.StatusNotFound, + expErrReason: models.ErrDashboardNotFound.Reason, + }, + { + desc: "when updating dashboard using unknown UID, it should succeed", + dashboardData: `{"title":"just testing", "uid": "unknown", "version": 1}`, + expStatus: http.StatusOK, }, } for _, tc := range testCases { @@ -186,17 +203,20 @@ providers: // nolint:gosec resp, err := http.Post(u, "application/json", buf) require.NoError(t, err) - assert.Equal(t, http.StatusBadRequest, resp.StatusCode) + assert.Equal(t, tc.expStatus, resp.StatusCode) t.Cleanup(func() { err := resp.Body.Close() require.NoError(t, err) }) + if tc.expErrReason == "" { + return + } b, err := ioutil.ReadAll(resp.Body) require.NoError(t, err) dashboardErr := &errorResponseBody{} err = json.Unmarshal(b, dashboardErr) require.NoError(t, err) - assert.Equal(t, models.ErrDashboardCannotSaveProvisionedDashboard.Reason, dashboardErr.Message) + assert.Equal(t, tc.expErrReason, dashboardErr.Message) }) }