Skip to content

Commit 76ff5a8

Browse files
committed
improve code
1 parent 53a31d8 commit 76ff5a8

File tree

3 files changed

+68
-176
lines changed

3 files changed

+68
-176
lines changed

integrations/api_helper_for_declarative_test.go

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ func doAPICreateRepository(ctx APITestContext, empty bool, callback ...func(*tes
7676

7777
func doAPIEditRepository(ctx APITestContext, editRepoOption *api.EditRepoOption, callback ...func(*testing.T, api.Repository)) func(*testing.T) {
7878
return func(t *testing.T) {
79-
req := NewRequestWithJSON(t, "PATCH", fmt.Sprintf("/api/v1/repos/%s/%s?token=%s", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame), ctx.Token), editRepoOption)
79+
req := NewRequestWithJSON(t, "PATCH", fmt.Sprintf("/api/v1/repos/%s/%s", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame)), editRepoOption)
8080
if ctx.ExpectedCode != 0 {
81-
MakeRequest(t, req, ctx.ExpectedCode)
81+
ctx.MakeRequest(t, req, ctx.ExpectedCode)
8282
return
8383
}
84-
resp := MakeRequest(t, req, http.StatusOK)
84+
resp := ctx.MakeRequest(t, req, http.StatusOK)
8585

8686
var repository api.Repository
8787
DecodeJSON(t, resp, &repository)
@@ -103,24 +103,24 @@ func doAPIAddCollaborator(ctx APITestContext, username string, mode perm.AccessM
103103
addCollaboratorOption := &api.AddCollaboratorOption{
104104
Permission: &permission,
105105
}
106-
req := NewRequestWithJSON(t, "PUT", fmt.Sprintf("/api/v1/repos/%s/%s/collaborators/%s?token=%s", ctx.Username, ctx.Reponame, username, ctx.Token), addCollaboratorOption)
106+
req := NewRequestWithJSON(t, "PUT", fmt.Sprintf("/api/v1/repos/%s/%s/collaborators/%s", ctx.Username, ctx.Reponame, username), addCollaboratorOption)
107107
if ctx.ExpectedCode != 0 {
108-
MakeRequest(t, req, ctx.ExpectedCode)
108+
ctx.MakeRequest(t, req, ctx.ExpectedCode)
109109
return
110110
}
111-
MakeRequest(t, req, http.StatusNoContent)
111+
ctx.MakeRequest(t, req, http.StatusNoContent)
112112
}
113113
}
114114

115115
func doAPIForkRepository(ctx APITestContext, username string, callback ...func(*testing.T, api.Repository)) func(*testing.T) {
116116
return func(t *testing.T) {
117117
createForkOption := &api.CreateForkOption{}
118-
req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/forks?token=%s", username, ctx.Reponame, ctx.Token), createForkOption)
118+
req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/forks", username, ctx.Reponame), createForkOption)
119119
if ctx.ExpectedCode != 0 {
120-
MakeRequest(t, req, ctx.ExpectedCode)
120+
ctx.MakeRequest(t, req, ctx.ExpectedCode)
121121
return
122122
}
123-
resp := MakeRequest(t, req, http.StatusAccepted)
123+
resp := ctx.MakeRequest(t, req, http.StatusAccepted)
124124
var repository api.Repository
125125
DecodeJSON(t, resp, &repository)
126126
if len(callback) > 0 {
@@ -259,6 +259,41 @@ func doAPIGetPullRequest(ctx APITestContext, owner, repo string, index int64) fu
259259
}
260260
}
261261

262+
func doAPIGetPullRequest(ctx APITestContext, owner, repo string, index int64) func(*testing.T) (api.PullRequest, error) {
263+
return func(t *testing.T) (api.PullRequest, error) {
264+
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d", owner, repo, index)
265+
req := NewRequest(t, http.MethodGet, urlStr)
266+
267+
expected := 200
268+
if ctx.ExpectedCode != 0 {
269+
expected = ctx.ExpectedCode
270+
}
271+
resp := ctx.MakeRequest(t, req, expected)
272+
273+
decoder := json.NewDecoder(resp.Body)
274+
pr := api.PullRequest{}
275+
err := decoder.Decode(&pr)
276+
return pr, err
277+
}
278+
}
279+
280+
func doAPIManuallyMergePullRequest(ctx APITestContext, owner, repo, commitID string, index int64) func(*testing.T) {
281+
return func(t *testing.T) {
282+
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/merge",
283+
owner, repo, index)
284+
req := NewRequestWithJSON(t, http.MethodPost, urlStr, &forms.MergePullRequestForm{
285+
Do: string(models.MergeStyleManuallyMerged),
286+
MergeCommitID: commitID,
287+
})
288+
289+
if ctx.ExpectedCode != 0 {
290+
ctx.MakeRequest(t, req, ctx.ExpectedCode)
291+
return
292+
}
293+
ctx.MakeRequest(t, req, 200)
294+
}
295+
}
296+
262297
func doAPIMergePullRequest(ctx APITestContext, owner, repo string, index int64) func(*testing.T) {
263298
return func(t *testing.T) {
264299
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/merge?token=%s",

integrations/api_ui_helper_for_declarative_test.go

Lines changed: 2 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@ import (
99
"fmt"
1010
"io/ioutil"
1111
"net/http"
12-
"net/url"
1312
"testing"
1413
"time"
1514

1615
"code.gitea.io/gitea/models"
17-
"code.gitea.io/gitea/modules/json"
1816
"code.gitea.io/gitea/modules/queue"
1917
api "code.gitea.io/gitea/modules/structs"
2018
"code.gitea.io/gitea/services/forms"
@@ -44,34 +42,6 @@ func (ctx TestContext) CreateAPITestContext(t *testing.T) APITestContext {
4442
return NewAPITestContext(t, ctx.Username, ctx.Reponame)
4543
}
4644

47-
func doCreateRepository(ctx TestContext, empty bool, callback ...func(*testing.T, api.Repository)) func(*testing.T) {
48-
return func(t *testing.T) {
49-
createRepoOption := &api.CreateRepoOption{
50-
AutoInit: !empty,
51-
Description: "Temporary repo",
52-
Name: ctx.Reponame,
53-
Private: true,
54-
Template: true,
55-
Gitignores: "",
56-
License: "WTFPL",
57-
Readme: "Default",
58-
}
59-
req := NewRequestWithJSON(t, "POST", "/api/v1/user/repos", createRepoOption)
60-
apiCtx := ctx.CreateAPITestContext(t)
61-
if ctx.ExpectedCode != 0 {
62-
apiCtx.MakeRequest(t, req, ctx.ExpectedCode)
63-
return
64-
}
65-
resp := apiCtx.MakeRequest(t, req, http.StatusCreated)
66-
67-
var repository api.Repository
68-
DecodeJSON(t, resp, &repository)
69-
if len(callback) > 0 {
70-
callback[0](t, repository)
71-
}
72-
}
73-
}
74-
7545
func doDeleteRepository(ctx TestContext) func(*testing.T) {
7646
return func(t *testing.T) {
7747
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s", ctx.Username, ctx.Reponame)
@@ -85,86 +55,6 @@ func doDeleteRepository(ctx TestContext) func(*testing.T) {
8555
}
8656
}
8757

88-
func doAddCollaborator(ctx TestContext, username string, mode models.AccessMode) func(*testing.T) {
89-
return func(t *testing.T) {
90-
permission := "read"
91-
92-
if mode == models.AccessModeAdmin {
93-
permission = "admin"
94-
} else if mode > models.AccessModeRead {
95-
permission = "write"
96-
}
97-
addCollaboratorOption := &api.AddCollaboratorOption{
98-
Permission: &permission,
99-
}
100-
apiCtx := ctx.CreateAPITestContext(t)
101-
req := NewRequestWithJSON(t, "PUT", fmt.Sprintf("/api/v1/repos/%s/%s/collaborators/%s", ctx.Username, ctx.Reponame, username), addCollaboratorOption)
102-
if ctx.ExpectedCode != 0 {
103-
apiCtx.MakeRequest(t, req, ctx.ExpectedCode)
104-
return
105-
}
106-
apiCtx.MakeRequest(t, req, http.StatusNoContent)
107-
}
108-
}
109-
110-
func doForkRepository(ctx TestContext, username string, callback ...func(*testing.T, api.Repository)) func(*testing.T) {
111-
return func(t *testing.T) {
112-
createForkOption := &api.CreateForkOption{}
113-
apiCtx := ctx.CreateAPITestContext(t)
114-
req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/forks", username, ctx.Reponame), createForkOption)
115-
if ctx.ExpectedCode != 0 {
116-
apiCtx.MakeRequest(t, req, ctx.ExpectedCode)
117-
return
118-
}
119-
resp := apiCtx.MakeRequest(t, req, http.StatusAccepted)
120-
var repository api.Repository
121-
DecodeJSON(t, resp, &repository)
122-
if len(callback) > 0 {
123-
callback[0](t, repository)
124-
}
125-
}
126-
}
127-
128-
func doEditRepository(ctx TestContext, editRepoOption *api.EditRepoOption, callback ...func(*testing.T, api.Repository)) func(*testing.T) {
129-
return func(t *testing.T) {
130-
apiCtx := ctx.CreateAPITestContext(t)
131-
req := NewRequestWithJSON(t, "PATCH", fmt.Sprintf("/api/v1/repos/%s/%s", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame)), editRepoOption)
132-
if ctx.ExpectedCode != 0 {
133-
apiCtx.MakeRequest(t, req, ctx.ExpectedCode)
134-
return
135-
}
136-
resp := apiCtx.MakeRequest(t, req, http.StatusOK)
137-
138-
var repository api.Repository
139-
DecodeJSON(t, resp, &repository)
140-
if len(callback) > 0 {
141-
callback[0](t, repository)
142-
}
143-
}
144-
}
145-
146-
func doCreatePullRequest(ctx APITestContext, owner, repo, baseBranch, headBranch string) func(*testing.T) (api.PullRequest, error) {
147-
return func(t *testing.T) (api.PullRequest, error) {
148-
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/pulls", owner, repo)
149-
req := NewRequestWithJSON(t, http.MethodPost, urlStr, &api.CreatePullRequestOption{
150-
Head: headBranch,
151-
Base: baseBranch,
152-
Title: fmt.Sprintf("create a pr from %s to %s", headBranch, baseBranch),
153-
})
154-
155-
expected := 201
156-
if ctx.ExpectedCode != 0 {
157-
expected = ctx.ExpectedCode
158-
}
159-
160-
resp := ctx.MakeRequest(t, req, expected)
161-
decoder := json.NewDecoder(resp.Body)
162-
pr := api.PullRequest{}
163-
err := decoder.Decode(&pr)
164-
return pr, err
165-
}
166-
}
167-
16858
func doCreateUserKey(ctx TestContext, keyname, keyFile string, callback ...func(*testing.T, api.PublicKey)) func(*testing.T) {
16959
return func(t *testing.T) {
17060
urlStr := "/api/v1/user/keys"
@@ -189,25 +79,6 @@ func doCreateUserKey(ctx TestContext, keyname, keyFile string, callback ...func(
18979
}
19080
}
19181

192-
func doGetPullRequest(ctx TestContext, owner, repo string, index int64) func(*testing.T) (api.PullRequest, error) {
193-
return func(t *testing.T) (api.PullRequest, error) {
194-
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d", owner, repo, index)
195-
req := NewRequest(t, http.MethodGet, urlStr)
196-
197-
expected := 200
198-
if ctx.ExpectedCode != 0 {
199-
expected = ctx.ExpectedCode
200-
}
201-
apiCtx := ctx.CreateAPITestContext(t)
202-
resp := apiCtx.MakeRequest(t, req, expected)
203-
204-
decoder := json.NewDecoder(resp.Body)
205-
pr := api.PullRequest{}
206-
err := decoder.Decode(&pr)
207-
return pr, err
208-
}
209-
}
210-
21182
func doMergePullRequest(ctx TestContext, owner, repo string, index int64) func(*testing.T) {
21283
return func(t *testing.T) {
21384
urlStr := fmt.Sprintf("/api/ui/repos/%s/%s/pulls/%d/merge",
@@ -217,7 +88,7 @@ func doMergePullRequest(ctx TestContext, owner, repo string, index int64) func(*
21788
Do: string(models.MergeStyleMerge),
21889
})
21990

220-
resp := MakeRequest(t, req, NoExpectedStatus)
91+
resp := ctx.Session.MakeRequest(t, req, NoExpectedStatus)
22192

22293
if resp.Code == http.StatusMethodNotAllowed {
22394
err := api.APIError{}
@@ -228,7 +99,7 @@ func doMergePullRequest(ctx TestContext, owner, repo string, index int64) func(*
22899
MergeMessageField: "doMergePullRequest Merge",
229100
Do: string(models.MergeStyleMerge),
230101
})
231-
resp = MakeRequest(t, req, NoExpectedStatus)
102+
resp = ctx.Session.MakeRequest(t, req, NoExpectedStatus)
232103
}
233104

234105
expected := ctx.ExpectedCode
@@ -242,22 +113,3 @@ func doMergePullRequest(ctx TestContext, owner, repo string, index int64) func(*
242113
}
243114
}
244115
}
245-
246-
func doManuallyMergePullRequest(ctx TestContext, owner, repo, commitID string, index int64) func(*testing.T) {
247-
return func(t *testing.T) {
248-
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/merge",
249-
owner, repo, index)
250-
req := NewRequestWithJSON(t, http.MethodPost, urlStr, &forms.MergePullRequestForm{
251-
Do: string(models.MergeStyleManuallyMerged),
252-
MergeCommitID: commitID,
253-
})
254-
255-
apiCtx := ctx.CreateAPITestContext(t)
256-
257-
if ctx.ExpectedCode != 0 {
258-
apiCtx.MakeRequest(t, req, ctx.ExpectedCode)
259-
return
260-
}
261-
apiCtx.MakeRequest(t, req, 200)
262-
}
263-
}

0 commit comments

Comments
 (0)