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_internal_test.go 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package markup
  4. import (
  5. "fmt"
  6. "strconv"
  7. "strings"
  8. "testing"
  9. "code.gitea.io/gitea/modules/git"
  10. "code.gitea.io/gitea/modules/setting"
  11. "code.gitea.io/gitea/modules/util"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. const (
  15. TestAppURL = "http://localhost:3000/"
  16. TestOrgRepo = "gogits/gogs"
  17. TestRepoURL = TestAppURL + TestOrgRepo + "/"
  18. )
  19. // externalIssueLink an HTML link to an alphanumeric-style issue
  20. func externalIssueLink(baseURL, class, name string) string {
  21. return link(util.URLJoin(baseURL, name), class, name)
  22. }
  23. // numericLink an HTML to a numeric-style issue
  24. func numericIssueLink(baseURL, class string, index int, marker string) string {
  25. return link(util.URLJoin(baseURL, strconv.Itoa(index)), class, fmt.Sprintf("%s%d", marker, index))
  26. }
  27. // link an HTML link
  28. func link(href, class, contents string) string {
  29. if class != "" {
  30. class = " class=\"" + class + "\""
  31. }
  32. return fmt.Sprintf("<a href=\"%s\"%s>%s</a>", href, class, contents)
  33. }
  34. var numericMetas = map[string]string{
  35. "format": "https://someurl.com/{user}/{repo}/{index}",
  36. "user": "someUser",
  37. "repo": "someRepo",
  38. "style": IssueNameStyleNumeric,
  39. }
  40. var alphanumericMetas = map[string]string{
  41. "format": "https://someurl.com/{user}/{repo}/{index}",
  42. "user": "someUser",
  43. "repo": "someRepo",
  44. "style": IssueNameStyleAlphanumeric,
  45. }
  46. var regexpMetas = map[string]string{
  47. "format": "https://someurl.com/{user}/{repo}/{index}",
  48. "user": "someUser",
  49. "repo": "someRepo",
  50. "style": IssueNameStyleRegexp,
  51. }
  52. // these values should match the TestOrgRepo const above
  53. var localMetas = map[string]string{
  54. "user": "gogits",
  55. "repo": "gogs",
  56. }
  57. func TestRender_IssueIndexPattern(t *testing.T) {
  58. // numeric: render inputs without valid mentions
  59. test := func(s string) {
  60. testRenderIssueIndexPattern(t, s, s, &RenderContext{
  61. Ctx: git.DefaultContext,
  62. })
  63. testRenderIssueIndexPattern(t, s, s, &RenderContext{
  64. Ctx: git.DefaultContext,
  65. Metas: numericMetas,
  66. })
  67. }
  68. // should not render anything when there are no mentions
  69. test("")
  70. test("this is a test")
  71. test("test 123 123 1234")
  72. test("#")
  73. test("# # #")
  74. test("# 123")
  75. test("#abcd")
  76. test("test#1234")
  77. test("#1234test")
  78. test("#abcd")
  79. test("test!1234")
  80. test("!1234test")
  81. test(" test !1234test")
  82. test("/home/gitea/#1234")
  83. test("/home/gitea/!1234")
  84. // should not render issue mention without leading space
  85. test("test#54321 issue")
  86. // should not render issue mention without trailing space
  87. test("test #54321issue")
  88. }
  89. func TestRender_IssueIndexPattern2(t *testing.T) {
  90. setting.AppURL = TestAppURL
  91. // numeric: render inputs with valid mentions
  92. test := func(s, expectedFmt, marker string, indices ...int) {
  93. var path, prefix string
  94. isExternal := false
  95. if marker == "!" {
  96. path = "pulls"
  97. prefix = "http://localhost:3000/someUser/someRepo/pulls/"
  98. } else {
  99. path = "issues"
  100. prefix = "https://someurl.com/someUser/someRepo/"
  101. isExternal = true
  102. }
  103. links := make([]any, len(indices))
  104. for i, index := range indices {
  105. links[i] = numericIssueLink(util.URLJoin(TestRepoURL, path), "ref-issue", index, marker)
  106. }
  107. expectedNil := fmt.Sprintf(expectedFmt, links...)
  108. testRenderIssueIndexPattern(t, s, expectedNil, &RenderContext{
  109. Ctx: git.DefaultContext,
  110. Metas: localMetas,
  111. })
  112. class := "ref-issue"
  113. if isExternal {
  114. class += " ref-external-issue"
  115. }
  116. for i, index := range indices {
  117. links[i] = numericIssueLink(prefix, class, index, marker)
  118. }
  119. expectedNum := fmt.Sprintf(expectedFmt, links...)
  120. testRenderIssueIndexPattern(t, s, expectedNum, &RenderContext{
  121. Ctx: git.DefaultContext,
  122. Metas: numericMetas,
  123. })
  124. }
  125. // should render freestanding mentions
  126. test("#1234 test", "%s test", "#", 1234)
  127. test("test #8 issue", "test %s issue", "#", 8)
  128. test("!1234 test", "%s test", "!", 1234)
  129. test("test !8 issue", "test %s issue", "!", 8)
  130. test("test issue #1234", "test issue %s", "#", 1234)
  131. test("fixes issue #1234.", "fixes issue %s.", "#", 1234)
  132. // should render mentions in parentheses / brackets
  133. test("(#54321 issue)", "(%s issue)", "#", 54321)
  134. test("[#54321 issue]", "[%s issue]", "#", 54321)
  135. test("test (#9801 extra) issue", "test (%s extra) issue", "#", 9801)
  136. test("test (!9801 extra) issue", "test (%s extra) issue", "!", 9801)
  137. test("test (#1)", "test (%s)", "#", 1)
  138. // should render multiple issue mentions in the same line
  139. test("#54321 #1243", "%s %s", "#", 54321, 1243)
  140. test("wow (#54321 #1243)", "wow (%s %s)", "#", 54321, 1243)
  141. test("(#4)(#5)", "(%s)(%s)", "#", 4, 5)
  142. test("#1 (#4321) test", "%s (%s) test", "#", 1, 4321)
  143. // should render with :
  144. test("#1234: test", "%s: test", "#", 1234)
  145. test("wow (#54321: test)", "wow (%s: test)", "#", 54321)
  146. }
  147. func TestRender_IssueIndexPattern3(t *testing.T) {
  148. setting.AppURL = TestAppURL
  149. // alphanumeric: render inputs without valid mentions
  150. test := func(s string) {
  151. testRenderIssueIndexPattern(t, s, s, &RenderContext{
  152. Ctx: git.DefaultContext,
  153. Metas: alphanumericMetas,
  154. })
  155. }
  156. test("")
  157. test("this is a test")
  158. test("test 123 123 1234")
  159. test("#")
  160. test("# 123")
  161. test("#abcd")
  162. test("test #123")
  163. test("abc-1234") // issue prefix must be capital
  164. test("ABc-1234") // issue prefix must be _all_ capital
  165. test("ABCDEFGHIJK-1234") // the limit is 10 characters in the prefix
  166. test("ABC1234") // dash is required
  167. test("test ABC- test") // number is required
  168. test("test -1234 test") // prefix is required
  169. test("testABC-123 test") // leading space is required
  170. test("test ABC-123test") // trailing space is required
  171. test("ABC-0123") // no leading zero
  172. }
  173. func TestRender_IssueIndexPattern4(t *testing.T) {
  174. setting.AppURL = TestAppURL
  175. // alphanumeric: render inputs with valid mentions
  176. test := func(s, expectedFmt string, names ...string) {
  177. links := make([]any, len(names))
  178. for i, name := range names {
  179. links[i] = externalIssueLink("https://someurl.com/someUser/someRepo/", "ref-issue ref-external-issue", name)
  180. }
  181. expected := fmt.Sprintf(expectedFmt, links...)
  182. testRenderIssueIndexPattern(t, s, expected, &RenderContext{
  183. Ctx: git.DefaultContext,
  184. Metas: alphanumericMetas,
  185. })
  186. }
  187. test("OTT-1234 test", "%s test", "OTT-1234")
  188. test("test T-12 issue", "test %s issue", "T-12")
  189. test("test issue ABCDEFGHIJ-1234567890", "test issue %s", "ABCDEFGHIJ-1234567890")
  190. }
  191. func TestRender_IssueIndexPattern5(t *testing.T) {
  192. setting.AppURL = TestAppURL
  193. // regexp: render inputs without valid mentions
  194. test := func(s, expectedFmt, pattern string, ids, names []string) {
  195. metas := regexpMetas
  196. metas["regexp"] = pattern
  197. links := make([]any, len(ids))
  198. for i, id := range ids {
  199. links[i] = link(util.URLJoin("https://someurl.com/someUser/someRepo/", id), "ref-issue ref-external-issue", names[i])
  200. }
  201. expected := fmt.Sprintf(expectedFmt, links...)
  202. testRenderIssueIndexPattern(t, s, expected, &RenderContext{
  203. Ctx: git.DefaultContext,
  204. Metas: metas,
  205. })
  206. }
  207. test("abc ISSUE-123 def", "abc %s def",
  208. "ISSUE-(\\d+)",
  209. []string{"123"},
  210. []string{"ISSUE-123"},
  211. )
  212. test("abc (ISSUE 123) def", "abc %s def",
  213. "\\(ISSUE (\\d+)\\)",
  214. []string{"123"},
  215. []string{"(ISSUE 123)"},
  216. )
  217. test("abc ISSUE-123 def", "abc %s def",
  218. "(ISSUE-(\\d+))",
  219. []string{"ISSUE-123"},
  220. []string{"ISSUE-123"},
  221. )
  222. testRenderIssueIndexPattern(t, "will not match", "will not match", &RenderContext{
  223. Ctx: git.DefaultContext,
  224. Metas: regexpMetas,
  225. })
  226. }
  227. func TestRender_IssueIndexPattern_Document(t *testing.T) {
  228. setting.AppURL = TestAppURL
  229. metas := map[string]string{
  230. "format": "https://someurl.com/{user}/{repo}/{index}",
  231. "user": "someUser",
  232. "repo": "someRepo",
  233. "style": IssueNameStyleNumeric,
  234. "mode": "document",
  235. }
  236. testRenderIssueIndexPattern(t, "#1", "#1", &RenderContext{
  237. Ctx: git.DefaultContext,
  238. Metas: metas,
  239. })
  240. testRenderIssueIndexPattern(t, "#1312", "#1312", &RenderContext{
  241. Ctx: git.DefaultContext,
  242. Metas: metas,
  243. })
  244. testRenderIssueIndexPattern(t, "!1", "!1", &RenderContext{
  245. Ctx: git.DefaultContext,
  246. Metas: metas,
  247. })
  248. }
  249. func testRenderIssueIndexPattern(t *testing.T, input, expected string, ctx *RenderContext) {
  250. if ctx.Links.Base == "" {
  251. ctx.Links.Base = TestRepoURL
  252. }
  253. var buf strings.Builder
  254. err := postProcess(ctx, []processor{issueIndexPatternProcessor}, strings.NewReader(input), &buf)
  255. assert.NoError(t, err)
  256. assert.Equal(t, expected, buf.String(), "input=%q", input)
  257. }
  258. func TestRender_AutoLink(t *testing.T) {
  259. setting.AppURL = TestAppURL
  260. test := func(input, expected string) {
  261. var buffer strings.Builder
  262. err := PostProcess(&RenderContext{
  263. Ctx: git.DefaultContext,
  264. Links: Links{
  265. Base: TestRepoURL,
  266. },
  267. Metas: localMetas,
  268. }, strings.NewReader(input), &buffer)
  269. assert.Equal(t, err, nil)
  270. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer.String()))
  271. buffer.Reset()
  272. err = PostProcess(&RenderContext{
  273. Ctx: git.DefaultContext,
  274. Links: Links{
  275. Base: TestRepoURL,
  276. },
  277. Metas: localMetas,
  278. IsWiki: true,
  279. }, strings.NewReader(input), &buffer)
  280. assert.Equal(t, err, nil)
  281. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer.String()))
  282. }
  283. // render valid issue URLs
  284. test(util.URLJoin(TestRepoURL, "issues", "3333"),
  285. numericIssueLink(util.URLJoin(TestRepoURL, "issues"), "ref-issue", 3333, "#"))
  286. // render valid commit URLs
  287. tmp := util.URLJoin(TestRepoURL, "commit", "d8a994ef243349f321568f9e36d5c3f444b99cae")
  288. test(tmp, "<a href=\""+tmp+"\" class=\"commit\"><code class=\"nohighlight\">d8a994ef24</code></a>")
  289. tmp += "#diff-2"
  290. test(tmp, "<a href=\""+tmp+"\" class=\"commit\"><code class=\"nohighlight\">d8a994ef24 (diff-2)</code></a>")
  291. // render other commit URLs
  292. tmp = "https://external-link.gitea.io/go-gitea/gitea/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2"
  293. test(tmp, "<a href=\""+tmp+"\" class=\"commit\"><code class=\"nohighlight\">d8a994ef24 (diff-2)</code></a>")
  294. }
  295. func TestRender_FullIssueURLs(t *testing.T) {
  296. setting.AppURL = TestAppURL
  297. test := func(input, expected string) {
  298. var result strings.Builder
  299. err := postProcess(&RenderContext{
  300. Ctx: git.DefaultContext,
  301. Links: Links{
  302. Base: TestRepoURL,
  303. },
  304. Metas: localMetas,
  305. }, []processor{fullIssuePatternProcessor}, strings.NewReader(input), &result)
  306. assert.NoError(t, err)
  307. assert.Equal(t, expected, result.String())
  308. }
  309. test("Here is a link https://git.osgeo.org/gogs/postgis/postgis/pulls/6",
  310. "Here is a link https://git.osgeo.org/gogs/postgis/postgis/pulls/6")
  311. test("Look here http://localhost:3000/person/repo/issues/4",
  312. `Look here <a href="http://localhost:3000/person/repo/issues/4" class="ref-issue">person/repo#4</a>`)
  313. test("http://localhost:3000/person/repo/issues/4#issuecomment-1234",
  314. `<a href="http://localhost:3000/person/repo/issues/4#issuecomment-1234" class="ref-issue">person/repo#4 (comment)</a>`)
  315. test("http://localhost:3000/gogits/gogs/issues/4",
  316. `<a href="http://localhost:3000/gogits/gogs/issues/4" class="ref-issue">#4</a>`)
  317. test("http://localhost:3000/gogits/gogs/issues/4 test",
  318. `<a href="http://localhost:3000/gogits/gogs/issues/4" class="ref-issue">#4</a> test`)
  319. test("http://localhost:3000/gogits/gogs/issues/4?a=1&b=2#comment-123 test",
  320. `<a href="http://localhost:3000/gogits/gogs/issues/4?a=1&amp;b=2#comment-123" class="ref-issue">#4 (comment)</a> test`)
  321. test("http://localhost:3000/testOrg/testOrgRepo/pulls/2/files#issuecomment-24",
  322. "http://localhost:3000/testOrg/testOrgRepo/pulls/2/files#issuecomment-24")
  323. test("http://localhost:3000/testOrg/testOrgRepo/pulls/2/files",
  324. "http://localhost:3000/testOrg/testOrgRepo/pulls/2/files")
  325. }
  326. func TestRegExp_sha1CurrentPattern(t *testing.T) {
  327. trueTestCases := []string{
  328. "d8a994ef243349f321568f9e36d5c3f444b99cae",
  329. "abcdefabcdefabcdefabcdefabcdefabcdefabcd",
  330. "(abcdefabcdefabcdefabcdefabcdefabcdefabcd)",
  331. "[abcdefabcdefabcdefabcdefabcdefabcdefabcd]",
  332. "abcdefabcdefabcdefabcdefabcdefabcdefabcd.",
  333. }
  334. falseTestCases := []string{
  335. "test",
  336. "abcdefg",
  337. "e59ff077-2d03-4e6b-964d-63fbaea81f",
  338. "abcdefghijklmnopqrstuvwxyzabcdefghijklmn",
  339. "abcdefghijklmnopqrstuvwxyzabcdefghijklmO",
  340. }
  341. for _, testCase := range trueTestCases {
  342. assert.True(t, sha1CurrentPattern.MatchString(testCase))
  343. }
  344. for _, testCase := range falseTestCases {
  345. assert.False(t, sha1CurrentPattern.MatchString(testCase))
  346. }
  347. }
  348. func TestRegExp_anySHA1Pattern(t *testing.T) {
  349. testCases := map[string][]string{
  350. "https://github.com/jquery/jquery/blob/a644101ed04d0beacea864ce805e0c4f86ba1cd1/test/unit/event.js#L2703": {
  351. "a644101ed04d0beacea864ce805e0c4f86ba1cd1",
  352. "/test/unit/event.js",
  353. "#L2703",
  354. },
  355. "https://github.com/jquery/jquery/blob/a644101ed04d0beacea864ce805e0c4f86ba1cd1/test/unit/event.js": {
  356. "a644101ed04d0beacea864ce805e0c4f86ba1cd1",
  357. "/test/unit/event.js",
  358. "",
  359. },
  360. "https://github.com/jquery/jquery/commit/0705be475092aede1eddae01319ec931fb9c65fc": {
  361. "0705be475092aede1eddae01319ec931fb9c65fc",
  362. "",
  363. "",
  364. },
  365. "https://github.com/jquery/jquery/tree/0705be475092aede1eddae01319ec931fb9c65fc/src": {
  366. "0705be475092aede1eddae01319ec931fb9c65fc",
  367. "/src",
  368. "",
  369. },
  370. "https://try.gogs.io/gogs/gogs/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2": {
  371. "d8a994ef243349f321568f9e36d5c3f444b99cae",
  372. "",
  373. "#diff-2",
  374. },
  375. }
  376. for k, v := range testCases {
  377. assert.Equal(t, anySHA1Pattern.FindStringSubmatch(k)[1:], v)
  378. }
  379. }
  380. func TestRegExp_shortLinkPattern(t *testing.T) {
  381. trueTestCases := []string{
  382. "[[stuff]]",
  383. "[[]]",
  384. "[[stuff|title=Difficult name with spaces*!]]",
  385. }
  386. falseTestCases := []string{
  387. "test",
  388. "abcdefg",
  389. "[[]",
  390. "[[",
  391. "[]",
  392. "]]",
  393. "abcdefghijklmnopqrstuvwxyz",
  394. }
  395. for _, testCase := range trueTestCases {
  396. assert.True(t, shortLinkPattern.MatchString(testCase))
  397. }
  398. for _, testCase := range falseTestCases {
  399. assert.False(t, shortLinkPattern.MatchString(testCase))
  400. }
  401. }