2022-07-08 06:07:00 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
2022-08-10 04:56:48 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/org"
|
2022-08-04 06:22:43 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/user"
|
|
|
|
"github.com/grafana/grafana/pkg/services/user/usertest"
|
2022-07-08 06:07:00 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestOrgInvitesAPIEndpointAccess(t *testing.T) {
|
|
|
|
type accessControlTestCase2 struct {
|
|
|
|
expectedCode int
|
|
|
|
desc string
|
|
|
|
url string
|
|
|
|
method string
|
|
|
|
permissions []accesscontrol.Permission
|
|
|
|
input string
|
|
|
|
}
|
|
|
|
tests := []accessControlTestCase2{
|
|
|
|
{
|
|
|
|
expectedCode: http.StatusOK,
|
2022-07-27 11:37:27 -05:00
|
|
|
desc: "org viewer with the correct permissions can invite an existing user to his org",
|
2022-07-08 06:07:00 -05:00
|
|
|
url: "/api/org/invites",
|
|
|
|
method: http.MethodPost,
|
|
|
|
permissions: []accesscontrol.Permission{{Action: accesscontrol.ActionOrgUsersAdd, Scope: accesscontrol.ScopeUsersAll}},
|
2022-08-10 04:56:48 -05:00
|
|
|
input: `{"loginOrEmail": "` + testAdminOrg2.Login + `", "role": "` + string(org.RoleViewer) + `"}`,
|
2022-07-08 06:07:00 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
expectedCode: http.StatusForbidden,
|
2022-07-27 11:37:27 -05:00
|
|
|
desc: "org viewer with missing permissions cannot invite an existing user to his org",
|
2022-07-08 06:07:00 -05:00
|
|
|
url: "/api/org/invites",
|
|
|
|
method: http.MethodPost,
|
|
|
|
permissions: []accesscontrol.Permission{},
|
2022-08-10 04:56:48 -05:00
|
|
|
input: `{"loginOrEmail": "` + testAdminOrg2.Login + `", "role": "` + string(org.RoleViewer) + `"}`,
|
2022-07-08 06:07:00 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
expectedCode: http.StatusForbidden,
|
2022-07-27 11:37:27 -05:00
|
|
|
desc: "org viewer with the wrong scope cannot invite an existing user to his org",
|
2022-07-08 06:07:00 -05:00
|
|
|
url: "/api/org/invites",
|
|
|
|
method: http.MethodPost,
|
|
|
|
permissions: []accesscontrol.Permission{{Action: accesscontrol.ActionOrgUsersAdd, Scope: "users:id:100"}},
|
2022-08-10 04:56:48 -05:00
|
|
|
input: `{"loginOrEmail": "` + testAdminOrg2.Login + `", "role": "` + string(org.RoleViewer) + `"}`,
|
2022-07-08 06:07:00 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
expectedCode: http.StatusOK,
|
|
|
|
desc: "org viewer with the correct permissions can invite a new user to his org",
|
|
|
|
url: "/api/org/invites",
|
|
|
|
method: http.MethodPost,
|
2022-07-27 11:37:27 -05:00
|
|
|
permissions: []accesscontrol.Permission{{Action: accesscontrol.ActionOrgUsersAdd, Scope: accesscontrol.ScopeUsersAll}},
|
2022-08-10 04:56:48 -05:00
|
|
|
input: `{"loginOrEmail": "new user", "role": "` + string(org.RoleViewer) + `"}`,
|
2022-07-08 06:07:00 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
expectedCode: http.StatusForbidden,
|
|
|
|
desc: "org viewer with missing permissions cannot invite a new user to his org",
|
|
|
|
url: "/api/org/invites",
|
|
|
|
method: http.MethodPost,
|
|
|
|
permissions: []accesscontrol.Permission{},
|
2022-08-10 04:56:48 -05:00
|
|
|
input: `{"loginOrEmail": "new user", "role": "` + string(org.RoleViewer) + `"}`,
|
2022-07-08 06:07:00 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
2022-08-11 08:37:31 -05:00
|
|
|
sc := setupHTTPServer(t, true)
|
2022-08-04 06:22:43 -05:00
|
|
|
userService := usertest.NewUserServiceFake()
|
|
|
|
userService.ExpectedUser = &user.User{ID: 2}
|
|
|
|
sc.hs.userService = userService
|
2022-07-08 06:07:00 -05:00
|
|
|
setInitCtxSignedInViewer(sc.initCtx)
|
|
|
|
setupOrgUsersDBForAccessControlTests(t, sc.db)
|
2022-08-11 06:28:55 -05:00
|
|
|
setAccessControlPermissions(sc.acmock, test.permissions, sc.initCtx.OrgID)
|
2022-07-08 06:07:00 -05:00
|
|
|
|
|
|
|
input := strings.NewReader(test.input)
|
|
|
|
response := callAPI(sc.server, test.method, test.url, input, t)
|
|
|
|
assert.Equal(t, test.expectedCode, response.Code)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|