2021-10-12 10:47:19 +00:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-10-12 10:47:19 +00:00
|
|
|
|
2022-09-02 19:18:23 +00:00
|
|
|
package integration
|
2021-10-12 10:47:19 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
2023-01-17 21:46:03 +00:00
|
|
|
auth_model "code.gitea.io/gitea/models/auth"
|
2021-10-12 10:47:19 +00:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2022-09-02 19:18:23 +00:00
|
|
|
"code.gitea.io/gitea/tests"
|
2021-11-17 12:34:35 +00:00
|
|
|
|
2021-10-12 10:47:19 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
type apiUserOrgPermTestCase struct {
|
|
|
|
LoginUser string
|
|
|
|
User string
|
|
|
|
Organization string
|
|
|
|
ExpectedOrganizationPermissions api.OrganizationPermissions
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTokenNeeded(t *testing.T) {
|
2022-09-02 19:18:23 +00:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2021-10-12 10:47:19 +00:00
|
|
|
|
|
|
|
req := NewRequest(t, "GET", "/api/v1/users/user1/orgs/user6/permissions")
|
2022-12-02 03:39:42 +00:00
|
|
|
MakeRequest(t, req, http.StatusUnauthorized)
|
2021-10-12 10:47:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func sampleTest(t *testing.T, auoptc apiUserOrgPermTestCase) {
|
2022-09-02 19:18:23 +00:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2021-10-12 10:47:19 +00:00
|
|
|
|
|
|
|
session := loginUser(t, auoptc.LoginUser)
|
2023-01-17 21:46:03 +00:00
|
|
|
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadOrg)
|
2021-10-12 10:47:19 +00:00
|
|
|
|
|
|
|
req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/users/%s/orgs/%s/permissions?token=%s", auoptc.User, auoptc.Organization, token))
|
2022-12-02 03:39:42 +00:00
|
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
2021-10-12 10:47:19 +00:00
|
|
|
|
|
|
|
var apiOP api.OrganizationPermissions
|
|
|
|
DecodeJSON(t, resp, &apiOP)
|
|
|
|
assert.Equal(t, auoptc.ExpectedOrganizationPermissions.IsOwner, apiOP.IsOwner)
|
|
|
|
assert.Equal(t, auoptc.ExpectedOrganizationPermissions.IsAdmin, apiOP.IsAdmin)
|
|
|
|
assert.Equal(t, auoptc.ExpectedOrganizationPermissions.CanWrite, apiOP.CanWrite)
|
|
|
|
assert.Equal(t, auoptc.ExpectedOrganizationPermissions.CanRead, apiOP.CanRead)
|
|
|
|
assert.Equal(t, auoptc.ExpectedOrganizationPermissions.CanCreateRepository, apiOP.CanCreateRepository)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWithOwnerUser(t *testing.T) {
|
|
|
|
sampleTest(t, apiUserOrgPermTestCase{
|
|
|
|
LoginUser: "user2",
|
|
|
|
User: "user2",
|
|
|
|
Organization: "user3",
|
|
|
|
ExpectedOrganizationPermissions: api.OrganizationPermissions{
|
|
|
|
IsOwner: true,
|
|
|
|
IsAdmin: true,
|
|
|
|
CanWrite: true,
|
|
|
|
CanRead: true,
|
|
|
|
CanCreateRepository: true,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCanWriteUser(t *testing.T) {
|
|
|
|
sampleTest(t, apiUserOrgPermTestCase{
|
|
|
|
LoginUser: "user4",
|
|
|
|
User: "user4",
|
|
|
|
Organization: "user3",
|
|
|
|
ExpectedOrganizationPermissions: api.OrganizationPermissions{
|
|
|
|
IsOwner: false,
|
|
|
|
IsAdmin: false,
|
|
|
|
CanWrite: true,
|
|
|
|
CanRead: true,
|
|
|
|
CanCreateRepository: false,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAdminUser(t *testing.T) {
|
|
|
|
sampleTest(t, apiUserOrgPermTestCase{
|
|
|
|
LoginUser: "user1",
|
|
|
|
User: "user28",
|
|
|
|
Organization: "user3",
|
|
|
|
ExpectedOrganizationPermissions: api.OrganizationPermissions{
|
|
|
|
IsOwner: false,
|
|
|
|
IsAdmin: true,
|
|
|
|
CanWrite: true,
|
|
|
|
CanRead: true,
|
|
|
|
CanCreateRepository: true,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAdminCanNotCreateRepo(t *testing.T) {
|
|
|
|
sampleTest(t, apiUserOrgPermTestCase{
|
|
|
|
LoginUser: "user1",
|
|
|
|
User: "user28",
|
|
|
|
Organization: "user6",
|
|
|
|
ExpectedOrganizationPermissions: api.OrganizationPermissions{
|
|
|
|
IsOwner: false,
|
|
|
|
IsAdmin: true,
|
|
|
|
CanWrite: true,
|
|
|
|
CanRead: true,
|
|
|
|
CanCreateRepository: false,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCanReadUser(t *testing.T) {
|
|
|
|
sampleTest(t, apiUserOrgPermTestCase{
|
|
|
|
LoginUser: "user1",
|
|
|
|
User: "user24",
|
|
|
|
Organization: "org25",
|
|
|
|
ExpectedOrganizationPermissions: api.OrganizationPermissions{
|
|
|
|
IsOwner: false,
|
|
|
|
IsAdmin: false,
|
|
|
|
CanWrite: false,
|
|
|
|
CanRead: true,
|
|
|
|
CanCreateRepository: false,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnknowUser(t *testing.T) {
|
2022-09-02 19:18:23 +00:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2021-10-12 10:47:19 +00:00
|
|
|
|
|
|
|
session := loginUser(t, "user1")
|
2023-01-17 21:46:03 +00:00
|
|
|
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadOrg)
|
2021-10-12 10:47:19 +00:00
|
|
|
|
|
|
|
req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/users/unknow/orgs/org25/permissions?token=%s", token))
|
2022-12-02 03:39:42 +00:00
|
|
|
resp := MakeRequest(t, req, http.StatusNotFound)
|
2021-10-12 10:47:19 +00:00
|
|
|
|
|
|
|
var apiError api.APIError
|
|
|
|
DecodeJSON(t, resp, &apiError)
|
2022-03-26 09:04:22 +00:00
|
|
|
assert.Equal(t, "user redirect does not exist [name: unknow]", apiError.Message)
|
2021-10-12 10:47:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnknowOrganization(t *testing.T) {
|
2022-09-02 19:18:23 +00:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2021-10-12 10:47:19 +00:00
|
|
|
|
|
|
|
session := loginUser(t, "user1")
|
2023-01-17 21:46:03 +00:00
|
|
|
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadOrg)
|
2021-10-12 10:47:19 +00:00
|
|
|
|
|
|
|
req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/users/user1/orgs/unknow/permissions?token=%s", token))
|
2022-12-02 03:39:42 +00:00
|
|
|
resp := MakeRequest(t, req, http.StatusNotFound)
|
2021-10-12 10:47:19 +00:00
|
|
|
var apiError api.APIError
|
|
|
|
DecodeJSON(t, resp, &apiError)
|
|
|
|
assert.Equal(t, "GetUserByName", apiError.Message)
|
|
|
|
}
|