本站源代码
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

180 rindas
4.5KB

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