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

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