dashboard_folder_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. updateTestDashboardWithAcl(folder.Id, otherUser, 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. updateTestDashboardWithAcl(folder.Id, currentUser.Id, 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. aclId := updateTestDashboardWithAcl(folder.Id, otherUser, m.PERMISSION_EDIT)
  83. removeAcl(aclId)
  84. updateTestDashboardWithAcl(childDash.Id, otherUser, m.PERMISSION_EDIT)
  85. Convey("should not return folder or child", func() {
  86. 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}}
  87. err := SearchDashboards(query)
  88. So(err, ShouldBeNil)
  89. So(len(query.Result), ShouldEqual, 1)
  90. So(query.Result[0].Id, ShouldEqual, dashInRoot.Id)
  91. })
  92. Convey("when the user is given permission to child", func() {
  93. updateTestDashboardWithAcl(childDash.Id, currentUser.Id, m.PERMISSION_EDIT)
  94. Convey("should be able to search for child dashboard but not folder", func() {
  95. 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}}
  96. err := SearchDashboards(query)
  97. So(err, ShouldBeNil)
  98. So(len(query.Result), ShouldEqual, 2)
  99. So(query.Result[0].Id, ShouldEqual, childDash.Id)
  100. So(query.Result[1].Id, ShouldEqual, dashInRoot.Id)
  101. })
  102. })
  103. Convey("when the user is an admin", func() {
  104. Convey("should be able to search for child dash and folder", func() {
  105. query := &search.FindPersistedDashboardsQuery{
  106. SignedInUser: &m.SignedInUser{
  107. UserId: currentUser.Id,
  108. OrgId: 1,
  109. OrgRole: m.ROLE_ADMIN,
  110. },
  111. OrgId: 1,
  112. DashboardIds: []int64{folder.Id, dashInRoot.Id, childDash.Id},
  113. }
  114. err := SearchDashboards(query)
  115. So(err, ShouldBeNil)
  116. So(len(query.Result), ShouldEqual, 3)
  117. So(query.Result[0].Id, ShouldEqual, folder.Id)
  118. So(query.Result[1].Id, ShouldEqual, childDash.Id)
  119. So(query.Result[2].Id, ShouldEqual, dashInRoot.Id)
  120. })
  121. })
  122. })
  123. })
  124. Convey("Given two dashboard folders with one dashboard each and one dashboard in the root folder", func() {
  125. folder1 := insertTestDashboard("1 test dash folder", 1, 0, true, "prod")
  126. folder2 := insertTestDashboard("2 test dash folder", 1, 0, true, "prod")
  127. dashInRoot := insertTestDashboard("test dash 67", 1, 0, false, "prod")
  128. childDash1 := insertTestDashboard("child dash 1", 1, folder1.Id, false, "prod")
  129. childDash2 := insertTestDashboard("child dash 2", 1, folder2.Id, false, "prod")
  130. currentUser := createUser("viewer", "Viewer", false)
  131. var rootFolderId int64 = 0
  132. Convey("and one folder is expanded, the other collapsed", func() {
  133. Convey("should return dashboards in root and expanded folder", func() {
  134. query := &search.FindPersistedDashboardsQuery{FolderIds: []int64{rootFolderId, folder1.Id}, SignedInUser: &m.SignedInUser{UserId: currentUser.Id, OrgId: 1, OrgRole: m.ROLE_VIEWER}, OrgId: 1}
  135. err := SearchDashboards(query)
  136. So(err, ShouldBeNil)
  137. So(len(query.Result), ShouldEqual, 4)
  138. So(query.Result[0].Id, ShouldEqual, folder1.Id)
  139. So(query.Result[1].Id, ShouldEqual, folder2.Id)
  140. So(query.Result[2].Id, ShouldEqual, childDash1.Id)
  141. So(query.Result[3].Id, ShouldEqual, dashInRoot.Id)
  142. })
  143. })
  144. Convey("and acl is set for one dashboard folder", func() {
  145. var otherUser int64 = 999
  146. updateTestDashboardWithAcl(folder1.Id, otherUser, m.PERMISSION_EDIT)
  147. Convey("and a dashboard is moved from folder without acl to the folder with an acl", func() {
  148. movedDash := moveDashboard(1, childDash2.Data, folder1.Id)
  149. So(movedDash.HasAcl, ShouldBeTrue)
  150. Convey("should not return folder with acl or its children", func() {
  151. query := &search.FindPersistedDashboardsQuery{
  152. SignedInUser: &m.SignedInUser{UserId: currentUser.Id, OrgId: 1, OrgRole: m.ROLE_VIEWER},
  153. OrgId: 1,
  154. DashboardIds: []int64{folder1.Id, childDash1.Id, childDash2.Id, dashInRoot.Id},
  155. }
  156. err := SearchDashboards(query)
  157. So(err, ShouldBeNil)
  158. So(len(query.Result), ShouldEqual, 1)
  159. So(query.Result[0].Id, ShouldEqual, dashInRoot.Id)
  160. })
  161. })
  162. Convey("and a dashboard is moved from folder with acl to the folder without an acl", func() {
  163. movedDash := moveDashboard(1, childDash1.Data, folder2.Id)
  164. So(movedDash.HasAcl, ShouldBeFalse)
  165. Convey("should return folder without acl and its children", func() {
  166. query := &search.FindPersistedDashboardsQuery{
  167. SignedInUser: &m.SignedInUser{UserId: currentUser.Id, OrgId: 1, OrgRole: m.ROLE_VIEWER},
  168. OrgId: 1,
  169. DashboardIds: []int64{folder2.Id, childDash1.Id, childDash2.Id, dashInRoot.Id},
  170. }
  171. err := SearchDashboards(query)
  172. So(err, ShouldBeNil)
  173. So(len(query.Result), ShouldEqual, 4)
  174. So(query.Result[0].Id, ShouldEqual, folder2.Id)
  175. So(query.Result[1].Id, ShouldEqual, childDash1.Id)
  176. So(query.Result[2].Id, ShouldEqual, childDash2.Id)
  177. So(query.Result[3].Id, ShouldEqual, dashInRoot.Id)
  178. })
  179. })
  180. Convey("and a dashboard with an acl is moved to the folder without an acl", func() {
  181. updateTestDashboardWithAcl(childDash1.Id, otherUser, m.PERMISSION_EDIT)
  182. movedDash := moveDashboard(1, childDash1.Data, folder2.Id)
  183. So(movedDash.HasAcl, ShouldBeTrue)
  184. Convey("should return folder without acl but not the dashboard with acl", func() {
  185. query := &search.FindPersistedDashboardsQuery{
  186. SignedInUser: &m.SignedInUser{UserId: currentUser.Id, OrgId: 1, OrgRole: m.ROLE_VIEWER},
  187. OrgId: 1,
  188. DashboardIds: []int64{folder2.Id, childDash1.Id, childDash2.Id, dashInRoot.Id},
  189. }
  190. err := SearchDashboards(query)
  191. So(err, ShouldBeNil)
  192. So(len(query.Result), ShouldEqual, 4)
  193. So(query.Result[0].Id, ShouldEqual, folder2.Id)
  194. So(query.Result[1].Id, ShouldEqual, childDash1.Id)
  195. So(query.Result[2].Id, ShouldEqual, childDash2.Id)
  196. So(query.Result[3].Id, ShouldEqual, dashInRoot.Id)
  197. })
  198. })
  199. })
  200. })
  201. Convey("Given two dashboard folders", func() {
  202. folder1 := insertTestDashboard("1 test dash folder", 1, 0, true, "prod")
  203. folder2 := insertTestDashboard("2 test dash folder", 1, 0, true, "prod")
  204. insertTestDashboard("folder in another org", 2, 0, true, "prod")
  205. adminUser := createUser("admin", "Admin", true)
  206. editorUser := createUser("editor", "Editor", false)
  207. viewerUser := createUser("viewer", "Viewer", false)
  208. Convey("Admin users", func() {
  209. Convey("Should have write access to all dashboard folders in their org", func() {
  210. query := search.FindPersistedDashboardsQuery{
  211. OrgId: 1,
  212. SignedInUser: &m.SignedInUser{UserId: adminUser.Id, OrgRole: m.ROLE_ADMIN, OrgId: 1},
  213. Permission: m.PERMISSION_VIEW,
  214. Type: "dash-folder",
  215. }
  216. err := SearchDashboards(&query)
  217. So(err, ShouldBeNil)
  218. So(len(query.Result), ShouldEqual, 2)
  219. So(query.Result[0].Id, ShouldEqual, folder1.Id)
  220. So(query.Result[1].Id, ShouldEqual, folder2.Id)
  221. })
  222. Convey("should have write access to all folders and dashboards", func() {
  223. query := m.GetDashboardPermissionsForUserQuery{
  224. DashboardIds: []int64{folder1.Id, folder2.Id},
  225. OrgId: 1,
  226. UserId: adminUser.Id,
  227. OrgRole: m.ROLE_ADMIN,
  228. }
  229. err := GetDashboardPermissionsForUser(&query)
  230. So(err, ShouldBeNil)
  231. So(len(query.Result), ShouldEqual, 2)
  232. So(query.Result[0].DashboardId, ShouldEqual, folder1.Id)
  233. So(query.Result[0].Permission, ShouldEqual, m.PERMISSION_ADMIN)
  234. So(query.Result[1].DashboardId, ShouldEqual, folder2.Id)
  235. So(query.Result[1].Permission, ShouldEqual, m.PERMISSION_ADMIN)
  236. })
  237. })
  238. Convey("Editor users", func() {
  239. query := search.FindPersistedDashboardsQuery{
  240. OrgId: 1,
  241. SignedInUser: &m.SignedInUser{UserId: editorUser.Id, OrgRole: m.ROLE_EDITOR, OrgId: 1},
  242. Permission: m.PERMISSION_EDIT,
  243. }
  244. Convey("Should have write access to all dashboard folders with default ACL", func() {
  245. err := SearchDashboards(&query)
  246. So(err, ShouldBeNil)
  247. So(len(query.Result), ShouldEqual, 2)
  248. So(query.Result[0].Id, ShouldEqual, folder1.Id)
  249. So(query.Result[1].Id, ShouldEqual, folder2.Id)
  250. })
  251. Convey("should have edit access to folders with default ACL", func() {
  252. query := m.GetDashboardPermissionsForUserQuery{
  253. DashboardIds: []int64{folder1.Id, folder2.Id},
  254. OrgId: 1,
  255. UserId: editorUser.Id,
  256. OrgRole: m.ROLE_EDITOR,
  257. }
  258. err := GetDashboardPermissionsForUser(&query)
  259. So(err, ShouldBeNil)
  260. So(len(query.Result), ShouldEqual, 2)
  261. So(query.Result[0].DashboardId, ShouldEqual, folder1.Id)
  262. So(query.Result[0].Permission, ShouldEqual, m.PERMISSION_EDIT)
  263. So(query.Result[1].DashboardId, ShouldEqual, folder2.Id)
  264. So(query.Result[1].Permission, ShouldEqual, m.PERMISSION_EDIT)
  265. })
  266. Convey("Should have write access to one dashboard folder if default role changed to view for one folder", func() {
  267. updateTestDashboardWithAcl(folder1.Id, editorUser.Id, m.PERMISSION_VIEW)
  268. err := SearchDashboards(&query)
  269. So(err, ShouldBeNil)
  270. So(len(query.Result), ShouldEqual, 1)
  271. So(query.Result[0].Id, ShouldEqual, folder2.Id)
  272. })
  273. })
  274. Convey("Viewer users", func() {
  275. query := search.FindPersistedDashboardsQuery{
  276. OrgId: 1,
  277. SignedInUser: &m.SignedInUser{UserId: viewerUser.Id, OrgRole: m.ROLE_VIEWER, OrgId: 1},
  278. Permission: m.PERMISSION_EDIT,
  279. }
  280. Convey("Should have no write access to any dashboard folders with default ACL", func() {
  281. err := SearchDashboards(&query)
  282. So(err, ShouldBeNil)
  283. So(len(query.Result), ShouldEqual, 0)
  284. })
  285. Convey("should have view access to folders with default ACL", func() {
  286. query := m.GetDashboardPermissionsForUserQuery{
  287. DashboardIds: []int64{folder1.Id, folder2.Id},
  288. OrgId: 1,
  289. UserId: viewerUser.Id,
  290. OrgRole: m.ROLE_VIEWER,
  291. }
  292. err := GetDashboardPermissionsForUser(&query)
  293. So(err, ShouldBeNil)
  294. So(len(query.Result), ShouldEqual, 2)
  295. So(query.Result[0].DashboardId, ShouldEqual, folder1.Id)
  296. So(query.Result[0].Permission, ShouldEqual, m.PERMISSION_VIEW)
  297. So(query.Result[1].DashboardId, ShouldEqual, folder2.Id)
  298. So(query.Result[1].Permission, ShouldEqual, m.PERMISSION_VIEW)
  299. })
  300. Convey("Should be able to get one dashboard folder if default role changed to edit for one folder", func() {
  301. updateTestDashboardWithAcl(folder1.Id, viewerUser.Id, m.PERMISSION_EDIT)
  302. err := SearchDashboards(&query)
  303. So(err, ShouldBeNil)
  304. So(len(query.Result), ShouldEqual, 1)
  305. So(query.Result[0].Id, ShouldEqual, folder1.Id)
  306. })
  307. })
  308. })
  309. })
  310. }