Skip to content

Commit

Permalink
Move web/api context related testing function into a separate package (
Browse files Browse the repository at this point in the history
…#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
  • Loading branch information
wxiaoguang authored Sep 1, 2023
1 parent fcb4941 commit e8aae43
Show file tree
Hide file tree
Showing 21 changed files with 244 additions and 244 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Copyright 2017 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package test
// Package contexttest provides utilities for testing Web/API contexts with models.
package contexttest

import (
gocontext "context"
Expand All @@ -22,7 +23,7 @@ import (
"code.gitea.io/gitea/modules/translation"
"code.gitea.io/gitea/modules/web/middleware"

chi "github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5"
"github.com/stretchr/testify/assert"
)

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

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

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

// LoadUser load a user into a test context.
// LoadUser load a user into a test context
func LoadUser(t *testing.T, ctx gocontext.Context, userID int64) {
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: userID})
switch ctx := ctx.(type) {
Expand Down
10 changes: 5 additions & 5 deletions routers/api/v1/misc/markup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
"strings"
"testing"

"code.gitea.io/gitea/modules/contexttest"
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/test"
"code.gitea.io/gitea/modules/web"

"github.com/stretchr/testify/assert"
Expand All @@ -34,7 +34,7 @@ func testRenderMarkup(t *testing.T, mode, filePath, text, responseBody string, r
Wiki: true,
FilePath: filePath,
}
ctx, resp := test.MockAPIContext(t, "POST /api/v1/markup")
ctx, resp := contexttest.MockAPIContext(t, "POST /api/v1/markup")
web.SetForm(ctx, &options)
Markup(ctx)
assert.Equal(t, responseBody, resp.Body.String())
Expand All @@ -50,7 +50,7 @@ func testRenderMarkdown(t *testing.T, mode, text, responseBody string, responseC
Context: Repo,
Wiki: true,
}
ctx, resp := test.MockAPIContext(t, "POST /api/v1/markdown")
ctx, resp := contexttest.MockAPIContext(t, "POST /api/v1/markdown")
web.SetForm(ctx, &options)
Markdown(ctx)
assert.Equal(t, responseBody, resp.Body.String())
Expand Down Expand Up @@ -162,7 +162,7 @@ func TestAPI_RenderSimple(t *testing.T) {
Text: "",
Context: Repo,
}
ctx, resp := test.MockAPIContext(t, "POST /api/v1/markdown")
ctx, resp := contexttest.MockAPIContext(t, "POST /api/v1/markdown")
for i := 0; i < len(simpleCases); i += 2 {
options.Text = simpleCases[i]
web.SetForm(ctx, &options)
Expand All @@ -174,7 +174,7 @@ func TestAPI_RenderSimple(t *testing.T) {

func TestAPI_RenderRaw(t *testing.T) {
setting.AppURL = AppURL
ctx, resp := test.MockAPIContext(t, "POST /api/v1/markdown")
ctx, resp := contexttest.MockAPIContext(t, "POST /api/v1/markdown")
for i := 0; i < len(simpleCases); i += 2 {
ctx.Req.Body = io.NopCloser(strings.NewReader(simpleCases[i]))
MarkdownRaw(ctx)
Expand Down
10 changes: 5 additions & 5 deletions routers/api/v1/repo/hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ import (

"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/models/webhook"
"code.gitea.io/gitea/modules/test"
"code.gitea.io/gitea/modules/contexttest"

"github.com/stretchr/testify/assert"
)

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

ctx, _ := test.MockAPIContext(t, "user2/repo1/wiki/_pages")
ctx, _ := contexttest.MockAPIContext(t, "user2/repo1/wiki/_pages")
ctx.SetParams(":id", "1")
test.LoadRepo(t, ctx, 1)
test.LoadRepoCommit(t, ctx)
test.LoadUser(t, ctx, 2)
contexttest.LoadRepo(t, ctx, 1)
contexttest.LoadRepoCommit(t, ctx)
contexttest.LoadUser(t, ctx, 2)
TestHook(ctx)
assert.EqualValues(t, http.StatusNoContent, ctx.Resp.Status())

Expand Down
14 changes: 7 additions & 7 deletions routers/api/v1/repo/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/contexttest"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/test"
"code.gitea.io/gitea/modules/web"

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

ctx, _ := test.MockAPIContext(t, "user2/repo1")
test.LoadRepo(t, ctx, 1)
test.LoadUser(t, ctx, 2)
ctx, _ := contexttest.MockAPIContext(t, "user2/repo1")
contexttest.LoadRepo(t, ctx, 1)
contexttest.LoadUser(t, ctx, 2)
ctx.Repo.Owner = ctx.Doer
description := "new description"
website := "http://wwww.newwebsite.com"
Expand Down Expand Up @@ -65,9 +65,9 @@ func TestRepoEdit(t *testing.T) {
func TestRepoEditNameChange(t *testing.T) {
unittest.PrepareTestEnv(t)

ctx, _ := test.MockAPIContext(t, "user2/repo1")
test.LoadRepo(t, ctx, 1)
test.LoadUser(t, ctx, 2)
ctx, _ := contexttest.MockAPIContext(t, "user2/repo1")
contexttest.LoadRepo(t, ctx, 1)
contexttest.LoadUser(t, ctx, 2)
ctx.Repo.Owner = ctx.Doer
name := "newname"
opts := api.EditRepoOption{
Expand Down
12 changes: 6 additions & 6 deletions routers/web/admin/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (

"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/contexttest"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/test"
"code.gitea.io/gitea/modules/web"
"code.gitea.io/gitea/services/forms"

Expand All @@ -19,7 +19,7 @@ import (

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

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

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

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

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

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

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

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

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

u := unittest.AssertExistsAndLoadBean(t, &user_model.User{
IsAdmin: true,
Expand Down
6 changes: 3 additions & 3 deletions routers/web/org/projects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import (
"testing"

"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/test"
"code.gitea.io/gitea/modules/contexttest"
"code.gitea.io/gitea/routers/web/org"

"github.com/stretchr/testify/assert"
)

func TestCheckProjectBoardChangePermissions(t *testing.T) {
unittest.PrepareTestEnv(t)
ctx, _ := test.MockContext(t, "user2/-/projects/4/4")
test.LoadUser(t, ctx, 2)
ctx, _ := contexttest.MockContext(t, "user2/-/projects/4/4")
contexttest.LoadUser(t, ctx, 2)
ctx.ContextUser = ctx.Doer // user2
ctx.SetParams(":id", "4")
ctx.SetParams(":boardID", "4")
Expand Down
22 changes: 11 additions & 11 deletions routers/web/repo/editor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"testing"

"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/contexttest"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/test"

"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -41,12 +41,12 @@ func TestCleanUploadName(t *testing.T) {

func TestGetUniquePatchBranchName(t *testing.T) {
unittest.PrepareTestEnv(t)
ctx, _ := test.MockContext(t, "user2/repo1")
ctx, _ := contexttest.MockContext(t, "user2/repo1")
ctx.SetParams(":id", "1")
test.LoadRepo(t, ctx, 1)
test.LoadRepoCommit(t, ctx)
test.LoadUser(t, ctx, 2)
test.LoadGitRepo(t, ctx)
contexttest.LoadRepo(t, ctx, 1)
contexttest.LoadRepoCommit(t, ctx)
contexttest.LoadUser(t, ctx, 2)
contexttest.LoadGitRepo(t, ctx)
defer ctx.Repo.GitRepo.Close()

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

func TestGetClosestParentWithFiles(t *testing.T) {
unittest.PrepareTestEnv(t)
ctx, _ := test.MockContext(t, "user2/repo1")
ctx, _ := contexttest.MockContext(t, "user2/repo1")
ctx.SetParams(":id", "1")
test.LoadRepo(t, ctx, 1)
test.LoadRepoCommit(t, ctx)
test.LoadUser(t, ctx, 2)
test.LoadGitRepo(t, ctx)
contexttest.LoadRepo(t, ctx, 1)
contexttest.LoadRepoCommit(t, ctx)
contexttest.LoadUser(t, ctx, 2)
contexttest.LoadGitRepo(t, ctx)
defer ctx.Repo.GitRepo.Close()

repo := ctx.Repo.Repository
Expand Down
43 changes: 22 additions & 21 deletions routers/web/repo/issue_label_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

issues_model "code.gitea.io/gitea/models/issues"
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/contexttest"
"code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/test"
"code.gitea.io/gitea/modules/web"
Expand All @@ -32,9 +33,9 @@ func int64SliceToCommaSeparated(a []int64) string {
func TestInitializeLabels(t *testing.T) {
unittest.PrepareTestEnv(t)
assert.NoError(t, repository.LoadRepoConfig())
ctx, _ := test.MockContext(t, "user2/repo1/labels/initialize")
test.LoadUser(t, ctx, 2)
test.LoadRepo(t, ctx, 2)
ctx, _ := contexttest.MockContext(t, "user2/repo1/labels/initialize")
contexttest.LoadUser(t, ctx, 2)
contexttest.LoadRepo(t, ctx, 2)
web.SetForm(ctx, &forms.InitializeLabelsForm{TemplateName: "Default"})
InitializeLabels(ctx)
assert.EqualValues(t, http.StatusSeeOther, ctx.Resp.Status())
Expand All @@ -57,9 +58,9 @@ func TestRetrieveLabels(t *testing.T) {
{1, "leastissues", []int64{2, 1}},
{2, "", []int64{}},
} {
ctx, _ := test.MockContext(t, "user/repo/issues")
test.LoadUser(t, ctx, 2)
test.LoadRepo(t, ctx, testCase.RepoID)
ctx, _ := contexttest.MockContext(t, "user/repo/issues")
contexttest.LoadUser(t, ctx, 2)
contexttest.LoadRepo(t, ctx, testCase.RepoID)
ctx.Req.Form.Set("sort", testCase.Sort)
RetrieveLabels(ctx)
assert.False(t, ctx.Written())
Expand All @@ -75,9 +76,9 @@ func TestRetrieveLabels(t *testing.T) {

func TestNewLabel(t *testing.T) {
unittest.PrepareTestEnv(t)
ctx, _ := test.MockContext(t, "user2/repo1/labels/edit")
test.LoadUser(t, ctx, 2)
test.LoadRepo(t, ctx, 1)
ctx, _ := contexttest.MockContext(t, "user2/repo1/labels/edit")
contexttest.LoadUser(t, ctx, 2)
contexttest.LoadRepo(t, ctx, 1)
web.SetForm(ctx, &forms.CreateLabelForm{
Title: "newlabel",
Color: "#abcdef",
Expand All @@ -93,9 +94,9 @@ func TestNewLabel(t *testing.T) {

func TestUpdateLabel(t *testing.T) {
unittest.PrepareTestEnv(t)
ctx, _ := test.MockContext(t, "user2/repo1/labels/edit")
test.LoadUser(t, ctx, 2)
test.LoadRepo(t, ctx, 1)
ctx, _ := contexttest.MockContext(t, "user2/repo1/labels/edit")
contexttest.LoadUser(t, ctx, 2)
contexttest.LoadRepo(t, ctx, 1)
web.SetForm(ctx, &forms.CreateLabelForm{
ID: 2,
Title: "newnameforlabel",
Expand All @@ -114,9 +115,9 @@ func TestUpdateLabel(t *testing.T) {

func TestDeleteLabel(t *testing.T) {
unittest.PrepareTestEnv(t)
ctx, _ := test.MockContext(t, "user2/repo1/labels/delete")
test.LoadUser(t, ctx, 2)
test.LoadRepo(t, ctx, 1)
ctx, _ := contexttest.MockContext(t, "user2/repo1/labels/delete")
contexttest.LoadUser(t, ctx, 2)
contexttest.LoadRepo(t, ctx, 1)
ctx.Req.Form.Set("id", "2")
DeleteLabel(ctx)
assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
Expand All @@ -127,9 +128,9 @@ func TestDeleteLabel(t *testing.T) {

func TestUpdateIssueLabel_Clear(t *testing.T) {
unittest.PrepareTestEnv(t)
ctx, _ := test.MockContext(t, "user2/repo1/issues/labels")
test.LoadUser(t, ctx, 2)
test.LoadRepo(t, ctx, 1)
ctx, _ := contexttest.MockContext(t, "user2/repo1/issues/labels")
contexttest.LoadUser(t, ctx, 2)
contexttest.LoadRepo(t, ctx, 1)
ctx.Req.Form.Set("issue_ids", "1,3")
ctx.Req.Form.Set("action", "clear")
UpdateIssueLabel(ctx)
Expand All @@ -152,9 +153,9 @@ func TestUpdateIssueLabel_Toggle(t *testing.T) {
{"toggle", []int64{1, 2}, 2, true},
} {
unittest.PrepareTestEnv(t)
ctx, _ := test.MockContext(t, "user2/repo1/issues/labels")
test.LoadUser(t, ctx, 2)
test.LoadRepo(t, ctx, 1)
ctx, _ := contexttest.MockContext(t, "user2/repo1/issues/labels")
contexttest.LoadUser(t, ctx, 2)
contexttest.LoadRepo(t, ctx, 1)
ctx.Req.Form.Set("issue_ids", int64SliceToCommaSeparated(testCase.IssueIDs))
ctx.Req.Form.Set("action", testCase.Action)
ctx.Req.Form.Set("id", strconv.Itoa(int(testCase.LabelID)))
Expand Down
8 changes: 4 additions & 4 deletions routers/web/repo/projects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import (
"testing"

"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/test"
"code.gitea.io/gitea/modules/contexttest"

"github.com/stretchr/testify/assert"
)

func TestCheckProjectBoardChangePermissions(t *testing.T) {
unittest.PrepareTestEnv(t)
ctx, _ := test.MockContext(t, "user2/repo1/projects/1/2")
test.LoadUser(t, ctx, 2)
test.LoadRepo(t, ctx, 1)
ctx, _ := contexttest.MockContext(t, "user2/repo1/projects/1/2")
contexttest.LoadUser(t, ctx, 2)
contexttest.LoadRepo(t, ctx, 1)
ctx.SetParams(":id", "1")
ctx.SetParams(":boardID", "2")

Expand Down
Loading

0 comments on commit e8aae43

Please sign in to comment.