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.

211 lines
6.3 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. const wikiContent = "This is the wiki content"
  139. const commitMsg = "Commit message"
  140. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  141. doer := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  142. for _, wikiName := range []string{
  143. "Another page",
  144. "Here's a <tag> and a/slash",
  145. } {
  146. PrepareTestEnv(t)
  147. assert.NoError(t, repo.AddWikiPage(doer, wikiName, wikiContent, commitMsg))
  148. expectedPath := path.Join(repo.LocalWikiPath(), WikiNameToFilename(wikiName))
  149. assert.True(t, com.IsExist(expectedPath))
  150. }
  151. // test for already-existing wiki name
  152. PrepareTestEnv(t)
  153. err := repo.AddWikiPage(doer, "Home", wikiContent, commitMsg)
  154. assert.Error(t, err)
  155. assert.True(t, IsErrWikiAlreadyExist(err))
  156. // test for reserved wiki name
  157. PrepareTestEnv(t)
  158. err = repo.AddWikiPage(doer, "_edit", wikiContent, commitMsg)
  159. assert.Error(t, err)
  160. assert.True(t, IsErrWikiReservedName(err))
  161. }
  162. func TestRepository_EditWikiPage(t *testing.T) {
  163. const newWikiContent = "This is the new content"
  164. const commitMsg = "Commit message"
  165. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  166. doer := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  167. for _, newWikiName := range []string{
  168. "Home", // same name as before
  169. "New home",
  170. "New/name/with/slashes",
  171. } {
  172. PrepareTestEnv(t)
  173. assert.NoError(t, repo.EditWikiPage(doer, "Home", newWikiName, newWikiContent, commitMsg))
  174. newPath := path.Join(repo.LocalWikiPath(), WikiNameToFilename(newWikiName))
  175. assert.True(t, com.IsExist(newPath))
  176. if newWikiName != "Home" {
  177. oldPath := path.Join(repo.LocalWikiPath(), "Home.md")
  178. assert.False(t, com.IsExist(oldPath))
  179. }
  180. }
  181. }
  182. func TestRepository_DeleteWikiPage(t *testing.T) {
  183. PrepareTestEnv(t)
  184. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  185. doer := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  186. assert.NoError(t, repo.DeleteWikiPage(doer, "Home"))
  187. wikiPath := path.Join(repo.LocalWikiPath(), "Home.md")
  188. assert.False(t, com.IsExist(wikiPath))
  189. }