dashboard_acl_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package api
  2. import (
  3. "testing"
  4. "github.com/grafana/grafana/pkg/bus"
  5. "github.com/grafana/grafana/pkg/components/simplejson"
  6. "github.com/grafana/grafana/pkg/models"
  7. . "github.com/smartystreets/goconvey/convey"
  8. )
  9. func TestDashboardAclApiEndpoint(t *testing.T) {
  10. Convey("Given a dashboard acl", t, func() {
  11. mockResult := []*models.DashboardAclInfoDTO{
  12. {Id: 1, OrgId: 1, DashboardId: 1, UserId: 2, Permissions: models.PERMISSION_EDIT},
  13. {Id: 2, OrgId: 1, DashboardId: 1, UserId: 3, Permissions: models.PERMISSION_VIEW},
  14. }
  15. bus.AddHandler("test", func(query *models.GetDashboardPermissionsQuery) error {
  16. query.Result = mockResult
  17. return nil
  18. })
  19. Convey("When user is org admin", func() {
  20. loggedInUserScenarioWithRole("When calling GET on", "/api/dashboard/1/acl", models.ROLE_ADMIN, func(sc *scenarioContext) {
  21. Convey("Should be able to access ACL", func() {
  22. sc.handlerFunc = GetDashboardAcl
  23. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  24. So(sc.resp.Code, ShouldEqual, 200)
  25. respJSON, err := simplejson.NewJson(sc.resp.Body.Bytes())
  26. So(err, ShouldBeNil)
  27. So(respJSON.GetIndex(0).Get("userId").MustInt(), ShouldEqual, 2)
  28. So(respJSON.GetIndex(0).Get("permissions").MustInt(), ShouldEqual, models.PERMISSION_EDIT)
  29. })
  30. })
  31. })
  32. Convey("When user is editor and not in the ACL", func() {
  33. loggedInUserScenarioWithRole("When calling GET on", "/api/dashboard/1/acl", models.ROLE_EDITOR, func(sc *scenarioContext) {
  34. bus.AddHandler("test2", func(query *models.GetAllowedDashboardsQuery) error {
  35. query.Result = []int64{1}
  36. return nil
  37. })
  38. Convey("Should not be able to access ACL", func() {
  39. sc.handlerFunc = GetDashboardAcl
  40. sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
  41. So(sc.resp.Code, ShouldEqual, 403)
  42. })
  43. })
  44. })
  45. })
  46. }