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.

177 lines
4.4 KiB

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