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.8KB

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