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.

wiki_test.go 5.9KB

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