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.

163 lines
7.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 markup_test
  5. import (
  6. "strings"
  7. "testing"
  8. . "code.gitea.io/gitea/modules/markup"
  9. "code.gitea.io/gitea/modules/markup/markdown"
  10. "code.gitea.io/gitea/modules/setting"
  11. "code.gitea.io/gitea/modules/util"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. func TestRender_Commits(t *testing.T) {
  15. setting.AppURL = AppURL
  16. setting.AppSubURL = AppSubURL
  17. test := func(input, expected string) {
  18. buffer := RenderString(".md", input, setting.AppSubURL, nil)
  19. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
  20. }
  21. var sha = "b6dd6210eaebc915fd5be5579c58cce4da2e2579"
  22. var commit = util.URLJoin(AppSubURL, "commit", sha)
  23. var subtree = util.URLJoin(commit, "src")
  24. var tree = strings.Replace(subtree, "/commit/", "/tree/", -1)
  25. test(sha, `<p><a href="`+commit+`" rel="nofollow">b6dd6210ea</a></p>`)
  26. test(sha[:7], `<p><a href="`+commit[:len(commit)-(40-7)]+`" rel="nofollow">b6dd621</a></p>`)
  27. test(sha[:39], `<p><a href="`+commit[:len(commit)-(40-39)]+`" rel="nofollow">b6dd6210ea</a></p>`)
  28. test(commit, `<p><a href="`+commit+`" rel="nofollow">b6dd6210ea</a></p>`)
  29. test(tree, `<p><a href="`+tree+`" rel="nofollow">b6dd6210ea/src</a></p>`)
  30. test("commit "+sha, `<p>commit <a href="`+commit+`" rel="nofollow">b6dd6210ea</a></p>`)
  31. }
  32. func TestRender_CrossReferences(t *testing.T) {
  33. setting.AppURL = AppURL
  34. setting.AppSubURL = AppSubURL
  35. test := func(input, expected string) {
  36. buffer := RenderString("a.md", input, setting.AppSubURL, nil)
  37. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
  38. }
  39. test(
  40. "gogits/gogs#12345",
  41. `<p><a href="`+util.URLJoin(AppURL, "gogits", "gogs", "issues", "12345")+`" rel="nofollow">gogits/gogs#12345</a></p>`)
  42. test(
  43. "go-gitea/gitea#12345",
  44. `<p><a href="`+util.URLJoin(AppURL, "go-gitea", "gitea", "issues", "12345")+`" rel="nofollow">go-gitea/gitea#12345</a></p>`)
  45. }
  46. func TestMisc_IsSameDomain(t *testing.T) {
  47. setting.AppURL = AppURL
  48. setting.AppSubURL = AppSubURL
  49. var sha = "b6dd6210eaebc915fd5be5579c58cce4da2e2579"
  50. var commit = util.URLJoin(AppSubURL, "commit", sha)
  51. assert.True(t, IsSameDomain(commit))
  52. assert.False(t, IsSameDomain("http://google.com/ncr"))
  53. assert.False(t, IsSameDomain("favicon.ico"))
  54. }
  55. func TestRender_ShortLinks(t *testing.T) {
  56. setting.AppURL = AppURL
  57. setting.AppSubURL = AppSubURL
  58. tree := util.URLJoin(AppSubURL, "src", "master")
  59. test := func(input, expected, expectedWiki string) {
  60. buffer := markdown.RenderString(input, tree, nil)
  61. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
  62. buffer = markdown.RenderWiki([]byte(input), setting.AppSubURL, nil)
  63. assert.Equal(t, strings.TrimSpace(expectedWiki), strings.TrimSpace(string(buffer)))
  64. }
  65. rawtree := util.URLJoin(AppSubURL, "raw", "master")
  66. url := util.URLJoin(tree, "Link")
  67. otherURL := util.URLJoin(tree, "Other-Link")
  68. encodedURL := util.URLJoin(tree, "Link%3F")
  69. imgurl := util.URLJoin(rawtree, "Link.jpg")
  70. otherImgurl := util.URLJoin(rawtree, "Link+Other.jpg")
  71. encodedImgurl := util.URLJoin(rawtree, "Link+%23.jpg")
  72. notencodedImgurl := util.URLJoin(rawtree, "some", "path", "Link+#.jpg")
  73. urlWiki := util.URLJoin(AppSubURL, "wiki", "Link")
  74. otherURLWiki := util.URLJoin(AppSubURL, "wiki", "Other-Link")
  75. encodedURLWiki := util.URLJoin(AppSubURL, "wiki", "Link%3F")
  76. imgurlWiki := util.URLJoin(AppSubURL, "wiki", "raw", "Link.jpg")
  77. otherImgurlWiki := util.URLJoin(AppSubURL, "wiki", "raw", "Link+Other.jpg")
  78. encodedImgurlWiki := util.URLJoin(AppSubURL, "wiki", "raw", "Link+%23.jpg")
  79. notencodedImgurlWiki := util.URLJoin(AppSubURL, "wiki", "raw", "some", "path", "Link+#.jpg")
  80. favicon := "http://google.com/favicon.ico"
  81. test(
  82. "[[Link]]",
  83. `<p><a href="`+url+`" rel="nofollow">Link</a></p>`,
  84. `<p><a href="`+urlWiki+`" rel="nofollow">Link</a></p>`)
  85. test(
  86. "[[Link.jpg]]",
  87. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Link.jpg" alt="Link.jpg"/></a></p>`,
  88. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="Link.jpg" alt="Link.jpg"/></a></p>`)
  89. test(
  90. "[["+favicon+"]]",
  91. `<p><a href="`+favicon+`" rel="nofollow"><img src="`+favicon+`" title="favicon.ico"/></a></p>`,
  92. `<p><a href="`+favicon+`" rel="nofollow"><img src="`+favicon+`" title="favicon.ico"/></a></p>`)
  93. test(
  94. "[[Name|Link]]",
  95. `<p><a href="`+url+`" rel="nofollow">Name</a></p>`,
  96. `<p><a href="`+urlWiki+`" rel="nofollow">Name</a></p>`)
  97. test(
  98. "[[Name|Link.jpg]]",
  99. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Name" alt="Name"/></a></p>`,
  100. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="Name" alt="Name"/></a></p>`)
  101. test(
  102. "[[Name|Link.jpg|alt=AltName]]",
  103. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="AltName" alt="AltName"/></a></p>`,
  104. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="AltName" alt="AltName"/></a></p>`)
  105. test(
  106. "[[Name|Link.jpg|title=Title]]",
  107. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Title" alt="Title"/></a></p>`,
  108. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="Title" alt="Title"/></a></p>`)
  109. test(
  110. "[[Name|Link.jpg|alt=AltName|title=Title]]",
  111. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Title" alt="AltName"/></a></p>`,
  112. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="Title" alt="AltName"/></a></p>`)
  113. test(
  114. "[[Name|Link.jpg|alt=\"AltName\"|title='Title']]",
  115. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Title" alt="AltName"/></a></p>`,
  116. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="Title" alt="AltName"/></a></p>`)
  117. test(
  118. "[[Name|Link Other.jpg|alt=\"AltName\"|title='Title']]",
  119. `<p><a href="`+otherImgurl+`" rel="nofollow"><img src="`+otherImgurl+`" title="Title" alt="AltName"/></a></p>`,
  120. `<p><a href="`+otherImgurlWiki+`" rel="nofollow"><img src="`+otherImgurlWiki+`" title="Title" alt="AltName"/></a></p>`)
  121. test(
  122. "[[Link]] [[Other Link]]",
  123. `<p><a href="`+url+`" rel="nofollow">Link</a> <a href="`+otherURL+`" rel="nofollow">Other Link</a></p>`,
  124. `<p><a href="`+urlWiki+`" rel="nofollow">Link</a> <a href="`+otherURLWiki+`" rel="nofollow">Other Link</a></p>`)
  125. test(
  126. "[[Link?]]",
  127. `<p><a href="`+encodedURL+`" rel="nofollow">Link?</a></p>`,
  128. `<p><a href="`+encodedURLWiki+`" rel="nofollow">Link?</a></p>`)
  129. test(
  130. "[[Link]] [[Other Link]] [[Link?]]",
  131. `<p><a href="`+url+`" rel="nofollow">Link</a> <a href="`+otherURL+`" rel="nofollow">Other Link</a> <a href="`+encodedURL+`" rel="nofollow">Link?</a></p>`,
  132. `<p><a href="`+urlWiki+`" rel="nofollow">Link</a> <a href="`+otherURLWiki+`" rel="nofollow">Other Link</a> <a href="`+encodedURLWiki+`" rel="nofollow">Link?</a></p>`)
  133. test(
  134. "[[Link #.jpg]]",
  135. `<p><a href="`+encodedImgurl+`" rel="nofollow"><img src="`+encodedImgurl+`"/></a></p>`,
  136. `<p><a href="`+encodedImgurlWiki+`" rel="nofollow"><img src="`+encodedImgurlWiki+`"/></a></p>`)
  137. test(
  138. "[[Name|Link #.jpg|alt=\"AltName\"|title='Title']]",
  139. `<p><a href="`+encodedImgurl+`" rel="nofollow"><img src="`+encodedImgurl+`" title="Title" alt="AltName"/></a></p>`,
  140. `<p><a href="`+encodedImgurlWiki+`" rel="nofollow"><img src="`+encodedImgurlWiki+`" title="Title" alt="AltName"/></a></p>`)
  141. test(
  142. "[[some/path/Link #.jpg]]",
  143. `<p><a href="`+notencodedImgurl+`" rel="nofollow"><img src="`+notencodedImgurl+`"/></a></p>`,
  144. `<p><a href="`+notencodedImgurlWiki+`" rel="nofollow"><img src="`+notencodedImgurlWiki+`"/></a></p>`)
  145. }