Skip to content

Commit

Permalink
improve test
Browse files Browse the repository at this point in the history
  • Loading branch information
yp05327 committed May 21, 2024
1 parent 8b28a6d commit 3bf5a76
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
6 changes: 6 additions & 0 deletions models/fixtures/collaboration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,9 @@
repo_id: 32
user_id: 10
mode: 2 # write

-
id: 12
repo_id: 10
user_id: 13
mode: 2 # write
10 changes: 6 additions & 4 deletions services/pull/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ func NewPullRequest(ctx context.Context, repo *repo_model.Repository, issue *iss
}

// user should be a collaborator or a member of the organization
if canCreate, err := repo_model.IsOwnerMemberCollaborator(ctx, repo, issue.Poster.ID); err != nil {
return err
} else if !canCreate {
return issues_model.ErrMustCollaborator
if !issue.Poster.IsAdmin {
if canCreate, err := repo_model.IsOwnerMemberCollaborator(ctx, repo, issue.Poster.ID); err != nil {
return err
} else if !canCreate {
return issues_model.ErrMustCollaborator
}
}

prCtx, cancel, err := createTemporaryRepoForPR(ctx, pr)
Expand Down
29 changes: 20 additions & 9 deletions tests/integration/api_pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,21 +123,32 @@ func TestAPICreatePullSuccess(t *testing.T) {
Base: "master",
Title: "create a failure pr",
}).AddTokenAuth(token)
// user should be a collaborator
MakeRequest(t, req, http.StatusCreated)
MakeRequest(t, req, http.StatusUnprocessableEntity) // second request should fail
}

func TestAPICreatePullCollaboratorSuccess(t *testing.T) {
defer tests.PrepareTestEnv(t)()
repo10 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 10})
owner10 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo10.OwnerID})

session := loginUser(t, "user2")
opts := &api.CreatePullRequestOption{
Head: "user13:master",
Base: "master",
Title: "create a collaborator pr",
}
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
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 owner11 to be a collaborator
// add user2 to be a collaborator
testCtx := NewAPITestContext(t, repo10.OwnerName, repo10.Name, auth_model.AccessTokenScopeWriteRepository)
t.Run("AddOwner11AsCollaborator", doAPIAddCollaborator(testCtx, owner11.Name, perm.AccessModeRead))
t.Run("AddUser2AsCollaborator", doAPIAddCollaborator(testCtx, "user2", perm.AccessModeRead))

// create again
req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls", owner10.Name, repo10.Name), &api.CreatePullRequestOption{
Head: fmt.Sprintf("%s:master", owner11.Name),
Base: "master",
Title: "create a failure pr",
}).AddTokenAuth(token)
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)
MakeRequest(t, req, http.StatusUnprocessableEntity) // second request should fail
}

func TestAPICreatePullSameRepoSuccess(t *testing.T) {
Expand Down

0 comments on commit 3bf5a76

Please sign in to comment.