alerting_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package api
  2. import (
  3. "testing"
  4. "github.com/grafana/grafana/pkg/api/dtos"
  5. "github.com/grafana/grafana/pkg/bus"
  6. "github.com/grafana/grafana/pkg/models"
  7. "github.com/grafana/grafana/pkg/services/search"
  8. . "github.com/smartystreets/goconvey/convey"
  9. )
  10. func TestAlertingApiEndpoint(t *testing.T) {
  11. Convey("Given an alert in a dashboard with an acl", t, func() {
  12. singleAlert := &models.Alert{Id: 1, DashboardId: 1, Name: "singlealert"}
  13. bus.AddHandler("test", func(query *models.GetAlertByIdQuery) error {
  14. query.Result = singleAlert
  15. return nil
  16. })
  17. viewerRole := models.ROLE_VIEWER
  18. editorRole := models.ROLE_EDITOR
  19. aclMockResp := []*models.DashboardAclInfoDTO{}
  20. bus.AddHandler("test", func(query *models.GetDashboardAclInfoListQuery) error {
  21. query.Result = aclMockResp
  22. return nil
  23. })
  24. bus.AddHandler("test", func(query *models.GetTeamsByUserQuery) error {
  25. query.Result = []*models.TeamDTO{}
  26. return nil
  27. })
  28. Convey("When user is editor and not in the ACL", func() {
  29. Convey("Should not be able to pause the alert", func() {
  30. cmd := dtos.PauseAlertCommand{
  31. AlertId: 1,
  32. Paused: true,
  33. }
  34. postAlertScenario("When calling POST on", "/api/alerts/1/pause", "/api/alerts/:alertId/pause", models.ROLE_EDITOR, cmd, func(sc *scenarioContext) {
  35. CallPauseAlert(sc)
  36. So(sc.resp.Code, ShouldEqual, 403)
  37. })
  38. })
  39. })
  40. Convey("When user is editor and dashboard has default ACL", func() {
  41. aclMockResp = []*models.DashboardAclInfoDTO{
  42. {Role: &viewerRole, Permission: models.PERMISSION_VIEW},
  43. {Role: &editorRole, Permission: models.PERMISSION_EDIT},
  44. }
  45. Convey("Should be able to pause the alert", func() {
  46. cmd := dtos.PauseAlertCommand{
  47. AlertId: 1,
  48. Paused: true,
  49. }
  50. postAlertScenario("When calling POST on", "/api/alerts/1/pause", "/api/alerts/:alertId/pause", models.ROLE_EDITOR, cmd, func(sc *scenarioContext) {
  51. CallPauseAlert(sc)
  52. So(sc.resp.Code, ShouldEqual, 200)
  53. })
  54. })
  55. })
  56. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/alerts?dashboardId=1", "/api/alerts", models.ROLE_EDITOR, func(sc *scenarioContext) {
  57. var searchQuery *search.Query
  58. bus.AddHandler("test", func(query *search.Query) error {
  59. searchQuery = query
  60. return nil
  61. })
  62. var getAlertsQuery *models.GetAlertsQuery
  63. bus.AddHandler("test", func(query *models.GetAlertsQuery) error {
  64. getAlertsQuery = query
  65. return nil
  66. })
  67. sc.handlerFunc = GetAlerts
  68. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  69. So(searchQuery, ShouldBeNil)
  70. So(getAlertsQuery, ShouldNotBeNil)
  71. })
  72. loggedInUserScenarioWithRole("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) {
  73. var searchQuery *search.Query
  74. bus.AddHandler("test", func(query *search.Query) error {
  75. searchQuery = query
  76. query.Result = search.HitList{
  77. &search.Hit{Id: 1},
  78. &search.Hit{Id: 2},
  79. }
  80. return nil
  81. })
  82. var getAlertsQuery *models.GetAlertsQuery
  83. bus.AddHandler("test", func(query *models.GetAlertsQuery) error {
  84. getAlertsQuery = query
  85. return nil
  86. })
  87. sc.handlerFunc = GetAlerts
  88. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  89. So(searchQuery, ShouldNotBeNil)
  90. So(searchQuery.DashboardIds[0], ShouldEqual, 1)
  91. So(searchQuery.DashboardIds[1], ShouldEqual, 2)
  92. So(searchQuery.FolderIds[0], ShouldEqual, 3)
  93. So(searchQuery.Tags[0], ShouldEqual, "abc")
  94. So(searchQuery.Title, ShouldEqual, "dbQuery")
  95. So(getAlertsQuery, ShouldNotBeNil)
  96. So(getAlertsQuery.DashboardIDs[0], ShouldEqual, 1)
  97. So(getAlertsQuery.DashboardIDs[1], ShouldEqual, 2)
  98. So(getAlertsQuery.Limit, ShouldEqual, 5)
  99. So(getAlertsQuery.Query, ShouldEqual, "alertQuery")
  100. })
  101. loggedInUserScenarioWithRole("When calling GET on", "GET", "/api/alert-notifications/1", "/alert-notifications/:notificationId", models.ROLE_ADMIN, func(sc *scenarioContext) {
  102. sc.handlerFunc = GetAlertNotificationByID
  103. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  104. So(sc.resp.Code, ShouldEqual, 404)
  105. })
  106. })
  107. }
  108. func CallPauseAlert(sc *scenarioContext) {
  109. bus.AddHandler("test", func(cmd *models.PauseAlertCommand) error {
  110. return nil
  111. })
  112. sc.fakeReqWithParams("POST", sc.url, map[string]string{}).exec()
  113. }
  114. func postAlertScenario(desc string, url string, routePattern string, role models.RoleType, cmd dtos.PauseAlertCommand, fn scenarioFunc) {
  115. Convey(desc+" "+url, func() {
  116. defer bus.ClearBusHandlers()
  117. sc := setupScenarioContext(url)
  118. sc.defaultHandler = Wrap(func(c *models.ReqContext) Response {
  119. sc.context = c
  120. sc.context.UserId = TestUserID
  121. sc.context.OrgId = TestOrgID
  122. sc.context.OrgRole = role
  123. return PauseAlert(c, cmd)
  124. })
  125. sc.m.Post(routePattern, sc.defaultHandler)
  126. fn(sc)
  127. })
  128. }