mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-31 21:28:11 +09:00 
			
		
		
		
	Add permission check when creating PR (#31033)
user should be a collaborator of the base repo to create a PR
This commit is contained in:
		| @@ -12,6 +12,7 @@ import ( | ||||
| 	auth_model "code.gitea.io/gitea/models/auth" | ||||
| 	"code.gitea.io/gitea/models/db" | ||||
| 	issues_model "code.gitea.io/gitea/models/issues" | ||||
| 	"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" | ||||
| @@ -126,6 +127,65 @@ func TestAPICreatePullSuccess(t *testing.T) { | ||||
| 	MakeRequest(t, req, http.StatusUnprocessableEntity) // second request should fail | ||||
| } | ||||
|  | ||||
| func TestAPICreatePullBasePermission(t *testing.T) { | ||||
| 	defer tests.PrepareTestEnv(t)() | ||||
| 	repo10 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 10}) | ||||
| 	// repo10 have code, pulls units. | ||||
| 	repo11 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 11}) | ||||
| 	// repo11 only have code unit but should still create pulls | ||||
| 	owner10 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo10.OwnerID}) | ||||
| 	user4 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4}) | ||||
|  | ||||
| 	session := loginUser(t, user4.Name) | ||||
| 	token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository) | ||||
| 	opts := &api.CreatePullRequestOption{ | ||||
| 		Head:  fmt.Sprintf("%s:master", repo11.OwnerName), | ||||
| 		Base:  "master", | ||||
| 		Title: "create a failure pr", | ||||
| 	} | ||||
| 	req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls", owner10.Name, repo10.Name), &opts).AddTokenAuth(token) | ||||
| 	MakeRequest(t, req, http.StatusForbidden) | ||||
|  | ||||
| 	// add user4 to be a collaborator to base repo | ||||
| 	ctx := NewAPITestContext(t, repo10.OwnerName, repo10.Name, auth_model.AccessTokenScopeWriteRepository) | ||||
| 	t.Run("AddUser4AsCollaborator", doAPIAddCollaborator(ctx, user4.Name, perm.AccessModeRead)) | ||||
|  | ||||
| 	// create again | ||||
| 	req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls", owner10.Name, repo10.Name), &opts).AddTokenAuth(token) | ||||
| 	MakeRequest(t, req, http.StatusCreated) | ||||
| } | ||||
|  | ||||
| func TestAPICreatePullHeadPermission(t *testing.T) { | ||||
| 	defer tests.PrepareTestEnv(t)() | ||||
| 	repo10 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 10}) | ||||
| 	// repo10 have code, pulls units. | ||||
| 	repo11 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 11}) | ||||
| 	// repo11 only have code unit but should still create pulls | ||||
| 	owner10 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo10.OwnerID}) | ||||
| 	user4 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4}) | ||||
|  | ||||
| 	session := loginUser(t, user4.Name) | ||||
| 	token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository) | ||||
| 	opts := &api.CreatePullRequestOption{ | ||||
| 		Head:  fmt.Sprintf("%s:master", repo11.OwnerName), | ||||
| 		Base:  "master", | ||||
| 		Title: "create a failure pr", | ||||
| 	} | ||||
| 	req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls", owner10.Name, repo10.Name), &opts).AddTokenAuth(token) | ||||
| 	MakeRequest(t, req, http.StatusForbidden) | ||||
|  | ||||
| 	// add user4 to be a collaborator to head repo with read permission | ||||
| 	ctx := NewAPITestContext(t, repo11.OwnerName, repo11.Name, auth_model.AccessTokenScopeWriteRepository) | ||||
| 	t.Run("AddUser4AsCollaboratorWithRead", doAPIAddCollaborator(ctx, user4.Name, perm.AccessModeRead)) | ||||
| 	req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls", owner10.Name, repo10.Name), &opts).AddTokenAuth(token) | ||||
| 	MakeRequest(t, req, http.StatusForbidden) | ||||
|  | ||||
| 	// add user4 to be a collaborator to head repo with write permission | ||||
| 	t.Run("AddUser4AsCollaboratorWithWrite", doAPIAddCollaborator(ctx, user4.Name, perm.AccessModeWrite)) | ||||
| 	req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls", owner10.Name, repo10.Name), &opts).AddTokenAuth(token) | ||||
| 	MakeRequest(t, req, http.StatusCreated) | ||||
| } | ||||
|  | ||||
| func TestAPICreatePullSameRepoSuccess(t *testing.T) { | ||||
| 	defer tests.PrepareTestEnv(t)() | ||||
| 	repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user