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

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