guardian_test.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. // insertTestDashboard("test dash 23", 1, folder.Id, false, "prod", "webapp")
  14. // insertTestDashboard("test dash 45", 1, folder.Id, false, "prod")
  15. dashInRoot := insertTestDashboard("test dash 67", 1, 0, false, "prod", "webapp")
  16. currentUser := createUser("viewer")
  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. Convey("should not return folder", func() {
  29. var otherUser int64 = 999
  30. updateTestDashboardWithAcl(folder.Id, otherUser, m.PERMISSION_EDIT)
  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. })
  38. })
  39. })
  40. }
  41. func createUser(name string) m.User {
  42. setting.AutoAssignOrg = true
  43. setting.AutoAssignOrgRole = "Viewer"
  44. currentUserCmd := m.CreateUserCommand{Login: name, Email: name + "@test.com", Name: "a " + name, IsAdmin: false}
  45. err := CreateUser(&currentUserCmd)
  46. So(err, ShouldBeNil)
  47. q1 := m.GetUserOrgListQuery{UserId: currentUserCmd.Result.Id}
  48. GetUserOrgList(&q1)
  49. So(q1.Result[0].Role, ShouldEqual, "Viewer")
  50. return currentUserCmd.Result
  51. }
  52. func updateTestDashboardWithAcl(dashId int64, userId int64, permissionType m.PermissionType) {
  53. err := AddOrUpdateDashboardPermission(&m.AddOrUpdateDashboardPermissionCommand{
  54. OrgId: 1,
  55. UserId: userId,
  56. DashboardId: dashId,
  57. PermissionType: permissionType,
  58. })
  59. So(err, ShouldBeNil)
  60. }