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.

210 lines
6.2 KiB

  1. // Copyright 2019 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 wiki
  5. import (
  6. "path/filepath"
  7. "testing"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/git"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func TestMain(m *testing.M) {
  13. models.MainTest(m, filepath.Join("..", ".."))
  14. }
  15. func TestWikiNameToSubURL(t *testing.T) {
  16. type test struct {
  17. Expected string
  18. WikiName string
  19. }
  20. for _, test := range []test{
  21. {"wiki-name", "wiki name"},
  22. {"wiki-name", "wiki-name"},
  23. {"name-with%2Fslash", "name with/slash"},
  24. {"name-with%25percent", "name with%percent"},
  25. } {
  26. assert.Equal(t, test.Expected, NameToSubURL(test.WikiName))
  27. }
  28. }
  29. func TestNormalizeWikiName(t *testing.T) {
  30. type test struct {
  31. Expected string
  32. WikiName string
  33. }
  34. for _, test := range []test{
  35. {"wiki name", "wiki name"},
  36. {"wiki name", "wiki-name"},
  37. {"name with/slash", "name with/slash"},
  38. {"name with%percent", "name-with%percent"},
  39. {"%2F", "%2F"},
  40. } {
  41. assert.Equal(t, test.Expected, NormalizeWikiName(test.WikiName))
  42. }
  43. }
  44. func TestWikiNameToFilename(t *testing.T) {
  45. type test struct {
  46. Expected string
  47. WikiName string
  48. }
  49. for _, test := range []test{
  50. {"wiki-name.md", "wiki name"},
  51. {"wiki-name.md", "wiki-name"},
  52. {"name-with%2Fslash.md", "name with/slash"},
  53. {"name-with%25percent.md", "name with%percent"},
  54. } {
  55. assert.Equal(t, test.Expected, NameToFilename(test.WikiName))
  56. }
  57. }
  58. func TestWikiFilenameToName(t *testing.T) {
  59. type test struct {
  60. Expected string
  61. Filename string
  62. }
  63. for _, test := range []test{
  64. {"hello world", "hello-world.md"},
  65. {"symbols/?*", "symbols%2F%3F%2A.md"},
  66. } {
  67. name, err := FilenameToName(test.Filename)
  68. assert.NoError(t, err)
  69. assert.Equal(t, test.Expected, name)
  70. }
  71. for _, badFilename := range []string{
  72. "nofileextension",
  73. "wrongfileextension.txt",
  74. } {
  75. _, err := FilenameToName(badFilename)
  76. assert.Error(t, err)
  77. assert.True(t, models.IsErrWikiInvalidFileName(err))
  78. }
  79. _, err := FilenameToName("badescaping%%.md")
  80. assert.Error(t, err)
  81. assert.False(t, models.IsErrWikiInvalidFileName(err))
  82. }
  83. func TestWikiNameToFilenameToName(t *testing.T) {
  84. // converting from wiki name to filename, then back to wiki name should
  85. // return the original (normalized) name
  86. for _, name := range []string{
  87. "wiki-name",
  88. "wiki name",
  89. "wiki name with/slash",
  90. "$$$%%%^^&&!@#$(),.<>",
  91. } {
  92. filename := NameToFilename(name)
  93. resultName, err := FilenameToName(filename)
  94. assert.NoError(t, err)
  95. assert.Equal(t, NormalizeWikiName(name), resultName)
  96. }
  97. }
  98. func TestRepository_InitWiki(t *testing.T) {
  99. models.PrepareTestEnv(t)
  100. // repo1 already has a wiki
  101. repo1 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
  102. assert.NoError(t, InitWiki(repo1))
  103. // repo2 does not already have a wiki
  104. repo2 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository)
  105. assert.NoError(t, InitWiki(repo2))
  106. assert.True(t, repo2.HasWiki())
  107. }
  108. func TestRepository_AddWikiPage(t *testing.T) {
  109. assert.NoError(t, models.PrepareTestDatabase())
  110. const wikiContent = "This is the wiki content"
  111. const commitMsg = "Commit message"
  112. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
  113. doer := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  114. for _, wikiName := range []string{
  115. "Another page",
  116. "Here's a <tag> and a/slash",
  117. } {
  118. wikiName := wikiName
  119. t.Run("test wiki exist: "+wikiName, func(t *testing.T) {
  120. t.Parallel()
  121. assert.NoError(t, AddWikiPage(doer, repo, wikiName, wikiContent, commitMsg))
  122. // Now need to show that the page has been added:
  123. gitRepo, err := git.OpenRepository(repo.WikiPath())
  124. assert.NoError(t, err)
  125. defer gitRepo.Close()
  126. masterTree, err := gitRepo.GetTree("master")
  127. assert.NoError(t, err)
  128. wikiPath := NameToFilename(wikiName)
  129. entry, err := masterTree.GetTreeEntryByPath(wikiPath)
  130. assert.NoError(t, err)
  131. assert.Equal(t, wikiPath, entry.Name(), "%s not addded correctly", wikiName)
  132. })
  133. }
  134. t.Run("check wiki already exist", func(t *testing.T) {
  135. t.Parallel()
  136. // test for already-existing wiki name
  137. err := AddWikiPage(doer, repo, "Home", wikiContent, commitMsg)
  138. assert.Error(t, err)
  139. assert.True(t, models.IsErrWikiAlreadyExist(err))
  140. })
  141. t.Run("check wiki reserved name", func(t *testing.T) {
  142. t.Parallel()
  143. // test for reserved wiki name
  144. err := AddWikiPage(doer, repo, "_edit", wikiContent, commitMsg)
  145. assert.Error(t, err)
  146. assert.True(t, models.IsErrWikiReservedName(err))
  147. })
  148. }
  149. func TestRepository_EditWikiPage(t *testing.T) {
  150. const newWikiContent = "This is the new content"
  151. const commitMsg = "Commit message"
  152. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
  153. doer := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  154. for _, newWikiName := range []string{
  155. "Home", // same name as before
  156. "New home",
  157. "New/name/with/slashes",
  158. } {
  159. models.PrepareTestEnv(t)
  160. assert.NoError(t, EditWikiPage(doer, repo, "Home", newWikiName, newWikiContent, commitMsg))
  161. // Now need to show that the page has been added:
  162. gitRepo, err := git.OpenRepository(repo.WikiPath())
  163. assert.NoError(t, err)
  164. masterTree, err := gitRepo.GetTree("master")
  165. assert.NoError(t, err)
  166. wikiPath := NameToFilename(newWikiName)
  167. entry, err := masterTree.GetTreeEntryByPath(wikiPath)
  168. assert.NoError(t, err)
  169. assert.Equal(t, wikiPath, entry.Name(), "%s not editted correctly", newWikiName)
  170. if newWikiName != "Home" {
  171. _, err := masterTree.GetTreeEntryByPath("Home.md")
  172. assert.Error(t, err)
  173. }
  174. gitRepo.Close()
  175. }
  176. }
  177. func TestRepository_DeleteWikiPage(t *testing.T) {
  178. models.PrepareTestEnv(t)
  179. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
  180. doer := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  181. assert.NoError(t, DeleteWikiPage(doer, repo, "Home"))
  182. // Now need to show that the page has been added:
  183. gitRepo, err := git.OpenRepository(repo.WikiPath())
  184. assert.NoError(t, err)
  185. defer gitRepo.Close()
  186. masterTree, err := gitRepo.GetTree("master")
  187. assert.NoError(t, err)
  188. wikiPath := NameToFilename("Home")
  189. _, err = masterTree.GetTreeEntryByPath(wikiPath)
  190. assert.Error(t, err)
  191. }