You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

context_tests.go 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package test
  5. import (
  6. "net/http"
  7. "net/http/httptest"
  8. "net/url"
  9. "testing"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/context"
  12. "code.gitea.io/gitea/modules/git"
  13. "github.com/go-macaron/session"
  14. "github.com/stretchr/testify/assert"
  15. "gopkg.in/macaron.v1"
  16. )
  17. // MockContext mock context for unit tests
  18. func MockContext(t *testing.T, path string) *context.Context {
  19. var macaronContext macaron.Context
  20. macaronContext.ReplaceAllParams(macaron.Params{})
  21. macaronContext.Locale = &mockLocale{}
  22. requestURL, err := url.Parse(path)
  23. assert.NoError(t, err)
  24. macaronContext.Req = macaron.Request{Request: &http.Request{
  25. URL: requestURL,
  26. Form: url.Values{},
  27. }}
  28. macaronContext.Resp = &mockResponseWriter{}
  29. macaronContext.Render = &mockRender{ResponseWriter: macaronContext.Resp}
  30. macaronContext.Data = map[string]interface{}{}
  31. return &context.Context{
  32. Context: &macaronContext,
  33. Flash: &session.Flash{
  34. Values: make(url.Values),
  35. },
  36. }
  37. }
  38. // LoadRepo load a repo into a test context.
  39. func LoadRepo(t *testing.T, ctx *context.Context, repoID int64) {
  40. ctx.Repo = &context.Repository{}
  41. ctx.Repo.Repository = models.AssertExistsAndLoadBean(t, &models.Repository{ID: repoID}).(*models.Repository)
  42. ctx.Repo.RepoLink = ctx.Repo.Repository.Link()
  43. var err error
  44. ctx.Repo.Permission, err = models.GetUserRepoPermission(ctx.Repo.Repository, ctx.User)
  45. assert.NoError(t, err)
  46. }
  47. // LoadRepoCommit loads a repo's commit into a test context.
  48. func LoadRepoCommit(t *testing.T, ctx *context.Context) {
  49. gitRepo, err := git.OpenRepository(ctx.Repo.Repository.RepoPath())
  50. assert.NoError(t, err)
  51. defer gitRepo.Close()
  52. branch, err := gitRepo.GetHEADBranch()
  53. assert.NoError(t, err)
  54. ctx.Repo.Commit, err = gitRepo.GetBranchCommit(branch.Name)
  55. assert.NoError(t, err)
  56. }
  57. // LoadUser load a user into a test context.
  58. func LoadUser(t *testing.T, ctx *context.Context, userID int64) {
  59. ctx.User = models.AssertExistsAndLoadBean(t, &models.User{ID: userID}).(*models.User)
  60. }
  61. // LoadGitRepo load a git repo into a test context. Requires that ctx.Repo has
  62. // already been populated.
  63. func LoadGitRepo(t *testing.T, ctx *context.Context) {
  64. assert.NoError(t, ctx.Repo.Repository.GetOwner())
  65. var err error
  66. ctx.Repo.GitRepo, err = git.OpenRepository(ctx.Repo.Repository.RepoPath())
  67. assert.NoError(t, err)
  68. }
  69. type mockLocale struct{}
  70. func (l mockLocale) Language() string {
  71. return "en"
  72. }
  73. func (l mockLocale) Tr(s string, _ ...interface{}) string {
  74. return s
  75. }
  76. type mockResponseWriter struct {
  77. httptest.ResponseRecorder
  78. size int
  79. }
  80. func (rw *mockResponseWriter) Write(b []byte) (int, error) {
  81. rw.size += len(b)
  82. return rw.ResponseRecorder.Write(b)
  83. }
  84. func (rw *mockResponseWriter) Status() int {
  85. return rw.ResponseRecorder.Code
  86. }
  87. func (rw *mockResponseWriter) Written() bool {
  88. return rw.ResponseRecorder.Code > 0
  89. }
  90. func (rw *mockResponseWriter) Size() int {
  91. return rw.size
  92. }
  93. func (rw *mockResponseWriter) Before(b macaron.BeforeFunc) {
  94. b(rw)
  95. }
  96. type mockRender struct {
  97. http.ResponseWriter
  98. }
  99. func (tr *mockRender) SetResponseWriter(rw http.ResponseWriter) {
  100. tr.ResponseWriter = rw
  101. }
  102. func (tr *mockRender) JSON(status int, _ interface{}) {
  103. tr.Status(status)
  104. }
  105. func (tr *mockRender) JSONString(interface{}) (string, error) {
  106. return "", nil
  107. }
  108. func (tr *mockRender) RawData(status int, _ []byte) {
  109. tr.Status(status)
  110. }
  111. func (tr *mockRender) PlainText(status int, _ []byte) {
  112. tr.Status(status)
  113. }
  114. func (tr *mockRender) HTML(status int, _ string, _ interface{}, _ ...macaron.HTMLOptions) {
  115. tr.Status(status)
  116. }
  117. func (tr *mockRender) HTMLSet(status int, _ string, _ string, _ interface{}, _ ...macaron.HTMLOptions) {
  118. tr.Status(status)
  119. }
  120. func (tr *mockRender) HTMLSetString(string, string, interface{}, ...macaron.HTMLOptions) (string, error) {
  121. return "", nil
  122. }
  123. func (tr *mockRender) HTMLString(string, interface{}, ...macaron.HTMLOptions) (string, error) {
  124. return "", nil
  125. }
  126. func (tr *mockRender) HTMLSetBytes(string, string, interface{}, ...macaron.HTMLOptions) ([]byte, error) {
  127. return nil, nil
  128. }
  129. func (tr *mockRender) HTMLBytes(string, interface{}, ...macaron.HTMLOptions) ([]byte, error) {
  130. return nil, nil
  131. }
  132. func (tr *mockRender) XML(status int, _ interface{}) {
  133. tr.Status(status)
  134. }
  135. func (tr *mockRender) Error(status int, _ ...string) {
  136. tr.Status(status)
  137. }
  138. func (tr *mockRender) Status(status int) {
  139. tr.ResponseWriter.WriteHeader(status)
  140. }
  141. func (tr *mockRender) SetTemplatePath(string, string) {
  142. }
  143. func (tr *mockRender) HasTemplateSet(string) bool {
  144. return true
  145. }