alerting_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package api
  2. import (
  3. "testing"
  4. "github.com/grafana/grafana/pkg/api/dtos"
  5. "github.com/grafana/grafana/pkg/bus"
  6. m "github.com/grafana/grafana/pkg/models"
  7. . "github.com/smartystreets/goconvey/convey"
  8. )
  9. func TestAlertingApiEndpoint(t *testing.T) {
  10. Convey("Given an alert in a dashboard with an acl", t, func() {
  11. singleAlert := &m.Alert{Id: 1, DashboardId: 1, Name: "singlealert"}
  12. bus.AddHandler("test", func(query *m.GetAlertByIdQuery) error {
  13. query.Result = singleAlert
  14. return nil
  15. })
  16. viewerRole := m.ROLE_VIEWER
  17. editorRole := m.ROLE_EDITOR
  18. aclMockResp := []*m.DashboardAclInfoDTO{}
  19. bus.AddHandler("test", func(query *m.GetDashboardAclInfoListQuery) error {
  20. query.Result = aclMockResp
  21. return nil
  22. })
  23. bus.AddHandler("test", func(query *m.GetTeamsByUserQuery) error {
  24. query.Result = []*m.Team{}
  25. return nil
  26. })
  27. Convey("When user is editor and not in the ACL", func() {
  28. Convey("Should not be able to pause the alert", func() {
  29. cmd := dtos.PauseAlertCommand{
  30. AlertId: 1,
  31. Paused: true,
  32. }
  33. postAlertScenario("When calling POST on", "/api/alerts/1/pause", "/api/alerts/:alertId/pause", m.ROLE_EDITOR, cmd, func(sc *scenarioContext) {
  34. CallPauseAlert(sc)
  35. So(sc.resp.Code, ShouldEqual, 403)
  36. })
  37. })
  38. })
  39. Convey("When user is editor and dashboard has default ACL", func() {
  40. aclMockResp = []*m.DashboardAclInfoDTO{
  41. {Role: &viewerRole, Permission: m.PERMISSION_VIEW},
  42. {Role: &editorRole, Permission: m.PERMISSION_EDIT},
  43. }
  44. Convey("Should be able to pause the alert", func() {
  45. cmd := dtos.PauseAlertCommand{
  46. AlertId: 1,
  47. Paused: true,
  48. }
  49. postAlertScenario("When calling POST on", "/api/alerts/1/pause", "/api/alerts/:alertId/pause", m.ROLE_EDITOR, cmd, func(sc *scenarioContext) {
  50. CallPauseAlert(sc)
  51. So(sc.resp.Code, ShouldEqual, 200)
  52. })
  53. })
  54. })
  55. })
  56. }
  57. func CallPauseAlert(sc *scenarioContext) {
  58. bus.AddHandler("test", func(cmd *m.PauseAlertCommand) error {
  59. return nil
  60. })
  61. sc.fakeReqWithParams("POST", sc.url, map[string]string{}).exec()
  62. }
  63. func postAlertScenario(desc string, url string, routePattern string, role m.RoleType, cmd dtos.PauseAlertCommand, fn scenarioFunc) {
  64. Convey(desc+" "+url, func() {
  65. defer bus.ClearBusHandlers()
  66. sc := setupScenarioContext(url)
  67. sc.defaultHandler = wrap(func(c *m.ReqContext) Response {
  68. sc.context = c
  69. sc.context.UserId = TestUserID
  70. sc.context.OrgId = TestOrgID
  71. sc.context.OrgRole = role
  72. return PauseAlert(c, cmd)
  73. })
  74. sc.m.Post(routePattern, sc.defaultHandler)
  75. fn(sc)
  76. })
  77. }