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 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. ctx.Links.AbsolutePrefix = true
  251. if ctx.Links.Base == "" {
  252. ctx.Links.Base = TestRepoURL
  253. }
  254. var buf strings.Builder
  255. err := postProcess(ctx, []processor{issueIndexPatternProcessor}, strings.NewReader(input), &buf)
  256. assert.NoError(t, err)
  257. assert.Equal(t, expected, buf.String(), "input=%q", input)
  258. }
  259. func TestRender_AutoLink(t *testing.T) {
  260. setting.AppURL = TestAppURL
  261. test := func(input, expected string) {
  262. var buffer strings.Builder
  263. err := PostProcess(&RenderContext{
  264. Ctx: git.DefaultContext,
  265. Links: Links{
  266. Base: TestRepoURL,
  267. },
  268. Metas: localMetas,
  269. }, strings.NewReader(input), &buffer)
  270. assert.Equal(t, err, nil)
  271. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer.String()))
  272. buffer.Reset()
  273. err = PostProcess(&RenderContext{
  274. Ctx: git.DefaultContext,
  275. Links: Links{
  276. Base: TestRepoURL,
  277. },
  278. Metas: localMetas,
  279. IsWiki: true,
  280. }, strings.NewReader(input), &buffer)
  281. assert.Equal(t, err, nil)
  282. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer.String()))
  283. }
  284. // render valid issue URLs
  285. test(util.URLJoin(TestRepoURL, "issues", "3333"),
  286. numericIssueLink(util.URLJoin(TestRepoURL, "issues"), "ref-issue", 3333, "#"))
  287. // render valid commit URLs
  288. tmp := util.URLJoin(TestRepoURL, "commit", "d8a994ef243349f321568f9e36d5c3f444b99cae")
  289. test(tmp, "<a href=\""+tmp+"\" class=\"commit\"><code class=\"nohighlight\">d8a994ef24</code></a>")
  290. tmp += "#diff-2"
  291. test(tmp, "<a href=\""+tmp+"\" class=\"commit\"><code class=\"nohighlight\">d8a994ef24 (diff-2)</code></a>")
  292. // render other commit URLs
  293. tmp = "https://external-link.gitea.io/go-gitea/gitea/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2"
  294. test(tmp, "<a href=\""+tmp+"\" class=\"commit\"><code class=\"nohighlight\">d8a994ef24 (diff-2)</code></a>")
  295. }
  296. func TestRender_FullIssueURLs(t *testing.T) {
  297. setting.AppURL = TestAppURL
  298. test := func(input, expected string) {
  299. var result strings.Builder
  300. err := postProcess(&RenderContext{
  301. Ctx: git.DefaultContext,
  302. Links: Links{
  303. Base: TestRepoURL,
  304. },
  305. Metas: localMetas,
  306. }, []processor{fullIssuePatternProcessor}, strings.NewReader(input), &result)
  307. assert.NoError(t, err)
  308. assert.Equal(t, expected, result.String())
  309. }
  310. test("Here is a link https://git.osgeo.org/gogs/postgis/postgis/pulls/6",
  311. "Here is a link https://git.osgeo.org/gogs/postgis/postgis/pulls/6")
  312. test("Look here http://localhost:3000/person/repo/issues/4",
  313. `Look here <a href="http://localhost:3000/person/repo/issues/4" class="ref-issue">person/repo#4</a>`)
  314. test("http://localhost:3000/person/repo/issues/4#issuecomment-1234",
  315. `<a href="http://localhost:3000/person/repo/issues/4#issuecomment-1234" class="ref-issue">person/repo#4 (comment)</a>`)
  316. test("http://localhost:3000/gogits/gogs/issues/4",
  317. `<a href="http://localhost:3000/gogits/gogs/issues/4" class="ref-issue">#4</a>`)
  318. test("http://localhost:3000/gogits/gogs/issues/4 test",
  319. `<a href="http://localhost:3000/gogits/gogs/issues/4" class="ref-issue">#4</a> test`)
  320. test("http://localhost:3000/gogits/gogs/issues/4?a=1&b=2#comment-123 test",
  321. `<a href="http://localhost:3000/gogits/gogs/issues/4?a=1&amp;b=2#comment-123" class="ref-issue">#4 (comment)</a> test`)
  322. test("http://localhost:3000/testOrg/testOrgRepo/pulls/2/files#issuecomment-24",
  323. "http://localhost:3000/testOrg/testOrgRepo/pulls/2/files#issuecomment-24")
  324. test("http://localhost:3000/testOrg/testOrgRepo/pulls/2/files",
  325. "http://localhost:3000/testOrg/testOrgRepo/pulls/2/files")
  326. }
  327. func TestRegExp_sha1CurrentPattern(t *testing.T) {
  328. trueTestCases := []string{
  329. "d8a994ef243349f321568f9e36d5c3f444b99cae",
  330. "abcdefabcdefabcdefabcdefabcdefabcdefabcd",
  331. "(abcdefabcdefabcdefabcdefabcdefabcdefabcd)",
  332. "[abcdefabcdefabcdefabcdefabcdefabcdefabcd]",
  333. "abcdefabcdefabcdefabcdefabcdefabcdefabcd.",
  334. }
  335. falseTestCases := []string{
  336. "test",
  337. "abcdefg",
  338. "e59ff077-2d03-4e6b-964d-63fbaea81f",
  339. "abcdefghijklmnopqrstuvwxyzabcdefghijklmn",
  340. "abcdefghijklmnopqrstuvwxyzabcdefghijklmO",
  341. }
  342. for _, testCase := range trueTestCases {
  343. assert.True(t, hashCurrentPattern.MatchString(testCase))
  344. }
  345. for _, testCase := range falseTestCases {
  346. assert.False(t, hashCurrentPattern.MatchString(testCase))
  347. }
  348. }
  349. func TestRegExp_anySHA1Pattern(t *testing.T) {
  350. testCases := map[string]anyHashPatternResult{
  351. "https://github.com/jquery/jquery/blob/a644101ed04d0beacea864ce805e0c4f86ba1cd1/test/unit/event.js#L2703": {
  352. CommitID: "a644101ed04d0beacea864ce805e0c4f86ba1cd1",
  353. SubPath: "/test/unit/event.js",
  354. QueryHash: "L2703",
  355. },
  356. "https://github.com/jquery/jquery/blob/a644101ed04d0beacea864ce805e0c4f86ba1cd1/test/unit/event.js": {
  357. CommitID: "a644101ed04d0beacea864ce805e0c4f86ba1cd1",
  358. SubPath: "/test/unit/event.js",
  359. },
  360. "https://github.com/jquery/jquery/commit/0705be475092aede1eddae01319ec931fb9c65fc": {
  361. CommitID: "0705be475092aede1eddae01319ec931fb9c65fc",
  362. },
  363. "https://github.com/jquery/jquery/tree/0705be475092aede1eddae01319ec931fb9c65fc/src": {
  364. CommitID: "0705be475092aede1eddae01319ec931fb9c65fc",
  365. SubPath: "/src",
  366. },
  367. "https://try.gogs.io/gogs/gogs/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2": {
  368. CommitID: "d8a994ef243349f321568f9e36d5c3f444b99cae",
  369. QueryHash: "diff-2",
  370. },
  371. "non-url": {},
  372. "http://a/b/c/d/e/1234567812345678123456781234567812345678123456781234567812345678?a=b#L1-L2": {
  373. CommitID: "1234567812345678123456781234567812345678123456781234567812345678",
  374. QueryHash: "L1-L2",
  375. },
  376. "http://a/b/c/d/e/1234567812345678123456781234567812345678123456781234567812345678.": {
  377. CommitID: "1234567812345678123456781234567812345678123456781234567812345678",
  378. },
  379. "http://a/b/c/d/e/1234567812345678123456781234567812345678123456781234567812345678/sub.": {
  380. CommitID: "1234567812345678123456781234567812345678123456781234567812345678",
  381. SubPath: "/sub",
  382. },
  383. "http://a/b/c/d/e/1234567812345678123456781234567812345678123456781234567812345678?a=b.": {
  384. CommitID: "1234567812345678123456781234567812345678123456781234567812345678",
  385. },
  386. "http://a/b/c/d/e/1234567812345678123456781234567812345678123456781234567812345678?a=b&c=d": {
  387. CommitID: "1234567812345678123456781234567812345678123456781234567812345678",
  388. },
  389. "http://a/b/c/d/e/1234567812345678123456781234567812345678123456781234567812345678#hash.": {
  390. CommitID: "1234567812345678123456781234567812345678123456781234567812345678",
  391. QueryHash: "hash",
  392. },
  393. }
  394. for k, v := range testCases {
  395. ret, ok := anyHashPatternExtract(k)
  396. if v.CommitID == "" {
  397. assert.False(t, ok)
  398. } else {
  399. assert.EqualValues(t, strings.TrimSuffix(k, "."), ret.FullURL)
  400. assert.EqualValues(t, v.CommitID, ret.CommitID)
  401. assert.EqualValues(t, v.SubPath, ret.SubPath)
  402. assert.EqualValues(t, v.QueryHash, ret.QueryHash)
  403. }
  404. }
  405. }
  406. func TestRegExp_shortLinkPattern(t *testing.T) {
  407. trueTestCases := []string{
  408. "[[stuff]]",
  409. "[[]]",
  410. "[[stuff|title=Difficult name with spaces*!]]",
  411. }
  412. falseTestCases := []string{
  413. "test",
  414. "abcdefg",
  415. "[[]",
  416. "[[",
  417. "[]",
  418. "]]",
  419. "abcdefghijklmnopqrstuvwxyz",
  420. }
  421. for _, testCase := range trueTestCases {
  422. assert.True(t, shortLinkPattern.MatchString(testCase))
  423. }
  424. for _, testCase := range falseTestCases {
  425. assert.False(t, shortLinkPattern.MatchString(testCase))
  426. }
  427. }