Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wxiaoguang committed May 18, 2023
1 parent 51ae1b7 commit e631caa
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 10 deletions.
7 changes: 6 additions & 1 deletion modules/context/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/templates"
)

// Package contains owner, access mode and optional the package descriptor
Expand Down Expand Up @@ -139,10 +140,14 @@ func determineAccessMode(ctx *Base, pkg *Package, doer *user_model.User) (perm.A

// PackageContexter initializes a package context for a request.
func PackageContexter() func(next http.Handler) http.Handler {
renderer := templates.HTMLRenderer()
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
base, baseCleanUp := NewBaseContext(resp, req)
ctx := &Context{Base: base}
ctx := &Context{
Base: base,
Render: renderer, // it is still needed when rendering 500 page in a package handler
}
defer baseCleanUp()

ctx.Base.AppendContextValue(contextKey, ctx)
Expand Down
25 changes: 23 additions & 2 deletions modules/test/context_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package test

import (
gocontext "context"
"io"
"net/http"
"net/http/httptest"
"net/url"
Expand All @@ -16,12 +17,27 @@ import (
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/templates"
"code.gitea.io/gitea/modules/translation"
"code.gitea.io/gitea/modules/web/middleware"

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

type mockRender struct{}

func (tr *mockRender) TemplateLookup(tmpl string) (templates.TemplateExecutor, error) {
return nil, nil
}

func (tr *mockRender) HTML(w io.Writer, status int, _ string, _ interface{}) error {
if resp, ok := w.(http.ResponseWriter); ok {
resp.WriteHeader(status)
}
return nil
}

// MockContext mock context for unit tests
// TODO: move this function to other packages, because it depends on "models" package
func MockContext(t *testing.T, path string) *context.Context {
Expand All @@ -34,9 +50,12 @@ func MockContext(t *testing.T, path string) *context.Context {
}

base, baseCleanUp := context.NewBaseContext(resp, req)
base.Data = middleware.ContextData{}
base.Locale = &translation.MockLocale{}
ctx := &context.Context{
Base: base,
Flash: &middleware.Flash{Values: url.Values{}},
Base: base,
Render: &mockRender{},
Flash: &middleware.Flash{Values: url.Values{}},
}
_ = baseCleanUp // during test, it doesn't need to do clean up. TODO: this can be improved later

Expand All @@ -57,6 +76,8 @@ func MockAPIContext(t *testing.T, path string) *context.APIContext {
}

base, baseCleanUp := context.NewBaseContext(resp, req)
base.Data = middleware.ContextData{}
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

Expand Down
14 changes: 11 additions & 3 deletions modules/translation/translation.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ type LangType struct {
}

var (
lock *sync.RWMutex
lock *sync.RWMutex

allLangs []*LangType
allLangMap map[string]*LangType

matcher language.Matcher
allLangs []*LangType
allLangMap map[string]*LangType
supportedTags []language.Tag
)

Expand Down Expand Up @@ -251,3 +253,9 @@ func (l *locale) PrettyNumber(v any) string {
}
return l.msgPrinter.Sprintf("%v", number.Decimal(v))
}

func init() {
// prepare a default matcher, especially for tests
supportedTags = []language.Tag{language.English}
matcher = language.NewMatcher(supportedTags)
}
2 changes: 2 additions & 0 deletions routers/api/v1/misc/markup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/modules/web"
"code.gitea.io/gitea/modules/web/middleware"

"github.com/stretchr/testify/assert"
)
Expand All @@ -31,6 +32,7 @@ const (
func createAPIContext(req *http.Request) (*context.APIContext, *httptest.ResponseRecorder) {
resp := httptest.NewRecorder()
base, baseCleanUp := context.NewBaseContext(resp, req)
base.Data = middleware.ContextData{}
c := &context.APIContext{Base: base}
_ = baseCleanUp // during test, it doesn't need to do clean up. TODO: this can be improved later

Expand Down
9 changes: 5 additions & 4 deletions services/markup/processorhelper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package markup
import (
"context"
"net/http"
"net/http/httptest"
"testing"

"code.gitea.io/gitea/models/db"
Expand Down Expand Up @@ -36,12 +37,12 @@ func TestProcessorHelper(t *testing.T) {
assert.False(t, ProcessorHelper().IsUsernameMentionable(context.Background(), userNoSuch))

// when using web context, use user.IsUserVisibleToViewer to check
var err error
giteaCtx := &gitea_context.Context{}
giteaCtx.Req, err = http.NewRequest("GET", "/", nil)
req, err := http.NewRequest("GET", "/", nil)
assert.NoError(t, err)
base, baseCleanUp := gitea_context.NewBaseContext(httptest.NewRecorder(), req)
defer baseCleanUp()
giteaCtx := &gitea_context.Context{Base: base}

giteaCtx.Doer = nil
assert.True(t, ProcessorHelper().IsUsernameMentionable(giteaCtx, userPublic))
assert.False(t, ProcessorHelper().IsUsernameMentionable(giteaCtx, userPrivate))

Expand Down

0 comments on commit e631caa

Please sign in to comment.