guardian_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package sqlstore
  2. import (
  3. "testing"
  4. m "github.com/grafana/grafana/pkg/models"
  5. "github.com/grafana/grafana/pkg/setting"
  6. . "github.com/smartystreets/goconvey/convey"
  7. )
  8. func TestGuardianDataAccess(t *testing.T) {
  9. Convey("Testing DB", t, func() {
  10. InitTestDB(t)
  11. Convey("Given one dashboard folder with two dashboard and one dashboard in the root folder", func() {
  12. folder := insertTestDashboard("1 test dash folder", 1, 0, true, "prod", "webapp")
  13. dashInRoot := insertTestDashboard("test dash 67", 1, 0, false, "prod", "webapp")
  14. insertTestDashboard("test dash 23", 1, folder.Id, false, "prod", "webapp")
  15. insertTestDashboard("test dash 45", 1, folder.Id, false, "prod")
  16. currentUser := createUser("viewer", "Viewer", false)
  17. Convey("and no acls are set", func() {
  18. Convey("should return all dashboards", func() {
  19. query := &m.GetAllowedDashboardsQuery{UserId: currentUser.Id, OrgId: 1, DashList: []int64{folder.Id, dashInRoot.Id}}
  20. err := GetAllowedDashboards(query)
  21. So(err, ShouldBeNil)
  22. So(len(query.Result), ShouldEqual, 2)
  23. So(query.Result[0], ShouldEqual, folder.Id)
  24. So(query.Result[1], ShouldEqual, dashInRoot.Id)
  25. })
  26. })
  27. Convey("and acl is set for dashboard folder", func() {
  28. var otherUser int64 = 999
  29. updateTestDashboardWithAcl(folder.Id, otherUser, m.PERMISSION_EDIT)
  30. Convey("should not return folder", func() {
  31. query := &m.GetAllowedDashboardsQuery{UserId: currentUser.Id, OrgId: 1, DashList: []int64{folder.Id, dashInRoot.Id}}
  32. err := GetAllowedDashboards(query)
  33. So(err, ShouldBeNil)
  34. So(len(query.Result), ShouldEqual, 1)
  35. So(query.Result[0], ShouldEqual, dashInRoot.Id)
  36. })
  37. Convey("when the user is given permission", func() {
  38. updateTestDashboardWithAcl(folder.Id, currentUser.Id, m.PERMISSION_EDIT)
  39. Convey("should folder", func() {
  40. query := &m.GetAllowedDashboardsQuery{UserId: currentUser.Id, OrgId: 1, DashList: []int64{folder.Id, dashInRoot.Id}}
  41. err := GetAllowedDashboards(query)
  42. So(err, ShouldBeNil)
  43. So(len(query.Result), ShouldEqual, 2)
  44. So(query.Result[0], ShouldEqual, folder.Id)
  45. So(query.Result[1], ShouldEqual, dashInRoot.Id)
  46. })
  47. })
  48. })
  49. })
  50. })
  51. }
  52. func createUser(name string, role string, isAdmin bool) m.User {
  53. setting.AutoAssignOrg = true
  54. setting.AutoAssignOrgRole = role
  55. currentUserCmd := m.CreateUserCommand{Login: name, Email: name + "@test.com", Name: "a " + name, IsAdmin: isAdmin}
  56. err := CreateUser(&currentUserCmd)
  57. So(err, ShouldBeNil)
  58. q1 := m.GetUserOrgListQuery{UserId: currentUserCmd.Result.Id}
  59. GetUserOrgList(&q1)
  60. So(q1.Result[0].Role, ShouldEqual, role)
  61. return currentUserCmd.Result
  62. }
  63. func updateTestDashboardWithAcl(dashId int64, userId int64, permissionType m.PermissionType) {
  64. err := AddOrUpdateDashboardPermission(&m.AddOrUpdateDashboardPermissionCommand{
  65. OrgId: 1,
  66. UserId: userId,
  67. DashboardId: dashId,
  68. PermissionType: permissionType,
  69. })
  70. So(err, ShouldBeNil)
  71. }