Chore: Remove bus from Alerting API (#44894)
* assign handlers to httpserver * turn sqlstore mock in to a pointer * add search service interface * fix tests for alerting and other apis * once again, make linter happy
This commit is contained in:
@@ -35,7 +35,7 @@ func TestAdminAPIEndpoint(t *testing.T) {
|
||||
updateCmd := dtos.AdminUpdateUserPermissionsForm{
|
||||
IsGrafanaAdmin: false,
|
||||
}
|
||||
mock := mockstore.SQLStoreMock{
|
||||
mock := &mockstore.SQLStoreMock{
|
||||
ExpectedError: models.ErrLastGrafanaAdmin,
|
||||
}
|
||||
putAdminScenario(t, "When calling PUT on", "/api/admin/users/1/permissions",
|
||||
@@ -60,7 +60,7 @@ func TestAdminAPIEndpoint(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("When a server admin attempts to logout a non-existing user from all devices", func(t *testing.T) {
|
||||
mock := mockstore.SQLStoreMock{
|
||||
mock := &mockstore.SQLStoreMock{
|
||||
ExpectedError: models.ErrUserNotFound,
|
||||
}
|
||||
adminLogoutUserScenario(t, "Should return not found when calling POST on", "/api/admin/users/200/logout",
|
||||
@@ -72,7 +72,7 @@ func TestAdminAPIEndpoint(t *testing.T) {
|
||||
|
||||
t.Run("When a server admin attempts to revoke an auth token for a non-existing user", func(t *testing.T) {
|
||||
cmd := models.RevokeAuthTokenCmd{AuthTokenId: 2}
|
||||
mock := mockstore.SQLStoreMock{
|
||||
mock := &mockstore.SQLStoreMock{
|
||||
ExpectedError: models.ErrUserNotFound,
|
||||
}
|
||||
adminRevokeUserAuthTokenScenario(t, "Should return not found when calling POST on",
|
||||
@@ -83,7 +83,7 @@ func TestAdminAPIEndpoint(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("When a server admin gets auth tokens for a non-existing user", func(t *testing.T) {
|
||||
mock := mockstore.SQLStoreMock{
|
||||
mock := &mockstore.SQLStoreMock{
|
||||
ExpectedError: models.ErrUserNotFound,
|
||||
}
|
||||
adminGetUserAuthTokensScenario(t, "Should return not found when calling GET on",
|
||||
|
||||
+38
-38
@@ -20,7 +20,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/web"
|
||||
)
|
||||
|
||||
func ValidateOrgAlert(c *models.ReqContext) {
|
||||
func (hs *HTTPServer) ValidateOrgAlert(c *models.ReqContext) {
|
||||
id, err := strconv.ParseInt(web.Params(c.Req)[":alertId"], 10, 64)
|
||||
if err != nil {
|
||||
c.JsonApiErr(http.StatusBadRequest, "alertId is invalid", nil)
|
||||
@@ -28,7 +28,7 @@ func ValidateOrgAlert(c *models.ReqContext) {
|
||||
}
|
||||
query := models.GetAlertByIdQuery{Id: id}
|
||||
|
||||
if err := bus.Dispatch(c.Req.Context(), &query); err != nil {
|
||||
if err := hs.SQLStore.GetAlertById(c.Req.Context(), &query); err != nil {
|
||||
c.JsonApiErr(404, "Alert not found", nil)
|
||||
return
|
||||
}
|
||||
@@ -39,7 +39,7 @@ func ValidateOrgAlert(c *models.ReqContext) {
|
||||
}
|
||||
}
|
||||
|
||||
func GetAlertStatesForDashboard(c *models.ReqContext) response.Response {
|
||||
func (hs *HTTPServer) GetAlertStatesForDashboard(c *models.ReqContext) response.Response {
|
||||
dashboardID := c.QueryInt64("dashboardId")
|
||||
|
||||
if dashboardID == 0 {
|
||||
@@ -51,7 +51,7 @@ func GetAlertStatesForDashboard(c *models.ReqContext) response.Response {
|
||||
DashboardId: c.QueryInt64("dashboardId"),
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(c.Req.Context(), &query); err != nil {
|
||||
if err := hs.SQLStore.GetAlertStatesForDashboard(c.Req.Context(), &query); err != nil {
|
||||
return response.Error(500, "Failed to fetch alert states", err)
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@ func GetAlertStatesForDashboard(c *models.ReqContext) response.Response {
|
||||
}
|
||||
|
||||
// GET /api/alerts
|
||||
func GetAlerts(c *models.ReqContext) response.Response {
|
||||
func (hs *HTTPServer) GetAlerts(c *models.ReqContext) response.Response {
|
||||
dashboardQuery := c.Query("dashboardQuery")
|
||||
dashboardTags := c.QueryStrings("dashboardTag")
|
||||
stringDashboardIDs := c.QueryStrings("dashboardId")
|
||||
@@ -94,7 +94,7 @@ func GetAlerts(c *models.ReqContext) response.Response {
|
||||
Permission: models.PERMISSION_VIEW,
|
||||
}
|
||||
|
||||
err := bus.Dispatch(c.Req.Context(), &searchQuery)
|
||||
err := hs.SearchService.SearchHandler(c.Req.Context(), &searchQuery)
|
||||
if err != nil {
|
||||
return response.Error(500, "List alerts failed", err)
|
||||
}
|
||||
@@ -125,7 +125,7 @@ func GetAlerts(c *models.ReqContext) response.Response {
|
||||
query.State = states
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(c.Req.Context(), &query); err != nil {
|
||||
if err := hs.SQLStore.HandleAlertsQuery(c.Req.Context(), &query); err != nil {
|
||||
return response.Error(500, "List alerts failed", err)
|
||||
}
|
||||
|
||||
@@ -181,21 +181,21 @@ func (hs *HTTPServer) AlertTest(c *models.ReqContext) response.Response {
|
||||
}
|
||||
|
||||
// GET /api/alerts/:id
|
||||
func GetAlert(c *models.ReqContext) response.Response {
|
||||
func (hs *HTTPServer) GetAlert(c *models.ReqContext) response.Response {
|
||||
id, err := strconv.ParseInt(web.Params(c.Req)[":alertId"], 10, 64)
|
||||
if err != nil {
|
||||
return response.Error(http.StatusBadRequest, "alertId is invalid", err)
|
||||
}
|
||||
query := models.GetAlertByIdQuery{Id: id}
|
||||
|
||||
if err := bus.Dispatch(c.Req.Context(), &query); err != nil {
|
||||
if err := hs.SQLStore.GetAlertById(c.Req.Context(), &query); err != nil {
|
||||
return response.Error(500, "List alerts failed", err)
|
||||
}
|
||||
|
||||
return response.JSON(200, &query.Result)
|
||||
}
|
||||
|
||||
func GetAlertNotifiers(ngalertEnabled bool) func(*models.ReqContext) response.Response {
|
||||
func (hs *HTTPServer) GetAlertNotifiers(ngalertEnabled bool) func(*models.ReqContext) response.Response {
|
||||
return func(_ *models.ReqContext) response.Response {
|
||||
if ngalertEnabled {
|
||||
return response.JSON(200, notifier.GetAvailableNotifiers())
|
||||
@@ -206,8 +206,8 @@ func GetAlertNotifiers(ngalertEnabled bool) func(*models.ReqContext) response.Re
|
||||
}
|
||||
}
|
||||
|
||||
func GetAlertNotificationLookup(c *models.ReqContext) response.Response {
|
||||
alertNotifications, err := getAlertNotificationsInternal(c)
|
||||
func (hs *HTTPServer) GetAlertNotificationLookup(c *models.ReqContext) response.Response {
|
||||
alertNotifications, err := hs.getAlertNotificationsInternal(c)
|
||||
if err != nil {
|
||||
return response.Error(500, "Failed to get alert notifications", err)
|
||||
}
|
||||
@@ -221,8 +221,8 @@ func GetAlertNotificationLookup(c *models.ReqContext) response.Response {
|
||||
return response.JSON(200, result)
|
||||
}
|
||||
|
||||
func GetAlertNotifications(c *models.ReqContext) response.Response {
|
||||
alertNotifications, err := getAlertNotificationsInternal(c)
|
||||
func (hs *HTTPServer) GetAlertNotifications(c *models.ReqContext) response.Response {
|
||||
alertNotifications, err := hs.getAlertNotificationsInternal(c)
|
||||
if err != nil {
|
||||
return response.Error(500, "Failed to get alert notifications", err)
|
||||
}
|
||||
@@ -236,17 +236,17 @@ func GetAlertNotifications(c *models.ReqContext) response.Response {
|
||||
return response.JSON(200, result)
|
||||
}
|
||||
|
||||
func getAlertNotificationsInternal(c *models.ReqContext) ([]*models.AlertNotification, error) {
|
||||
func (hs *HTTPServer) getAlertNotificationsInternal(c *models.ReqContext) ([]*models.AlertNotification, error) {
|
||||
query := &models.GetAllAlertNotificationsQuery{OrgId: c.OrgId}
|
||||
|
||||
if err := bus.Dispatch(c.Req.Context(), query); err != nil {
|
||||
if err := hs.SQLStore.GetAllAlertNotifications(c.Req.Context(), query); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return query.Result, nil
|
||||
}
|
||||
|
||||
func GetAlertNotificationByID(c *models.ReqContext) response.Response {
|
||||
func (hs *HTTPServer) GetAlertNotificationByID(c *models.ReqContext) response.Response {
|
||||
notificationId, err := strconv.ParseInt(web.Params(c.Req)[":notificationId"], 10, 64)
|
||||
if err != nil {
|
||||
return response.Error(http.StatusBadRequest, "notificationId is invalid", err)
|
||||
@@ -260,7 +260,7 @@ func GetAlertNotificationByID(c *models.ReqContext) response.Response {
|
||||
return response.Error(404, "Alert notification not found", nil)
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(c.Req.Context(), query); err != nil {
|
||||
if err := hs.SQLStore.GetAlertNotifications(c.Req.Context(), query); err != nil {
|
||||
return response.Error(500, "Failed to get alert notifications", err)
|
||||
}
|
||||
|
||||
@@ -271,7 +271,7 @@ func GetAlertNotificationByID(c *models.ReqContext) response.Response {
|
||||
return response.JSON(200, dtos.NewAlertNotification(query.Result))
|
||||
}
|
||||
|
||||
func GetAlertNotificationByUID(c *models.ReqContext) response.Response {
|
||||
func (hs *HTTPServer) GetAlertNotificationByUID(c *models.ReqContext) response.Response {
|
||||
query := &models.GetAlertNotificationsWithUidQuery{
|
||||
OrgId: c.OrgId,
|
||||
Uid: web.Params(c.Req)[":uid"],
|
||||
@@ -281,7 +281,7 @@ func GetAlertNotificationByUID(c *models.ReqContext) response.Response {
|
||||
return response.Error(404, "Alert notification not found", nil)
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(c.Req.Context(), query); err != nil {
|
||||
if err := hs.SQLStore.GetAlertNotificationsWithUid(c.Req.Context(), query); err != nil {
|
||||
return response.Error(500, "Failed to get alert notifications", err)
|
||||
}
|
||||
|
||||
@@ -292,14 +292,14 @@ func GetAlertNotificationByUID(c *models.ReqContext) response.Response {
|
||||
return response.JSON(200, dtos.NewAlertNotification(query.Result))
|
||||
}
|
||||
|
||||
func CreateAlertNotification(c *models.ReqContext) response.Response {
|
||||
func (hs *HTTPServer) CreateAlertNotification(c *models.ReqContext) response.Response {
|
||||
cmd := models.CreateAlertNotificationCommand{}
|
||||
if err := web.Bind(c.Req, &cmd); err != nil {
|
||||
return response.Error(http.StatusBadRequest, "bad request data", err)
|
||||
}
|
||||
cmd.OrgId = c.OrgId
|
||||
|
||||
if err := bus.Dispatch(c.Req.Context(), &cmd); err != nil {
|
||||
if err := hs.SQLStore.CreateAlertNotificationCommand(c.Req.Context(), &cmd); err != nil {
|
||||
if errors.Is(err, models.ErrAlertNotificationWithSameNameExists) || errors.Is(err, models.ErrAlertNotificationWithSameUIDExists) {
|
||||
return response.Error(409, "Failed to create alert notification", err)
|
||||
}
|
||||
@@ -325,7 +325,7 @@ func (hs *HTTPServer) UpdateAlertNotification(c *models.ReqContext) response.Res
|
||||
return response.Error(500, "Failed to update alert notification", err)
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(c.Req.Context(), &cmd); err != nil {
|
||||
if err := hs.SQLStore.UpdateAlertNotification(c.Req.Context(), &cmd); err != nil {
|
||||
if errors.Is(err, models.ErrAlertNotificationNotFound) {
|
||||
return response.Error(404, err.Error(), err)
|
||||
}
|
||||
@@ -341,7 +341,7 @@ func (hs *HTTPServer) UpdateAlertNotification(c *models.ReqContext) response.Res
|
||||
Id: cmd.Id,
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(c.Req.Context(), &query); err != nil {
|
||||
if err := hs.SQLStore.GetAlertNotifications(c.Req.Context(), &query); err != nil {
|
||||
return response.Error(500, "Failed to get alert notification", err)
|
||||
}
|
||||
|
||||
@@ -361,7 +361,7 @@ func (hs *HTTPServer) UpdateAlertNotificationByUID(c *models.ReqContext) respons
|
||||
return response.Error(500, "Failed to update alert notification", err)
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(c.Req.Context(), &cmd); err != nil {
|
||||
if err := hs.SQLStore.UpdateAlertNotificationWithUid(c.Req.Context(), &cmd); err != nil {
|
||||
if errors.Is(err, models.ErrAlertNotificationNotFound) {
|
||||
return response.Error(404, err.Error(), nil)
|
||||
}
|
||||
@@ -373,7 +373,7 @@ func (hs *HTTPServer) UpdateAlertNotificationByUID(c *models.ReqContext) respons
|
||||
Uid: cmd.Uid,
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(c.Req.Context(), &query); err != nil {
|
||||
if err := hs.SQLStore.GetAlertNotificationsWithUid(c.Req.Context(), &query); err != nil {
|
||||
return response.Error(500, "Failed to get alert notification", err)
|
||||
}
|
||||
|
||||
@@ -390,7 +390,7 @@ func (hs *HTTPServer) fillWithSecureSettingsData(ctx context.Context, cmd *model
|
||||
Id: cmd.Id,
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(ctx, query); err != nil {
|
||||
if err := hs.SQLStore.GetAlertNotifications(ctx, query); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -418,7 +418,7 @@ func (hs *HTTPServer) fillWithSecureSettingsDataByUID(ctx context.Context, cmd *
|
||||
Uid: cmd.Uid,
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(ctx, query); err != nil {
|
||||
if err := hs.SQLStore.GetAlertNotificationsWithUid(ctx, query); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -436,7 +436,7 @@ func (hs *HTTPServer) fillWithSecureSettingsDataByUID(ctx context.Context, cmd *
|
||||
return nil
|
||||
}
|
||||
|
||||
func DeleteAlertNotification(c *models.ReqContext) response.Response {
|
||||
func (hs *HTTPServer) DeleteAlertNotification(c *models.ReqContext) response.Response {
|
||||
notificationId, err := strconv.ParseInt(web.Params(c.Req)[":notificationId"], 10, 64)
|
||||
if err != nil {
|
||||
return response.Error(http.StatusBadRequest, "notificationId is invalid", err)
|
||||
@@ -447,7 +447,7 @@ func DeleteAlertNotification(c *models.ReqContext) response.Response {
|
||||
Id: notificationId,
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(c.Req.Context(), &cmd); err != nil {
|
||||
if err := hs.SQLStore.DeleteAlertNotification(c.Req.Context(), &cmd); err != nil {
|
||||
if errors.Is(err, models.ErrAlertNotificationNotFound) {
|
||||
return response.Error(404, err.Error(), nil)
|
||||
}
|
||||
@@ -457,13 +457,13 @@ func DeleteAlertNotification(c *models.ReqContext) response.Response {
|
||||
return response.Success("Notification deleted")
|
||||
}
|
||||
|
||||
func DeleteAlertNotificationByUID(c *models.ReqContext) response.Response {
|
||||
func (hs *HTTPServer) DeleteAlertNotificationByUID(c *models.ReqContext) response.Response {
|
||||
cmd := models.DeleteAlertNotificationWithUidCommand{
|
||||
OrgId: c.OrgId,
|
||||
Uid: web.Params(c.Req)[":uid"],
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(c.Req.Context(), &cmd); err != nil {
|
||||
if err := hs.SQLStore.DeleteAlertNotificationWithUid(c.Req.Context(), &cmd); err != nil {
|
||||
if errors.Is(err, models.ErrAlertNotificationNotFound) {
|
||||
return response.Error(404, err.Error(), nil)
|
||||
}
|
||||
@@ -477,7 +477,7 @@ func DeleteAlertNotificationByUID(c *models.ReqContext) response.Response {
|
||||
}
|
||||
|
||||
// POST /api/alert-notifications/test
|
||||
func NotificationTest(c *models.ReqContext) response.Response {
|
||||
func (hs *HTTPServer) NotificationTest(c *models.ReqContext) response.Response {
|
||||
dto := dtos.NotificationTestCommand{}
|
||||
if err := web.Bind(c.Req, &dto); err != nil {
|
||||
return response.Error(http.StatusBadRequest, "bad request data", err)
|
||||
@@ -507,7 +507,7 @@ func NotificationTest(c *models.ReqContext) response.Response {
|
||||
}
|
||||
|
||||
// POST /api/alerts/:alertId/pause
|
||||
func PauseAlert(c *models.ReqContext) response.Response {
|
||||
func (hs *HTTPServer) PauseAlert(c *models.ReqContext) response.Response {
|
||||
dto := dtos.PauseAlertCommand{}
|
||||
if err := web.Bind(c.Req, &dto); err != nil {
|
||||
return response.Error(http.StatusBadRequest, "bad request data", err)
|
||||
@@ -520,7 +520,7 @@ func PauseAlert(c *models.ReqContext) response.Response {
|
||||
result["alertId"] = alertID
|
||||
|
||||
query := models.GetAlertByIdQuery{Id: alertID}
|
||||
if err := bus.Dispatch(c.Req.Context(), &query); err != nil {
|
||||
if err := hs.SQLStore.GetAlertById(c.Req.Context(), &query); err != nil {
|
||||
return response.Error(500, "Get Alert failed", err)
|
||||
}
|
||||
|
||||
@@ -550,7 +550,7 @@ func PauseAlert(c *models.ReqContext) response.Response {
|
||||
Paused: dto.Paused,
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(c.Req.Context(), &cmd); err != nil {
|
||||
if err := hs.SQLStore.PauseAlert(c.Req.Context(), &cmd); err != nil {
|
||||
return response.Error(500, "", err)
|
||||
}
|
||||
|
||||
@@ -567,7 +567,7 @@ func PauseAlert(c *models.ReqContext) response.Response {
|
||||
}
|
||||
|
||||
// POST /api/admin/pause-all-alerts
|
||||
func PauseAllAlerts(c *models.ReqContext) response.Response {
|
||||
func (hs *HTTPServer) PauseAllAlerts(c *models.ReqContext) response.Response {
|
||||
dto := dtos.PauseAllAlertsCommand{}
|
||||
if err := web.Bind(c.Req, &dto); err != nil {
|
||||
return response.Error(http.StatusBadRequest, "bad request data", err)
|
||||
@@ -576,7 +576,7 @@ func PauseAllAlerts(c *models.ReqContext) response.Response {
|
||||
Paused: dto.Paused,
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(c.Req.Context(), &updateCmd); err != nil {
|
||||
if err := hs.SQLStore.PauseAllAlerts(c.Req.Context(), &updateCmd); err != nil {
|
||||
return response.Error(500, "Failed to pause alerts", err)
|
||||
}
|
||||
|
||||
|
||||
+64
-90
@@ -17,44 +17,55 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
var (
|
||||
viewerRole = models.ROLE_VIEWER
|
||||
editorRole = models.ROLE_EDITOR
|
||||
)
|
||||
|
||||
type setUpConf struct {
|
||||
aclMockResp []*models.DashboardAclInfoDTO
|
||||
}
|
||||
|
||||
func TestAlertingAPIEndpoint(t *testing.T) {
|
||||
type mockSearchService struct{ ExpectedResult search.HitList }
|
||||
|
||||
func (mss *mockSearchService) SearchHandler(_ context.Context, q *search.Query) error {
|
||||
q.Result = mss.ExpectedResult
|
||||
return nil
|
||||
}
|
||||
func (mss *mockSearchService) SortOptions() []search.SortOption { return nil }
|
||||
|
||||
func setUp(confs ...setUpConf) *HTTPServer {
|
||||
singleAlert := &models.Alert{Id: 1, DashboardId: 1, Name: "singlealert"}
|
||||
viewerRole := models.ROLE_VIEWER
|
||||
editorRole := models.ROLE_EDITOR
|
||||
store := mockstore.NewSQLStoreMock()
|
||||
hs := &HTTPServer{SQLStore: store, SearchService: &mockSearchService{}}
|
||||
store.ExpectedAlert = singleAlert
|
||||
|
||||
setUp := func(confs ...setUpConf) {
|
||||
bus.AddHandler("test", func(ctx context.Context, query *models.GetAlertByIdQuery) error {
|
||||
query.Result = singleAlert
|
||||
return nil
|
||||
})
|
||||
|
||||
aclMockResp := []*models.DashboardAclInfoDTO{}
|
||||
for _, c := range confs {
|
||||
if c.aclMockResp != nil {
|
||||
aclMockResp = c.aclMockResp
|
||||
}
|
||||
aclMockResp := []*models.DashboardAclInfoDTO{}
|
||||
for _, c := range confs {
|
||||
if c.aclMockResp != nil {
|
||||
aclMockResp = c.aclMockResp
|
||||
}
|
||||
bus.AddHandler("test", func(ctx context.Context, query *models.GetDashboardAclInfoListQuery) error {
|
||||
query.Result = aclMockResp
|
||||
return nil
|
||||
})
|
||||
|
||||
bus.AddHandler("test", func(ctx context.Context, query *models.GetTeamsByUserQuery) error {
|
||||
query.Result = []*models.TeamDTO{}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
bus.AddHandler("test", func(ctx context.Context, query *models.GetDashboardAclInfoListQuery) error {
|
||||
query.Result = aclMockResp
|
||||
return nil
|
||||
})
|
||||
|
||||
bus.AddHandler("test", func(ctx context.Context, query *models.GetTeamsByUserQuery) error {
|
||||
query.Result = []*models.TeamDTO{}
|
||||
return nil
|
||||
})
|
||||
return hs
|
||||
}
|
||||
|
||||
func TestAlertingAPIEndpoint(t *testing.T) {
|
||||
t.Run("When user is editor and not in the ACL", func(t *testing.T) {
|
||||
hs := setUp()
|
||||
cmd := dtos.PauseAlertCommand{
|
||||
AlertId: 1,
|
||||
Paused: true,
|
||||
}
|
||||
postAlertScenario(t, "When calling POST on", "/api/alerts/1/pause", "/api/alerts/:alertId/pause",
|
||||
postAlertScenario(t, hs, "When calling POST on", "/api/alerts/1/pause", "/api/alerts/:alertId/pause",
|
||||
models.ROLE_EDITOR, cmd, func(sc *scenarioContext) {
|
||||
setUp()
|
||||
|
||||
@@ -64,11 +75,12 @@ func TestAlertingAPIEndpoint(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("When user is editor and dashboard has default ACL", func(t *testing.T) {
|
||||
hs := setUp()
|
||||
cmd := dtos.PauseAlertCommand{
|
||||
AlertId: 1,
|
||||
Paused: true,
|
||||
}
|
||||
postAlertScenario(t, "When calling POST on", "/api/alerts/1/pause", "/api/alerts/:alertId/pause",
|
||||
postAlertScenario(t, hs, "When calling POST on", "/api/alerts/1/pause", "/api/alerts/:alertId/pause",
|
||||
models.ROLE_EDITOR, cmd, func(sc *scenarioContext) {
|
||||
setUp(setUpConf{
|
||||
aclMockResp: []*models.DashboardAclInfoDTO{
|
||||
@@ -82,76 +94,38 @@ func TestAlertingAPIEndpoint(t *testing.T) {
|
||||
})
|
||||
})
|
||||
|
||||
mock := mockstore.NewSQLStoreMock()
|
||||
loggedInUserScenarioWithRole(t, "When calling GET on", "GET", "/api/alerts?dashboardId=1", "/api/alerts",
|
||||
models.ROLE_EDITOR, func(sc *scenarioContext) {
|
||||
setUp()
|
||||
|
||||
var searchQuery *search.Query
|
||||
bus.AddHandler("test", func(ctx context.Context, query *search.Query) error {
|
||||
searchQuery = query
|
||||
return nil
|
||||
})
|
||||
|
||||
var getAlertsQuery *models.GetAlertsQuery
|
||||
bus.AddHandler("test", func(ctx context.Context, query *models.GetAlertsQuery) error {
|
||||
getAlertsQuery = query
|
||||
return nil
|
||||
})
|
||||
|
||||
sc.handlerFunc = GetAlerts
|
||||
sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
|
||||
|
||||
require.Nil(t, searchQuery)
|
||||
assert.NotNil(t, getAlertsQuery)
|
||||
}, mock)
|
||||
|
||||
loggedInUserScenarioWithRole(t, "When calling GET on", "GET",
|
||||
"/api/alerts?dashboardId=1&dashboardId=2&folderId=3&dashboardTag=abc&dashboardQuery=dbQuery&limit=5&query=alertQuery",
|
||||
"/api/alerts", models.ROLE_EDITOR, func(sc *scenarioContext) {
|
||||
setUp()
|
||||
|
||||
var searchQuery *search.Query
|
||||
bus.AddHandler("test", func(ctx context.Context, query *search.Query) error {
|
||||
searchQuery = query
|
||||
query.Result = search.HitList{
|
||||
t.Run("When calling GET", func(t *testing.T) {
|
||||
hs := setUp()
|
||||
loggedInUserScenarioWithRole(t, "When calling GET on", "GET",
|
||||
"/api/alerts?dashboardId=1&dashboardId=2&folderId=3&dashboardTag=abc&dashboardQuery=dbQuery&limit=5&query=alertQuery",
|
||||
"/api/alerts", models.ROLE_EDITOR, func(sc *scenarioContext) {
|
||||
hs.SearchService.(*mockSearchService).ExpectedResult = search.HitList{
|
||||
&search.Hit{ID: 1},
|
||||
&search.Hit{ID: 2},
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
var getAlertsQuery *models.GetAlertsQuery
|
||||
bus.AddHandler("test", func(ctx context.Context, query *models.GetAlertsQuery) error {
|
||||
getAlertsQuery = query
|
||||
return nil
|
||||
})
|
||||
sc.handlerFunc = hs.GetAlerts
|
||||
sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
|
||||
|
||||
sc.handlerFunc = GetAlerts
|
||||
sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
|
||||
getAlertsQuery := hs.SQLStore.(*mockstore.SQLStoreMock).LastGetAlertsQuery
|
||||
|
||||
require.NotNil(t, searchQuery)
|
||||
assert.Equal(t, int64(1), searchQuery.DashboardIds[0])
|
||||
assert.Equal(t, int64(2), searchQuery.DashboardIds[1])
|
||||
assert.Equal(t, int64(3), searchQuery.FolderIds[0])
|
||||
assert.Equal(t, "abc", searchQuery.Tags[0])
|
||||
assert.Equal(t, "dbQuery", searchQuery.Title)
|
||||
require.NotNil(t, getAlertsQuery)
|
||||
assert.Equal(t, int64(1), getAlertsQuery.DashboardIDs[0])
|
||||
assert.Equal(t, int64(2), getAlertsQuery.DashboardIDs[1])
|
||||
assert.Equal(t, int64(5), getAlertsQuery.Limit)
|
||||
assert.Equal(t, "alertQuery", getAlertsQuery.Query)
|
||||
}, hs.SQLStore)
|
||||
})
|
||||
|
||||
require.NotNil(t, getAlertsQuery)
|
||||
assert.Equal(t, int64(1), getAlertsQuery.DashboardIDs[0])
|
||||
assert.Equal(t, int64(2), getAlertsQuery.DashboardIDs[1])
|
||||
assert.Equal(t, int64(5), getAlertsQuery.Limit)
|
||||
assert.Equal(t, "alertQuery", getAlertsQuery.Query)
|
||||
}, mock)
|
||||
|
||||
loggedInUserScenarioWithRole(t, "When calling GET on", "GET", "/api/alert-notifications/1",
|
||||
"/alert-notifications/:notificationId", models.ROLE_ADMIN, func(sc *scenarioContext) {
|
||||
setUp()
|
||||
|
||||
sc.handlerFunc = GetAlertNotificationByID
|
||||
sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
|
||||
assert.Equal(t, 404, sc.resp.Code)
|
||||
}, mock)
|
||||
t.Run("When calling GET on alert-notifications", func(t *testing.T) {
|
||||
hs := setUp()
|
||||
loggedInUserScenarioWithRole(t, "When calling GET on", "GET", "/api/alert-notifications/1",
|
||||
"/alert-notifications/:notificationId", models.ROLE_ADMIN, func(sc *scenarioContext) {
|
||||
sc.handlerFunc = hs.GetAlertNotificationByID
|
||||
sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
|
||||
assert.Equal(t, 404, sc.resp.Code)
|
||||
}, hs.SQLStore)
|
||||
})
|
||||
}
|
||||
|
||||
func callPauseAlert(sc *scenarioContext) {
|
||||
@@ -162,7 +136,7 @@ func callPauseAlert(sc *scenarioContext) {
|
||||
sc.fakeReqWithParams("POST", sc.url, map[string]string{}).exec()
|
||||
}
|
||||
|
||||
func postAlertScenario(t *testing.T, desc string, url string, routePattern string, role models.RoleType,
|
||||
func postAlertScenario(t *testing.T, hs *HTTPServer, desc string, url string, routePattern string, role models.RoleType,
|
||||
cmd dtos.PauseAlertCommand, fn scenarioFunc) {
|
||||
t.Run(fmt.Sprintf("%s %s", desc, url), func(t *testing.T) {
|
||||
defer bus.ClearBusHandlers()
|
||||
@@ -175,7 +149,7 @@ func postAlertScenario(t *testing.T, desc string, url string, routePattern strin
|
||||
sc.context.OrgId = testOrgID
|
||||
sc.context.OrgRole = role
|
||||
|
||||
return PauseAlert(c)
|
||||
return hs.PauseAlert(c)
|
||||
})
|
||||
|
||||
sc.m.Post(routePattern, sc.defaultHandler)
|
||||
|
||||
+14
-14
@@ -383,31 +383,31 @@ func (hs *HTTPServer) registerRoutes() {
|
||||
|
||||
apiRoute.Group("/alerts", func(alertsRoute routing.RouteRegister) {
|
||||
alertsRoute.Post("/test", routing.Wrap(hs.AlertTest))
|
||||
alertsRoute.Post("/:alertId/pause", reqEditorRole, routing.Wrap(PauseAlert))
|
||||
alertsRoute.Get("/:alertId", ValidateOrgAlert, routing.Wrap(GetAlert))
|
||||
alertsRoute.Get("/", routing.Wrap(GetAlerts))
|
||||
alertsRoute.Get("/states-for-dashboard", routing.Wrap(GetAlertStatesForDashboard))
|
||||
alertsRoute.Post("/:alertId/pause", reqEditorRole, routing.Wrap(hs.PauseAlert))
|
||||
alertsRoute.Get("/:alertId", hs.ValidateOrgAlert, routing.Wrap(hs.GetAlert))
|
||||
alertsRoute.Get("/", routing.Wrap(hs.GetAlerts))
|
||||
alertsRoute.Get("/states-for-dashboard", routing.Wrap(hs.GetAlertStatesForDashboard))
|
||||
})
|
||||
|
||||
apiRoute.Get("/alert-notifiers", reqEditorRole, routing.Wrap(
|
||||
GetAlertNotifiers(hs.Cfg.UnifiedAlerting.IsEnabled())),
|
||||
hs.GetAlertNotifiers(hs.Cfg.UnifiedAlerting.IsEnabled())),
|
||||
)
|
||||
|
||||
apiRoute.Group("/alert-notifications", func(alertNotifications routing.RouteRegister) {
|
||||
alertNotifications.Get("/", routing.Wrap(GetAlertNotifications))
|
||||
alertNotifications.Post("/test", routing.Wrap(NotificationTest))
|
||||
alertNotifications.Post("/", routing.Wrap(CreateAlertNotification))
|
||||
alertNotifications.Get("/", routing.Wrap(hs.GetAlertNotifications))
|
||||
alertNotifications.Post("/test", routing.Wrap(hs.NotificationTest))
|
||||
alertNotifications.Post("/", routing.Wrap(hs.CreateAlertNotification))
|
||||
alertNotifications.Put("/:notificationId", routing.Wrap(hs.UpdateAlertNotification))
|
||||
alertNotifications.Get("/:notificationId", routing.Wrap(GetAlertNotificationByID))
|
||||
alertNotifications.Delete("/:notificationId", routing.Wrap(DeleteAlertNotification))
|
||||
alertNotifications.Get("/uid/:uid", routing.Wrap(GetAlertNotificationByUID))
|
||||
alertNotifications.Get("/:notificationId", routing.Wrap(hs.GetAlertNotificationByID))
|
||||
alertNotifications.Delete("/:notificationId", routing.Wrap(hs.DeleteAlertNotification))
|
||||
alertNotifications.Get("/uid/:uid", routing.Wrap(hs.GetAlertNotificationByUID))
|
||||
alertNotifications.Put("/uid/:uid", routing.Wrap(hs.UpdateAlertNotificationByUID))
|
||||
alertNotifications.Delete("/uid/:uid", routing.Wrap(DeleteAlertNotificationByUID))
|
||||
alertNotifications.Delete("/uid/:uid", routing.Wrap(hs.DeleteAlertNotificationByUID))
|
||||
}, reqEditorRole)
|
||||
|
||||
// alert notifications without requirement of user to be org editor
|
||||
apiRoute.Group("/alert-notifications", func(orgRoute routing.RouteRegister) {
|
||||
orgRoute.Get("/lookup", routing.Wrap(GetAlertNotificationLookup))
|
||||
orgRoute.Get("/lookup", routing.Wrap(hs.GetAlertNotificationLookup))
|
||||
})
|
||||
|
||||
apiRoute.Get("/annotations", routing.Wrap(GetAnnotations))
|
||||
@@ -464,7 +464,7 @@ func (hs *HTTPServer) registerRoutes() {
|
||||
adminRoute.Get("/settings/features", authorize(reqGrafanaAdmin, ac.EvalPermission(ac.ActionSettingsRead)), hs.Features.HandleGetSettings)
|
||||
}
|
||||
adminRoute.Get("/stats", authorize(reqGrafanaAdmin, ac.EvalPermission(ac.ActionServerStatsRead)), routing.Wrap(AdminGetStats))
|
||||
adminRoute.Post("/pause-all-alerts", reqGrafanaAdmin, routing.Wrap(PauseAllAlerts))
|
||||
adminRoute.Post("/pause-all-alerts", reqGrafanaAdmin, routing.Wrap(hs.PauseAllAlerts))
|
||||
|
||||
if hs.ThumbService != nil {
|
||||
adminRoute.Post("/crawler/start", reqGrafanaAdmin, routing.Wrap(hs.ThumbService.StartCrawler))
|
||||
|
||||
@@ -100,7 +100,7 @@ type HTTPServer struct {
|
||||
pluginDashboardManager plugins.PluginDashboardManager
|
||||
pluginStaticRouteResolver plugins.StaticRouteResolver
|
||||
pluginErrorResolver plugins.ErrorResolver
|
||||
SearchService *search.SearchService
|
||||
SearchService search.Service
|
||||
ShortURLService shorturls.Service
|
||||
QueryHistoryService queryhistory.Service
|
||||
Live *live.GrafanaLive
|
||||
|
||||
@@ -35,7 +35,7 @@ func TestTeamAPIEndpoint(t *testing.T) {
|
||||
t.Run("Given two teams", func(t *testing.T) {
|
||||
hs := setupSimpleHTTPServer(nil)
|
||||
hs.SQLStore = sqlstore.InitTestDB(t)
|
||||
mock := mockstore.SQLStoreMock{}
|
||||
mock := &mockstore.SQLStoreMock{}
|
||||
loggedInUserScenario(t, "When calling GET on", "/api/teams/search", "/api/teams/search", func(sc *scenarioContext) {
|
||||
_, err := hs.SQLStore.CreateTeam("team1", "", 1)
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -29,7 +29,7 @@ func TestUserTokenAPIEndpoint(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("When current user gets auth tokens for a non-existing user", func(t *testing.T) {
|
||||
mock := mockstore.SQLStoreMock{
|
||||
mock := &mockstore.SQLStoreMock{
|
||||
ExpectedUser: &models.User{Id: 200},
|
||||
ExpectedError: models.ErrUserNotFound,
|
||||
}
|
||||
@@ -40,7 +40,7 @@ func TestUserTokenAPIEndpoint(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("When logging out an existing user from all devices", func(t *testing.T) {
|
||||
mock := mockstore.SQLStoreMock{
|
||||
mock := &mockstore.SQLStoreMock{
|
||||
ExpectedUser: &models.User{Id: 200},
|
||||
}
|
||||
logoutUserFromAllDevicesInternalScenario(t, "Should be successful", 1, func(sc *scenarioContext) {
|
||||
@@ -61,7 +61,7 @@ func TestUserTokenAPIEndpoint(t *testing.T) {
|
||||
t.Run("When revoke an auth token for a user", func(t *testing.T) {
|
||||
cmd := models.RevokeAuthTokenCmd{AuthTokenId: 2}
|
||||
token := &models.UserToken{Id: 1}
|
||||
mock := mockstore.SQLStoreMock{
|
||||
mock := &mockstore.SQLStoreMock{
|
||||
ExpectedUser: &models.User{Id: 200},
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user