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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. "gitea.com/macaron/macaron"
  14. "gitea.com/macaron/session"
  15. "github.com/stretchr/testify/assert"
  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. var err error
  43. ctx.Repo.Owner, err = models.GetUserByID(ctx.Repo.Repository.OwnerID)
  44. assert.NoError(t, err)
  45. ctx.Repo.RepoLink = ctx.Repo.Repository.Link()
  46. ctx.Repo.Permission, err = models.GetUserRepoPermission(ctx.Repo.Repository, ctx.User)
  47. assert.NoError(t, err)
  48. }
  49. // LoadRepoCommit loads a repo's commit into a test context.
  50. func LoadRepoCommit(t *testing.T, ctx *context.Context) {
  51. gitRepo, err := git.OpenRepository(ctx.Repo.Repository.RepoPath())
  52. assert.NoError(t, err)
  53. defer gitRepo.Close()
  54. branch, err := gitRepo.GetHEADBranch()
  55. assert.NoError(t, err)
  56. assert.NotNil(t, branch)
  57. if branch != nil {
  58. ctx.Repo.Commit, err = gitRepo.GetBranchCommit(branch.Name)
  59. assert.NoError(t, err)
  60. }
  61. }
  62. // LoadUser load a user into a test context.
  63. func LoadUser(t *testing.T, ctx *context.Context, userID int64) {
  64. ctx.User = models.AssertExistsAndLoadBean(t, &models.User{ID: userID}).(*models.User)
  65. }
  66. // LoadGitRepo load a git repo into a test context. Requires that ctx.Repo has
  67. // already been populated.
  68. func LoadGitRepo(t *testing.T, ctx *context.Context) {
  69. assert.NoError(t, ctx.Repo.Repository.GetOwner())
  70. var err error
  71. ctx.Repo.GitRepo, err = git.OpenRepository(ctx.Repo.Repository.RepoPath())
  72. assert.NoError(t, err)
  73. }
  74. type mockLocale struct{}
  75. func (l mockLocale) Language() string {
  76. return "en"
  77. }
  78. func (l mockLocale) Tr(s string, _ ...interface{}) string {
  79. return s
  80. }
  81. type mockResponseWriter struct {
  82. httptest.ResponseRecorder
  83. size int
  84. }
  85. func (rw *mockResponseWriter) Write(b []byte) (int, error) {
  86. rw.size += len(b)
  87. return rw.ResponseRecorder.Write(b)
  88. }
  89. func (rw *mockResponseWriter) Status() int {
  90. return rw.ResponseRecorder.Code
  91. }
  92. func (rw *mockResponseWriter) Written() bool {
  93. return rw.ResponseRecorder.Code > 0
  94. }
  95. func (rw *mockResponseWriter) Size() int {
  96. return rw.size
  97. }
  98. func (rw *mockResponseWriter) Before(b macaron.BeforeFunc) {
  99. b(rw)
  100. }
  101. type mockRender struct {
  102. http.ResponseWriter
  103. }
  104. func (tr *mockRender) SetResponseWriter(rw http.ResponseWriter) {
  105. tr.ResponseWriter = rw
  106. }
  107. func (tr *mockRender) JSON(status int, _ interface{}) {
  108. tr.Status(status)
  109. }
  110. func (tr *mockRender) JSONString(interface{}) (string, error) {
  111. return "", nil
  112. }
  113. func (tr *mockRender) RawData(status int, _ []byte) {
  114. tr.Status(status)
  115. }
  116. func (tr *mockRender) PlainText(status int, _ []byte) {
  117. tr.Status(status)
  118. }
  119. func (tr *mockRender) HTML(status int, _ string, _ interface{}, _ ...macaron.HTMLOptions) {
  120. tr.Status(status)
  121. }
  122. func (tr *mockRender) HTMLSet(status int, _ string, _ string, _ interface{}, _ ...macaron.HTMLOptions) {
  123. tr.Status(status)
  124. }
  125. func (tr *mockRender) HTMLSetString(string, string, interface{}, ...macaron.HTMLOptions) (string, error) {
  126. return "", nil
  127. }
  128. func (tr *mockRender) HTMLString(string, interface{}, ...macaron.HTMLOptions) (string, error) {
  129. return "", nil
  130. }
  131. func (tr *mockRender) HTMLSetBytes(string, string, interface{}, ...macaron.HTMLOptions) ([]byte, error) {
  132. return nil, nil
  133. }
  134. func (tr *mockRender) HTMLBytes(string, interface{}, ...macaron.HTMLOptions) ([]byte, error) {
  135. return nil, nil
  136. }
  137. func (tr *mockRender) XML(status int, _ interface{}) {
  138. tr.Status(status)
  139. }
  140. func (tr *mockRender) Error(status int, _ ...string) {
  141. tr.Status(status)
  142. }
  143. func (tr *mockRender) Status(status int) {
  144. tr.ResponseWriter.WriteHeader(status)
  145. }
  146. func (tr *mockRender) SetTemplatePath(string, string) {
  147. }
  148. func (tr *mockRender) HasTemplateSet(string) bool {
  149. return true
  150. }