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.

187 lines
5.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 repo
  5. import (
  6. "io/ioutil"
  7. "net/http"
  8. "testing"
  9. "code.gitea.io/git"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/auth"
  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().Data()
  37. assert.NoError(t, err)
  38. bytes, err := ioutil.ReadAll(reader)
  39. assert.NoError(t, err)
  40. return string(bytes)
  41. }
  42. func assertWikiExists(t *testing.T, repo *models.Repository, wikiName string) {
  43. assert.NotNil(t, wikiEntry(t, repo, wikiName))
  44. }
  45. func assertWikiNotExists(t *testing.T, repo *models.Repository, wikiName string) {
  46. assert.Nil(t, wikiEntry(t, repo, wikiName))
  47. }
  48. func assertPagesMetas(t *testing.T, expectedNames []string, metas interface{}) {
  49. pageMetas, ok := metas.([]PageMeta)
  50. if !assert.True(t, ok) {
  51. return
  52. }
  53. if !assert.EqualValues(t, len(expectedNames), len(pageMetas)) {
  54. return
  55. }
  56. for i, pageMeta := range pageMetas {
  57. assert.EqualValues(t, expectedNames[i], pageMeta.Name)
  58. }
  59. }
  60. func TestWiki(t *testing.T) {
  61. models.PrepareTestEnv(t)
  62. ctx := test.MockContext(t, "user2/repo1/wiki/_pages")
  63. ctx.SetParams(":page", "Home")
  64. test.LoadRepo(t, ctx, 1)
  65. Wiki(ctx)
  66. assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
  67. assert.EqualValues(t, "Home", ctx.Data["Title"])
  68. assertPagesMetas(t, []string{"Home"}, ctx.Data["Pages"])
  69. }
  70. func TestWikiPages(t *testing.T) {
  71. models.PrepareTestEnv(t)
  72. ctx := test.MockContext(t, "user2/repo1/wiki/_pages")
  73. test.LoadRepo(t, ctx, 1)
  74. WikiPages(ctx)
  75. assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
  76. assertPagesMetas(t, []string{"Home"}, ctx.Data["Pages"])
  77. }
  78. func TestNewWiki(t *testing.T) {
  79. models.PrepareTestEnv(t)
  80. ctx := test.MockContext(t, "user2/repo1/wiki/_new")
  81. test.LoadUser(t, ctx, 2)
  82. test.LoadRepo(t, ctx, 1)
  83. NewWiki(ctx)
  84. assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
  85. assert.EqualValues(t, ctx.Tr("repo.wiki.new_page"), ctx.Data["Title"])
  86. }
  87. func TestNewWikiPost(t *testing.T) {
  88. for _, title := range []string{
  89. "New page",
  90. "&&&&",
  91. } {
  92. models.PrepareTestEnv(t)
  93. ctx := test.MockContext(t, "user2/repo1/wiki/_new")
  94. test.LoadUser(t, ctx, 2)
  95. test.LoadRepo(t, ctx, 1)
  96. NewWikiPost(ctx, auth.NewWikiForm{
  97. Title: title,
  98. Content: content,
  99. Message: message,
  100. })
  101. assert.EqualValues(t, http.StatusFound, ctx.Resp.Status())
  102. assertWikiExists(t, ctx.Repo.Repository, title)
  103. assert.Equal(t, wikiContent(t, ctx.Repo.Repository, title), content)
  104. }
  105. }
  106. func TestNewWikiPost_ReservedName(t *testing.T) {
  107. models.PrepareTestEnv(t)
  108. ctx := test.MockContext(t, "user2/repo1/wiki/_new")
  109. test.LoadUser(t, ctx, 2)
  110. test.LoadRepo(t, ctx, 1)
  111. NewWikiPost(ctx, auth.NewWikiForm{
  112. Title: "_edit",
  113. Content: content,
  114. Message: message,
  115. })
  116. assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
  117. assert.EqualValues(t, ctx.Tr("repo.wiki.reserved_page"), ctx.Flash.ErrorMsg)
  118. assertWikiNotExists(t, ctx.Repo.Repository, "_edit")
  119. }
  120. func TestEditWiki(t *testing.T) {
  121. models.PrepareTestEnv(t)
  122. ctx := test.MockContext(t, "user2/repo1/wiki/_edit/Home")
  123. ctx.SetParams(":page", "Home")
  124. test.LoadUser(t, ctx, 2)
  125. test.LoadRepo(t, ctx, 1)
  126. EditWiki(ctx)
  127. assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
  128. assert.EqualValues(t, "Home", ctx.Data["Title"])
  129. assert.Equal(t, wikiContent(t, ctx.Repo.Repository, "Home"), ctx.Data["content"])
  130. }
  131. func TestEditWikiPost(t *testing.T) {
  132. for _, title := range []string{
  133. "Home",
  134. "New/<page>",
  135. } {
  136. models.PrepareTestEnv(t)
  137. ctx := test.MockContext(t, "user2/repo1/wiki/_new/Home")
  138. ctx.SetParams(":page", "Home")
  139. test.LoadUser(t, ctx, 2)
  140. test.LoadRepo(t, ctx, 1)
  141. EditWikiPost(ctx, auth.NewWikiForm{
  142. Title: title,
  143. Content: content,
  144. Message: message,
  145. })
  146. assert.EqualValues(t, http.StatusFound, ctx.Resp.Status())
  147. assertWikiExists(t, ctx.Repo.Repository, title)
  148. assert.Equal(t, wikiContent(t, ctx.Repo.Repository, title), content)
  149. if title != "Home" {
  150. assertWikiNotExists(t, ctx.Repo.Repository, "Home")
  151. }
  152. }
  153. }
  154. func TestDeleteWikiPagePost(t *testing.T) {
  155. models.PrepareTestEnv(t)
  156. ctx := test.MockContext(t, "user2/repo1/wiki/Home/delete")
  157. test.LoadUser(t, ctx, 2)
  158. test.LoadRepo(t, ctx, 1)
  159. DeleteWikiPagePost(ctx)
  160. assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
  161. assertWikiNotExists(t, ctx.Repo.Repository, "Home")
  162. }