Stars: Remove deprecated internal ID apis (#110499)
This commit is contained in:
@@ -3,7 +3,6 @@ package api
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/api/response"
|
||||
@@ -69,46 +68,6 @@ func (api *API) GetStars(c *contextmodel.ReqContext) response.Response {
|
||||
return response.JSON(http.StatusOK, uids)
|
||||
}
|
||||
|
||||
// swagger:route POST /user/stars/dashboard/{dashboard_id} signed_in_user starDashboard
|
||||
//
|
||||
// Star a dashboard.
|
||||
//
|
||||
// Stars the given Dashboard for the actual user.
|
||||
//
|
||||
// Deprecated: true
|
||||
//
|
||||
// Responses:
|
||||
// 200: okResponse
|
||||
// 400: badRequestError
|
||||
// 401: unauthorisedError
|
||||
// 403: forbiddenError
|
||||
// 500: internalServerError
|
||||
func (api *API) StarDashboard(c *contextmodel.ReqContext) response.Response {
|
||||
userID, err := identity.UserIdentifier(c.GetID())
|
||||
if err != nil {
|
||||
return response.Error(http.StatusBadRequest, "Only users and service accounts can star dashboards", nil)
|
||||
}
|
||||
|
||||
id, err := strconv.ParseInt(web.Params(c.Req)[":id"], 10, 64)
|
||||
if err != nil {
|
||||
return response.Error(http.StatusBadRequest, "Invalid dashboard ID", nil)
|
||||
}
|
||||
|
||||
api.logger.Warn("POST /user/stars/dashboard/{dashboard_id} is deprecated, please use POST /user/stars/dashboard/uid/{dashboard_uid} instead")
|
||||
|
||||
cmd := star.StarDashboardCommand{UserID: userID, DashboardID: id, Updated: time.Now()}
|
||||
// nolint:staticcheck
|
||||
if cmd.DashboardID <= 0 {
|
||||
return response.Error(http.StatusBadRequest, "Missing dashboard id", nil)
|
||||
}
|
||||
|
||||
if err := api.starService.Add(c.Req.Context(), &cmd); err != nil {
|
||||
return response.Error(http.StatusInternalServerError, "Failed to star dashboard", err)
|
||||
}
|
||||
|
||||
return response.Success("Dashboard starred!")
|
||||
}
|
||||
|
||||
// swagger:route POST /user/stars/dashboard/uid/{dashboard_uid} signed_in_user starDashboardByUID
|
||||
//
|
||||
// Star a dashboard.
|
||||
@@ -146,48 +105,6 @@ func (api *API) StarDashboardByUID(c *contextmodel.ReqContext) response.Response
|
||||
return response.Success("Dashboard starred!")
|
||||
}
|
||||
|
||||
// swagger:route DELETE /user/stars/dashboard/{dashboard_id} signed_in_user unstarDashboard
|
||||
//
|
||||
// Unstar a dashboard.
|
||||
//
|
||||
// Deletes the starring of the given Dashboard for the actual user.
|
||||
//
|
||||
// Deprecated: true
|
||||
//
|
||||
// Please refer to the [new](#/signed_in_user/unstarDashboardByUID) API instead
|
||||
//
|
||||
// Responses:
|
||||
// 200: okResponse
|
||||
// 400: badRequestError
|
||||
// 401: unauthorisedError
|
||||
// 403: forbiddenError
|
||||
// 500: internalServerError
|
||||
func (api *API) UnstarDashboard(c *contextmodel.ReqContext) response.Response {
|
||||
id, err := strconv.ParseInt(web.Params(c.Req)[":id"], 10, 64)
|
||||
if err != nil {
|
||||
return response.Error(http.StatusBadRequest, "Invalid dashboard ID", nil)
|
||||
}
|
||||
|
||||
userID, err := identity.UserIdentifier(c.GetID())
|
||||
if err != nil {
|
||||
return response.Error(http.StatusBadRequest, "Only users and service accounts can star dashboards", nil)
|
||||
}
|
||||
|
||||
api.logger.Warn("DELETE /user/stars/dashboard/{dashboard_id} is deprecated, please use DELETE /user/stars/dashboard/uid/{dashboard_uid} instead")
|
||||
|
||||
cmd := star.UnstarDashboardCommand{UserID: userID, DashboardID: id}
|
||||
// nolint:staticcheck
|
||||
if cmd.DashboardID <= 0 {
|
||||
return response.Error(http.StatusBadRequest, "Missing dashboard id", nil)
|
||||
}
|
||||
|
||||
if err := api.starService.Delete(c.Req.Context(), &cmd); err != nil {
|
||||
return response.Error(http.StatusInternalServerError, "Failed to unstar dashboard", err)
|
||||
}
|
||||
|
||||
return response.Success("Dashboard unstarred")
|
||||
}
|
||||
|
||||
// swagger:route DELETE /user/stars/dashboard/uid/{dashboard_uid} signed_in_user unstarDashboardByUID
|
||||
//
|
||||
// Unstar a dashboard.
|
||||
|
||||
@@ -14,76 +14,6 @@ import (
|
||||
"github.com/grafana/grafana/pkg/web"
|
||||
)
|
||||
|
||||
func TestStarDashboardID(t *testing.T) {
|
||||
api := ProvideApi(startest.NewStarServiceFake(), dashboards.NewFakeDashboardService(t))
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
signedInUser *user.SignedInUser
|
||||
expectedStatus int
|
||||
params map[string]string
|
||||
}{
|
||||
{
|
||||
name: "Star dashboard with user",
|
||||
params: map[string]string{
|
||||
":id": "1",
|
||||
},
|
||||
signedInUser: &user.SignedInUser{
|
||||
UserID: 1,
|
||||
OrgID: 1,
|
||||
IsAnonymous: false,
|
||||
},
|
||||
expectedStatus: 200,
|
||||
},
|
||||
{
|
||||
name: "Star dashboard with anonymous user",
|
||||
params: map[string]string{
|
||||
":id": "1",
|
||||
},
|
||||
signedInUser: &user.SignedInUser{
|
||||
UserID: 0,
|
||||
OrgID: 1,
|
||||
IsAnonymous: true,
|
||||
},
|
||||
expectedStatus: 400,
|
||||
},
|
||||
{
|
||||
name: "Star dashboard with API Key",
|
||||
params: map[string]string{
|
||||
":id": "1",
|
||||
},
|
||||
signedInUser: &user.SignedInUser{
|
||||
UserID: 0,
|
||||
OrgID: 1,
|
||||
ApiKeyID: 3,
|
||||
IsAnonymous: false,
|
||||
},
|
||||
expectedStatus: 400,
|
||||
},
|
||||
{
|
||||
name: "Star dashboard with Service Account",
|
||||
params: map[string]string{
|
||||
":id": "1",
|
||||
},
|
||||
signedInUser: &user.SignedInUser{
|
||||
UserID: 1,
|
||||
OrgID: 3,
|
||||
IsServiceAccount: true,
|
||||
},
|
||||
expectedStatus: 200,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
req := web.SetURLParams(&http.Request{}, tc.params)
|
||||
c := &contextmodel.ReqContext{SignedInUser: tc.signedInUser, Context: &web.Context{Req: req}}
|
||||
resp := api.StarDashboard(c)
|
||||
assert.Equal(t, tc.expectedStatus, resp.Status())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestStarDashboardUID(t *testing.T) {
|
||||
svc := dashboards.NewFakeDashboardService(t)
|
||||
svc.On("GetDashboard", mock.Anything, mock.Anything).Return(&dashboards.Dashboard{UID: "test", OrgID: 1}, nil)
|
||||
|
||||
Reference in New Issue
Block a user