Skip to content

Commit e8aae43

Browse files
authored
Move web/api context related testing function into a separate package (#26859)
Just like `models/unittest`, the testing helper functions should be in a separate package: `contexttest` And complete the TODO: > // TODO: move this function to other packages, because it depends on "models" package
1 parent fcb4941 commit e8aae43

21 files changed

+244
-244
lines changed

modules/test/context_tests.go renamed to modules/contexttest/context_tests.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// Copyright 2017 The Gitea Authors. All rights reserved.
22
// SPDX-License-Identifier: MIT
33

4-
package test
4+
// Package contexttest provides utilities for testing Web/API contexts with models.
5+
package contexttest
56

67
import (
78
gocontext "context"
@@ -22,7 +23,7 @@ import (
2223
"code.gitea.io/gitea/modules/translation"
2324
"code.gitea.io/gitea/modules/web/middleware"
2425

25-
chi "github.com/go-chi/chi/v5"
26+
"github.com/go-chi/chi/v5"
2627
"github.com/stretchr/testify/assert"
2728
)
2829

@@ -40,7 +41,6 @@ func mockRequest(t *testing.T, reqPath string) *http.Request {
4041
}
4142

4243
// MockContext mock context for unit tests
43-
// TODO: move this function to other packages, because it depends on "models" package
4444
func MockContext(t *testing.T, reqPath string) (*context.Context, *httptest.ResponseRecorder) {
4545
resp := httptest.NewRecorder()
4646
req := mockRequest(t, reqPath)
@@ -58,7 +58,6 @@ func MockContext(t *testing.T, reqPath string) (*context.Context, *httptest.Resp
5858
}
5959

6060
// MockAPIContext mock context for unit tests
61-
// TODO: move this function to other packages, because it depends on "models" package
6261
func MockAPIContext(t *testing.T, reqPath string) (*context.APIContext, *httptest.ResponseRecorder) {
6362
resp := httptest.NewRecorder()
6463
req := mockRequest(t, reqPath)
@@ -123,7 +122,7 @@ func LoadRepoCommit(t *testing.T, ctx gocontext.Context) {
123122
}
124123
}
125124

126-
// LoadUser load a user into a test context.
125+
// LoadUser load a user into a test context
127126
func LoadUser(t *testing.T, ctx gocontext.Context, userID int64) {
128127
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: userID})
129128
switch ctx := ctx.(type) {

routers/api/v1/misc/markup_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import (
1010
"strings"
1111
"testing"
1212

13+
"code.gitea.io/gitea/modules/contexttest"
1314
"code.gitea.io/gitea/modules/markup"
1415
"code.gitea.io/gitea/modules/setting"
1516
api "code.gitea.io/gitea/modules/structs"
16-
"code.gitea.io/gitea/modules/test"
1717
"code.gitea.io/gitea/modules/web"
1818

1919
"github.com/stretchr/testify/assert"
@@ -34,7 +34,7 @@ func testRenderMarkup(t *testing.T, mode, filePath, text, responseBody string, r
3434
Wiki: true,
3535
FilePath: filePath,
3636
}
37-
ctx, resp := test.MockAPIContext(t, "POST /api/v1/markup")
37+
ctx, resp := contexttest.MockAPIContext(t, "POST /api/v1/markup")
3838
web.SetForm(ctx, &options)
3939
Markup(ctx)
4040
assert.Equal(t, responseBody, resp.Body.String())
@@ -50,7 +50,7 @@ func testRenderMarkdown(t *testing.T, mode, text, responseBody string, responseC
5050
Context: Repo,
5151
Wiki: true,
5252
}
53-
ctx, resp := test.MockAPIContext(t, "POST /api/v1/markdown")
53+
ctx, resp := contexttest.MockAPIContext(t, "POST /api/v1/markdown")
5454
web.SetForm(ctx, &options)
5555
Markdown(ctx)
5656
assert.Equal(t, responseBody, resp.Body.String())
@@ -162,7 +162,7 @@ func TestAPI_RenderSimple(t *testing.T) {
162162
Text: "",
163163
Context: Repo,
164164
}
165-
ctx, resp := test.MockAPIContext(t, "POST /api/v1/markdown")
165+
ctx, resp := contexttest.MockAPIContext(t, "POST /api/v1/markdown")
166166
for i := 0; i < len(simpleCases); i += 2 {
167167
options.Text = simpleCases[i]
168168
web.SetForm(ctx, &options)
@@ -174,7 +174,7 @@ func TestAPI_RenderSimple(t *testing.T) {
174174

175175
func TestAPI_RenderRaw(t *testing.T) {
176176
setting.AppURL = AppURL
177-
ctx, resp := test.MockAPIContext(t, "POST /api/v1/markdown")
177+
ctx, resp := contexttest.MockAPIContext(t, "POST /api/v1/markdown")
178178
for i := 0; i < len(simpleCases); i += 2 {
179179
ctx.Req.Body = io.NopCloser(strings.NewReader(simpleCases[i]))
180180
MarkdownRaw(ctx)

routers/api/v1/repo/hook_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@ import (
99

1010
"code.gitea.io/gitea/models/unittest"
1111
"code.gitea.io/gitea/models/webhook"
12-
"code.gitea.io/gitea/modules/test"
12+
"code.gitea.io/gitea/modules/contexttest"
1313

1414
"github.com/stretchr/testify/assert"
1515
)
1616

1717
func TestTestHook(t *testing.T) {
1818
unittest.PrepareTestEnv(t)
1919

20-
ctx, _ := test.MockAPIContext(t, "user2/repo1/wiki/_pages")
20+
ctx, _ := contexttest.MockAPIContext(t, "user2/repo1/wiki/_pages")
2121
ctx.SetParams(":id", "1")
22-
test.LoadRepo(t, ctx, 1)
23-
test.LoadRepoCommit(t, ctx)
24-
test.LoadUser(t, ctx, 2)
22+
contexttest.LoadRepo(t, ctx, 1)
23+
contexttest.LoadRepoCommit(t, ctx)
24+
contexttest.LoadUser(t, ctx, 2)
2525
TestHook(ctx)
2626
assert.EqualValues(t, http.StatusNoContent, ctx.Resp.Status())
2727

routers/api/v1/repo/repo_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99

1010
repo_model "code.gitea.io/gitea/models/repo"
1111
"code.gitea.io/gitea/models/unittest"
12+
"code.gitea.io/gitea/modules/contexttest"
1213
api "code.gitea.io/gitea/modules/structs"
13-
"code.gitea.io/gitea/modules/test"
1414
"code.gitea.io/gitea/modules/web"
1515

1616
"github.com/stretchr/testify/assert"
@@ -19,9 +19,9 @@ import (
1919
func TestRepoEdit(t *testing.T) {
2020
unittest.PrepareTestEnv(t)
2121

22-
ctx, _ := test.MockAPIContext(t, "user2/repo1")
23-
test.LoadRepo(t, ctx, 1)
24-
test.LoadUser(t, ctx, 2)
22+
ctx, _ := contexttest.MockAPIContext(t, "user2/repo1")
23+
contexttest.LoadRepo(t, ctx, 1)
24+
contexttest.LoadUser(t, ctx, 2)
2525
ctx.Repo.Owner = ctx.Doer
2626
description := "new description"
2727
website := "http://wwww.newwebsite.com"
@@ -65,9 +65,9 @@ func TestRepoEdit(t *testing.T) {
6565
func TestRepoEditNameChange(t *testing.T) {
6666
unittest.PrepareTestEnv(t)
6767

68-
ctx, _ := test.MockAPIContext(t, "user2/repo1")
69-
test.LoadRepo(t, ctx, 1)
70-
test.LoadUser(t, ctx, 2)
68+
ctx, _ := contexttest.MockAPIContext(t, "user2/repo1")
69+
contexttest.LoadRepo(t, ctx, 1)
70+
contexttest.LoadUser(t, ctx, 2)
7171
ctx.Repo.Owner = ctx.Doer
7272
name := "newname"
7373
opts := api.EditRepoOption{

routers/web/admin/users_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import (
88

99
"code.gitea.io/gitea/models/unittest"
1010
user_model "code.gitea.io/gitea/models/user"
11+
"code.gitea.io/gitea/modules/contexttest"
1112
"code.gitea.io/gitea/modules/setting"
1213
api "code.gitea.io/gitea/modules/structs"
13-
"code.gitea.io/gitea/modules/test"
1414
"code.gitea.io/gitea/modules/web"
1515
"code.gitea.io/gitea/services/forms"
1616

@@ -19,7 +19,7 @@ import (
1919

2020
func TestNewUserPost_MustChangePassword(t *testing.T) {
2121
unittest.PrepareTestEnv(t)
22-
ctx, _ := test.MockContext(t, "admin/users/new")
22+
ctx, _ := contexttest.MockContext(t, "admin/users/new")
2323

2424
u := unittest.AssertExistsAndLoadBean(t, &user_model.User{
2525
IsAdmin: true,
@@ -56,7 +56,7 @@ func TestNewUserPost_MustChangePassword(t *testing.T) {
5656

5757
func TestNewUserPost_MustChangePasswordFalse(t *testing.T) {
5858
unittest.PrepareTestEnv(t)
59-
ctx, _ := test.MockContext(t, "admin/users/new")
59+
ctx, _ := contexttest.MockContext(t, "admin/users/new")
6060

6161
u := unittest.AssertExistsAndLoadBean(t, &user_model.User{
6262
IsAdmin: true,
@@ -93,7 +93,7 @@ func TestNewUserPost_MustChangePasswordFalse(t *testing.T) {
9393

9494
func TestNewUserPost_InvalidEmail(t *testing.T) {
9595
unittest.PrepareTestEnv(t)
96-
ctx, _ := test.MockContext(t, "admin/users/new")
96+
ctx, _ := contexttest.MockContext(t, "admin/users/new")
9797

9898
u := unittest.AssertExistsAndLoadBean(t, &user_model.User{
9999
IsAdmin: true,
@@ -123,7 +123,7 @@ func TestNewUserPost_InvalidEmail(t *testing.T) {
123123

124124
func TestNewUserPost_VisibilityDefaultPublic(t *testing.T) {
125125
unittest.PrepareTestEnv(t)
126-
ctx, _ := test.MockContext(t, "admin/users/new")
126+
ctx, _ := contexttest.MockContext(t, "admin/users/new")
127127

128128
u := unittest.AssertExistsAndLoadBean(t, &user_model.User{
129129
IsAdmin: true,
@@ -161,7 +161,7 @@ func TestNewUserPost_VisibilityDefaultPublic(t *testing.T) {
161161

162162
func TestNewUserPost_VisibilityPrivate(t *testing.T) {
163163
unittest.PrepareTestEnv(t)
164-
ctx, _ := test.MockContext(t, "admin/users/new")
164+
ctx, _ := contexttest.MockContext(t, "admin/users/new")
165165

166166
u := unittest.AssertExistsAndLoadBean(t, &user_model.User{
167167
IsAdmin: true,

routers/web/org/projects_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ import (
77
"testing"
88

99
"code.gitea.io/gitea/models/unittest"
10-
"code.gitea.io/gitea/modules/test"
10+
"code.gitea.io/gitea/modules/contexttest"
1111
"code.gitea.io/gitea/routers/web/org"
1212

1313
"github.com/stretchr/testify/assert"
1414
)
1515

1616
func TestCheckProjectBoardChangePermissions(t *testing.T) {
1717
unittest.PrepareTestEnv(t)
18-
ctx, _ := test.MockContext(t, "user2/-/projects/4/4")
19-
test.LoadUser(t, ctx, 2)
18+
ctx, _ := contexttest.MockContext(t, "user2/-/projects/4/4")
19+
contexttest.LoadUser(t, ctx, 2)
2020
ctx.ContextUser = ctx.Doer // user2
2121
ctx.SetParams(":id", "4")
2222
ctx.SetParams(":boardID", "4")

routers/web/repo/editor_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"testing"
88

99
"code.gitea.io/gitea/models/unittest"
10+
"code.gitea.io/gitea/modules/contexttest"
1011
"code.gitea.io/gitea/modules/git"
11-
"code.gitea.io/gitea/modules/test"
1212

1313
"github.com/stretchr/testify/assert"
1414
)
@@ -41,12 +41,12 @@ func TestCleanUploadName(t *testing.T) {
4141

4242
func TestGetUniquePatchBranchName(t *testing.T) {
4343
unittest.PrepareTestEnv(t)
44-
ctx, _ := test.MockContext(t, "user2/repo1")
44+
ctx, _ := contexttest.MockContext(t, "user2/repo1")
4545
ctx.SetParams(":id", "1")
46-
test.LoadRepo(t, ctx, 1)
47-
test.LoadRepoCommit(t, ctx)
48-
test.LoadUser(t, ctx, 2)
49-
test.LoadGitRepo(t, ctx)
46+
contexttest.LoadRepo(t, ctx, 1)
47+
contexttest.LoadRepoCommit(t, ctx)
48+
contexttest.LoadUser(t, ctx, 2)
49+
contexttest.LoadGitRepo(t, ctx)
5050
defer ctx.Repo.GitRepo.Close()
5151

5252
expectedBranchName := "user2-patch-1"
@@ -56,12 +56,12 @@ func TestGetUniquePatchBranchName(t *testing.T) {
5656

5757
func TestGetClosestParentWithFiles(t *testing.T) {
5858
unittest.PrepareTestEnv(t)
59-
ctx, _ := test.MockContext(t, "user2/repo1")
59+
ctx, _ := contexttest.MockContext(t, "user2/repo1")
6060
ctx.SetParams(":id", "1")
61-
test.LoadRepo(t, ctx, 1)
62-
test.LoadRepoCommit(t, ctx)
63-
test.LoadUser(t, ctx, 2)
64-
test.LoadGitRepo(t, ctx)
61+
contexttest.LoadRepo(t, ctx, 1)
62+
contexttest.LoadRepoCommit(t, ctx)
63+
contexttest.LoadUser(t, ctx, 2)
64+
contexttest.LoadGitRepo(t, ctx)
6565
defer ctx.Repo.GitRepo.Close()
6666

6767
repo := ctx.Repo.Repository

routers/web/repo/issue_label_test.go

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
issues_model "code.gitea.io/gitea/models/issues"
1212
"code.gitea.io/gitea/models/unittest"
13+
"code.gitea.io/gitea/modules/contexttest"
1314
"code.gitea.io/gitea/modules/repository"
1415
"code.gitea.io/gitea/modules/test"
1516
"code.gitea.io/gitea/modules/web"
@@ -32,9 +33,9 @@ func int64SliceToCommaSeparated(a []int64) string {
3233
func TestInitializeLabels(t *testing.T) {
3334
unittest.PrepareTestEnv(t)
3435
assert.NoError(t, repository.LoadRepoConfig())
35-
ctx, _ := test.MockContext(t, "user2/repo1/labels/initialize")
36-
test.LoadUser(t, ctx, 2)
37-
test.LoadRepo(t, ctx, 2)
36+
ctx, _ := contexttest.MockContext(t, "user2/repo1/labels/initialize")
37+
contexttest.LoadUser(t, ctx, 2)
38+
contexttest.LoadRepo(t, ctx, 2)
3839
web.SetForm(ctx, &forms.InitializeLabelsForm{TemplateName: "Default"})
3940
InitializeLabels(ctx)
4041
assert.EqualValues(t, http.StatusSeeOther, ctx.Resp.Status())
@@ -57,9 +58,9 @@ func TestRetrieveLabels(t *testing.T) {
5758
{1, "leastissues", []int64{2, 1}},
5859
{2, "", []int64{}},
5960
} {
60-
ctx, _ := test.MockContext(t, "user/repo/issues")
61-
test.LoadUser(t, ctx, 2)
62-
test.LoadRepo(t, ctx, testCase.RepoID)
61+
ctx, _ := contexttest.MockContext(t, "user/repo/issues")
62+
contexttest.LoadUser(t, ctx, 2)
63+
contexttest.LoadRepo(t, ctx, testCase.RepoID)
6364
ctx.Req.Form.Set("sort", testCase.Sort)
6465
RetrieveLabels(ctx)
6566
assert.False(t, ctx.Written())
@@ -75,9 +76,9 @@ func TestRetrieveLabels(t *testing.T) {
7576

7677
func TestNewLabel(t *testing.T) {
7778
unittest.PrepareTestEnv(t)
78-
ctx, _ := test.MockContext(t, "user2/repo1/labels/edit")
79-
test.LoadUser(t, ctx, 2)
80-
test.LoadRepo(t, ctx, 1)
79+
ctx, _ := contexttest.MockContext(t, "user2/repo1/labels/edit")
80+
contexttest.LoadUser(t, ctx, 2)
81+
contexttest.LoadRepo(t, ctx, 1)
8182
web.SetForm(ctx, &forms.CreateLabelForm{
8283
Title: "newlabel",
8384
Color: "#abcdef",
@@ -93,9 +94,9 @@ func TestNewLabel(t *testing.T) {
9394

9495
func TestUpdateLabel(t *testing.T) {
9596
unittest.PrepareTestEnv(t)
96-
ctx, _ := test.MockContext(t, "user2/repo1/labels/edit")
97-
test.LoadUser(t, ctx, 2)
98-
test.LoadRepo(t, ctx, 1)
97+
ctx, _ := contexttest.MockContext(t, "user2/repo1/labels/edit")
98+
contexttest.LoadUser(t, ctx, 2)
99+
contexttest.LoadRepo(t, ctx, 1)
99100
web.SetForm(ctx, &forms.CreateLabelForm{
100101
ID: 2,
101102
Title: "newnameforlabel",
@@ -114,9 +115,9 @@ func TestUpdateLabel(t *testing.T) {
114115

115116
func TestDeleteLabel(t *testing.T) {
116117
unittest.PrepareTestEnv(t)
117-
ctx, _ := test.MockContext(t, "user2/repo1/labels/delete")
118-
test.LoadUser(t, ctx, 2)
119-
test.LoadRepo(t, ctx, 1)
118+
ctx, _ := contexttest.MockContext(t, "user2/repo1/labels/delete")
119+
contexttest.LoadUser(t, ctx, 2)
120+
contexttest.LoadRepo(t, ctx, 1)
120121
ctx.Req.Form.Set("id", "2")
121122
DeleteLabel(ctx)
122123
assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
@@ -127,9 +128,9 @@ func TestDeleteLabel(t *testing.T) {
127128

128129
func TestUpdateIssueLabel_Clear(t *testing.T) {
129130
unittest.PrepareTestEnv(t)
130-
ctx, _ := test.MockContext(t, "user2/repo1/issues/labels")
131-
test.LoadUser(t, ctx, 2)
132-
test.LoadRepo(t, ctx, 1)
131+
ctx, _ := contexttest.MockContext(t, "user2/repo1/issues/labels")
132+
contexttest.LoadUser(t, ctx, 2)
133+
contexttest.LoadRepo(t, ctx, 1)
133134
ctx.Req.Form.Set("issue_ids", "1,3")
134135
ctx.Req.Form.Set("action", "clear")
135136
UpdateIssueLabel(ctx)
@@ -152,9 +153,9 @@ func TestUpdateIssueLabel_Toggle(t *testing.T) {
152153
{"toggle", []int64{1, 2}, 2, true},
153154
} {
154155
unittest.PrepareTestEnv(t)
155-
ctx, _ := test.MockContext(t, "user2/repo1/issues/labels")
156-
test.LoadUser(t, ctx, 2)
157-
test.LoadRepo(t, ctx, 1)
156+
ctx, _ := contexttest.MockContext(t, "user2/repo1/issues/labels")
157+
contexttest.LoadUser(t, ctx, 2)
158+
contexttest.LoadRepo(t, ctx, 1)
158159
ctx.Req.Form.Set("issue_ids", int64SliceToCommaSeparated(testCase.IssueIDs))
159160
ctx.Req.Form.Set("action", testCase.Action)
160161
ctx.Req.Form.Set("id", strconv.Itoa(int(testCase.LabelID)))

routers/web/repo/projects_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ import (
77
"testing"
88

99
"code.gitea.io/gitea/models/unittest"
10-
"code.gitea.io/gitea/modules/test"
10+
"code.gitea.io/gitea/modules/contexttest"
1111

1212
"github.com/stretchr/testify/assert"
1313
)
1414

1515
func TestCheckProjectBoardChangePermissions(t *testing.T) {
1616
unittest.PrepareTestEnv(t)
17-
ctx, _ := test.MockContext(t, "user2/repo1/projects/1/2")
18-
test.LoadUser(t, ctx, 2)
19-
test.LoadRepo(t, ctx, 1)
17+
ctx, _ := contexttest.MockContext(t, "user2/repo1/projects/1/2")
18+
contexttest.LoadUser(t, ctx, 2)
19+
contexttest.LoadRepo(t, ctx, 1)
2020
ctx.SetParams(":id", "1")
2121
ctx.SetParams(":boardID", "2")
2222

0 commit comments

Comments
 (0)