| 
									
										
										
										
											2017-05-08 21:35:34 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2020-11-24 19:10:32 +08:00
										 |  |  | 	"encoding/json" | 
					
						
							| 
									
										
										
										
											2023-08-24 21:37:54 +08:00
										 |  |  | 	"net/http" | 
					
						
							|  |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2017-05-08 21:35:34 +08:00
										 |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-11 01:19:50 +08:00
										 |  |  | 	"github.com/stretchr/testify/assert" | 
					
						
							|  |  |  | 	"github.com/stretchr/testify/mock" | 
					
						
							|  |  |  | 	"github.com/stretchr/testify/require" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-17 23:54:39 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/accesscontrol" | 
					
						
							| 
									
										
										
										
											2023-08-24 21:37:54 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/accesscontrol/actest" | 
					
						
							| 
									
										
										
										
											2022-03-11 01:19:50 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/dashboards" | 
					
						
							| 
									
										
										
										
											2020-11-24 19:10:32 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/setting" | 
					
						
							| 
									
										
										
										
											2023-08-24 21:37:54 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/web/webtest" | 
					
						
							| 
									
										
										
										
											2017-05-08 21:35:34 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-24 21:37:54 +08:00
										 |  |  | func TestHTTPServer_GetDashboardPermissionList(t *testing.T) { | 
					
						
							|  |  |  | 	t.Run("should not be able to list acl when user does not have permission to do so", func(t *testing.T) { | 
					
						
							|  |  |  | 		server := SetupAPITestServer(t, func(hs *HTTPServer) {}) | 
					
						
							| 
									
										
										
										
											2022-02-16 21:15:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-24 21:37:54 +08:00
										 |  |  | 		res, err := server.Send(webtest.RequestWithSignedInUser(server.NewGetRequest("/api/dashboards/uid/1/permissions"), userWithPermissions(1, nil))) | 
					
						
							| 
									
										
										
										
											2023-04-14 17:17:23 +08:00
										 |  |  | 		require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2023-08-24 21:37:54 +08:00
										 |  |  | 		assert.Equal(t, http.StatusForbidden, res.StatusCode) | 
					
						
							|  |  |  | 		require.NoError(t, res.Body.Close()) | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2018-02-12 16:26:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-24 21:37:54 +08:00
										 |  |  | 	t.Run("should be able to list acl with correct permission", func(t *testing.T) { | 
					
						
							|  |  |  | 		server := SetupAPITestServer(t, func(hs *HTTPServer) { | 
					
						
							|  |  |  | 			svc := dashboards.NewFakeDashboardService(t) | 
					
						
							|  |  |  | 			svc.On("GetDashboard", mock.Anything, mock.Anything).Return(&dashboards.Dashboard{ID: 1, UID: "1"}, nil) | 
					
						
							|  |  |  | 			hs.DashboardService = svc | 
					
						
							|  |  |  | 			hs.dashboardPermissionsService = &actest.FakePermissionsService{ | 
					
						
							|  |  |  | 				ExpectedPermissions: []accesscontrol.ResourcePermission{}, | 
					
						
							| 
									
										
										
										
											2018-02-27 03:15:57 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2020-11-13 16:52:38 +08:00
										 |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2018-02-12 16:26:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-24 21:37:54 +08:00
										 |  |  | 		res, err := server.Send(webtest.RequestWithSignedInUser(server.NewGetRequest("/api/dashboards/uid/1/permissions"), userWithPermissions(1, []accesscontrol.Permission{ | 
					
						
							|  |  |  | 			{Action: dashboards.ActionDashboardsPermissionsRead, Scope: "dashboards:uid:1"}, | 
					
						
							|  |  |  | 		}))) | 
					
						
							| 
									
										
										
										
											2017-05-08 21:35:34 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-24 21:37:54 +08:00
										 |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 		assert.Equal(t, http.StatusOK, res.StatusCode) | 
					
						
							|  |  |  | 		require.NoError(t, res.Body.Close()) | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2020-11-24 19:10:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-24 21:37:54 +08:00
										 |  |  | 	t.Run("should filter out hidden users from acl", func(t *testing.T) { | 
					
						
							|  |  |  | 		server := SetupAPITestServer(t, func(hs *HTTPServer) { | 
					
						
							|  |  |  | 			cfg := setting.NewCfg() | 
					
						
							|  |  |  | 			cfg.HiddenUsers = map[string]struct{}{"hidden": {}} | 
					
						
							|  |  |  | 			hs.Cfg = cfg | 
					
						
							| 
									
										
										
										
											2020-11-24 19:10:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-24 21:37:54 +08:00
										 |  |  | 			svc := dashboards.NewFakeDashboardService(t) | 
					
						
							|  |  |  | 			svc.On("GetDashboard", mock.Anything, mock.Anything).Return(&dashboards.Dashboard{ID: 1, UID: "1"}, nil) | 
					
						
							| 
									
										
										
										
											2018-01-18 21:30:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-24 21:37:54 +08:00
										 |  |  | 			hs.DashboardService = svc | 
					
						
							|  |  |  | 			hs.dashboardPermissionsService = &actest.FakePermissionsService{ | 
					
						
							|  |  |  | 				ExpectedPermissions: []accesscontrol.ResourcePermission{ | 
					
						
							|  |  |  | 					{UserId: 1, UserLogin: "regular", IsManaged: true}, | 
					
						
							|  |  |  | 					{UserId: 2, UserLogin: "hidden", IsManaged: true}, | 
					
						
							| 
									
										
										
										
											2018-02-27 03:15:57 +08:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2018-01-18 21:30:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-24 21:37:54 +08:00
										 |  |  | 		res, err := server.Send(webtest.RequestWithSignedInUser(server.NewGetRequest("/api/dashboards/uid/1/permissions"), userWithPermissions(1, []accesscontrol.Permission{ | 
					
						
							|  |  |  | 			{Action: dashboards.ActionDashboardsPermissionsRead, Scope: "dashboards:uid:1"}, | 
					
						
							|  |  |  | 		}))) | 
					
						
							| 
									
										
										
										
											2021-10-14 02:16:58 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-24 21:37:54 +08:00
										 |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 		assert.Equal(t, http.StatusOK, res.StatusCode) | 
					
						
							| 
									
										
										
										
											2021-10-14 02:16:58 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-24 21:37:54 +08:00
										 |  |  | 		var result []dashboards.DashboardACLInfoDTO | 
					
						
							|  |  |  | 		require.NoError(t, json.NewDecoder(res.Body).Decode(&result)) | 
					
						
							| 
									
										
										
										
											2021-10-14 02:16:58 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-24 21:37:54 +08:00
										 |  |  | 		assert.Len(t, result, 1) | 
					
						
							|  |  |  | 		assert.Equal(t, result[0].UserLogin, "regular") | 
					
						
							|  |  |  | 		require.NoError(t, res.Body.Close()) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-01-18 21:30:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-24 21:37:54 +08:00
										 |  |  | func TestHTTPServer_UpdateDashboardPermissions(t *testing.T) { | 
					
						
							|  |  |  | 	t.Run("should not be able to update acl when user does not have permission to do so", func(t *testing.T) { | 
					
						
							|  |  |  | 		server := SetupAPITestServer(t, func(hs *HTTPServer) {}) | 
					
						
							| 
									
										
										
										
											2017-06-23 05:01:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-24 21:37:54 +08:00
										 |  |  | 		res, err := server.Send(webtest.RequestWithSignedInUser(server.NewPostRequest("/api/dashboards/uid/1/permissions", nil), userWithPermissions(1, nil))) | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 		assert.Equal(t, http.StatusForbidden, res.StatusCode) | 
					
						
							|  |  |  | 		require.NoError(t, res.Body.Close()) | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2017-06-23 05:01:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-24 21:37:54 +08:00
										 |  |  | 	t.Run("should be able to update acl with correct permissions", func(t *testing.T) { | 
					
						
							|  |  |  | 		server := SetupAPITestServer(t, func(hs *HTTPServer) { | 
					
						
							|  |  |  | 			svc := dashboards.NewFakeDashboardService(t) | 
					
						
							|  |  |  | 			svc.On("GetDashboard", mock.Anything, mock.Anything).Return(&dashboards.Dashboard{ID: 1, UID: "1"}, nil) | 
					
						
							| 
									
										
										
										
											2020-11-18 22:36:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-24 21:37:54 +08:00
										 |  |  | 			hs.DashboardService = svc | 
					
						
							|  |  |  | 			hs.dashboardPermissionsService = &actest.FakePermissionsService{} | 
					
						
							| 
									
										
										
										
											2020-11-18 22:36:41 +08:00
										 |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-24 21:37:54 +08:00
										 |  |  | 		body := `{"items": []}` | 
					
						
							|  |  |  | 		res, err := server.SendJSON(webtest.RequestWithSignedInUser(server.NewPostRequest("/api/dashboards/uid/1/permissions", strings.NewReader(body)), userWithPermissions(1, []accesscontrol.Permission{ | 
					
						
							|  |  |  | 			{Action: dashboards.ActionDashboardsPermissionsWrite, Scope: "dashboards:uid:1"}, | 
					
						
							|  |  |  | 		}))) | 
					
						
							| 
									
										
										
										
											2018-02-27 03:15:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-24 21:37:54 +08:00
										 |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 		assert.Equal(t, http.StatusOK, res.StatusCode) | 
					
						
							|  |  |  | 		require.NoError(t, res.Body.Close()) | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2017-05-22 16:36:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-24 21:37:54 +08:00
										 |  |  | 	t.Run("should not be able to specify team and user in same acl", func(t *testing.T) { | 
					
						
							|  |  |  | 		server := SetupAPITestServer(t, func(hs *HTTPServer) { | 
					
						
							|  |  |  | 			hs.DashboardService = dashboards.NewFakeDashboardService(t) | 
					
						
							|  |  |  | 			hs.dashboardPermissionsService = &actest.FakePermissionsService{} | 
					
						
							| 
									
										
										
										
											2020-11-24 19:10:32 +08:00
										 |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-24 21:37:54 +08:00
										 |  |  | 		body := `{"items": [{ userId:1, teamId: 2 }]}` | 
					
						
							|  |  |  | 		res, err := server.SendJSON(webtest.RequestWithSignedInUser(server.NewPostRequest("/api/dashboards/uid/1/permissions", strings.NewReader(body)), userWithPermissions(1, []accesscontrol.Permission{ | 
					
						
							|  |  |  | 			{Action: dashboards.ActionDashboardsPermissionsWrite, Scope: "dashboards:uid:1"}, | 
					
						
							|  |  |  | 		}))) | 
					
						
							| 
									
										
										
										
											2020-11-24 19:10:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-24 21:37:54 +08:00
										 |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 		assert.Equal(t, http.StatusBadRequest, res.StatusCode) | 
					
						
							|  |  |  | 		require.NoError(t, res.Body.Close()) | 
					
						
							| 
									
										
										
										
											2017-05-08 21:35:34 +08:00
										 |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2018-01-18 21:30:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-24 21:37:54 +08:00
										 |  |  | 	t.Run("should not be able to specify team and role in same acl", func(t *testing.T) { | 
					
						
							|  |  |  | 		server := SetupAPITestServer(t, func(hs *HTTPServer) { | 
					
						
							|  |  |  | 			hs.DashboardService = dashboards.NewFakeDashboardService(t) | 
					
						
							|  |  |  | 			hs.dashboardPermissionsService = &actest.FakePermissionsService{} | 
					
						
							|  |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2020-11-24 19:10:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-24 21:37:54 +08:00
										 |  |  | 		body := `{"items": [{ teamId:1, role: "Admin" }]}` | 
					
						
							|  |  |  | 		res, err := server.SendJSON(webtest.RequestWithSignedInUser(server.NewPostRequest("/api/dashboards/uid/1/permissions", strings.NewReader(body)), userWithPermissions(1, []accesscontrol.Permission{ | 
					
						
							|  |  |  | 			{Action: dashboards.ActionDashboardsPermissionsWrite, Scope: "dashboards:uid:1"}, | 
					
						
							|  |  |  | 		}))) | 
					
						
							| 
									
										
										
										
											2018-01-18 21:30:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-24 21:37:54 +08:00
										 |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 		assert.Equal(t, http.StatusBadRequest, res.StatusCode) | 
					
						
							|  |  |  | 		require.NoError(t, res.Body.Close()) | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2018-01-18 21:30:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-24 21:37:54 +08:00
										 |  |  | 	t.Run("should not be able to specify user and role in same acl", func(t *testing.T) { | 
					
						
							|  |  |  | 		server := SetupAPITestServer(t, func(hs *HTTPServer) { | 
					
						
							|  |  |  | 			hs.DashboardService = dashboards.NewFakeDashboardService(t) | 
					
						
							|  |  |  | 			hs.dashboardPermissionsService = &actest.FakePermissionsService{} | 
					
						
							| 
									
										
										
										
											2018-01-18 21:30:04 +08:00
										 |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-24 21:37:54 +08:00
										 |  |  | 		body := `{"items": [{ userId:1, role: "Admin" }]}` | 
					
						
							|  |  |  | 		res, err := server.SendJSON(webtest.RequestWithSignedInUser(server.NewPostRequest("/api/dashboards/uid/1/permissions", strings.NewReader(body)), userWithPermissions(1, []accesscontrol.Permission{ | 
					
						
							|  |  |  | 			{Action: dashboards.ActionDashboardsPermissionsWrite, Scope: "dashboards:uid:1"}, | 
					
						
							|  |  |  | 		}))) | 
					
						
							| 
									
										
										
										
											2018-01-18 21:30:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-24 21:37:54 +08:00
										 |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 		assert.Equal(t, http.StatusBadRequest, res.StatusCode) | 
					
						
							|  |  |  | 		require.NoError(t, res.Body.Close()) | 
					
						
							| 
									
										
										
										
											2018-01-18 21:30:04 +08:00
										 |  |  | 	}) | 
					
						
							|  |  |  | } |