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.

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