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.

137 lines
5.9 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. imgurl := util.URLJoin(rawtree, "Link.jpg")
  69. otherImgurl := util.URLJoin(rawtree, "Link+Other.jpg")
  70. urlWiki := util.URLJoin(AppSubURL, "wiki", "Link")
  71. otherURLWiki := util.URLJoin(AppSubURL, "wiki", "Other-Link")
  72. imgurlWiki := util.URLJoin(AppSubURL, "wiki", "raw", "Link.jpg")
  73. otherImgurlWiki := util.URLJoin(AppSubURL, "wiki", "raw", "Link+Other.jpg")
  74. favicon := "http://google.com/favicon.ico"
  75. test(
  76. "[[Link]]",
  77. `<p><a href="`+url+`" rel="nofollow">Link</a></p>`,
  78. `<p><a href="`+urlWiki+`" rel="nofollow">Link</a></p>`)
  79. test(
  80. "[[Link.jpg]]",
  81. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Link.jpg" alt="Link.jpg"/></a></p>`,
  82. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="Link.jpg" alt="Link.jpg"/></a></p>`)
  83. test(
  84. "[["+favicon+"]]",
  85. `<p><a href="`+favicon+`" rel="nofollow"><img src="`+favicon+`" title="favicon.ico"/></a></p>`,
  86. `<p><a href="`+favicon+`" rel="nofollow"><img src="`+favicon+`" title="favicon.ico"/></a></p>`)
  87. test(
  88. "[[Name|Link]]",
  89. `<p><a href="`+url+`" rel="nofollow">Name</a></p>`,
  90. `<p><a href="`+urlWiki+`" rel="nofollow">Name</a></p>`)
  91. test(
  92. "[[Name|Link.jpg]]",
  93. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Name" alt="Name"/></a></p>`,
  94. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="Name" alt="Name"/></a></p>`)
  95. test(
  96. "[[Name|Link.jpg|alt=AltName]]",
  97. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="AltName" alt="AltName"/></a></p>`,
  98. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="AltName" alt="AltName"/></a></p>`)
  99. test(
  100. "[[Name|Link.jpg|title=Title]]",
  101. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Title" alt="Title"/></a></p>`,
  102. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="Title" alt="Title"/></a></p>`)
  103. test(
  104. "[[Name|Link.jpg|alt=AltName|title=Title]]",
  105. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Title" alt="AltName"/></a></p>`,
  106. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="Title" alt="AltName"/></a></p>`)
  107. test(
  108. "[[Name|Link.jpg|alt=\"AltName\"|title='Title']]",
  109. `<p><a href="`+imgurl+`" rel="nofollow"><img src="`+imgurl+`" title="Title" alt="AltName"/></a></p>`,
  110. `<p><a href="`+imgurlWiki+`" rel="nofollow"><img src="`+imgurlWiki+`" title="Title" alt="AltName"/></a></p>`)
  111. test(
  112. "[[Name|Link Other.jpg|alt=\"AltName\"|title='Title']]",
  113. `<p><a href="`+otherImgurl+`" rel="nofollow"><img src="`+otherImgurl+`" title="Title" alt="AltName"/></a></p>`,
  114. `<p><a href="`+otherImgurlWiki+`" rel="nofollow"><img src="`+otherImgurlWiki+`" title="Title" alt="AltName"/></a></p>`)
  115. test(
  116. "[[Link]] [[Other Link]]",
  117. `<p><a href="`+url+`" rel="nofollow">Link</a> <a href="`+otherURL+`" rel="nofollow">Other Link</a></p>`,
  118. `<p><a href="`+urlWiki+`" rel="nofollow">Link</a> <a href="`+otherURLWiki+`" rel="nofollow">Other Link</a></p>`)
  119. }