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.

html_test.go 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. "fmt"
  7. "strconv"
  8. "strings"
  9. "testing"
  10. . "code.gitea.io/gitea/modules/markup"
  11. _ "code.gitea.io/gitea/modules/markup/markdown"
  12. "code.gitea.io/gitea/modules/setting"
  13. "github.com/stretchr/testify/assert"
  14. )
  15. const AppURL = "http://localhost:3000/"
  16. const Repo = "gogits/gogs"
  17. const AppSubURL = AppURL + Repo + "/"
  18. var numericMetas = map[string]string{
  19. "format": "https://someurl.com/{user}/{repo}/{index}",
  20. "user": "someUser",
  21. "repo": "someRepo",
  22. "style": IssueNameStyleNumeric,
  23. }
  24. var alphanumericMetas = map[string]string{
  25. "format": "https://someurl.com/{user}/{repo}/{index}",
  26. "user": "someUser",
  27. "repo": "someRepo",
  28. "style": IssueNameStyleAlphanumeric,
  29. }
  30. // numericLink an HTML to a numeric-style issue
  31. func numericIssueLink(baseURL string, index int) string {
  32. return link(URLJoin(baseURL, strconv.Itoa(index)), fmt.Sprintf("#%d", index))
  33. }
  34. // alphanumLink an HTML link to an alphanumeric-style issue
  35. func alphanumIssueLink(baseURL string, name string) string {
  36. return link(URLJoin(baseURL, name), name)
  37. }
  38. // urlContentsLink an HTML link whose contents is the target URL
  39. func urlContentsLink(href string) string {
  40. return link(href, href)
  41. }
  42. // link an HTML link
  43. func link(href, contents string) string {
  44. return fmt.Sprintf("<a href=\"%s\">%s</a>", href, contents)
  45. }
  46. func testRenderIssueIndexPattern(t *testing.T, input, expected string, opts RenderIssueIndexPatternOptions) {
  47. if len(opts.URLPrefix) == 0 {
  48. opts.URLPrefix = AppSubURL
  49. }
  50. actual := string(RenderIssueIndexPattern([]byte(input), opts))
  51. assert.Equal(t, expected, actual)
  52. }
  53. func TestURLJoin(t *testing.T) {
  54. type test struct {
  55. Expected string
  56. Base string
  57. Elements []string
  58. }
  59. newTest := func(expected, base string, elements ...string) test {
  60. return test{Expected: expected, Base: base, Elements: elements}
  61. }
  62. for _, test := range []test{
  63. newTest("https://try.gitea.io/a/b/c",
  64. "https://try.gitea.io", "a/b", "c"),
  65. newTest("https://try.gitea.io/a/b/c",
  66. "https://try.gitea.io/", "/a/b/", "/c/"),
  67. newTest("https://try.gitea.io/a/c",
  68. "https://try.gitea.io/", "/a/./b/", "../c/"),
  69. newTest("a/b/c",
  70. "a", "b/c/"),
  71. newTest("a/b/d",
  72. "a/", "b/c/", "/../d/"),
  73. } {
  74. assert.Equal(t, test.Expected, URLJoin(test.Base, test.Elements...))
  75. }
  76. }
  77. func TestRender_IssueIndexPattern(t *testing.T) {
  78. // numeric: render inputs without valid mentions
  79. test := func(s string) {
  80. testRenderIssueIndexPattern(t, s, s, RenderIssueIndexPatternOptions{})
  81. testRenderIssueIndexPattern(t, s, s, RenderIssueIndexPatternOptions{Metas: numericMetas})
  82. }
  83. // should not render anything when there are no mentions
  84. test("")
  85. test("this is a test")
  86. test("test 123 123 1234")
  87. test("#")
  88. test("# # #")
  89. test("# 123")
  90. test("#abcd")
  91. test("##1234")
  92. test("test#1234")
  93. test("#1234test")
  94. test(" test #1234test")
  95. // should not render issue mention without leading space
  96. test("test#54321 issue")
  97. // should not render issue mention without trailing space
  98. test("test #54321issue")
  99. }
  100. func TestRender_IssueIndexPattern2(t *testing.T) {
  101. setting.AppURL = AppURL
  102. setting.AppSubURL = AppSubURL
  103. // numeric: render inputs with valid mentions
  104. test := func(s, expectedFmt string, indices ...int) {
  105. links := make([]interface{}, len(indices))
  106. for i, index := range indices {
  107. links[i] = numericIssueLink(URLJoin(setting.AppSubURL, "issues"), index)
  108. }
  109. expectedNil := fmt.Sprintf(expectedFmt, links...)
  110. testRenderIssueIndexPattern(t, s, expectedNil, RenderIssueIndexPatternOptions{})
  111. for i, index := range indices {
  112. links[i] = numericIssueLink("https://someurl.com/someUser/someRepo/", index)
  113. }
  114. expectedNum := fmt.Sprintf(expectedFmt, links...)
  115. testRenderIssueIndexPattern(t, s, expectedNum, RenderIssueIndexPatternOptions{Metas: numericMetas})
  116. }
  117. // should render freestanding mentions
  118. test("#1234 test", "%s test", 1234)
  119. test("test #8 issue", "test %s issue", 8)
  120. test("test issue #1234", "test issue %s", 1234)
  121. // should render mentions in parentheses
  122. test("(#54321 issue)", "(%s issue)", 54321)
  123. test("test (#9801 extra) issue", "test (%s extra) issue", 9801)
  124. test("test (#1)", "test (%s)", 1)
  125. // should render multiple issue mentions in the same line
  126. test("#54321 #1243", "%s %s", 54321, 1243)
  127. test("wow (#54321 #1243)", "wow (%s %s)", 54321, 1243)
  128. test("(#4)(#5)", "(%s)(%s)", 4, 5)
  129. test("#1 (#4321) test", "%s (%s) test", 1, 4321)
  130. }
  131. func TestRender_IssueIndexPattern3(t *testing.T) {
  132. setting.AppURL = AppURL
  133. setting.AppSubURL = AppSubURL
  134. // alphanumeric: render inputs without valid mentions
  135. test := func(s string) {
  136. testRenderIssueIndexPattern(t, s, s, RenderIssueIndexPatternOptions{Metas: alphanumericMetas})
  137. }
  138. test("")
  139. test("this is a test")
  140. test("test 123 123 1234")
  141. test("#")
  142. test("##1234")
  143. test("# 123")
  144. test("#abcd")
  145. test("test #123")
  146. test("abc-1234") // issue prefix must be capital
  147. test("ABc-1234") // issue prefix must be _all_ capital
  148. test("ABCDEFGHIJK-1234") // the limit is 10 characters in the prefix
  149. test("ABC1234") // dash is required
  150. test("test ABC- test") // number is required
  151. test("test -1234 test") // prefix is required
  152. test("testABC-123 test") // leading space is required
  153. test("test ABC-123test") // trailing space is required
  154. test("ABC-0123") // no leading zero
  155. }
  156. func TestRender_IssueIndexPattern4(t *testing.T) {
  157. setting.AppURL = AppURL
  158. setting.AppSubURL = AppSubURL
  159. // alphanumeric: render inputs with valid mentions
  160. test := func(s, expectedFmt string, names ...string) {
  161. links := make([]interface{}, len(names))
  162. for i, name := range names {
  163. links[i] = alphanumIssueLink("https://someurl.com/someUser/someRepo/", name)
  164. }
  165. expected := fmt.Sprintf(expectedFmt, links...)
  166. testRenderIssueIndexPattern(t, s, expected, RenderIssueIndexPatternOptions{Metas: alphanumericMetas})
  167. }
  168. test("OTT-1234 test", "%s test", "OTT-1234")
  169. test("test T-12 issue", "test %s issue", "T-12")
  170. test("test issue ABCDEFGHIJ-1234567890", "test issue %s", "ABCDEFGHIJ-1234567890")
  171. }
  172. func TestRenderIssueIndexPatternWithDefaultURL(t *testing.T) {
  173. setting.AppURL = AppURL
  174. setting.AppSubURL = AppSubURL
  175. test := func(input string, expected string) {
  176. testRenderIssueIndexPattern(t, input, expected, RenderIssueIndexPatternOptions{
  177. DefaultURL: AppURL,
  178. })
  179. }
  180. test("hello #123 world",
  181. fmt.Sprintf(`<a rel="nofollow" href="%s">hello</a> `, AppURL)+
  182. fmt.Sprintf(`<a href="%sissues/123">#123</a> `, AppSubURL)+
  183. fmt.Sprintf(`<a rel="nofollow" href="%s">world</a>`, AppURL))
  184. test("hello (#123) world",
  185. fmt.Sprintf(`<a rel="nofollow" href="%s">hello </a>`, AppURL)+
  186. fmt.Sprintf(`(<a href="%sissues/123">#123</a>)`, AppSubURL)+
  187. fmt.Sprintf(`<a rel="nofollow" href="%s"> world</a>`, AppURL))
  188. }
  189. func TestRender_AutoLink(t *testing.T) {
  190. setting.AppURL = AppURL
  191. setting.AppSubURL = AppSubURL
  192. test := func(input, expected string) {
  193. buffer := RenderSpecialLink([]byte(input), setting.AppSubURL, nil, false)
  194. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
  195. buffer = RenderSpecialLink([]byte(input), setting.AppSubURL, nil, true)
  196. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
  197. }
  198. // render valid issue URLs
  199. test(URLJoin(setting.AppSubURL, "issues", "3333"),
  200. numericIssueLink(URLJoin(setting.AppSubURL, "issues"), 3333))
  201. // render external issue URLs
  202. for _, externalURL := range []string{
  203. "http://1111/2222/ssss-issues/3333?param=blah&blahh=333",
  204. "http://test.com/issues/33333",
  205. "https://issues/333"} {
  206. test(externalURL, externalURL)
  207. }
  208. // render valid commit URLs
  209. tmp := URLJoin(AppSubURL, "commit", "d8a994ef243349f321568f9e36d5c3f444b99cae")
  210. test(tmp, "<a href=\""+tmp+"\">d8a994ef24</a>")
  211. tmp += "#diff-2"
  212. test(tmp, "<a href=\""+tmp+"\">d8a994ef24 (diff-2)</a>")
  213. // render other commit URLs
  214. tmp = "https://external-link.gogs.io/gogs/gogs/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2"
  215. test(tmp, "<a href=\""+tmp+"\">d8a994ef24 (diff-2)</a>")
  216. }
  217. func TestRender_Commits(t *testing.T) {
  218. setting.AppURL = AppURL
  219. setting.AppSubURL = AppSubURL
  220. test := func(input, expected string) {
  221. buffer := RenderString(".md", input, setting.AppSubURL, nil)
  222. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
  223. }
  224. var sha = "b6dd6210eaebc915fd5be5579c58cce4da2e2579"
  225. var commit = URLJoin(AppSubURL, "commit", sha)
  226. var subtree = URLJoin(commit, "src")
  227. var tree = strings.Replace(subtree, "/commit/", "/tree/", -1)
  228. var src = strings.Replace(subtree, "/commit/", "/src/", -1)
  229. test(sha, `<p><a href="`+commit+`" rel="nofollow">b6dd6210ea</a></p>`)
  230. test(sha[:7], `<p><a href="`+commit[:len(commit)-(40-7)]+`" rel="nofollow">b6dd621</a></p>`)
  231. test(sha[:39], `<p><a href="`+commit[:len(commit)-(40-39)]+`" rel="nofollow">b6dd6210ea</a></p>`)
  232. test(commit, `<p><a href="`+commit+`" rel="nofollow">b6dd6210ea</a></p>`)
  233. test(tree, `<p><a href="`+src+`" rel="nofollow">b6dd6210ea/src</a></p>`)
  234. test("commit "+sha, `<p>commit <a href="`+commit+`" rel="nofollow">b6dd6210ea</a></p>`)
  235. }
  236. func TestRender_CrossReferences(t *testing.T) {
  237. setting.AppURL = AppURL
  238. setting.AppSubURL = AppSubURL
  239. test := func(input, expected string) {
  240. buffer := RenderString("a.md", input, setting.AppSubURL, nil)
  241. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
  242. }
  243. test(
  244. "gogits/gogs#12345",
  245. `<p><a href="`+URLJoin(AppURL, "gogits", "gogs", "issues", "12345")+`" rel="nofollow">gogits/gogs#12345</a></p>`)
  246. }
  247. func TestRender_FullIssueURLs(t *testing.T) {
  248. setting.AppURL = AppURL
  249. setting.AppSubURL = AppSubURL
  250. test := func(input, expected string) {
  251. result := RenderFullIssuePattern([]byte(input))
  252. assert.Equal(t, expected, string(result))
  253. }
  254. test("Here is a link https://git.osgeo.org/gogs/postgis/postgis/pulls/6",
  255. "Here is a link https://git.osgeo.org/gogs/postgis/postgis/pulls/6")
  256. test("Look here http://localhost:3000/person/repo/issues/4",
  257. `Look here <a href="http://localhost:3000/person/repo/issues/4">#4</a>`)
  258. test("http://localhost:3000/person/repo/issues/4#issuecomment-1234",
  259. `<a href="http://localhost:3000/person/repo/issues/4#issuecomment-1234">#4</a>`)
  260. }
  261. func TestRegExp_MentionPattern(t *testing.T) {
  262. trueTestCases := []string{
  263. "@Unknwon",
  264. "@ANT_123",
  265. "@xxx-DiN0-z-A..uru..s-xxx",
  266. " @lol ",
  267. " @Te/st",
  268. }
  269. falseTestCases := []string{
  270. "@ 0",
  271. "@ ",
  272. "@",
  273. "",
  274. "ABC",
  275. }
  276. for _, testCase := range trueTestCases {
  277. res := MentionPattern.MatchString(testCase)
  278. if !res {
  279. println()
  280. println(testCase)
  281. }
  282. assert.True(t, res)
  283. }
  284. for _, testCase := range falseTestCases {
  285. res := MentionPattern.MatchString(testCase)
  286. if res {
  287. println()
  288. println(testCase)
  289. }
  290. assert.False(t, res)
  291. }
  292. }
  293. func TestRegExp_IssueNumericPattern(t *testing.T) {
  294. trueTestCases := []string{
  295. "#1234",
  296. "#0",
  297. "#1234567890987654321",
  298. }
  299. falseTestCases := []string{
  300. "# 1234",
  301. "# 0",
  302. "# ",
  303. "#",
  304. "#ABC",
  305. "#1A2B",
  306. "",
  307. "ABC",
  308. }
  309. for _, testCase := range trueTestCases {
  310. assert.True(t, IssueNumericPattern.MatchString(testCase))
  311. }
  312. for _, testCase := range falseTestCases {
  313. assert.False(t, IssueNumericPattern.MatchString(testCase))
  314. }
  315. }
  316. func TestRegExp_IssueAlphanumericPattern(t *testing.T) {
  317. trueTestCases := []string{
  318. "ABC-1234",
  319. "A-1",
  320. "RC-80",
  321. "ABCDEFGHIJ-1234567890987654321234567890",
  322. }
  323. falseTestCases := []string{
  324. "RC-08",
  325. "PR-0",
  326. "ABCDEFGHIJK-1",
  327. "PR_1",
  328. "",
  329. "#ABC",
  330. "",
  331. "ABC",
  332. "GG-",
  333. "rm-1",
  334. }
  335. for _, testCase := range trueTestCases {
  336. assert.True(t, IssueAlphanumericPattern.MatchString(testCase))
  337. }
  338. for _, testCase := range falseTestCases {
  339. assert.False(t, IssueAlphanumericPattern.MatchString(testCase))
  340. }
  341. }
  342. func TestRegExp_Sha1CurrentPattern(t *testing.T) {
  343. trueTestCases := []string{
  344. "d8a994ef243349f321568f9e36d5c3f444b99cae",
  345. "abcdefabcdefabcdefabcdefabcdefabcdefabcd",
  346. }
  347. falseTestCases := []string{
  348. "test",
  349. "abcdefg",
  350. "abcdefghijklmnopqrstuvwxyzabcdefghijklmn",
  351. "abcdefghijklmnopqrstuvwxyzabcdefghijklmO",
  352. }
  353. for _, testCase := range trueTestCases {
  354. assert.True(t, Sha1CurrentPattern.MatchString(testCase))
  355. }
  356. for _, testCase := range falseTestCases {
  357. assert.False(t, Sha1CurrentPattern.MatchString(testCase))
  358. }
  359. }
  360. func TestRegExp_AnySHA1Pattern(t *testing.T) {
  361. testCases := map[string][]string{
  362. "https://github.com/jquery/jquery/blob/a644101ed04d0beacea864ce805e0c4f86ba1cd1/test/unit/event.js#L2703": {
  363. "https",
  364. "github.com",
  365. "jquery",
  366. "jquery",
  367. "blob",
  368. "a644101ed04d0beacea864ce805e0c4f86ba1cd1",
  369. "test/unit/event.js",
  370. "L2703",
  371. },
  372. "https://github.com/jquery/jquery/blob/a644101ed04d0beacea864ce805e0c4f86ba1cd1/test/unit/event.js": {
  373. "https",
  374. "github.com",
  375. "jquery",
  376. "jquery",
  377. "blob",
  378. "a644101ed04d0beacea864ce805e0c4f86ba1cd1",
  379. "test/unit/event.js",
  380. "",
  381. },
  382. "https://github.com/jquery/jquery/commit/0705be475092aede1eddae01319ec931fb9c65fc": {
  383. "https",
  384. "github.com",
  385. "jquery",
  386. "jquery",
  387. "commit",
  388. "0705be475092aede1eddae01319ec931fb9c65fc",
  389. "",
  390. "",
  391. },
  392. "https://github.com/jquery/jquery/tree/0705be475092aede1eddae01319ec931fb9c65fc/src": {
  393. "https",
  394. "github.com",
  395. "jquery",
  396. "jquery",
  397. "tree",
  398. "0705be475092aede1eddae01319ec931fb9c65fc",
  399. "src",
  400. "",
  401. },
  402. "https://try.gogs.io/gogs/gogs/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2": {
  403. "https",
  404. "try.gogs.io",
  405. "gogs",
  406. "gogs",
  407. "commit",
  408. "d8a994ef243349f321568f9e36d5c3f444b99cae",
  409. "",
  410. "diff-2",
  411. },
  412. }
  413. for k, v := range testCases {
  414. assert.Equal(t, AnySHA1Pattern.FindStringSubmatch(k)[1:], v)
  415. }
  416. }
  417. func TestMisc_IsSameDomain(t *testing.T) {
  418. setting.AppURL = AppURL
  419. setting.AppSubURL = AppSubURL
  420. var sha = "b6dd6210eaebc915fd5be5579c58cce4da2e2579"
  421. var commit = URLJoin(AppSubURL, "commit", sha)
  422. assert.True(t, IsSameDomain(commit))
  423. assert.False(t, IsSameDomain("http://google.com/ncr"))
  424. assert.False(t, IsSameDomain("favicon.ico"))
  425. }