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.

wiki_test.go 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. "badescaping%%.md",
  73. } {
  74. _, err := WikiFilenameToName(badFilename)
  75. assert.Error(t, err)
  76. }
  77. }
  78. func TestWikiNameToFilenameToName(t *testing.T) {
  79. // converting from wiki name to filename, then back to wiki name should
  80. // return the original (normalized) name
  81. for _, name := range []string{
  82. "wiki-name",
  83. "wiki name",
  84. "wiki name with/slash",
  85. "$$$%%%^^&&!@#$(),.<>",
  86. } {
  87. filename := WikiNameToFilename(name)
  88. resultName, err := WikiFilenameToName(filename)
  89. assert.NoError(t, err)
  90. assert.Equal(t, NormalizeWikiName(name), resultName)
  91. }
  92. }
  93. func TestRepository_WikiCloneLink(t *testing.T) {
  94. assert.NoError(t, PrepareTestDatabase())
  95. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  96. cloneLink := repo.WikiCloneLink()
  97. assert.Equal(t, "ssh://runuser@try.gitea.io:3000/user2/repo1.wiki.git", cloneLink.SSH)
  98. assert.Equal(t, "https://try.gitea.io/user2/repo1.wiki.git", cloneLink.HTTPS)
  99. }
  100. func TestWikiPath(t *testing.T) {
  101. assert.NoError(t, PrepareTestDatabase())
  102. expected := filepath.Join(setting.RepoRootPath, "user2/repo1.wiki.git")
  103. assert.Equal(t, expected, WikiPath("user2", "repo1"))
  104. }
  105. func TestRepository_WikiPath(t *testing.T) {
  106. assert.NoError(t, PrepareTestDatabase())
  107. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  108. expected := filepath.Join(setting.RepoRootPath, "user2/repo1.wiki.git")
  109. assert.Equal(t, expected, repo.WikiPath())
  110. }
  111. func TestRepository_HasWiki(t *testing.T) {
  112. PrepareTestEnv(t)
  113. repo1 := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  114. assert.True(t, repo1.HasWiki())
  115. repo2 := AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository)
  116. assert.False(t, repo2.HasWiki())
  117. }
  118. func TestRepository_InitWiki(t *testing.T) {
  119. PrepareTestEnv(t)
  120. // repo1 already has a wiki
  121. repo1 := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  122. assert.NoError(t, repo1.InitWiki())
  123. // repo2 does not already have a wiki
  124. repo2 := AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository)
  125. assert.NoError(t, repo2.InitWiki())
  126. assert.True(t, repo2.HasWiki())
  127. }
  128. func TestRepository_LocalWikiPath(t *testing.T) {
  129. PrepareTestEnv(t)
  130. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  131. expected := filepath.Join(setting.AppDataPath, "tmp/local-wiki/1")
  132. assert.Equal(t, expected, repo.LocalWikiPath())
  133. }
  134. func TestRepository_AddWikiPage(t *testing.T) {
  135. const wikiContent = "This is the wiki content"
  136. const commitMsg = "Commit message"
  137. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  138. doer := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  139. for _, wikiName := range []string{
  140. "Another page",
  141. "Here's a <tag> and a/slash",
  142. } {
  143. PrepareTestEnv(t)
  144. assert.NoError(t, repo.AddWikiPage(doer, wikiName, wikiContent, commitMsg))
  145. expectedPath := path.Join(repo.LocalWikiPath(), WikiNameToFilename(wikiName))
  146. assert.True(t, com.IsExist(expectedPath))
  147. }
  148. // test for already-existing wiki name
  149. PrepareTestEnv(t)
  150. err := repo.AddWikiPage(doer, "Home", wikiContent, commitMsg)
  151. assert.Error(t, err)
  152. assert.True(t, IsErrWikiAlreadyExist(err))
  153. // test for reserved wiki name
  154. PrepareTestEnv(t)
  155. err = repo.AddWikiPage(doer, "_edit", wikiContent, commitMsg)
  156. assert.Error(t, err)
  157. assert.True(t, IsErrWikiReservedName(err))
  158. }
  159. func TestRepository_EditWikiPage(t *testing.T) {
  160. const newWikiContent = "This is the new content"
  161. const commitMsg = "Commit message"
  162. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  163. doer := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  164. for _, newWikiName := range []string{
  165. "Home", // same name as before
  166. "New home",
  167. "New/name/with/slashes",
  168. } {
  169. PrepareTestEnv(t)
  170. assert.NoError(t, repo.EditWikiPage(doer, "Home", newWikiName, newWikiContent, commitMsg))
  171. newPath := path.Join(repo.LocalWikiPath(), WikiNameToFilename(newWikiName))
  172. assert.True(t, com.IsExist(newPath))
  173. if newWikiName != "Home" {
  174. oldPath := path.Join(repo.LocalWikiPath(), "Home.md")
  175. assert.False(t, com.IsExist(oldPath))
  176. }
  177. }
  178. }
  179. func TestRepository_DeleteWikiPage(t *testing.T) {
  180. PrepareTestEnv(t)
  181. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  182. doer := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  183. assert.NoError(t, repo.DeleteWikiPage(doer, "Home"))
  184. wikiPath := path.Join(repo.LocalWikiPath(), "Home.md")
  185. assert.False(t, com.IsExist(wikiPath))
  186. }