-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Add API to query collaborators permission for a repository #18761
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 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a3f65a4
Add API to query collaborators permission for a repository (#14936)
flozzone 415a237
Fix authorization
flozzone 7202bf7
fix tests
flozzone 86e69ee
improve readability
flozzone e3718f5
Extend user properties is test fixtures with their login name.
flozzone 9053227
extend tests with authorize tests.
flozzone 10cf3d7
remove redundant comment
flozzone 245b12e
Merge branch 'main' into fix-14936
lunny a2b851b
Merge branch 'main' into fix-14936
6543 1ba8b23
Update routers/api/v1/repo/collaborators.go
6543 5de28a6
Merge branch 'main' into fix-14936
6543 adf2ad1
Merge branch 'main' into fix-14936
6543 567bb11
Merge branch 'main' into fix-14936
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,101 @@ | ||
// Copyright 2022 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 ( | ||
"net/http" | ||
"testing" | ||
|
||
"code.gitea.io/gitea/models/perm" | ||
repo_model "code.gitea.io/gitea/models/repo" | ||
"code.gitea.io/gitea/models/unittest" | ||
user_model "code.gitea.io/gitea/models/user" | ||
api "code.gitea.io/gitea/modules/structs" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestAPIRepoCollaboratorPermission(t *testing.T) { | ||
defer prepareTestEnv(t)() | ||
user4 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4}).(*user_model.User) | ||
user20 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 20}).(*user_model.User) | ||
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository) | ||
repo1Owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo1.OwnerID}).(*user_model.User) | ||
|
||
// Login as User2. | ||
session := loginUser(t, repo1Owner.Name) | ||
testCtx := NewAPITestContext(t, repo1Owner.Name, repo1.Name) | ||
|
||
t.Run("RepoOwnerShouldBeAdmin", func(t *testing.T) { | ||
req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/collaborators/%s/permission?token=%s", repo1Owner.Name, repo1.Name, repo1Owner.Name, testCtx.Token) | ||
resp := session.MakeRequest(t, req, http.StatusOK) | ||
|
||
var repoPermission api.RepoCollaboratorPermission | ||
DecodeJSON(t, resp, &repoPermission) | ||
|
||
assert.Equal(t, "owner", repoPermission.Permission) | ||
}) | ||
|
||
t.Run("CollaboratorWithReadAccess", func(t *testing.T) { | ||
t.Run("AddUserAsCollaboratorWithReadAccess", doAPIAddCollaborator(testCtx, user4.Name, perm.AccessModeRead)) | ||
|
||
req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/collaborators/%s/permission?token=%s", repo1Owner.Name, repo1.Name, user4.Name, testCtx.Token) | ||
resp := session.MakeRequest(t, req, http.StatusOK) | ||
|
||
var repoPermission api.RepoCollaboratorPermission | ||
DecodeJSON(t, resp, &repoPermission) | ||
|
||
assert.Equal(t, "read", repoPermission.Permission) | ||
}) | ||
|
||
t.Run("CollaboratorWithWriteAccess", func(t *testing.T) { | ||
t.Run("AddUserAsCollaboratorWithWriteAccess", doAPIAddCollaborator(testCtx, user4.Name, perm.AccessModeWrite)) | ||
|
||
req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/collaborators/%s/permission?token=%s", repo1Owner.Name, repo1.Name, user4.Name, testCtx.Token) | ||
resp := session.MakeRequest(t, req, http.StatusOK) | ||
|
||
var repoPermission api.RepoCollaboratorPermission | ||
DecodeJSON(t, resp, &repoPermission) | ||
|
||
assert.Equal(t, "write", repoPermission.Permission) | ||
}) | ||
|
||
t.Run("CollaboratorWithAdminAccess", func(t *testing.T) { | ||
t.Run("AddUserAsCollaboratorWithAdminAccess", doAPIAddCollaborator(testCtx, user4.Name, perm.AccessModeAdmin)) | ||
|
||
req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/collaborators/%s/permission?token=%s", repo1Owner.Name, repo1.Name, user4.Name, testCtx.Token) | ||
resp := session.MakeRequest(t, req, http.StatusOK) | ||
|
||
var repoPermission api.RepoCollaboratorPermission | ||
DecodeJSON(t, resp, &repoPermission) | ||
|
||
assert.Equal(t, "admin", repoPermission.Permission) | ||
}) | ||
|
||
t.Run("WhyHasUser20ReadAccess", func(t *testing.T) { | ||
req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/collaborators/%s/permission?token=%s", repo1Owner.Name, repo1.Name, user20.Name, testCtx.Token) | ||
resp := session.MakeRequest(t, req, http.StatusOK) | ||
|
||
var repoPermission api.RepoCollaboratorPermission | ||
DecodeJSON(t, resp, &repoPermission) | ||
|
||
assert.Equal(t, "read", repoPermission.Permission) | ||
}) | ||
|
||
t.Run("WhyHasUser5ReadAccess", func(t *testing.T) { | ||
req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/collaborators/%s/permission?token=%s", repo1Owner.Name, repo1.Name, "user5", testCtx.Token) | ||
resp := session.MakeRequest(t, req, http.StatusOK) | ||
|
||
var repoPermission api.RepoCollaboratorPermission | ||
DecodeJSON(t, resp, &repoPermission) | ||
|
||
assert.Equal(t, "read", repoPermission.Permission) | ||
}) | ||
|
||
t.Run("CollaboratorNotFound", func(t *testing.T) { | ||
req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/collaborators/%s/permission?token=%s", repo1Owner.Name, repo1.Name, "non-existent-user", testCtx.Token) | ||
session.MakeRequest(t, req, http.StatusNotFound) | ||
}) | ||
} |
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.