2017-05-08 08:35:34 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2020-11-24 05:10:32 -06:00
|
|
|
"encoding/json"
|
2023-08-24 08:37:54 -05:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
2017-05-08 08:35:34 -05:00
|
|
|
"testing"
|
|
|
|
|
2022-03-10 11:19:50 -06:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2023-07-17 10:54:39 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
2023-08-24 08:37:54 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/accesscontrol/actest"
|
2022-03-10 11:19:50 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/dashboards"
|
2020-11-24 05:10:32 -06:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2023-08-24 08:37:54 -05:00
|
|
|
"github.com/grafana/grafana/pkg/web/webtest"
|
2017-05-08 08:35:34 -05:00
|
|
|
)
|
|
|
|
|
2023-08-24 08:37:54 -05: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 07:15:44 -06:00
|
|
|
|
2023-08-24 08:37:54 -05:00
|
|
|
res, err := server.Send(webtest.RequestWithSignedInUser(server.NewGetRequest("/api/dashboards/uid/1/permissions"), userWithPermissions(1, nil)))
|
2023-04-14 04:17:23 -05:00
|
|
|
require.NoError(t, err)
|
2023-08-24 08:37:54 -05:00
|
|
|
assert.Equal(t, http.StatusForbidden, res.StatusCode)
|
|
|
|
require.NoError(t, res.Body.Close())
|
|
|
|
})
|
2018-02-12 02:26:09 -06:00
|
|
|
|
2023-08-24 08:37:54 -05: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-26 13:15:57 -06:00
|
|
|
}
|
2020-11-13 02:52:38 -06:00
|
|
|
})
|
2018-02-12 02:26:09 -06:00
|
|
|
|
2023-08-24 08:37:54 -05: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 08:35:34 -05:00
|
|
|
|
2023-08-24 08:37:54 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, http.StatusOK, res.StatusCode)
|
|
|
|
require.NoError(t, res.Body.Close())
|
|
|
|
})
|
2020-11-24 05:10:32 -06:00
|
|
|
|
2023-08-24 08:37:54 -05: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 05:10:32 -06:00
|
|
|
|
2023-08-24 08:37:54 -05:00
|
|
|
svc := dashboards.NewFakeDashboardService(t)
|
|
|
|
svc.On("GetDashboard", mock.Anything, mock.Anything).Return(&dashboards.Dashboard{ID: 1, UID: "1"}, nil)
|
2018-01-18 07:30:04 -06:00
|
|
|
|
2023-08-24 08:37:54 -05: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-26 13:15:57 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
})
|
2018-01-18 07:30:04 -06:00
|
|
|
|
2023-08-24 08:37:54 -05: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-13 13:16:58 -05:00
|
|
|
|
2023-08-24 08:37:54 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, http.StatusOK, res.StatusCode)
|
2021-10-13 13:16:58 -05:00
|
|
|
|
2023-08-24 08:37:54 -05:00
|
|
|
var result []dashboards.DashboardACLInfoDTO
|
|
|
|
require.NoError(t, json.NewDecoder(res.Body).Decode(&result))
|
2021-10-13 13:16:58 -05:00
|
|
|
|
2023-08-24 08:37:54 -05:00
|
|
|
assert.Len(t, result, 1)
|
|
|
|
assert.Equal(t, result[0].UserLogin, "regular")
|
|
|
|
require.NoError(t, res.Body.Close())
|
|
|
|
})
|
|
|
|
}
|
2018-01-18 07:30:04 -06:00
|
|
|
|
2023-08-24 08:37:54 -05: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-22 16:01:04 -05:00
|
|
|
|
2023-08-24 08:37:54 -05: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-22 16:01:04 -05:00
|
|
|
|
2023-08-24 08:37:54 -05: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 08:36:41 -06:00
|
|
|
|
2023-08-24 08:37:54 -05:00
|
|
|
hs.DashboardService = svc
|
|
|
|
hs.dashboardPermissionsService = &actest.FakePermissionsService{}
|
2020-11-18 08:36:41 -06:00
|
|
|
})
|
|
|
|
|
2023-08-24 08:37:54 -05: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-26 13:15:57 -06:00
|
|
|
|
2023-08-24 08:37:54 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, http.StatusOK, res.StatusCode)
|
|
|
|
require.NoError(t, res.Body.Close())
|
|
|
|
})
|
2017-05-22 03:36:47 -05:00
|
|
|
|
2023-08-24 08:37:54 -05: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 05:10:32 -06:00
|
|
|
})
|
|
|
|
|
2023-08-24 08:37:54 -05: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 05:10:32 -06:00
|
|
|
|
2023-08-24 08:37:54 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, res.StatusCode)
|
|
|
|
require.NoError(t, res.Body.Close())
|
2017-05-08 08:35:34 -05:00
|
|
|
})
|
2018-01-18 07:30:04 -06:00
|
|
|
|
2023-08-24 08:37:54 -05: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 05:10:32 -06:00
|
|
|
|
2023-08-24 08:37:54 -05: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 07:30:04 -06:00
|
|
|
|
2023-08-24 08:37:54 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, res.StatusCode)
|
|
|
|
require.NoError(t, res.Body.Close())
|
|
|
|
})
|
2018-01-18 07:30:04 -06:00
|
|
|
|
2023-08-24 08:37:54 -05: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 07:30:04 -06:00
|
|
|
})
|
|
|
|
|
2023-08-24 08:37:54 -05: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 07:30:04 -06:00
|
|
|
|
2023-08-24 08:37:54 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, res.StatusCode)
|
|
|
|
require.NoError(t, res.Body.Close())
|
2018-01-18 07:30:04 -06:00
|
|
|
})
|
|
|
|
}
|