dashboard_folder_test.go 13 KB

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