Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

html_test.go 13KB

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