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.

219 lines
6.5 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"
  7. "path/filepath"
  8. "testing"
  9. "code.gitea.io/gitea/modules/setting"
  10. "github.com/Unknwon/com"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. func TestNormalizeWikiName(t *testing.T) {
  14. type test struct {
  15. Expected string
  16. WikiName string
  17. }
  18. for _, test := range []test{
  19. {"wiki name", "wiki name"},
  20. {"wiki name", "wiki-name"},
  21. {"name with/slash", "name with/slash"},
  22. {"name with%percent", "name-with%percent"},
  23. {"%2F", "%2F"},
  24. } {
  25. assert.Equal(t, test.Expected, NormalizeWikiName(test.WikiName))
  26. }
  27. }
  28. func TestWikiNameToFilename(t *testing.T) {
  29. type test struct {
  30. Expected string
  31. WikiName string
  32. }
  33. for _, test := range []test{
  34. {"wiki-name.md", "wiki name"},
  35. {"wiki-name.md", "wiki-name"},
  36. {"name-with%2Fslash.md", "name with/slash"},
  37. {"name-with%25percent.md", "name with%percent"},
  38. } {
  39. assert.Equal(t, test.Expected, WikiNameToFilename(test.WikiName))
  40. }
  41. }
  42. func TestWikiNameToSubURL(t *testing.T) {
  43. type test struct {
  44. Expected string
  45. WikiName string
  46. }
  47. for _, test := range []test{
  48. {"wiki-name", "wiki name"},
  49. {"wiki-name", "wiki-name"},
  50. {"name-with%2Fslash", "name with/slash"},
  51. {"name-with%25percent", "name with%percent"},
  52. } {
  53. assert.Equal(t, test.Expected, WikiNameToSubURL(test.WikiName))
  54. }
  55. }
  56. func TestWikiFilenameToName(t *testing.T) {
  57. type test struct {
  58. Expected string
  59. Filename string
  60. }
  61. for _, test := range []test{
  62. {"hello world", "hello-world.md"},
  63. {"symbols/?*", "symbols%2F%3F%2A.md"},
  64. } {
  65. name, err := WikiFilenameToName(test.Filename)
  66. assert.NoError(t, err)
  67. assert.Equal(t, test.Expected, name)
  68. }
  69. for _, badFilename := range []string{
  70. "nofileextension",
  71. "wrongfileextension.txt",
  72. } {
  73. _, err := WikiFilenameToName(badFilename)
  74. assert.Error(t, err)
  75. assert.True(t, IsErrWikiInvalidFileName(err))
  76. }
  77. _, err := WikiFilenameToName("badescaping%%.md")
  78. assert.Error(t, err)
  79. assert.False(t, IsErrWikiInvalidFileName(err))
  80. }
  81. func TestWikiNameToFilenameToName(t *testing.T) {
  82. // converting from wiki name to filename, then back to wiki name should
  83. // return the original (normalized) name
  84. for _, name := range []string{
  85. "wiki-name",
  86. "wiki name",
  87. "wiki name with/slash",
  88. "$$$%%%^^&&!@#$(),.<>",
  89. } {
  90. filename := WikiNameToFilename(name)
  91. resultName, err := WikiFilenameToName(filename)
  92. assert.NoError(t, err)
  93. assert.Equal(t, NormalizeWikiName(name), resultName)
  94. }
  95. }
  96. func TestRepository_WikiCloneLink(t *testing.T) {
  97. assert.NoError(t, PrepareTestDatabase())
  98. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  99. cloneLink := repo.WikiCloneLink()
  100. assert.Equal(t, "ssh://runuser@try.gitea.io:3000/user2/repo1.wiki.git", cloneLink.SSH)
  101. assert.Equal(t, "https://try.gitea.io/user2/repo1.wiki.git", cloneLink.HTTPS)
  102. }
  103. func TestWikiPath(t *testing.T) {
  104. assert.NoError(t, PrepareTestDatabase())
  105. expected := filepath.Join(setting.RepoRootPath, "user2/repo1.wiki.git")
  106. assert.Equal(t, expected, WikiPath("user2", "repo1"))
  107. }
  108. func TestRepository_WikiPath(t *testing.T) {
  109. assert.NoError(t, PrepareTestDatabase())
  110. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  111. expected := filepath.Join(setting.RepoRootPath, "user2/repo1.wiki.git")
  112. assert.Equal(t, expected, repo.WikiPath())
  113. }
  114. func TestRepository_HasWiki(t *testing.T) {
  115. PrepareTestEnv(t)
  116. repo1 := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  117. assert.True(t, repo1.HasWiki())
  118. repo2 := AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository)
  119. assert.False(t, repo2.HasWiki())
  120. }
  121. func TestRepository_InitWiki(t *testing.T) {
  122. PrepareTestEnv(t)
  123. // repo1 already has a wiki
  124. repo1 := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  125. assert.NoError(t, repo1.InitWiki())
  126. // repo2 does not already have a wiki
  127. repo2 := AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository)
  128. assert.NoError(t, repo2.InitWiki())
  129. assert.True(t, repo2.HasWiki())
  130. }
  131. func TestRepository_LocalWikiPath(t *testing.T) {
  132. PrepareTestEnv(t)
  133. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  134. expected := filepath.Join(setting.AppDataPath, setting.Repository.Local.LocalWikiPath, "1")
  135. assert.Equal(t, expected, repo.LocalWikiPath())
  136. }
  137. func TestRepository_AddWikiPage(t *testing.T) {
  138. assert.NoError(t, PrepareTestDatabase())
  139. const wikiContent = "This is the wiki content"
  140. const commitMsg = "Commit message"
  141. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  142. doer := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  143. for _, wikiName := range []string{
  144. "Another page",
  145. "Here's a <tag> and a/slash",
  146. } {
  147. wikiName := wikiName
  148. t.Run("test wiki exist: "+wikiName, func(t *testing.T) {
  149. t.Parallel()
  150. assert.NoError(t, repo.AddWikiPage(doer, wikiName, wikiContent, commitMsg))
  151. expectedPath := path.Join(repo.LocalWikiPath(), WikiNameToFilename(wikiName))
  152. assert.True(t, com.IsExist(expectedPath))
  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. newPath := path.Join(repo.LocalWikiPath(), WikiNameToFilename(newWikiName))
  183. assert.True(t, com.IsExist(newPath))
  184. if newWikiName != "Home" {
  185. oldPath := path.Join(repo.LocalWikiPath(), "Home.md")
  186. assert.False(t, com.IsExist(oldPath))
  187. }
  188. }
  189. }
  190. func TestRepository_DeleteWikiPage(t *testing.T) {
  191. PrepareTestEnv(t)
  192. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  193. doer := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  194. assert.NoError(t, repo.DeleteWikiPage(doer, "Home"))
  195. wikiPath := path.Join(repo.LocalWikiPath(), "Home.md")
  196. assert.False(t, com.IsExist(wikiPath))
  197. }