mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-03-26 21:03:04 -04:00
The new filters are especially useful for status monotoring like kuma to have more relevant results. The wrong status check seems to be a regression of #6300 ## Checklist The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org). ### Tests for Go changes (can be removed for JavaScript changes) - I added test coverage for Go changes... - [x] in their respective `*_test.go` for unit tests. - [ ] in the `tests/integration` directory if it involves interactions with a live Forgejo server. - I ran... - [x] `make pr-go` before pushing ### Tests for JavaScript changes (can be removed for Go changes) - I added test coverage for JavaScript changes... - [ ] in `web_src/js/*.test.js` if it can be unit tested. - [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)). ### Documentation - [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change. - [ ] I did not document these changes and I do not expect someone else to do it. ### Release notes - [x] This change will be noticed by a Forgejo user or admin (feature, bug fix, performance, etc.). I suggest to include a release note for this change. - [ ] This change is not visible to a Forgejo user or admin (refactor, dependency upgrade, etc.). I think there is no need to add a release note for this change. *The decision if the pull request will be shown in the release notes is up to the mergers / release team.* The content of the `release-notes/<pull request number>.md` file will serve as the basis for the release notes. If the file does not exist, the title of the pull request will be used instead. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/11584 Reviewed-by: Mathieu Fenniak <mfenniak@noreply.codeberg.org> Co-authored-by: Michael Kriese <michael.kriese@visualon.de> Co-committed-by: Michael Kriese <michael.kriese@visualon.de>
217 lines
6.5 KiB
Go
217 lines
6.5 KiB
Go
// Copyright 2017 The Gitea Authors. All rights reserved.
|
|
// Copyright 2026 The Forgejo Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
// Package contexttest provides utilities for testing Web/API contexts with models.
|
|
package contexttest
|
|
|
|
import (
|
|
gocontext "context"
|
|
"io"
|
|
"maps"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
org_model "forgejo.org/models/organization"
|
|
access_model "forgejo.org/models/perm/access"
|
|
repo_model "forgejo.org/models/repo"
|
|
"forgejo.org/models/unittest"
|
|
user_model "forgejo.org/models/user"
|
|
"forgejo.org/modules/gitrepo"
|
|
"forgejo.org/modules/json"
|
|
"forgejo.org/modules/templates"
|
|
"forgejo.org/modules/translation"
|
|
"forgejo.org/modules/web/middleware"
|
|
"forgejo.org/services/context"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func mockRequest(t *testing.T, reqPath string) *http.Request {
|
|
method, path, found := strings.Cut(reqPath, " ")
|
|
if !found {
|
|
method = "GET"
|
|
path = reqPath
|
|
}
|
|
requestURL, err := url.Parse(path)
|
|
require.NoError(t, err)
|
|
req := &http.Request{Method: method, URL: requestURL, Form: maps.Clone(requestURL.Query()), Header: http.Header{}}
|
|
req = req.WithContext(middleware.WithContextData(req.Context()))
|
|
return req
|
|
}
|
|
|
|
type MockContextOption struct {
|
|
Render context.Render
|
|
}
|
|
|
|
// MockContext mock context for unit tests
|
|
func MockContext(t *testing.T, reqPath string, opts ...MockContextOption) (*context.Context, *httptest.ResponseRecorder) {
|
|
var opt MockContextOption
|
|
if len(opts) > 0 {
|
|
opt = opts[0]
|
|
}
|
|
if opt.Render == nil {
|
|
opt.Render = &MockRender{}
|
|
}
|
|
resp := httptest.NewRecorder()
|
|
req := mockRequest(t, reqPath)
|
|
base, baseCleanUp := context.NewBaseContext(resp, req)
|
|
_ = baseCleanUp // during test, it doesn't need to do clean up. TODO: this can be improved later
|
|
base.Data = middleware.GetContextData(req.Context())
|
|
base.Locale = &translation.MockLocale{}
|
|
|
|
ctx := context.NewWebContext(base, opt.Render, nil)
|
|
ctx.PageData = map[string]any{}
|
|
ctx.Data["PageStartTime"] = time.Now()
|
|
chiCtx := chi.NewRouteContext()
|
|
ctx.AppendContextValue(chi.RouteCtxKey, chiCtx)
|
|
return ctx, resp
|
|
}
|
|
|
|
// MockAPIContext mock context for unit tests
|
|
func MockAPIContext(t *testing.T, reqPath string) (*context.APIContext, *httptest.ResponseRecorder) {
|
|
resp := httptest.NewRecorder()
|
|
req := mockRequest(t, reqPath)
|
|
base, baseCleanUp := context.NewBaseContext(resp, req)
|
|
base.Data = middleware.GetContextData(req.Context())
|
|
base.Locale = &translation.MockLocale{}
|
|
ctx := &context.APIContext{Base: base}
|
|
_ = baseCleanUp // during test, it doesn't need to do clean up. TODO: this can be improved later
|
|
|
|
chiCtx := chi.NewRouteContext()
|
|
ctx.AppendContextValue(chi.RouteCtxKey, chiCtx)
|
|
return ctx, resp
|
|
}
|
|
|
|
func MockPrivateContext(t *testing.T, reqPath string) (*context.PrivateContext, *httptest.ResponseRecorder) {
|
|
resp := httptest.NewRecorder()
|
|
req := mockRequest(t, reqPath)
|
|
base, baseCleanUp := context.NewBaseContext(resp, req)
|
|
base.Data = middleware.GetContextData(req.Context())
|
|
base.Locale = &translation.MockLocale{}
|
|
ctx := &context.PrivateContext{Base: base}
|
|
_ = baseCleanUp // during test, it doesn't need to do clean up. TODO: this can be improved later
|
|
chiCtx := chi.NewRouteContext()
|
|
ctx.AppendContextValue(chi.RouteCtxKey, chiCtx)
|
|
return ctx, resp
|
|
}
|
|
|
|
// LoadRepo load a repo into a test context.
|
|
func LoadRepo(t *testing.T, ctx gocontext.Context, repoID int64) {
|
|
var doer *user_model.User
|
|
repo := &context.Repository{}
|
|
switch ctx := ctx.(type) {
|
|
case *context.Context:
|
|
ctx.Repo = repo
|
|
doer = ctx.Doer
|
|
case *context.APIContext:
|
|
ctx.Repo = repo
|
|
doer = ctx.Doer
|
|
default:
|
|
assert.FailNow(t, "context is not *context.Context or *context.APIContext")
|
|
}
|
|
|
|
repo.Repository = unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: repoID})
|
|
var err error
|
|
repo.Owner, err = user_model.GetUserByID(ctx, repo.Repository.OwnerID)
|
|
require.NoError(t, err)
|
|
repo.RepoLink = repo.Repository.Link()
|
|
repo.Permission, err = access_model.GetUserRepoPermission(ctx, repo.Repository, doer)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
// LoadRepoCommit loads a repo's commit into a test context.
|
|
func LoadRepoCommit(t *testing.T, ctx gocontext.Context) {
|
|
var repo *context.Repository
|
|
switch ctx := ctx.(type) {
|
|
case *context.Context:
|
|
repo = ctx.Repo
|
|
case *context.APIContext:
|
|
repo = ctx.Repo
|
|
default:
|
|
assert.FailNow(t, "context is not *context.Context or *context.APIContext")
|
|
}
|
|
|
|
if repo.GitRepo == nil {
|
|
assert.FailNow(t, "must call LoadGitRepo")
|
|
}
|
|
|
|
branch, err := repo.GitRepo.GetHEADBranch()
|
|
require.NoError(t, err)
|
|
assert.NotNil(t, branch)
|
|
if branch != nil {
|
|
repo.Commit, err = repo.GitRepo.GetBranchCommit(branch.Name)
|
|
require.NoError(t, err)
|
|
}
|
|
}
|
|
|
|
// 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) {
|
|
case *context.Context:
|
|
ctx.Doer = doer
|
|
case *context.APIContext:
|
|
ctx.Doer = doer
|
|
default:
|
|
assert.FailNow(t, "context is not *context.Context or *context.APIContext")
|
|
}
|
|
}
|
|
|
|
// LoadOrganization load an org into a test context
|
|
func LoadOrganization(t *testing.T, ctx gocontext.Context, orgID int64) {
|
|
org := unittest.AssertExistsAndLoadBean(t, &org_model.Organization{ID: orgID})
|
|
switch ctx := ctx.(type) {
|
|
case *context.Context:
|
|
ctx.Org.Organization = org
|
|
case *context.APIContext:
|
|
ctx.Org.Organization = org
|
|
default:
|
|
assert.FailNow(t, "context is not *context.Context or *context.APIContext")
|
|
}
|
|
}
|
|
|
|
// LoadGitRepo load a git repo into a test context. Requires that ctx.Repo has
|
|
// already been populated.
|
|
func LoadGitRepo(t *testing.T, ctx gocontext.Context) {
|
|
var repo *context.Repository
|
|
switch ctx := ctx.(type) {
|
|
case *context.Context:
|
|
repo = ctx.Repo
|
|
case *context.APIContext:
|
|
repo = ctx.Repo
|
|
default:
|
|
assert.FailNow(t, "context is not *context.Context or *context.APIContext")
|
|
}
|
|
|
|
require.NoError(t, repo.Repository.LoadOwner(ctx))
|
|
var err error
|
|
repo.GitRepo, err = gitrepo.OpenRepository(ctx, repo.Repository)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
type MockRender struct{}
|
|
|
|
func (tr *MockRender) TemplateLookup(tmpl string, _ gocontext.Context) (templates.TemplateExecutor, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
func (tr *MockRender) HTML(w io.Writer, status int, _ string, _ any, _ gocontext.Context) error {
|
|
if resp, ok := w.(http.ResponseWriter); ok {
|
|
resp.WriteHeader(status)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func DecodeJSON(t testing.TB, resp *httptest.ResponseRecorder, v any) {
|
|
t.Helper()
|
|
|
|
decoder := json.NewDecoder(resp.Body)
|
|
require.NoError(t, decoder.Decode(v))
|
|
}
|