You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

html_internal_test.go 11 kB

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