dashboard_folder_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. package sqlstore
  2. import (
  3. "testing"
  4. "github.com/go-xorm/xorm"
  5. . "github.com/smartystreets/goconvey/convey"
  6. m "github.com/grafana/grafana/pkg/models"
  7. "github.com/grafana/grafana/pkg/services/search"
  8. )
  9. func TestDashboardFolderDataAccess(t *testing.T) {
  10. var x *xorm.Engine
  11. Convey("Testing DB", t, func() {
  12. x = InitTestDB(t)
  13. Convey("Given one dashboard folder with two dashboards and one dashboard in the root folder", func() {
  14. folder := insertTestDashboard("1 test dash folder", 1, 0, true, "prod", "webapp")
  15. dashInRoot := insertTestDashboard("test dash 67", 1, 0, false, "prod", "webapp")
  16. childDash := insertTestDashboard("test dash 23", 1, folder.Id, false, "prod", "webapp")
  17. insertTestDashboard("test dash 45", 1, folder.Id, false, "prod")
  18. currentUser := createUser("viewer", "Viewer", false)
  19. Convey("and no acls are set", func() {
  20. Convey("should return all dashboards", func() {
  21. query := &search.FindPersistedDashboardsQuery{
  22. SignedInUser: &m.SignedInUser{UserId: currentUser.Id, OrgId: 1, OrgRole: m.ROLE_VIEWER},
  23. OrgId: 1,
  24. DashboardIds: []int64{folder.Id, dashInRoot.Id},
  25. }
  26. err := SearchDashboards(query)
  27. So(err, ShouldBeNil)
  28. So(len(query.Result), ShouldEqual, 2)
  29. So(query.Result[0].Id, ShouldEqual, folder.Id)
  30. So(query.Result[1].Id, ShouldEqual, dashInRoot.Id)
  31. })
  32. })
  33. Convey("and acl is set for dashboard folder", func() {
  34. var otherUser int64 = 999
  35. testHelperUpdateDashboardAcl(folder.Id, m.DashboardAcl{DashboardId: folder.Id, OrgId: 1, UserId: otherUser, Permission: m.PERMISSION_EDIT})
  36. Convey("should not return folder", func() {
  37. query := &search.FindPersistedDashboardsQuery{
  38. SignedInUser: &m.SignedInUser{UserId: currentUser.Id, OrgId: 1, OrgRole: m.ROLE_VIEWER},
  39. OrgId: 1, DashboardIds: []int64{folder.Id, dashInRoot.Id},
  40. }
  41. err := SearchDashboards(query)
  42. So(err, ShouldBeNil)
  43. So(len(query.Result), ShouldEqual, 1)
  44. So(query.Result[0].Id, ShouldEqual, dashInRoot.Id)
  45. })
  46. Convey("when the user is given permission", func() {
  47. testHelperUpdateDashboardAcl(folder.Id, m.DashboardAcl{DashboardId: folder.Id, OrgId: 1, UserId: currentUser.Id, Permission: m.PERMISSION_EDIT})
  48. Convey("should be able to access folder", func() {
  49. query := &search.FindPersistedDashboardsQuery{
  50. SignedInUser: &m.SignedInUser{UserId: currentUser.Id, OrgId: 1, OrgRole: m.ROLE_VIEWER},
  51. OrgId: 1,
  52. DashboardIds: []int64{folder.Id, dashInRoot.Id},
  53. }
  54. err := SearchDashboards(query)
  55. So(err, ShouldBeNil)
  56. So(len(query.Result), ShouldEqual, 2)
  57. So(query.Result[0].Id, ShouldEqual, folder.Id)
  58. So(query.Result[1].Id, ShouldEqual, dashInRoot.Id)
  59. })
  60. })
  61. Convey("when the user is an admin", func() {
  62. Convey("should be able to access folder", func() {
  63. query := &search.FindPersistedDashboardsQuery{
  64. SignedInUser: &m.SignedInUser{
  65. UserId: currentUser.Id,
  66. OrgId: 1,
  67. OrgRole: m.ROLE_ADMIN,
  68. },
  69. OrgId: 1,
  70. DashboardIds: []int64{folder.Id, dashInRoot.Id},
  71. }
  72. err := SearchDashboards(query)
  73. So(err, ShouldBeNil)
  74. So(len(query.Result), ShouldEqual, 2)
  75. So(query.Result[0].Id, ShouldEqual, folder.Id)
  76. So(query.Result[1].Id, ShouldEqual, dashInRoot.Id)
  77. })
  78. })
  79. })
  80. Convey("and acl is set for dashboard child and folder has all permissions removed", func() {
  81. var otherUser int64 = 999
  82. testHelperUpdateDashboardAcl(folder.Id)
  83. testHelperUpdateDashboardAcl(childDash.Id, m.DashboardAcl{DashboardId: folder.Id, OrgId: 1, UserId: otherUser, Permission: m.PERMISSION_EDIT})
  84. Convey("should not return folder or child", func() {
  85. query := &search.FindPersistedDashboardsQuery{SignedInUser: &m.SignedInUser{UserId: currentUser.Id, OrgId: 1, OrgRole: m.ROLE_VIEWER}, OrgId: 1, DashboardIds: []int64{folder.Id, childDash.Id, dashInRoot.Id}}
  86. err := SearchDashboards(query)
  87. So(err, ShouldBeNil)
  88. So(len(query.Result), ShouldEqual, 1)
  89. So(query.Result[0].Id, ShouldEqual, dashInRoot.Id)
  90. })
  91. Convey("when the user is given permission to child", func() {
  92. testHelperUpdateDashboardAcl(childDash.Id, m.DashboardAcl{DashboardId: childDash.Id, OrgId: 1, UserId: currentUser.Id, Permission: m.PERMISSION_EDIT})
  93. Convey("should be able to search for child dashboard but not folder", func() {
  94. query := &search.FindPersistedDashboardsQuery{SignedInUser: &m.SignedInUser{UserId: currentUser.Id, OrgId: 1, OrgRole: m.ROLE_VIEWER}, OrgId: 1, DashboardIds: []int64{folder.Id, childDash.Id, dashInRoot.Id}}
  95. err := SearchDashboards(query)
  96. So(err, ShouldBeNil)
  97. So(len(query.Result), ShouldEqual, 2)
  98. So(query.Result[0].Id, ShouldEqual, childDash.Id)
  99. So(query.Result[1].Id, ShouldEqual, dashInRoot.Id)
  100. })
  101. })
  102. Convey("when the user is an admin", func() {
  103. Convey("should be able to search for child dash and folder", func() {
  104. query := &search.FindPersistedDashboardsQuery{
  105. SignedInUser: &m.SignedInUser{
  106. UserId: currentUser.Id,
  107. OrgId: 1,
  108. OrgRole: m.ROLE_ADMIN,
  109. },
  110. OrgId: 1,
  111. DashboardIds: []int64{folder.Id, dashInRoot.Id, childDash.Id},
  112. }
  113. err := SearchDashboards(query)
  114. So(err, ShouldBeNil)
  115. So(len(query.Result), ShouldEqual, 3)
  116. So(query.Result[0].Id, ShouldEqual, folder.Id)
  117. So(query.Result[1].Id, ShouldEqual, childDash.Id)
  118. So(query.Result[2].Id, ShouldEqual, dashInRoot.Id)
  119. })
  120. })
  121. })
  122. })
  123. Convey("Given two dashboard folders with one dashboard each and one dashboard in the root folder", func() {
  124. folder1 := insertTestDashboard("1 test dash folder", 1, 0, true, "prod")
  125. folder2 := insertTestDashboard("2 test dash folder", 1, 0, true, "prod")
  126. dashInRoot := insertTestDashboard("test dash 67", 1, 0, false, "prod")
  127. childDash1 := insertTestDashboard("child dash 1", 1, folder1.Id, false, "prod")
  128. childDash2 := insertTestDashboard("child dash 2", 1, folder2.Id, false, "prod")
  129. currentUser := createUser("viewer", "Viewer", false)
  130. var rootFolderId int64 = 0
  131. Convey("and one folder is expanded, the other collapsed", func() {
  132. Convey("should return dashboards in root and expanded folder", func() {
  133. query := &search.FindPersistedDashboardsQuery{FolderIds: []int64{rootFolderId, folder1.Id}, SignedInUser: &m.SignedInUser{UserId: currentUser.Id, OrgId: 1, OrgRole: m.ROLE_VIEWER}, OrgId: 1}
  134. err := SearchDashboards(query)
  135. So(err, ShouldBeNil)
  136. So(len(query.Result), ShouldEqual, 4)
  137. So(query.Result[0].Id, ShouldEqual, folder1.Id)
  138. So(query.Result[1].Id, ShouldEqual, folder2.Id)
  139. So(query.Result[2].Id, ShouldEqual, childDash1.Id)
  140. So(query.Result[3].Id, ShouldEqual, dashInRoot.Id)
  141. })
  142. })
  143. Convey("and acl is set for one dashboard folder", func() {
  144. var otherUser int64 = 999
  145. testHelperUpdateDashboardAcl(folder1.Id, m.DashboardAcl{DashboardId: folder1.Id, OrgId: 1, UserId: otherUser, Permission: m.PERMISSION_EDIT})
  146. Convey("and a dashboard is moved from folder without acl to the folder with an acl", func() {
  147. moveDashboard(1, childDash2.Data, folder1.Id)
  148. Convey("should not return folder with acl or its children", func() {
  149. query := &search.FindPersistedDashboardsQuery{
  150. SignedInUser: &m.SignedInUser{UserId: currentUser.Id, OrgId: 1, OrgRole: m.ROLE_VIEWER},
  151. OrgId: 1,
  152. DashboardIds: []int64{folder1.Id, childDash1.Id, childDash2.Id, dashInRoot.Id},
  153. }
  154. err := SearchDashboards(query)
  155. So(err, ShouldBeNil)
  156. So(len(query.Result), ShouldEqual, 1)
  157. So(query.Result[0].Id, ShouldEqual, dashInRoot.Id)
  158. })
  159. })
  160. Convey("and a dashboard is moved from folder with acl to the folder without an acl", func() {
  161. moveDashboard(1, childDash1.Data, folder2.Id)
  162. Convey("should return folder without acl and its children", func() {
  163. query := &search.FindPersistedDashboardsQuery{
  164. SignedInUser: &m.SignedInUser{UserId: currentUser.Id, OrgId: 1, OrgRole: m.ROLE_VIEWER},
  165. OrgId: 1,
  166. DashboardIds: []int64{folder2.Id, childDash1.Id, childDash2.Id, dashInRoot.Id},
  167. }
  168. err := SearchDashboards(query)
  169. So(err, ShouldBeNil)
  170. So(len(query.Result), ShouldEqual, 4)
  171. So(query.Result[0].Id, ShouldEqual, folder2.Id)
  172. So(query.Result[1].Id, ShouldEqual, childDash1.Id)
  173. So(query.Result[2].Id, ShouldEqual, childDash2.Id)
  174. So(query.Result[3].Id, ShouldEqual, dashInRoot.Id)
  175. })
  176. })
  177. Convey("and a dashboard with an acl is moved to the folder without an acl", func() {
  178. testHelperUpdateDashboardAcl(childDash1.Id, m.DashboardAcl{DashboardId: childDash1.Id, OrgId: 1, UserId: otherUser, Permission: m.PERMISSION_EDIT})
  179. moveDashboard(1, childDash1.Data, folder2.Id)
  180. Convey("should return folder without acl but not the dashboard with acl", func() {
  181. query := &search.FindPersistedDashboardsQuery{
  182. SignedInUser: &m.SignedInUser{UserId: currentUser.Id, OrgId: 1, OrgRole: m.ROLE_VIEWER},
  183. OrgId: 1,
  184. DashboardIds: []int64{folder2.Id, childDash1.Id, childDash2.Id, dashInRoot.Id},
  185. }
  186. err := SearchDashboards(query)
  187. So(err, ShouldBeNil)
  188. So(len(query.Result), ShouldEqual, 4)
  189. So(query.Result[0].Id, ShouldEqual, folder2.Id)
  190. So(query.Result[1].Id, ShouldEqual, childDash1.Id)
  191. So(query.Result[2].Id, ShouldEqual, childDash2.Id)
  192. So(query.Result[3].Id, ShouldEqual, dashInRoot.Id)
  193. })
  194. })
  195. })
  196. })
  197. Convey("Given two dashboard folders", func() {
  198. folder1 := insertTestDashboard("1 test dash folder", 1, 0, true, "prod")
  199. folder2 := insertTestDashboard("2 test dash folder", 1, 0, true, "prod")
  200. insertTestDashboard("folder in another org", 2, 0, true, "prod")
  201. adminUser := createUser("admin", "Admin", true)
  202. editorUser := createUser("editor", "Editor", false)
  203. viewerUser := createUser("viewer", "Viewer", false)
  204. Convey("Admin users", func() {
  205. Convey("Should have write access to all dashboard folders in their org", func() {
  206. query := search.FindPersistedDashboardsQuery{
  207. OrgId: 1,
  208. SignedInUser: &m.SignedInUser{UserId: adminUser.Id, OrgRole: m.ROLE_ADMIN, OrgId: 1},
  209. Permission: m.PERMISSION_VIEW,
  210. Type: "dash-folder",
  211. }
  212. err := SearchDashboards(&query)
  213. So(err, ShouldBeNil)
  214. So(len(query.Result), ShouldEqual, 2)
  215. So(query.Result[0].Id, ShouldEqual, folder1.Id)
  216. So(query.Result[1].Id, ShouldEqual, folder2.Id)
  217. })
  218. Convey("should have write access to all folders and dashboards", func() {
  219. query := m.GetDashboardPermissionsForUserQuery{
  220. DashboardIds: []int64{folder1.Id, folder2.Id},
  221. OrgId: 1,
  222. UserId: adminUser.Id,
  223. OrgRole: m.ROLE_ADMIN,
  224. }
  225. err := GetDashboardPermissionsForUser(&query)
  226. So(err, ShouldBeNil)
  227. So(len(query.Result), ShouldEqual, 2)
  228. So(query.Result[0].DashboardId, ShouldEqual, folder1.Id)
  229. So(query.Result[0].Permission, ShouldEqual, m.PERMISSION_ADMIN)
  230. So(query.Result[1].DashboardId, ShouldEqual, folder2.Id)
  231. So(query.Result[1].Permission, ShouldEqual, m.PERMISSION_ADMIN)
  232. })
  233. })
  234. Convey("Editor users", func() {
  235. query := search.FindPersistedDashboardsQuery{
  236. OrgId: 1,
  237. SignedInUser: &m.SignedInUser{UserId: editorUser.Id, OrgRole: m.ROLE_EDITOR, OrgId: 1},
  238. Permission: m.PERMISSION_EDIT,
  239. }
  240. Convey("Should have write access to all dashboard folders with default ACL", func() {
  241. err := SearchDashboards(&query)
  242. So(err, ShouldBeNil)
  243. So(len(query.Result), ShouldEqual, 2)
  244. So(query.Result[0].Id, ShouldEqual, folder1.Id)
  245. So(query.Result[1].Id, ShouldEqual, folder2.Id)
  246. })
  247. Convey("should have edit access to folders with default ACL", func() {
  248. query := m.GetDashboardPermissionsForUserQuery{
  249. DashboardIds: []int64{folder1.Id, folder2.Id},
  250. OrgId: 1,
  251. UserId: editorUser.Id,
  252. OrgRole: m.ROLE_EDITOR,
  253. }
  254. err := GetDashboardPermissionsForUser(&query)
  255. So(err, ShouldBeNil)
  256. So(len(query.Result), ShouldEqual, 2)
  257. So(query.Result[0].DashboardId, ShouldEqual, folder1.Id)
  258. So(query.Result[0].Permission, ShouldEqual, m.PERMISSION_EDIT)
  259. So(query.Result[1].DashboardId, ShouldEqual, folder2.Id)
  260. So(query.Result[1].Permission, ShouldEqual, m.PERMISSION_EDIT)
  261. })
  262. Convey("Should have write access to one dashboard folder if default role changed to view for one folder", func() {
  263. testHelperUpdateDashboardAcl(folder1.Id, m.DashboardAcl{DashboardId: folder1.Id, OrgId: 1, UserId: editorUser.Id, Permission: m.PERMISSION_VIEW})
  264. err := SearchDashboards(&query)
  265. So(err, ShouldBeNil)
  266. So(len(query.Result), ShouldEqual, 1)
  267. So(query.Result[0].Id, ShouldEqual, folder2.Id)
  268. })
  269. })
  270. Convey("Viewer users", func() {
  271. query := search.FindPersistedDashboardsQuery{
  272. OrgId: 1,
  273. SignedInUser: &m.SignedInUser{UserId: viewerUser.Id, OrgRole: m.ROLE_VIEWER, OrgId: 1},
  274. Permission: m.PERMISSION_EDIT,
  275. }
  276. Convey("Should have no write access to any dashboard folders with default ACL", func() {
  277. err := SearchDashboards(&query)
  278. So(err, ShouldBeNil)
  279. So(len(query.Result), ShouldEqual, 0)
  280. })
  281. Convey("should have view access to folders with default ACL", func() {
  282. query := m.GetDashboardPermissionsForUserQuery{
  283. DashboardIds: []int64{folder1.Id, folder2.Id},
  284. OrgId: 1,
  285. UserId: viewerUser.Id,
  286. OrgRole: m.ROLE_VIEWER,
  287. }
  288. err := GetDashboardPermissionsForUser(&query)
  289. So(err, ShouldBeNil)
  290. So(len(query.Result), ShouldEqual, 2)
  291. So(query.Result[0].DashboardId, ShouldEqual, folder1.Id)
  292. So(query.Result[0].Permission, ShouldEqual, m.PERMISSION_VIEW)
  293. So(query.Result[1].DashboardId, ShouldEqual, folder2.Id)
  294. So(query.Result[1].Permission, ShouldEqual, m.PERMISSION_VIEW)
  295. })
  296. Convey("Should be able to get one dashboard folder if default role changed to edit for one folder", func() {
  297. testHelperUpdateDashboardAcl(folder1.Id, m.DashboardAcl{DashboardId: folder1.Id, OrgId: 1, UserId: viewerUser.Id, Permission: m.PERMISSION_EDIT})
  298. err := SearchDashboards(&query)
  299. So(err, ShouldBeNil)
  300. So(len(query.Result), ShouldEqual, 1)
  301. So(query.Result[0].Id, ShouldEqual, folder1.Id)
  302. })
  303. })
  304. })
  305. })
  306. }