Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

wiki_test.go 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. "path/filepath"
  9. "testing"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/auth"
  12. "code.gitea.io/gitea/modules/test"
  13. "github.com/Unknwon/com"
  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 wikiPath(repo *models.Repository, wikiName string) string {
  19. return filepath.Join(repo.LocalWikiPath(), models.WikiNameToFilename(wikiName))
  20. }
  21. func wikiContent(t *testing.T, repo *models.Repository, wikiName string) string {
  22. bytes, err := ioutil.ReadFile(wikiPath(repo, wikiName))
  23. assert.NoError(t, err)
  24. return string(bytes)
  25. }
  26. func assertWikiExists(t *testing.T, repo *models.Repository, wikiName string) {
  27. assert.True(t, com.IsExist(wikiPath(repo, wikiName)))
  28. }
  29. func assertWikiNotExists(t *testing.T, repo *models.Repository, wikiName string) {
  30. assert.False(t, com.IsExist(wikiPath(repo, wikiName)))
  31. }
  32. func assertPagesMetas(t *testing.T, expectedNames []string, metas interface{}) {
  33. pageMetas, ok := metas.([]PageMeta)
  34. if !assert.True(t, ok) {
  35. return
  36. }
  37. if !assert.EqualValues(t, len(expectedNames), len(pageMetas)) {
  38. return
  39. }
  40. for i, pageMeta := range pageMetas {
  41. assert.EqualValues(t, expectedNames[i], pageMeta.Name)
  42. }
  43. }
  44. func TestWiki(t *testing.T) {
  45. models.PrepareTestEnv(t)
  46. ctx := test.MockContext(t, "user2/repo1/wiki/_pages")
  47. ctx.SetParams(":page", "Home")
  48. test.LoadRepo(t, ctx, 1)
  49. Wiki(ctx)
  50. assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
  51. assert.EqualValues(t, "Home", ctx.Data["Title"])
  52. assertPagesMetas(t, []string{"Home"}, ctx.Data["Pages"])
  53. }
  54. func TestWikiPages(t *testing.T) {
  55. models.PrepareTestEnv(t)
  56. ctx := test.MockContext(t, "user2/repo1/wiki/_pages")
  57. test.LoadRepo(t, ctx, 1)
  58. WikiPages(ctx)
  59. assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
  60. assertPagesMetas(t, []string{"Home"}, ctx.Data["Pages"])
  61. }
  62. func TestNewWiki(t *testing.T) {
  63. models.PrepareTestEnv(t)
  64. ctx := test.MockContext(t, "user2/repo1/wiki/_new")
  65. test.LoadUser(t, ctx, 2)
  66. test.LoadRepo(t, ctx, 1)
  67. NewWiki(ctx)
  68. assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
  69. assert.EqualValues(t, ctx.Tr("repo.wiki.new_page"), ctx.Data["Title"])
  70. }
  71. func TestNewWikiPost(t *testing.T) {
  72. for _, title := range []string{
  73. "New page",
  74. "&&&&",
  75. } {
  76. models.PrepareTestEnv(t)
  77. ctx := test.MockContext(t, "user2/repo1/wiki/_new")
  78. test.LoadUser(t, ctx, 2)
  79. test.LoadRepo(t, ctx, 1)
  80. NewWikiPost(ctx, auth.NewWikiForm{
  81. Title: title,
  82. Content: content,
  83. Message: message,
  84. })
  85. assert.EqualValues(t, http.StatusFound, ctx.Resp.Status())
  86. assertWikiExists(t, ctx.Repo.Repository, title)
  87. assert.Equal(t, wikiContent(t, ctx.Repo.Repository, title), content)
  88. }
  89. }
  90. func TestNewWikiPost_ReservedName(t *testing.T) {
  91. models.PrepareTestEnv(t)
  92. ctx := test.MockContext(t, "user2/repo1/wiki/_new")
  93. test.LoadUser(t, ctx, 2)
  94. test.LoadRepo(t, ctx, 1)
  95. NewWikiPost(ctx, auth.NewWikiForm{
  96. Title: "_edit",
  97. Content: content,
  98. Message: message,
  99. })
  100. assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
  101. assert.EqualValues(t, ctx.Tr("repo.wiki.reserved_page"), ctx.Flash.ErrorMsg)
  102. assertWikiNotExists(t, ctx.Repo.Repository, "_edit")
  103. }
  104. func TestEditWiki(t *testing.T) {
  105. models.PrepareTestEnv(t)
  106. ctx := test.MockContext(t, "user2/repo1/wiki/_edit/Home")
  107. ctx.SetParams(":page", "Home")
  108. test.LoadUser(t, ctx, 2)
  109. test.LoadRepo(t, ctx, 1)
  110. EditWiki(ctx)
  111. assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
  112. assert.EqualValues(t, "Home", ctx.Data["Title"])
  113. assert.Equal(t, wikiContent(t, ctx.Repo.Repository, "Home"), ctx.Data["content"])
  114. }
  115. func TestEditWikiPost(t *testing.T) {
  116. for _, title := range []string{
  117. "Home",
  118. "New/<page>",
  119. } {
  120. models.PrepareTestEnv(t)
  121. ctx := test.MockContext(t, "user2/repo1/wiki/_new/Home")
  122. ctx.SetParams(":page", "Home")
  123. test.LoadUser(t, ctx, 2)
  124. test.LoadRepo(t, ctx, 1)
  125. EditWikiPost(ctx, auth.NewWikiForm{
  126. Title: title,
  127. Content: content,
  128. Message: message,
  129. })
  130. assert.EqualValues(t, http.StatusFound, ctx.Resp.Status())
  131. assertWikiExists(t, ctx.Repo.Repository, title)
  132. assert.Equal(t, wikiContent(t, ctx.Repo.Repository, title), content)
  133. if title != "Home" {
  134. assertWikiNotExists(t, ctx.Repo.Repository, "Home")
  135. }
  136. }
  137. }
  138. func TestDeleteWikiPagePost(t *testing.T) {
  139. models.PrepareTestEnv(t)
  140. ctx := test.MockContext(t, "user2/repo1/wiki/Home/delete")
  141. test.LoadUser(t, ctx, 2)
  142. test.LoadRepo(t, ctx, 1)
  143. DeleteWikiPagePost(ctx)
  144. assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
  145. assertWikiNotExists(t, ctx.Repo.Repository, "Home")
  146. }