Skip to content

Commit 5c48b7c

Browse files
committed
improve unit tests
1 parent 4ba6d51 commit 5c48b7c

20 files changed

+93
-135
lines changed

modules/test/context_tests.go

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net/http"
1010
"net/http/httptest"
1111
"net/url"
12+
"strings"
1213
"testing"
1314

1415
access_model "code.gitea.io/gitea/models/perm/access"
@@ -25,17 +26,24 @@ import (
2526
"github.com/stretchr/testify/assert"
2627
)
2728

28-
// MockContext mock context for unit tests
29-
// TODO: move this function to other packages, because it depends on "models" package
30-
func MockContext(t *testing.T, path string) *context.Context {
31-
resp := httptest.NewRecorder()
29+
func mockRequest(t *testing.T, reqPath string) *http.Request {
30+
method, path, found := strings.Cut(reqPath, " ")
31+
if !found {
32+
method = "GET"
33+
path = reqPath
34+
}
3235
requestURL, err := url.Parse(path)
3336
assert.NoError(t, err)
34-
req := &http.Request{
35-
URL: requestURL,
36-
Form: url.Values{},
37-
}
37+
req := &http.Request{Method: method, URL: requestURL, Form: url.Values{}}
3838
req = req.WithContext(middleware.WithContextData(req.Context()))
39+
return req
40+
}
41+
42+
// MockContext mock context for unit tests
43+
// TODO: move this function to other packages, because it depends on "models" package
44+
func MockContext(t *testing.T, reqPath string) (*context.Context, *httptest.ResponseRecorder) {
45+
resp := httptest.NewRecorder()
46+
req := mockRequest(t, reqPath)
3947
base, baseCleanUp := context.NewBaseContext(resp, req)
4048
base.Data = middleware.GetContextData(req.Context())
4149
base.Locale = &translation.MockLocale{}
@@ -48,20 +56,14 @@ func MockContext(t *testing.T, path string) *context.Context {
4856

4957
chiCtx := chi.NewRouteContext()
5058
ctx.Base.AppendContextValue(chi.RouteCtxKey, chiCtx)
51-
return ctx
59+
return ctx, resp
5260
}
5361

5462
// MockAPIContext mock context for unit tests
5563
// TODO: move this function to other packages, because it depends on "models" package
56-
func MockAPIContext(t *testing.T, path string) *context.APIContext {
64+
func MockAPIContext(t *testing.T, reqPath string) (*context.APIContext, *httptest.ResponseRecorder) {
5765
resp := httptest.NewRecorder()
58-
requestURL, err := url.Parse(path)
59-
assert.NoError(t, err)
60-
req := &http.Request{
61-
URL: requestURL,
62-
Form: url.Values{},
63-
}
64-
req = req.WithContext(middleware.WithContextData(req.Context()))
66+
req := mockRequest(t, reqPath)
6567
base, baseCleanUp := context.NewBaseContext(resp, req)
6668
base.Data = middleware.GetContextData(req.Context())
6769
base.Locale = &translation.MockLocale{}
@@ -70,7 +72,7 @@ func MockAPIContext(t *testing.T, path string) *context.APIContext {
7072

7173
chiCtx := chi.NewRouteContext()
7274
ctx.Base.AppendContextValue(chi.RouteCtxKey, chiCtx)
73-
return ctx
75+
return ctx, resp
7476
}
7577

7678
// LoadRepo load a repo into a test context.

routers/api/v1/misc/markup_test.go

Lines changed: 7 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,14 @@ import (
77
go_context "context"
88
"io"
99
"net/http"
10-
"net/http/httptest"
11-
"net/url"
1210
"strings"
1311
"testing"
1412

15-
"code.gitea.io/gitea/modules/context"
1613
"code.gitea.io/gitea/modules/markup"
1714
"code.gitea.io/gitea/modules/setting"
1815
api "code.gitea.io/gitea/modules/structs"
19-
"code.gitea.io/gitea/modules/util"
16+
"code.gitea.io/gitea/modules/test"
2017
"code.gitea.io/gitea/modules/web"
21-
"code.gitea.io/gitea/modules/web/middleware"
2218

2319
"github.com/stretchr/testify/assert"
2420
)
@@ -29,34 +25,16 @@ const (
2925
AppSubURL = AppURL + Repo + "/"
3026
)
3127

32-
func createAPIContext(req *http.Request) (*context.APIContext, *httptest.ResponseRecorder) {
33-
resp := httptest.NewRecorder()
34-
base, baseCleanUp := context.NewBaseContext(resp, req)
35-
base.Data = middleware.ContextData{}
36-
c := &context.APIContext{Base: base}
37-
_ = baseCleanUp // during test, it doesn't need to do clean up. TODO: this can be improved later
38-
39-
return c, resp
40-
}
41-
4228
func testRenderMarkup(t *testing.T, mode, filePath, text, responseBody string, responseCode int) {
4329
setting.AppURL = AppURL
44-
4530
options := api.MarkupOption{
4631
Mode: mode,
47-
Text: "",
32+
Text: text,
4833
Context: Repo,
4934
Wiki: true,
5035
FilePath: filePath,
5136
}
52-
requrl, _ := url.Parse(util.URLJoin(AppURL, "api", "v1", "markup"))
53-
req := &http.Request{
54-
Method: "POST",
55-
URL: requrl,
56-
}
57-
ctx, resp := createAPIContext(req)
58-
59-
options.Text = text
37+
ctx, resp := test.MockAPIContext(t, "POST /api/v1/markup")
6038
web.SetForm(ctx, &options)
6139
Markup(ctx)
6240
assert.Equal(t, responseBody, resp.Body.String())
@@ -66,21 +44,13 @@ func testRenderMarkup(t *testing.T, mode, filePath, text, responseBody string, r
6644

6745
func testRenderMarkdown(t *testing.T, mode, text, responseBody string, responseCode int) {
6846
setting.AppURL = AppURL
69-
7047
options := api.MarkdownOption{
7148
Mode: mode,
72-
Text: "",
49+
Text: text,
7350
Context: Repo,
7451
Wiki: true,
7552
}
76-
requrl, _ := url.Parse(util.URLJoin(AppURL, "api", "v1", "markdown"))
77-
req := &http.Request{
78-
Method: "POST",
79-
URL: requrl,
80-
}
81-
ctx, resp := createAPIContext(req)
82-
83-
options.Text = text
53+
ctx, resp := test.MockAPIContext(t, "POST /api/v1/markdown")
8454
web.SetForm(ctx, &options)
8555
Markdown(ctx)
8656
assert.Equal(t, responseBody, resp.Body.String())
@@ -187,19 +157,12 @@ var simpleCases = []string{
187157

188158
func TestAPI_RenderSimple(t *testing.T) {
189159
setting.AppURL = AppURL
190-
191160
options := api.MarkdownOption{
192161
Mode: "markdown",
193162
Text: "",
194163
Context: Repo,
195164
}
196-
requrl, _ := url.Parse(util.URLJoin(AppURL, "api", "v1", "markdown"))
197-
req := &http.Request{
198-
Method: "POST",
199-
URL: requrl,
200-
}
201-
ctx, resp := createAPIContext(req)
202-
165+
ctx, resp := test.MockAPIContext(t, "POST /api/v1/markdown")
203166
for i := 0; i < len(simpleCases); i += 2 {
204167
options.Text = simpleCases[i]
205168
web.SetForm(ctx, &options)
@@ -211,14 +174,7 @@ func TestAPI_RenderSimple(t *testing.T) {
211174

212175
func TestAPI_RenderRaw(t *testing.T) {
213176
setting.AppURL = AppURL
214-
215-
requrl, _ := url.Parse(util.URLJoin(AppURL, "api", "v1", "markdown"))
216-
req := &http.Request{
217-
Method: "POST",
218-
URL: requrl,
219-
}
220-
ctx, resp := createAPIContext(req)
221-
177+
ctx, resp := test.MockAPIContext(t, "POST /api/v1/markdown")
222178
for i := 0; i < len(simpleCases); i += 2 {
223179
ctx.Req.Body = io.NopCloser(strings.NewReader(simpleCases[i]))
224180
MarkdownRaw(ctx)

routers/api/v1/repo/hook_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
func TestTestHook(t *testing.T) {
1818
unittest.PrepareTestEnv(t)
1919

20-
ctx := test.MockAPIContext(t, "user2/repo1/wiki/_pages")
20+
ctx, _ := test.MockAPIContext(t, "user2/repo1/wiki/_pages")
2121
ctx.SetParams(":id", "1")
2222
test.LoadRepo(t, ctx, 1)
2323
test.LoadRepoCommit(t, ctx)

routers/api/v1/repo/repo_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
func TestRepoEdit(t *testing.T) {
2020
unittest.PrepareTestEnv(t)
2121

22-
ctx := test.MockAPIContext(t, "user2/repo1")
22+
ctx, _ := test.MockAPIContext(t, "user2/repo1")
2323
test.LoadRepo(t, ctx, 1)
2424
test.LoadUser(t, ctx, 2)
2525
ctx.Repo.Owner = ctx.Doer
@@ -65,7 +65,7 @@ func TestRepoEdit(t *testing.T) {
6565
func TestRepoEditNameChange(t *testing.T) {
6666
unittest.PrepareTestEnv(t)
6767

68-
ctx := test.MockAPIContext(t, "user2/repo1")
68+
ctx, _ := test.MockAPIContext(t, "user2/repo1")
6969
test.LoadRepo(t, ctx, 1)
7070
test.LoadUser(t, ctx, 2)
7171
ctx.Repo.Owner = ctx.Doer

routers/web/admin/users_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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, _ := test.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, _ := test.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, _ := test.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, _ := test.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, _ := test.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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515

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

routers/web/repo/editor_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ 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, _ := test.MockContext(t, "user2/repo1")
4545
ctx.SetParams(":id", "1")
4646
test.LoadRepo(t, ctx, 1)
4747
test.LoadRepoCommit(t, ctx)
@@ -56,7 +56,7 @@ 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, _ := test.MockContext(t, "user2/repo1")
6060
ctx.SetParams(":id", "1")
6161
test.LoadRepo(t, ctx, 1)
6262
test.LoadRepoCommit(t, ctx)

routers/web/repo/issue_label_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func int64SliceToCommaSeparated(a []int64) string {
3232
func TestInitializeLabels(t *testing.T) {
3333
unittest.PrepareTestEnv(t)
3434
assert.NoError(t, repository.LoadRepoConfig())
35-
ctx := test.MockContext(t, "user2/repo1/labels/initialize")
35+
ctx, _ := test.MockContext(t, "user2/repo1/labels/initialize")
3636
test.LoadUser(t, ctx, 2)
3737
test.LoadRepo(t, ctx, 2)
3838
web.SetForm(ctx, &forms.InitializeLabelsForm{TemplateName: "Default"})
@@ -57,7 +57,7 @@ func TestRetrieveLabels(t *testing.T) {
5757
{1, "leastissues", []int64{2, 1}},
5858
{2, "", []int64{}},
5959
} {
60-
ctx := test.MockContext(t, "user/repo/issues")
60+
ctx, _ := test.MockContext(t, "user/repo/issues")
6161
test.LoadUser(t, ctx, 2)
6262
test.LoadRepo(t, ctx, testCase.RepoID)
6363
ctx.Req.Form.Set("sort", testCase.Sort)
@@ -75,7 +75,7 @@ func TestRetrieveLabels(t *testing.T) {
7575

7676
func TestNewLabel(t *testing.T) {
7777
unittest.PrepareTestEnv(t)
78-
ctx := test.MockContext(t, "user2/repo1/labels/edit")
78+
ctx, _ := test.MockContext(t, "user2/repo1/labels/edit")
7979
test.LoadUser(t, ctx, 2)
8080
test.LoadRepo(t, ctx, 1)
8181
web.SetForm(ctx, &forms.CreateLabelForm{
@@ -93,7 +93,7 @@ func TestNewLabel(t *testing.T) {
9393

9494
func TestUpdateLabel(t *testing.T) {
9595
unittest.PrepareTestEnv(t)
96-
ctx := test.MockContext(t, "user2/repo1/labels/edit")
96+
ctx, _ := test.MockContext(t, "user2/repo1/labels/edit")
9797
test.LoadUser(t, ctx, 2)
9898
test.LoadRepo(t, ctx, 1)
9999
web.SetForm(ctx, &forms.CreateLabelForm{
@@ -113,7 +113,7 @@ func TestUpdateLabel(t *testing.T) {
113113

114114
func TestDeleteLabel(t *testing.T) {
115115
unittest.PrepareTestEnv(t)
116-
ctx := test.MockContext(t, "user2/repo1/labels/delete")
116+
ctx, _ := test.MockContext(t, "user2/repo1/labels/delete")
117117
test.LoadUser(t, ctx, 2)
118118
test.LoadRepo(t, ctx, 1)
119119
ctx.Req.Form.Set("id", "2")
@@ -126,7 +126,7 @@ func TestDeleteLabel(t *testing.T) {
126126

127127
func TestUpdateIssueLabel_Clear(t *testing.T) {
128128
unittest.PrepareTestEnv(t)
129-
ctx := test.MockContext(t, "user2/repo1/issues/labels")
129+
ctx, _ := test.MockContext(t, "user2/repo1/issues/labels")
130130
test.LoadUser(t, ctx, 2)
131131
test.LoadRepo(t, ctx, 1)
132132
ctx.Req.Form.Set("issue_ids", "1,3")
@@ -151,7 +151,7 @@ func TestUpdateIssueLabel_Toggle(t *testing.T) {
151151
{"toggle", []int64{1, 2}, 2, true},
152152
} {
153153
unittest.PrepareTestEnv(t)
154-
ctx := test.MockContext(t, "user2/repo1/issues/labels")
154+
ctx, _ := test.MockContext(t, "user2/repo1/issues/labels")
155155
test.LoadUser(t, ctx, 2)
156156
test.LoadRepo(t, ctx, 1)
157157
ctx.Req.Form.Set("issue_ids", int64SliceToCommaSeparated(testCase.IssueIDs))

routers/web/repo/projects_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414

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

routers/web/repo/release_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func TestNewReleasePost(t *testing.T) {
4747
} {
4848
unittest.PrepareTestEnv(t)
4949

50-
ctx := test.MockContext(t, "user2/repo1/releases/new")
50+
ctx, _ := test.MockContext(t, "user2/repo1/releases/new")
5151
test.LoadUser(t, ctx, 2)
5252
test.LoadRepo(t, ctx, 1)
5353
test.LoadGitRepo(t, ctx)
@@ -67,7 +67,7 @@ func TestNewReleasePost(t *testing.T) {
6767

6868
func TestNewReleasesList(t *testing.T) {
6969
unittest.PrepareTestEnv(t)
70-
ctx := test.MockContext(t, "user2/repo-release/releases")
70+
ctx, _ := test.MockContext(t, "user2/repo-release/releases")
7171
test.LoadUser(t, ctx, 2)
7272
test.LoadRepo(t, ctx, 57)
7373
test.LoadGitRepo(t, ctx)

0 commit comments

Comments
 (0)