guardian_test.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package sqlstore
  2. import (
  3. "testing"
  4. m "github.com/grafana/grafana/pkg/models"
  5. . "github.com/smartystreets/goconvey/convey"
  6. )
  7. func TestGuardianDataAccess(t *testing.T) {
  8. Convey("Testing DB", t, func() {
  9. InitTestDB(t)
  10. Convey("Given one dashboard folder with two dashboard and one dashboard in the root folder", func() {
  11. folder := insertTestDashboard("1 test dash folder", 1, 0, true, "prod", "webapp")
  12. dashInRoot := insertTestDashboard("test dash 67", 1, 0, false, "prod", "webapp")
  13. insertTestDashboard("test dash 23", 1, folder.Id, false, "prod", "webapp")
  14. insertTestDashboard("test dash 45", 1, folder.Id, false, "prod")
  15. currentUser := createUser("viewer", "Viewer", false)
  16. Convey("and no acls are set", func() {
  17. Convey("should return all dashboards", func() {
  18. query := &m.GetAllowedDashboardsQuery{UserId: currentUser.Id, OrgId: 1, DashList: []int64{folder.Id, dashInRoot.Id}}
  19. err := GetAllowedDashboards(query)
  20. So(err, ShouldBeNil)
  21. So(len(query.Result), ShouldEqual, 2)
  22. So(query.Result[0], ShouldEqual, folder.Id)
  23. So(query.Result[1], ShouldEqual, dashInRoot.Id)
  24. })
  25. })
  26. Convey("and acl is set for dashboard folder", func() {
  27. var otherUser int64 = 999
  28. updateTestDashboardWithAcl(folder.Id, otherUser, m.PERMISSION_EDIT)
  29. Convey("should not return folder", func() {
  30. query := &m.GetAllowedDashboardsQuery{UserId: currentUser.Id, OrgId: 1, DashList: []int64{folder.Id, dashInRoot.Id}}
  31. err := GetAllowedDashboards(query)
  32. So(err, ShouldBeNil)
  33. So(len(query.Result), ShouldEqual, 1)
  34. So(query.Result[0], ShouldEqual, dashInRoot.Id)
  35. })
  36. Convey("when the user is given permission", func() {
  37. updateTestDashboardWithAcl(folder.Id, currentUser.Id, m.PERMISSION_EDIT)
  38. Convey("should folder", func() {
  39. query := &m.GetAllowedDashboardsQuery{UserId: currentUser.Id, OrgId: 1, DashList: []int64{folder.Id, dashInRoot.Id}}
  40. err := GetAllowedDashboards(query)
  41. So(err, ShouldBeNil)
  42. So(len(query.Result), ShouldEqual, 2)
  43. So(query.Result[0], ShouldEqual, folder.Id)
  44. So(query.Result[1], ShouldEqual, dashInRoot.Id)
  45. })
  46. })
  47. })
  48. })
  49. })
  50. }