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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 repo
  5. import (
  6. "io/ioutil"
  7. "net/http"
  8. "testing"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/git"
  11. "code.gitea.io/gitea/modules/test"
  12. "code.gitea.io/gitea/modules/web"
  13. "code.gitea.io/gitea/services/forms"
  14. wiki_service "code.gitea.io/gitea/services/wiki"
  15. "github.com/stretchr/testify/assert"
  16. )
  17. const content = "Wiki contents for unit tests"
  18. const message = "Wiki commit message for unit tests"
  19. func wikiEntry(t *testing.T, repo *models.Repository, wikiName string) *git.TreeEntry {
  20. wikiRepo, err := git.OpenRepository(repo.WikiPath())
  21. assert.NoError(t, err)
  22. defer wikiRepo.Close()
  23. commit, err := wikiRepo.GetBranchCommit("master")
  24. assert.NoError(t, err)
  25. entries, err := commit.ListEntries()
  26. assert.NoError(t, err)
  27. for _, entry := range entries {
  28. if entry.Name() == wiki_service.NameToFilename(wikiName) {
  29. return entry
  30. }
  31. }
  32. return nil
  33. }
  34. func wikiContent(t *testing.T, repo *models.Repository, wikiName string) string {
  35. entry := wikiEntry(t, repo, wikiName)
  36. if !assert.NotNil(t, entry) {
  37. return ""
  38. }
  39. reader, err := entry.Blob().DataAsync()
  40. assert.NoError(t, err)
  41. defer reader.Close()
  42. bytes, err := ioutil.ReadAll(reader)
  43. assert.NoError(t, err)
  44. return string(bytes)
  45. }
  46. func assertWikiExists(t *testing.T, repo *models.Repository, wikiName string) {
  47. assert.NotNil(t, wikiEntry(t, repo, wikiName))
  48. }
  49. func assertWikiNotExists(t *testing.T, repo *models.Repository, wikiName string) {
  50. assert.Nil(t, wikiEntry(t, repo, wikiName))
  51. }
  52. func assertPagesMetas(t *testing.T, expectedNames []string, metas interface{}) {
  53. pageMetas, ok := metas.([]PageMeta)
  54. if !assert.True(t, ok) {
  55. return
  56. }
  57. if !assert.Len(t, pageMetas, len(expectedNames)) {
  58. return
  59. }
  60. for i, pageMeta := range pageMetas {
  61. assert.EqualValues(t, expectedNames[i], pageMeta.Name)
  62. }
  63. }
  64. func TestWiki(t *testing.T) {
  65. models.PrepareTestEnv(t)
  66. ctx := test.MockContext(t, "user2/repo1/wiki/_pages")
  67. ctx.SetParams(":page", "Home")
  68. test.LoadRepo(t, ctx, 1)
  69. Wiki(ctx)
  70. assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
  71. assert.EqualValues(t, "Home", ctx.Data["Title"])
  72. assertPagesMetas(t, []string{"Home", "Page With Image", "Page With Spaced Name"}, ctx.Data["Pages"])
  73. }
  74. func TestWikiPages(t *testing.T) {
  75. models.PrepareTestEnv(t)
  76. ctx := test.MockContext(t, "user2/repo1/wiki/_pages")
  77. test.LoadRepo(t, ctx, 1)
  78. WikiPages(ctx)
  79. assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
  80. assertPagesMetas(t, []string{"Home", "Page With Image", "Page With Spaced Name"}, ctx.Data["Pages"])
  81. }
  82. func TestNewWiki(t *testing.T) {
  83. models.PrepareTestEnv(t)
  84. ctx := test.MockContext(t, "user2/repo1/wiki/_new")
  85. test.LoadUser(t, ctx, 2)
  86. test.LoadRepo(t, ctx, 1)
  87. NewWiki(ctx)
  88. assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
  89. assert.EqualValues(t, ctx.Tr("repo.wiki.new_page"), ctx.Data["Title"])
  90. }
  91. func TestNewWikiPost(t *testing.T) {
  92. for _, title := range []string{
  93. "New page",
  94. "&&&&",
  95. } {
  96. models.PrepareTestEnv(t)
  97. ctx := test.MockContext(t, "user2/repo1/wiki/_new")
  98. test.LoadUser(t, ctx, 2)
  99. test.LoadRepo(t, ctx, 1)
  100. web.SetForm(ctx, &forms.NewWikiForm{
  101. Title: title,
  102. Content: content,
  103. Message: message,
  104. })
  105. NewWikiPost(ctx)
  106. assert.EqualValues(t, http.StatusFound, ctx.Resp.Status())
  107. assertWikiExists(t, ctx.Repo.Repository, title)
  108. assert.Equal(t, wikiContent(t, ctx.Repo.Repository, title), content)
  109. }
  110. }
  111. func TestNewWikiPost_ReservedName(t *testing.T) {
  112. models.PrepareTestEnv(t)
  113. ctx := test.MockContext(t, "user2/repo1/wiki/_new")
  114. test.LoadUser(t, ctx, 2)
  115. test.LoadRepo(t, ctx, 1)
  116. web.SetForm(ctx, &forms.NewWikiForm{
  117. Title: "_edit",
  118. Content: content,
  119. Message: message,
  120. })
  121. NewWikiPost(ctx)
  122. assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
  123. assert.EqualValues(t, ctx.Tr("repo.wiki.reserved_page"), ctx.Flash.ErrorMsg)
  124. assertWikiNotExists(t, ctx.Repo.Repository, "_edit")
  125. }
  126. func TestEditWiki(t *testing.T) {
  127. models.PrepareTestEnv(t)
  128. ctx := test.MockContext(t, "user2/repo1/wiki/_edit/Home")
  129. ctx.SetParams(":page", "Home")
  130. test.LoadUser(t, ctx, 2)
  131. test.LoadRepo(t, ctx, 1)
  132. EditWiki(ctx)
  133. assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
  134. assert.EqualValues(t, "Home", ctx.Data["Title"])
  135. assert.Equal(t, wikiContent(t, ctx.Repo.Repository, "Home"), ctx.Data["content"])
  136. }
  137. func TestEditWikiPost(t *testing.T) {
  138. for _, title := range []string{
  139. "Home",
  140. "New/<page>",
  141. } {
  142. models.PrepareTestEnv(t)
  143. ctx := test.MockContext(t, "user2/repo1/wiki/_new/Home")
  144. ctx.SetParams(":page", "Home")
  145. test.LoadUser(t, ctx, 2)
  146. test.LoadRepo(t, ctx, 1)
  147. web.SetForm(ctx, &forms.NewWikiForm{
  148. Title: title,
  149. Content: content,
  150. Message: message,
  151. })
  152. EditWikiPost(ctx)
  153. assert.EqualValues(t, http.StatusFound, ctx.Resp.Status())
  154. assertWikiExists(t, ctx.Repo.Repository, title)
  155. assert.Equal(t, wikiContent(t, ctx.Repo.Repository, title), content)
  156. if title != "Home" {
  157. assertWikiNotExists(t, ctx.Repo.Repository, "Home")
  158. }
  159. }
  160. }
  161. func TestDeleteWikiPagePost(t *testing.T) {
  162. models.PrepareTestEnv(t)
  163. ctx := test.MockContext(t, "user2/repo1/wiki/Home/delete")
  164. test.LoadUser(t, ctx, 2)
  165. test.LoadRepo(t, ctx, 1)
  166. DeleteWikiPagePost(ctx)
  167. assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
  168. assertWikiNotExists(t, ctx.Repo.Repository, "Home")
  169. }
  170. func TestWikiRaw(t *testing.T) {
  171. for filepath, filetype := range map[string]string{
  172. "jpeg.jpg": "image/jpeg",
  173. "images/jpeg.jpg": "image/jpeg",
  174. "Page With Spaced Name": "text/plain; charset=utf-8",
  175. "Page-With-Spaced-Name": "text/plain; charset=utf-8",
  176. "Page With Spaced Name.md": "text/plain; charset=utf-8",
  177. "Page-With-Spaced-Name.md": "text/plain; charset=utf-8",
  178. } {
  179. models.PrepareTestEnv(t)
  180. ctx := test.MockContext(t, "user2/repo1/wiki/raw/"+filepath)
  181. ctx.SetParams("*", filepath)
  182. test.LoadUser(t, ctx, 2)
  183. test.LoadRepo(t, ctx, 1)
  184. WikiRaw(ctx)
  185. assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
  186. assert.EqualValues(t, filetype, ctx.Resp.Header().Get("Content-Type"))
  187. }
  188. }