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

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