2020-11-24 05:10:32 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2022-01-26 08:48:41 -06:00
|
|
|
"strings"
|
2020-11-24 05:10:32 -06:00
|
|
|
"testing"
|
|
|
|
|
2022-02-09 10:46:37 -06:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
ac "github.com/grafana/grafana/pkg/services/accesscontrol"
|
2023-01-26 07:46:30 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/accesscontrol/actest"
|
2023-01-05 13:08:07 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/team/teamtest"
|
2020-11-24 05:10:32 -06:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2023-01-05 13:08:07 -06:00
|
|
|
"github.com/grafana/grafana/pkg/web/webtest"
|
2020-11-24 05:10:32 -06:00
|
|
|
)
|
|
|
|
|
2023-05-22 11:41:53 -05:00
|
|
|
func TestAddTeamMembersAPIEndpoint(t *testing.T) {
|
2023-01-05 13:08:07 -06:00
|
|
|
server := SetupAPITestServer(t, func(hs *HTTPServer) {
|
|
|
|
hs.Cfg = setting.NewCfg()
|
|
|
|
hs.teamService = teamtest.NewFakeService()
|
|
|
|
hs.teamPermissionsService = &actest.FakePermissionsService{}
|
2022-01-26 08:48:41 -06:00
|
|
|
})
|
|
|
|
|
2023-01-05 13:08:07 -06:00
|
|
|
t.Run("should be able to add team member with correct permission", func(t *testing.T) {
|
|
|
|
req := webtest.RequestWithSignedInUser(
|
|
|
|
server.NewRequest(http.MethodPost, "/api/teams/1/members", strings.NewReader("{\"userId\": 1}")),
|
|
|
|
userWithPermissions(1, []ac.Permission{{Action: ac.ActionTeamsPermissionsWrite, Scope: "teams:id:1"}}),
|
2022-02-09 10:46:37 -06:00
|
|
|
)
|
2023-01-05 13:08:07 -06:00
|
|
|
res, err := server.SendJSON(req)
|
2022-02-09 10:46:37 -06:00
|
|
|
require.NoError(t, err)
|
2023-01-05 13:08:07 -06:00
|
|
|
assert.Equal(t, http.StatusOK, res.StatusCode)
|
|
|
|
require.NoError(t, res.Body.Close())
|
2022-02-09 10:46:37 -06:00
|
|
|
})
|
|
|
|
|
2023-01-05 13:08:07 -06:00
|
|
|
t.Run("should not be able to add team member without correct permission", func(t *testing.T) {
|
|
|
|
req := webtest.RequestWithSignedInUser(
|
|
|
|
server.NewRequest(http.MethodPost, "/api/teams/1/members", strings.NewReader("{\"userId\": 1}")),
|
|
|
|
userWithPermissions(1, []ac.Permission{{Action: ac.ActionTeamsPermissionsWrite, Scope: "teams:id:2"}}),
|
|
|
|
)
|
|
|
|
res, err := server.SendJSON(req)
|
2022-02-09 10:46:37 -06:00
|
|
|
require.NoError(t, err)
|
2023-01-05 13:08:07 -06:00
|
|
|
assert.Equal(t, http.StatusForbidden, res.StatusCode)
|
|
|
|
require.NoError(t, res.Body.Close())
|
2022-02-09 10:46:37 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-05-22 11:41:53 -05:00
|
|
|
func TestGetTeamMembersAPIEndpoint(t *testing.T) {
|
2023-01-05 13:08:07 -06:00
|
|
|
server := SetupAPITestServer(t, func(hs *HTTPServer) {
|
|
|
|
hs.Cfg = setting.NewCfg()
|
|
|
|
hs.teamService = teamtest.NewFakeService()
|
|
|
|
hs.teamPermissionsService = &actest.FakePermissionsService{}
|
2022-01-26 08:48:41 -06:00
|
|
|
})
|
|
|
|
|
2023-01-05 13:08:07 -06:00
|
|
|
t.Run("should be able to get team members with correct permission", func(t *testing.T) {
|
|
|
|
req := webtest.RequestWithSignedInUser(
|
|
|
|
server.NewGetRequest("/api/teams/1/members"),
|
|
|
|
userWithPermissions(1, []ac.Permission{{Action: ac.ActionTeamsPermissionsRead, Scope: "teams:id:1"}}),
|
|
|
|
)
|
|
|
|
res, err := server.SendJSON(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, http.StatusOK, res.StatusCode)
|
|
|
|
require.NoError(t, res.Body.Close())
|
2022-01-26 08:48:41 -06:00
|
|
|
})
|
2023-01-05 13:08:07 -06:00
|
|
|
t.Run("should not be able to get team members without correct permission", func(t *testing.T) {
|
|
|
|
req := webtest.RequestWithSignedInUser(
|
|
|
|
server.NewGetRequest("/api/teams/1/members"),
|
|
|
|
userWithPermissions(1, []ac.Permission{{Action: ac.ActionTeamsPermissionsRead, Scope: "teams:id:2"}}),
|
|
|
|
)
|
|
|
|
res, err := server.SendJSON(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, http.StatusForbidden, res.StatusCode)
|
|
|
|
require.NoError(t, res.Body.Close())
|
2022-01-26 08:48:41 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-05-22 11:41:53 -05:00
|
|
|
func TestUpdateTeamMembersAPIEndpoint(t *testing.T) {
|
2023-01-05 13:08:07 -06:00
|
|
|
server := SetupAPITestServer(t, func(hs *HTTPServer) {
|
|
|
|
hs.Cfg = setting.NewCfg()
|
|
|
|
hs.teamService = &teamtest.FakeService{ExpectedIsMember: true}
|
|
|
|
hs.teamPermissionsService = &actest.FakePermissionsService{}
|
2022-01-26 08:48:41 -06:00
|
|
|
})
|
|
|
|
|
2023-01-05 13:08:07 -06:00
|
|
|
t.Run("should be able to update team member with correct permission", func(t *testing.T) {
|
|
|
|
req := webtest.RequestWithSignedInUser(
|
|
|
|
server.NewRequest(http.MethodPut, "/api/teams/1/members/1", strings.NewReader("{\"permission\": 1}")),
|
|
|
|
userWithPermissions(1, []ac.Permission{{Action: ac.ActionTeamsPermissionsWrite, Scope: "teams:id:1"}}),
|
|
|
|
)
|
|
|
|
res, err := server.SendJSON(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, http.StatusOK, res.StatusCode)
|
|
|
|
require.NoError(t, res.Body.Close())
|
2022-01-26 08:48:41 -06:00
|
|
|
})
|
2023-01-05 13:08:07 -06:00
|
|
|
t.Run("should not be able to update team member without correct permission", func(t *testing.T) {
|
|
|
|
req := webtest.RequestWithSignedInUser(
|
|
|
|
server.NewRequest(http.MethodPut, "/api/teams/1/members/1", strings.NewReader("{\"permission\": 1}")),
|
|
|
|
userWithPermissions(1, []ac.Permission{{Action: ac.ActionTeamsPermissionsWrite, Scope: "teams:id:2"}}),
|
|
|
|
)
|
|
|
|
res, err := server.SendJSON(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, http.StatusForbidden, res.StatusCode)
|
|
|
|
require.NoError(t, res.Body.Close())
|
2022-01-26 08:48:41 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-05-22 11:41:53 -05:00
|
|
|
func TestDeleteTeamMembersAPIEndpoint(t *testing.T) {
|
2023-01-05 13:08:07 -06:00
|
|
|
server := SetupAPITestServer(t, func(hs *HTTPServer) {
|
|
|
|
hs.Cfg = setting.NewCfg()
|
|
|
|
hs.teamService = &teamtest.FakeService{ExpectedIsMember: true}
|
|
|
|
hs.teamPermissionsService = &actest.FakePermissionsService{}
|
2022-01-26 08:48:41 -06:00
|
|
|
})
|
|
|
|
|
2023-01-05 13:08:07 -06:00
|
|
|
t.Run("should be able to delete team member with correct permission", func(t *testing.T) {
|
|
|
|
req := webtest.RequestWithSignedInUser(
|
|
|
|
server.NewRequest(http.MethodDelete, "/api/teams/1/members/1", nil),
|
|
|
|
userWithPermissions(1, []ac.Permission{{Action: ac.ActionTeamsPermissionsWrite, Scope: "teams:id:1"}}),
|
|
|
|
)
|
|
|
|
res, err := server.SendJSON(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, http.StatusOK, res.StatusCode)
|
|
|
|
require.NoError(t, res.Body.Close())
|
2022-01-26 08:48:41 -06:00
|
|
|
})
|
2023-01-05 13:08:07 -06:00
|
|
|
t.Run("should not be able to delete member without correct permission", func(t *testing.T) {
|
|
|
|
req := webtest.RequestWithSignedInUser(
|
|
|
|
server.NewRequest(http.MethodDelete, "/api/teams/1/members/1", nil),
|
|
|
|
userWithPermissions(1, []ac.Permission{{Action: ac.ActionTeamsPermissionsWrite, Scope: "teams:id:2"}}),
|
|
|
|
)
|
|
|
|
res, err := server.SendJSON(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, http.StatusForbidden, res.StatusCode)
|
|
|
|
require.NoError(t, res.Body.Close())
|
2022-01-26 08:48:41 -06:00
|
|
|
})
|
|
|
|
}
|