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 6.0KB

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