-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Add API for get user org permissions #17232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4dd4abe
[API] Get user org permissions
romdum e130057
Add swagger response + generate swagger
romdum ae8aa4a
Merge branch 'main' into feature/api_user_org_perm
6543 2c0feda
Stop execution if user / org is not found
romdum e84241f
Add tests (#17232)
romdum 7e502e2
Merge branch 'main' into feature/api_user_org_perm
romdum e8b8d94
finetuning
6543 bb2752d
Merge branch 'main' into feature/api_user_org_perm
6543 9d78c9c
Merge branch 'main' into feature/api_user_org_perm
romdum 92bc3ed
Merge branch 'main' into feature/api_user_org_perm
6543 4a583f9
Merge branch 'main' into feature/api_user_org_perm
6543 2b3794e
rm noop code
6543 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
// Copyright 2021 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package integrations | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
api "code.gitea.io/gitea/modules/structs" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type apiUserOrgPermTestCase struct { | ||
LoginUser string | ||
User string | ||
Organization string | ||
ExpectedOrganizationPermissions api.OrganizationPermissions | ||
} | ||
|
||
func TestTokenNeeded(t *testing.T) { | ||
defer prepareTestEnv(t)() | ||
|
||
session := emptyTestSession(t) | ||
req := NewRequest(t, "GET", "/api/v1/users/user1/orgs/user6/permissions") | ||
session.MakeRequest(t, req, http.StatusUnauthorized) | ||
} | ||
|
||
func sampleTest(t *testing.T, auoptc apiUserOrgPermTestCase) { | ||
defer prepareTestEnv(t)() | ||
|
||
session := loginUser(t, auoptc.LoginUser) | ||
token := getTokenForLoggedInUser(t, session) | ||
|
||
req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/users/%s/orgs/%s/permissions?token=%s", auoptc.User, auoptc.Organization, token)) | ||
resp := session.MakeRequest(t, req, http.StatusOK) | ||
|
||
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) { | ||
defer prepareTestEnv(t)() | ||
|
||
session := loginUser(t, "user1") | ||
token := getTokenForLoggedInUser(t, session) | ||
|
||
req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/users/unknow/orgs/org25/permissions?token=%s", token)) | ||
resp := session.MakeRequest(t, req, http.StatusNotFound) | ||
|
||
var apiError api.APIError | ||
DecodeJSON(t, resp, &apiError) | ||
assert.Equal(t, "GetUserByName", apiError.Message) | ||
} | ||
|
||
func TestUnknowOrganization(t *testing.T) { | ||
defer prepareTestEnv(t)() | ||
|
||
session := loginUser(t, "user1") | ||
token := getTokenForLoggedInUser(t, session) | ||
|
||
req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/users/user1/orgs/unknow/permissions?token=%s", token)) | ||
resp := session.MakeRequest(t, req, http.StatusNotFound) | ||
var apiError api.APIError | ||
DecodeJSON(t, resp, &apiError) | ||
assert.Equal(t, "GetUserByName", apiError.Message) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.