You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

wiki_test.go 5.6 kB

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