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.

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