2020-11-24 05:10:32 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2021-12-01 08:43:31 -06:00
|
|
|
"context"
|
2020-11-24 05:10:32 -06:00
|
|
|
"encoding/json"
|
2022-01-24 04:52:35 -06:00
|
|
|
"fmt"
|
2020-11-24 05:10:32 -06:00
|
|
|
"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"
|
2023-01-05 13:08:07 -06:00
|
|
|
"github.com/stretchr/testify/mock"
|
2022-02-09 10:46:37 -06:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2022-10-19 08:02:15 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/db"
|
2023-01-18 09:01:25 -06:00
|
|
|
"github.com/grafana/grafana/pkg/infra/db/dbtest"
|
2022-02-09 10:46:37 -06:00
|
|
|
ac "github.com/grafana/grafana/pkg/services/accesscontrol"
|
2023-01-26 07:46:30 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/accesscontrol/actest"
|
|
|
|
"github.com/grafana/grafana/pkg/services/dashboards"
|
2020-11-24 05:10:32 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/licensing"
|
2022-08-10 04:56:48 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/org"
|
2022-11-28 05:05:46 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/org/orgimpl"
|
2022-12-07 10:03:22 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/quota/quotaimpl"
|
2022-01-24 04:52:35 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/sqlstore"
|
2023-02-06 10:50:03 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/supportbundles/supportbundlestest"
|
2023-01-26 07:46:30 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/team"
|
2022-09-20 11:58:04 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/team/teamimpl"
|
2023-01-05 13:08:07 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/team/teamtest"
|
2022-01-26 08:48:41 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/teamguardian/database"
|
|
|
|
"github.com/grafana/grafana/pkg/services/teamguardian/manager"
|
2022-06-28 07:32:25 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/user"
|
2022-12-07 10:03:22 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/user/userimpl"
|
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
|
|
|
)
|
|
|
|
|
2022-02-09 06:44:38 -06:00
|
|
|
type TeamGuardianMock struct {
|
|
|
|
result error
|
|
|
|
}
|
|
|
|
|
2022-08-10 04:56:48 -05:00
|
|
|
func (t *TeamGuardianMock) CanAdmin(ctx context.Context, orgId int64, teamId int64, user *user.SignedInUser) error {
|
2022-02-09 06:44:38 -06:00
|
|
|
return t.result
|
|
|
|
}
|
|
|
|
|
2022-07-15 11:06:44 -05:00
|
|
|
func (t *TeamGuardianMock) DeleteByUser(ctx context.Context, userID int64) error {
|
|
|
|
return t.result
|
|
|
|
}
|
|
|
|
|
2022-01-24 04:52:35 -06:00
|
|
|
func setUpGetTeamMembersHandler(t *testing.T, sqlStore *sqlstore.SQLStore) {
|
|
|
|
const testOrgID int64 = 1
|
2022-06-28 07:32:25 -05:00
|
|
|
var userCmd user.CreateUserCommand
|
2022-09-22 12:16:21 -05:00
|
|
|
teamSvc := teamimpl.ProvideService(sqlStore, setting.NewCfg())
|
|
|
|
team, err := teamSvc.CreateTeam("group1 name", "test1@test.com", testOrgID)
|
2022-01-24 04:52:35 -06:00
|
|
|
require.NoError(t, err)
|
2022-12-07 10:03:22 -06:00
|
|
|
quotaService := quotaimpl.ProvideService(sqlStore, sqlStore.Cfg)
|
|
|
|
orgService, err := orgimpl.ProvideService(sqlStore, sqlStore.Cfg, quotaService)
|
|
|
|
require.NoError(t, err)
|
2023-02-06 10:50:03 -06:00
|
|
|
usrSvc, err := userimpl.ProvideService(sqlStore, orgService, sqlStore.Cfg, nil, nil, quotaService, supportbundlestest.NewFakeBundleService())
|
2022-12-07 10:03:22 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-01-24 04:52:35 -06:00
|
|
|
for i := 0; i < 3; i++ {
|
2022-06-28 07:32:25 -05:00
|
|
|
userCmd = user.CreateUserCommand{
|
2022-01-24 04:52:35 -06:00
|
|
|
Email: fmt.Sprint("user", i, "@test.com"),
|
|
|
|
Name: fmt.Sprint("user", i),
|
|
|
|
Login: fmt.Sprint("loginuser", i),
|
2020-11-24 05:10:32 -06:00
|
|
|
}
|
2022-01-24 04:52:35 -06:00
|
|
|
// user
|
2023-03-03 10:01:23 -06:00
|
|
|
user, err := usrSvc.Create(context.Background(), &userCmd)
|
2022-01-24 04:52:35 -06:00
|
|
|
require.NoError(t, err)
|
2023-01-11 07:20:09 -06:00
|
|
|
err = teamSvc.AddTeamMember(user.ID, testOrgID, team.ID, false, 1)
|
2022-01-24 04:52:35 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2020-11-24 05:10:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTeamMembersAPIEndpoint_userLoggedIn(t *testing.T) {
|
2022-02-09 10:46:37 -06:00
|
|
|
hs := setupSimpleHTTPServer(nil)
|
|
|
|
settings := hs.Cfg
|
2022-10-19 08:02:15 -05:00
|
|
|
sqlStore := db.InitTestDB(t)
|
2022-02-09 10:46:37 -06:00
|
|
|
sqlStore.Cfg = settings
|
|
|
|
|
|
|
|
hs.SQLStore = sqlStore
|
2022-09-22 12:16:21 -05:00
|
|
|
hs.teamService = teamimpl.ProvideService(sqlStore, settings)
|
2022-02-09 10:46:37 -06:00
|
|
|
hs.License = &licensing.OSSLicensingService{}
|
|
|
|
hs.teamGuardian = &TeamGuardianMock{}
|
2023-01-18 09:01:25 -06:00
|
|
|
mock := dbtest.NewFakeDB()
|
2020-11-24 05:10:32 -06:00
|
|
|
|
|
|
|
loggedInUserScenarioWithRole(t, "When calling GET on", "GET", "api/teams/1/members",
|
2022-08-10 04:56:48 -05:00
|
|
|
"api/teams/:teamId/members", org.RoleAdmin, func(sc *scenarioContext) {
|
2022-01-24 04:52:35 -06:00
|
|
|
setUpGetTeamMembersHandler(t, sqlStore)
|
2020-11-24 05:10:32 -06:00
|
|
|
|
|
|
|
sc.handlerFunc = hs.GetTeamMembers
|
|
|
|
sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
|
|
|
|
|
|
|
|
require.Equal(t, http.StatusOK, sc.resp.Code)
|
|
|
|
|
2023-01-13 02:43:38 -06:00
|
|
|
var resp []team.TeamMemberDTO
|
2020-11-24 05:10:32 -06:00
|
|
|
err := json.Unmarshal(sc.resp.Body.Bytes(), &resp)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, resp, 3)
|
2022-02-03 02:20:20 -06:00
|
|
|
}, mock)
|
2020-11-24 05:10:32 -06:00
|
|
|
|
|
|
|
t.Run("Given there is two hidden users", func(t *testing.T) {
|
|
|
|
settings.HiddenUsers = map[string]struct{}{
|
|
|
|
"user1": {},
|
|
|
|
testUserLogin: {},
|
|
|
|
}
|
|
|
|
t.Cleanup(func() { settings.HiddenUsers = make(map[string]struct{}) })
|
|
|
|
|
|
|
|
loggedInUserScenarioWithRole(t, "When calling GET on", "GET", "api/teams/1/members",
|
2022-08-10 04:56:48 -05:00
|
|
|
"api/teams/:teamId/members", org.RoleAdmin, func(sc *scenarioContext) {
|
2022-01-24 04:52:35 -06:00
|
|
|
setUpGetTeamMembersHandler(t, sqlStore)
|
2020-11-24 05:10:32 -06:00
|
|
|
|
|
|
|
sc.handlerFunc = hs.GetTeamMembers
|
|
|
|
sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
|
|
|
|
|
|
|
|
require.Equal(t, http.StatusOK, sc.resp.Code)
|
|
|
|
|
2023-01-13 02:43:38 -06:00
|
|
|
var resp []team.TeamMemberDTO
|
2020-11-24 05:10:32 -06:00
|
|
|
err := json.Unmarshal(sc.resp.Body.Bytes(), &resp)
|
|
|
|
require.NoError(t, err)
|
2022-01-24 04:52:35 -06:00
|
|
|
assert.Len(t, resp, 3)
|
|
|
|
assert.Equal(t, "loginuser0", resp[0].Login)
|
|
|
|
assert.Equal(t, "loginuser1", resp[1].Login)
|
|
|
|
assert.Equal(t, "loginuser2", resp[2].Login)
|
2022-02-03 02:20:20 -06:00
|
|
|
}, mock)
|
2020-11-24 05:10:32 -06:00
|
|
|
})
|
|
|
|
}
|
2022-01-26 08:48:41 -06:00
|
|
|
|
2023-01-05 13:08:07 -06:00
|
|
|
func TestAddTeamMembersAPIEndpoint_LegacyAccessControl(t *testing.T) {
|
|
|
|
server := SetupAPITestServer(t, func(hs *HTTPServer) {
|
|
|
|
cfg := setting.NewCfg()
|
|
|
|
cfg.RBACEnabled = false
|
|
|
|
cfg.EditorsCanAdmin = true
|
|
|
|
hs.Cfg = cfg
|
|
|
|
hs.teamService = teamtest.NewFakeService()
|
|
|
|
store := &database.TeamGuardianStoreMock{}
|
2023-01-13 02:43:38 -06:00
|
|
|
store.On("GetTeamMembers", mock.Anything, mock.Anything).Return([]*team.TeamMemberDTO{
|
2023-01-26 07:46:30 -06:00
|
|
|
{UserID: 2, Permission: dashboards.PERMISSION_ADMIN},
|
|
|
|
{UserID: 3, Permission: dashboards.PERMISSION_VIEW},
|
2023-01-05 13:08:07 -06:00
|
|
|
}, nil).Maybe()
|
|
|
|
hs.teamGuardian = manager.ProvideService(store)
|
|
|
|
hs.teamPermissionsService = &actest.FakePermissionsService{}
|
2022-01-26 08:48:41 -06:00
|
|
|
})
|
|
|
|
|
2023-01-05 13:08:07 -06:00
|
|
|
t.Run("Admin can add team member", func(t *testing.T) {
|
|
|
|
req := webtest.RequestWithSignedInUser(
|
|
|
|
server.NewRequest(http.MethodPost, "/api/teams/1/members", strings.NewReader("{\"userId\": 1}")),
|
|
|
|
&user.SignedInUser{OrgID: 1, OrgRole: org.RoleAdmin},
|
|
|
|
)
|
|
|
|
res, err := server.SendJSON(req)
|
2022-01-26 08:48:41 -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-01-26 08:48:41 -06:00
|
|
|
|
2023-01-05 13:08:07 -06:00
|
|
|
t.Run("Editor cannot add team member", func(t *testing.T) {
|
|
|
|
req := webtest.RequestWithSignedInUser(
|
|
|
|
server.NewRequest(http.MethodPost, "/api/teams/1/members", strings.NewReader("{\"userId\": 1}")),
|
|
|
|
&user.SignedInUser{OrgID: 1, OrgRole: org.RoleEditor},
|
|
|
|
)
|
|
|
|
res, err := server.SendJSON(req)
|
2022-01-26 08:48:41 -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-01-26 08:48:41 -06:00
|
|
|
})
|
|
|
|
|
2023-01-05 13:08:07 -06:00
|
|
|
t.Run("team member cannot add members", func(t *testing.T) {
|
|
|
|
req := webtest.RequestWithSignedInUser(
|
|
|
|
server.NewRequest(http.MethodPost, "/api/teams/1/members", strings.NewReader("{\"userId\": 1}")),
|
|
|
|
&user.SignedInUser{UserID: 3, OrgID: 1, OrgRole: org.RoleViewer},
|
|
|
|
)
|
|
|
|
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-01-05 13:08:07 -06:00
|
|
|
t.Run("team admin can add members", func(t *testing.T) {
|
|
|
|
req := webtest.RequestWithSignedInUser(
|
|
|
|
server.NewRequest(http.MethodPost, "/api/teams/1/members", strings.NewReader("{\"userId\": 1}")),
|
|
|
|
&user.SignedInUser{UserID: 2, OrgID: 1, OrgRole: org.RoleEditor},
|
|
|
|
)
|
|
|
|
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
|
|
|
}
|
2022-01-26 08:48:41 -06:00
|
|
|
|
2023-01-05 13:08:07 -06:00
|
|
|
func TestAddTeamMembersAPIEndpoint_RBAC(t *testing.T) {
|
|
|
|
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-01-05 13:08:07 -06:00
|
|
|
func TestGetTeamMembersAPIEndpoint_RBAC(t *testing.T) {
|
|
|
|
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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateTeamMembersAPIEndpoint_LegacyAccessControl(t *testing.T) {
|
2023-01-05 13:08:07 -06:00
|
|
|
server := SetupAPITestServer(t, func(hs *HTTPServer) {
|
|
|
|
cfg := setting.NewCfg()
|
|
|
|
cfg.RBACEnabled = false
|
|
|
|
cfg.EditorsCanAdmin = true
|
|
|
|
hs.Cfg = cfg
|
|
|
|
hs.teamService = &teamtest.FakeService{ExpectedIsMember: true}
|
|
|
|
store := &database.TeamGuardianStoreMock{}
|
2023-01-13 02:43:38 -06:00
|
|
|
store.On("GetTeamMembers", mock.Anything, mock.Anything).Return([]*team.TeamMemberDTO{
|
2023-01-26 07:46:30 -06:00
|
|
|
{UserID: 2, Permission: dashboards.PERMISSION_ADMIN},
|
|
|
|
{UserID: 3, Permission: dashboards.PERMISSION_VIEW},
|
2023-01-05 13:08:07 -06:00
|
|
|
}, nil).Maybe()
|
|
|
|
hs.teamGuardian = manager.ProvideService(store)
|
|
|
|
hs.teamPermissionsService = &actest.FakePermissionsService{}
|
2022-01-26 08:48:41 -06:00
|
|
|
})
|
|
|
|
|
2023-01-05 13:08:07 -06:00
|
|
|
t.Run("Admin can update team member", func(t *testing.T) {
|
|
|
|
req := webtest.RequestWithSignedInUser(
|
|
|
|
server.NewRequest(http.MethodPut, "/api/teams/1/members/1", strings.NewReader("{\"permission\": 4}")),
|
|
|
|
&user.SignedInUser{OrgID: 1, OrgRole: org.RoleAdmin},
|
|
|
|
)
|
|
|
|
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("Editor cannot update team member", func(t *testing.T) {
|
|
|
|
req := webtest.RequestWithSignedInUser(
|
|
|
|
server.NewRequest(http.MethodPut, "/api/teams/1/members/1", strings.NewReader("{\"permission\": 4}")),
|
|
|
|
&user.SignedInUser{OrgID: 1, OrgRole: org.RoleEditor},
|
|
|
|
)
|
|
|
|
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-01-05 13:08:07 -06:00
|
|
|
t.Run("team member cannot update member", func(t *testing.T) {
|
|
|
|
req := webtest.RequestWithSignedInUser(
|
|
|
|
server.NewRequest(http.MethodPut, "/api/teams/1/members/1", strings.NewReader("{\"permission\": 4}")),
|
|
|
|
&user.SignedInUser{UserID: 3, OrgID: 1, OrgRole: org.RoleViewer},
|
|
|
|
)
|
|
|
|
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-01-05 13:08:07 -06:00
|
|
|
|
|
|
|
t.Run("team admin can add members", func(t *testing.T) {
|
|
|
|
req := webtest.RequestWithSignedInUser(
|
|
|
|
server.NewRequest(http.MethodPut, "/api/teams/1/members/1", strings.NewReader("{\"permission\": 4}")),
|
|
|
|
&user.SignedInUser{UserID: 2, OrgID: 1, OrgRole: org.RoleEditor},
|
|
|
|
)
|
|
|
|
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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-21 07:31:02 -05:00
|
|
|
func TestUpdateTeamMembersAPIEndpoint_RBAC(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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteTeamMembersAPIEndpoint_LegacyAccessControl(t *testing.T) {
|
2023-01-05 13:08:07 -06:00
|
|
|
server := SetupAPITestServer(t, func(hs *HTTPServer) {
|
|
|
|
cfg := setting.NewCfg()
|
|
|
|
cfg.RBACEnabled = false
|
|
|
|
cfg.EditorsCanAdmin = true
|
|
|
|
hs.Cfg = cfg
|
|
|
|
hs.teamService = &teamtest.FakeService{ExpectedIsMember: true}
|
|
|
|
store := &database.TeamGuardianStoreMock{}
|
2023-01-13 02:43:38 -06:00
|
|
|
store.On("GetTeamMembers", mock.Anything, mock.Anything).Return([]*team.TeamMemberDTO{
|
2023-01-26 07:46:30 -06:00
|
|
|
{UserID: 2, Permission: dashboards.PERMISSION_ADMIN},
|
|
|
|
{UserID: 3, Permission: dashboards.PERMISSION_VIEW},
|
2023-01-05 13:08:07 -06:00
|
|
|
}, nil).Maybe()
|
|
|
|
hs.teamGuardian = manager.ProvideService(store)
|
|
|
|
hs.teamPermissionsService = &actest.FakePermissionsService{}
|
2022-01-26 08:48:41 -06:00
|
|
|
})
|
|
|
|
|
2023-01-05 13:08:07 -06:00
|
|
|
t.Run("Admin can delete team member", func(t *testing.T) {
|
|
|
|
req := webtest.RequestWithSignedInUser(
|
|
|
|
server.NewRequest(http.MethodDelete, "/api/teams/1/members/1", nil),
|
|
|
|
&user.SignedInUser{OrgID: 1, OrgRole: org.RoleAdmin},
|
|
|
|
)
|
|
|
|
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("Editor cannot delete team member", func(t *testing.T) {
|
|
|
|
req := webtest.RequestWithSignedInUser(
|
|
|
|
server.NewRequest(http.MethodDelete, "/api/teams/1/members/1", nil),
|
|
|
|
&user.SignedInUser{OrgID: 1, OrgRole: org.RoleEditor},
|
|
|
|
)
|
|
|
|
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-01-05 13:08:07 -06:00
|
|
|
t.Run("team member cannot delete member", func(t *testing.T) {
|
|
|
|
req := webtest.RequestWithSignedInUser(
|
|
|
|
server.NewRequest(http.MethodDelete, "/api/teams/1/members/1", nil),
|
|
|
|
&user.SignedInUser{UserID: 3, OrgID: 1, OrgRole: org.RoleViewer},
|
|
|
|
)
|
|
|
|
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-01-05 13:08:07 -06:00
|
|
|
|
|
|
|
t.Run("team admin can delete members", func(t *testing.T) {
|
|
|
|
req := webtest.RequestWithSignedInUser(
|
|
|
|
server.NewRequest(http.MethodDelete, "/api/teams/1/members/1", nil),
|
|
|
|
&user.SignedInUser{UserID: 2, OrgID: 1, OrgRole: org.RoleEditor},
|
|
|
|
)
|
|
|
|
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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-21 07:31:02 -05:00
|
|
|
func TestDeleteTeamMembersAPIEndpoint_RBAC(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
|
|
|
})
|
|
|
|
}
|