dashboard_folder_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. insertTestDashboard("folder in another org", 2, 0, true, "prod")
  193. adminUser := createUser("admin", "Admin", true)
  194. editorUser := createUser("editor", "Editor", false)
  195. viewerUser := createUser("viewer", "Viewer", false)
  196. Convey("Admin users", func() {
  197. Convey("Should have write access to all dashboard folders in their org", func() {
  198. query := m.GetFoldersForSignedInUserQuery{
  199. OrgId: 1,
  200. SignedInUser: &m.SignedInUser{UserId: adminUser.Id, OrgRole: m.ROLE_ADMIN},
  201. }
  202. err := GetFoldersForSignedInUser(&query)
  203. So(err, ShouldBeNil)
  204. So(len(query.Result), ShouldEqual, 2)
  205. So(query.Result[0].Id, ShouldEqual, folder1.Id)
  206. So(query.Result[1].Id, ShouldEqual, folder2.Id)
  207. })
  208. Convey("should have write access to all folders and dashboards", func() {
  209. query := m.GetDashboardPermissionsForUserQuery{
  210. DashboardIds: []int64{folder1.Id, folder2.Id},
  211. OrgId: 1,
  212. UserId: adminUser.Id,
  213. OrgRole: m.ROLE_ADMIN,
  214. }
  215. err := GetDashboardPermissionsForUser(&query)
  216. So(err, ShouldBeNil)
  217. So(len(query.Result), ShouldEqual, 2)
  218. So(query.Result[0].DashboardId, ShouldEqual, folder1.Id)
  219. So(query.Result[0].Permission, ShouldEqual, m.PERMISSION_ADMIN)
  220. So(query.Result[1].DashboardId, ShouldEqual, folder2.Id)
  221. So(query.Result[1].Permission, ShouldEqual, m.PERMISSION_ADMIN)
  222. })
  223. })
  224. Convey("Editor users", func() {
  225. query := m.GetFoldersForSignedInUserQuery{
  226. OrgId: 1,
  227. SignedInUser: &m.SignedInUser{UserId: editorUser.Id, OrgRole: m.ROLE_EDITOR},
  228. }
  229. Convey("Should have write access to all dashboard folders with default ACL", func() {
  230. err := GetFoldersForSignedInUser(&query)
  231. So(err, ShouldBeNil)
  232. So(len(query.Result), ShouldEqual, 2)
  233. So(query.Result[0].Id, ShouldEqual, folder1.Id)
  234. So(query.Result[1].Id, ShouldEqual, folder2.Id)
  235. })
  236. Convey("should have edit access to folders with default ACL", func() {
  237. query := m.GetDashboardPermissionsForUserQuery{
  238. DashboardIds: []int64{folder1.Id, folder2.Id},
  239. OrgId: 1,
  240. UserId: editorUser.Id,
  241. OrgRole: m.ROLE_EDITOR,
  242. }
  243. err := GetDashboardPermissionsForUser(&query)
  244. So(err, ShouldBeNil)
  245. So(len(query.Result), ShouldEqual, 2)
  246. So(query.Result[0].DashboardId, ShouldEqual, folder1.Id)
  247. So(query.Result[0].Permission, ShouldEqual, m.PERMISSION_EDIT)
  248. So(query.Result[1].DashboardId, ShouldEqual, folder2.Id)
  249. So(query.Result[1].Permission, ShouldEqual, m.PERMISSION_EDIT)
  250. })
  251. Convey("Should have write access to one dashboard folder if default role changed to view for one folder", func() {
  252. updateTestDashboardWithAcl(folder1.Id, editorUser.Id, m.PERMISSION_VIEW)
  253. err := GetFoldersForSignedInUser(&query)
  254. So(err, ShouldBeNil)
  255. So(len(query.Result), ShouldEqual, 1)
  256. So(query.Result[0].Id, ShouldEqual, folder2.Id)
  257. })
  258. })
  259. Convey("Viewer users", func() {
  260. query := m.GetFoldersForSignedInUserQuery{
  261. OrgId: 1,
  262. SignedInUser: &m.SignedInUser{UserId: viewerUser.Id, OrgRole: m.ROLE_VIEWER},
  263. }
  264. Convey("Should have no write access to any dashboard folders with default ACL", func() {
  265. err := GetFoldersForSignedInUser(&query)
  266. So(err, ShouldBeNil)
  267. So(len(query.Result), ShouldEqual, 0)
  268. })
  269. Convey("should have view access to folders with default ACL", func() {
  270. query := m.GetDashboardPermissionsForUserQuery{
  271. DashboardIds: []int64{folder1.Id, folder2.Id},
  272. OrgId: 1,
  273. UserId: viewerUser.Id,
  274. OrgRole: m.ROLE_VIEWER,
  275. }
  276. err := GetDashboardPermissionsForUser(&query)
  277. So(err, ShouldBeNil)
  278. So(len(query.Result), ShouldEqual, 2)
  279. So(query.Result[0].DashboardId, ShouldEqual, folder1.Id)
  280. So(query.Result[0].Permission, ShouldEqual, m.PERMISSION_VIEW)
  281. So(query.Result[1].DashboardId, ShouldEqual, folder2.Id)
  282. So(query.Result[1].Permission, ShouldEqual, m.PERMISSION_VIEW)
  283. })
  284. Convey("Should be able to get one dashboard folder if default role changed to edit for one folder", func() {
  285. updateTestDashboardWithAcl(folder1.Id, viewerUser.Id, m.PERMISSION_EDIT)
  286. err := GetFoldersForSignedInUser(&query)
  287. So(err, ShouldBeNil)
  288. So(len(query.Result), ShouldEqual, 1)
  289. So(query.Result[0].Id, ShouldEqual, folder1.Id)
  290. })
  291. })
  292. })
  293. })
  294. }