diff options
author | Ethan Koenig <ethantkoenig@gmail.com> | 2017-11-30 07:52:15 -0800 |
---|---|---|
committer | Lauris BH <lauris@nix.lv> | 2017-11-30 17:52:15 +0200 |
commit | 91f3d77ceb66bedc7bb4d792306beb547f104dce (patch) | |
tree | b081210cb7b6b26415e0e28ceb3a51477407a97e /modules/test | |
parent | 82e8486f13253e5a2b1a06c286b1e2b2b6049473 (diff) | |
download | gitea-91f3d77ceb66bedc7bb4d792306beb547f104dce.tar.gz gitea-91f3d77ceb66bedc7bb4d792306beb547f104dce.zip |
Unit tests for wiki routers (#3022)
Diffstat (limited to 'modules/test')
-rw-r--r-- | modules/test/context_tests.go | 46 |
1 files changed, 30 insertions, 16 deletions
diff --git a/modules/test/context_tests.go b/modules/test/context_tests.go index 6bb7ffe987..da15b64395 100644 --- a/modules/test/context_tests.go +++ b/modules/test/context_tests.go @@ -9,33 +9,46 @@ import ( "net/url" "testing" + "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" + "github.com/go-macaron/session" + _ "github.com/mattn/go-sqlite3" // for the test engine "github.com/stretchr/testify/assert" - macaron "gopkg.in/macaron.v1" + "gopkg.in/macaron.v1" ) // MockContext mock context for unit tests -func MockContext(t *testing.T) *context.Context { - var macaronContext *macaron.Context - mac := macaron.New() - mac.Get("*/", func(ctx *macaron.Context) { - macaronContext = ctx - }) - req, err := http.NewRequest("GET", "star", nil) - assert.NoError(t, err) - req.Form = url.Values{} - mac.ServeHTTP(&mockResponseWriter{}, req) - assert.NotNil(t, macaronContext) - assert.EqualValues(t, req, macaronContext.Req.Request) +func MockContext(t *testing.T, path string) *context.Context { + var macaronContext macaron.Context + macaronContext.ReplaceAllParams(macaron.Params{}) macaronContext.Locale = &mockLocale{} + requestURL, err := url.Parse(path) + assert.NoError(t, err) + macaronContext.Req = macaron.Request{Request: &http.Request{ + URL: requestURL, + Form: url.Values{}, + }} macaronContext.Resp = &mockResponseWriter{} macaronContext.Render = &mockRender{ResponseWriter: macaronContext.Resp} + macaronContext.Data = map[string]interface{}{} return &context.Context{ - Context: macaronContext, + Context: &macaronContext, + Flash: &session.Flash{}, } } +// LoadRepo load a repo into a test context. +func LoadRepo(t *testing.T, ctx *context.Context, repoID int64) { + ctx.Repo = &context.Repository{} + ctx.Repo.Repository = models.AssertExistsAndLoadBean(t, &models.Repository{ID: repoID}).(*models.Repository) +} + +// LoadUser load a user into a test context. +func LoadUser(t *testing.T, ctx *context.Context, userID int64) { + ctx.User = models.AssertExistsAndLoadBean(t, &models.User{ID: userID}).(*models.User) +} + type mockLocale struct{} func (l mockLocale) Language() string { @@ -43,7 +56,7 @@ func (l mockLocale) Language() string { } func (l mockLocale) Tr(s string, _ ...interface{}) string { - return "test translation" + return s } type mockResponseWriter struct { @@ -91,7 +104,8 @@ func (tr *mockRender) SetResponseWriter(rw http.ResponseWriter) { tr.ResponseWriter = rw } -func (tr *mockRender) JSON(int, interface{}) { +func (tr *mockRender) JSON(status int, _ interface{}) { + tr.Status(status) } func (tr *mockRender) JSONString(interface{}) (string, error) { |