alerting_test.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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/middleware"
  7. m "github.com/grafana/grafana/pkg/models"
  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 := &m.Alert{Id: 1, DashboardId: 1, Name: "singlealert"}
  13. bus.AddHandler("test", func(query *m.GetAlertByIdQuery) error {
  14. query.Result = singleAlert
  15. return nil
  16. })
  17. viewerRole := m.ROLE_VIEWER
  18. editorRole := m.ROLE_EDITOR
  19. aclMockResp := []*m.DashboardAclInfoDTO{}
  20. bus.AddHandler("test", func(query *m.GetDashboardAclInfoListQuery) error {
  21. query.Result = aclMockResp
  22. return nil
  23. })
  24. bus.AddHandler("test", func(query *m.GetTeamsByUserQuery) error {
  25. query.Result = []*m.Team{}
  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", m.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 = []*m.DashboardAclInfoDTO{
  42. {Role: &viewerRole, Permission: m.PERMISSION_VIEW},
  43. {Role: &editorRole, Permission: m.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", m.ROLE_EDITOR, cmd, func(sc *scenarioContext) {
  51. CallPauseAlert(sc)
  52. So(sc.resp.Code, ShouldEqual, 200)
  53. })
  54. })
  55. })
  56. })
  57. }
  58. func CallPauseAlert(sc *scenarioContext) {
  59. bus.AddHandler("test", func(cmd *m.PauseAlertCommand) error {
  60. return nil
  61. })
  62. sc.fakeReqWithParams("POST", sc.url, map[string]string{}).exec()
  63. }
  64. func postAlertScenario(desc string, url string, routePattern string, role m.RoleType, cmd dtos.PauseAlertCommand, fn scenarioFunc) {
  65. Convey(desc+" "+url, func() {
  66. defer bus.ClearBusHandlers()
  67. sc := setupScenarioContext(url)
  68. sc.defaultHandler = wrap(func(c *middleware.Context) Response {
  69. sc.context = c
  70. sc.context.UserId = TestUserID
  71. sc.context.OrgId = TestOrgID
  72. sc.context.OrgRole = role
  73. return PauseAlert(c, cmd)
  74. })
  75. sc.m.Post(routePattern, sc.defaultHandler)
  76. fn(sc)
  77. })
  78. }