code.gitea.io/gitea@v1.19.3/modules/markup/html_internal_test.go (about) 1 // Copyright 2018 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package markup 5 6 import ( 7 "fmt" 8 "strconv" 9 "strings" 10 "testing" 11 12 "code.gitea.io/gitea/modules/git" 13 "code.gitea.io/gitea/modules/setting" 14 "code.gitea.io/gitea/modules/util" 15 16 "github.com/stretchr/testify/assert" 17 ) 18 19 const ( 20 TestAppURL = "http://localhost:3000/" 21 TestOrgRepo = "gogits/gogs" 22 TestRepoURL = TestAppURL + TestOrgRepo + "/" 23 ) 24 25 // externalIssueLink an HTML link to an alphanumeric-style issue 26 func externalIssueLink(baseURL, class, name string) string { 27 return link(util.URLJoin(baseURL, name), class, name) 28 } 29 30 // numericLink an HTML to a numeric-style issue 31 func numericIssueLink(baseURL, class string, index int, marker string) string { 32 return link(util.URLJoin(baseURL, strconv.Itoa(index)), class, fmt.Sprintf("%s%d", marker, index)) 33 } 34 35 // link an HTML link 36 func link(href, class, contents string) string { 37 if class != "" { 38 class = " class=\"" + class + "\"" 39 } 40 41 return fmt.Sprintf("<a href=\"%s\"%s>%s</a>", href, class, contents) 42 } 43 44 var numericMetas = map[string]string{ 45 "format": "https://someurl.com/{user}/{repo}/{index}", 46 "user": "someUser", 47 "repo": "someRepo", 48 "style": IssueNameStyleNumeric, 49 } 50 51 var alphanumericMetas = map[string]string{ 52 "format": "https://someurl.com/{user}/{repo}/{index}", 53 "user": "someUser", 54 "repo": "someRepo", 55 "style": IssueNameStyleAlphanumeric, 56 } 57 58 var regexpMetas = map[string]string{ 59 "format": "https://someurl.com/{user}/{repo}/{index}", 60 "user": "someUser", 61 "repo": "someRepo", 62 "style": IssueNameStyleRegexp, 63 } 64 65 // these values should match the TestOrgRepo const above 66 var localMetas = map[string]string{ 67 "user": "gogits", 68 "repo": "gogs", 69 } 70 71 func TestRender_IssueIndexPattern(t *testing.T) { 72 // numeric: render inputs without valid mentions 73 test := func(s string) { 74 testRenderIssueIndexPattern(t, s, s, &RenderContext{ 75 Ctx: git.DefaultContext, 76 }) 77 testRenderIssueIndexPattern(t, s, s, &RenderContext{ 78 Ctx: git.DefaultContext, 79 Metas: numericMetas, 80 }) 81 } 82 83 // should not render anything when there are no mentions 84 test("") 85 test("this is a test") 86 test("test 123 123 1234") 87 test("#") 88 test("# # #") 89 test("# 123") 90 test("#abcd") 91 test("test#1234") 92 test("#1234test") 93 test("#abcd") 94 test("test!1234") 95 test("!1234test") 96 test(" test !1234test") 97 test("/home/gitea/#1234") 98 test("/home/gitea/!1234") 99 100 // should not render issue mention without leading space 101 test("test#54321 issue") 102 103 // should not render issue mention without trailing space 104 test("test #54321issue") 105 } 106 107 func TestRender_IssueIndexPattern2(t *testing.T) { 108 setting.AppURL = TestAppURL 109 110 // numeric: render inputs with valid mentions 111 test := func(s, expectedFmt, marker string, indices ...int) { 112 var path, prefix string 113 isExternal := false 114 if marker == "!" { 115 path = "pulls" 116 prefix = "http://localhost:3000/someUser/someRepo/pulls/" 117 } else { 118 path = "issues" 119 prefix = "https://someurl.com/someUser/someRepo/" 120 isExternal = true 121 } 122 123 links := make([]interface{}, len(indices)) 124 for i, index := range indices { 125 links[i] = numericIssueLink(util.URLJoin(TestRepoURL, path), "ref-issue", index, marker) 126 } 127 expectedNil := fmt.Sprintf(expectedFmt, links...) 128 testRenderIssueIndexPattern(t, s, expectedNil, &RenderContext{ 129 Ctx: git.DefaultContext, 130 Metas: localMetas, 131 }) 132 133 class := "ref-issue" 134 if isExternal { 135 class += " ref-external-issue" 136 } 137 138 for i, index := range indices { 139 links[i] = numericIssueLink(prefix, class, index, marker) 140 } 141 expectedNum := fmt.Sprintf(expectedFmt, links...) 142 testRenderIssueIndexPattern(t, s, expectedNum, &RenderContext{ 143 Ctx: git.DefaultContext, 144 Metas: numericMetas, 145 }) 146 } 147 148 // should render freestanding mentions 149 test("#1234 test", "%s test", "#", 1234) 150 test("test #8 issue", "test %s issue", "#", 8) 151 test("!1234 test", "%s test", "!", 1234) 152 test("test !8 issue", "test %s issue", "!", 8) 153 test("test issue #1234", "test issue %s", "#", 1234) 154 test("fixes issue #1234.", "fixes issue %s.", "#", 1234) 155 156 // should render mentions in parentheses / brackets 157 test("(#54321 issue)", "(%s issue)", "#", 54321) 158 test("[#54321 issue]", "[%s issue]", "#", 54321) 159 test("test (#9801 extra) issue", "test (%s extra) issue", "#", 9801) 160 test("test (!9801 extra) issue", "test (%s extra) issue", "!", 9801) 161 test("test (#1)", "test (%s)", "#", 1) 162 163 // should render multiple issue mentions in the same line 164 test("#54321 #1243", "%s %s", "#", 54321, 1243) 165 test("wow (#54321 #1243)", "wow (%s %s)", "#", 54321, 1243) 166 test("(#4)(#5)", "(%s)(%s)", "#", 4, 5) 167 test("#1 (#4321) test", "%s (%s) test", "#", 1, 4321) 168 169 // should render with : 170 test("#1234: test", "%s: test", "#", 1234) 171 test("wow (#54321: test)", "wow (%s: test)", "#", 54321) 172 } 173 174 func TestRender_IssueIndexPattern3(t *testing.T) { 175 setting.AppURL = TestAppURL 176 177 // alphanumeric: render inputs without valid mentions 178 test := func(s string) { 179 testRenderIssueIndexPattern(t, s, s, &RenderContext{ 180 Ctx: git.DefaultContext, 181 Metas: alphanumericMetas, 182 }) 183 } 184 test("") 185 test("this is a test") 186 test("test 123 123 1234") 187 test("#") 188 test("# 123") 189 test("#abcd") 190 test("test #123") 191 test("abc-1234") // issue prefix must be capital 192 test("ABc-1234") // issue prefix must be _all_ capital 193 test("ABCDEFGHIJK-1234") // the limit is 10 characters in the prefix 194 test("ABC1234") // dash is required 195 test("test ABC- test") // number is required 196 test("test -1234 test") // prefix is required 197 test("testABC-123 test") // leading space is required 198 test("test ABC-123test") // trailing space is required 199 test("ABC-0123") // no leading zero 200 } 201 202 func TestRender_IssueIndexPattern4(t *testing.T) { 203 setting.AppURL = TestAppURL 204 205 // alphanumeric: render inputs with valid mentions 206 test := func(s, expectedFmt string, names ...string) { 207 links := make([]interface{}, len(names)) 208 for i, name := range names { 209 links[i] = externalIssueLink("https://someurl.com/someUser/someRepo/", "ref-issue ref-external-issue", name) 210 } 211 expected := fmt.Sprintf(expectedFmt, links...) 212 testRenderIssueIndexPattern(t, s, expected, &RenderContext{ 213 Ctx: git.DefaultContext, 214 Metas: alphanumericMetas, 215 }) 216 } 217 test("OTT-1234 test", "%s test", "OTT-1234") 218 test("test T-12 issue", "test %s issue", "T-12") 219 test("test issue ABCDEFGHIJ-1234567890", "test issue %s", "ABCDEFGHIJ-1234567890") 220 } 221 222 func TestRender_IssueIndexPattern5(t *testing.T) { 223 setting.AppURL = TestAppURL 224 225 // regexp: render inputs without valid mentions 226 test := func(s, expectedFmt, pattern string, ids, names []string) { 227 metas := regexpMetas 228 metas["regexp"] = pattern 229 links := make([]interface{}, len(ids)) 230 for i, id := range ids { 231 links[i] = link(util.URLJoin("https://someurl.com/someUser/someRepo/", id), "ref-issue ref-external-issue", names[i]) 232 } 233 234 expected := fmt.Sprintf(expectedFmt, links...) 235 testRenderIssueIndexPattern(t, s, expected, &RenderContext{ 236 Ctx: git.DefaultContext, 237 Metas: metas, 238 }) 239 } 240 241 test("abc ISSUE-123 def", "abc %s def", 242 "ISSUE-(\\d+)", 243 []string{"123"}, 244 []string{"ISSUE-123"}, 245 ) 246 247 test("abc (ISSUE 123) def", "abc %s def", 248 "\\(ISSUE (\\d+)\\)", 249 []string{"123"}, 250 []string{"(ISSUE 123)"}, 251 ) 252 253 test("abc ISSUE-123 def", "abc %s def", 254 "(ISSUE-(\\d+))", 255 []string{"ISSUE-123"}, 256 []string{"ISSUE-123"}, 257 ) 258 259 testRenderIssueIndexPattern(t, "will not match", "will not match", &RenderContext{ 260 Ctx: git.DefaultContext, 261 Metas: regexpMetas, 262 }) 263 } 264 265 func testRenderIssueIndexPattern(t *testing.T, input, expected string, ctx *RenderContext) { 266 if ctx.URLPrefix == "" { 267 ctx.URLPrefix = TestAppURL 268 } 269 270 var buf strings.Builder 271 err := postProcess(ctx, []processor{issueIndexPatternProcessor}, strings.NewReader(input), &buf) 272 assert.NoError(t, err) 273 assert.Equal(t, expected, buf.String(), "input=%q", input) 274 } 275 276 func TestRender_AutoLink(t *testing.T) { 277 setting.AppURL = TestAppURL 278 279 test := func(input, expected string) { 280 var buffer strings.Builder 281 err := PostProcess(&RenderContext{ 282 Ctx: git.DefaultContext, 283 URLPrefix: TestRepoURL, 284 Metas: localMetas, 285 }, strings.NewReader(input), &buffer) 286 assert.Equal(t, err, nil) 287 assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer.String())) 288 289 buffer.Reset() 290 err = PostProcess(&RenderContext{ 291 Ctx: git.DefaultContext, 292 URLPrefix: TestRepoURL, 293 Metas: localMetas, 294 IsWiki: true, 295 }, strings.NewReader(input), &buffer) 296 assert.Equal(t, err, nil) 297 assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer.String())) 298 } 299 300 // render valid issue URLs 301 test(util.URLJoin(TestRepoURL, "issues", "3333"), 302 numericIssueLink(util.URLJoin(TestRepoURL, "issues"), "ref-issue", 3333, "#")) 303 304 // render valid commit URLs 305 tmp := util.URLJoin(TestRepoURL, "commit", "d8a994ef243349f321568f9e36d5c3f444b99cae") 306 test(tmp, "<a href=\""+tmp+"\" class=\"commit\"><code class=\"nohighlight\">d8a994ef24</code></a>") 307 tmp += "#diff-2" 308 test(tmp, "<a href=\""+tmp+"\" class=\"commit\"><code class=\"nohighlight\">d8a994ef24 (diff-2)</code></a>") 309 310 // render other commit URLs 311 tmp = "https://external-link.gitea.io/go-gitea/gitea/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2" 312 test(tmp, "<a href=\""+tmp+"\" class=\"commit\"><code class=\"nohighlight\">d8a994ef24 (diff-2)</code></a>") 313 } 314 315 func TestRender_FullIssueURLs(t *testing.T) { 316 setting.AppURL = TestAppURL 317 318 test := func(input, expected string) { 319 var result strings.Builder 320 err := postProcess(&RenderContext{ 321 Ctx: git.DefaultContext, 322 URLPrefix: TestRepoURL, 323 Metas: localMetas, 324 }, []processor{fullIssuePatternProcessor}, strings.NewReader(input), &result) 325 assert.NoError(t, err) 326 assert.Equal(t, expected, result.String()) 327 } 328 test("Here is a link https://git.osgeo.org/gogs/postgis/postgis/pulls/6", 329 "Here is a link https://git.osgeo.org/gogs/postgis/postgis/pulls/6") 330 test("Look here http://localhost:3000/person/repo/issues/4", 331 `Look here <a href="http://localhost:3000/person/repo/issues/4" class="ref-issue">person/repo#4</a>`) 332 test("http://localhost:3000/person/repo/issues/4#issuecomment-1234", 333 `<a href="http://localhost:3000/person/repo/issues/4#issuecomment-1234" class="ref-issue">person/repo#4</a>`) 334 test("http://localhost:3000/gogits/gogs/issues/4", 335 `<a href="http://localhost:3000/gogits/gogs/issues/4" class="ref-issue">#4</a>`) 336 test("http://localhost:3000/gogits/gogs/issues/4 test", 337 `<a href="http://localhost:3000/gogits/gogs/issues/4" class="ref-issue">#4</a> test`) 338 test("http://localhost:3000/gogits/gogs/issues/4?a=1&b=2#comment-123 test", 339 `<a href="http://localhost:3000/gogits/gogs/issues/4?a=1&b=2#comment-123" class="ref-issue">#4</a> test`) 340 } 341 342 func TestRegExp_sha1CurrentPattern(t *testing.T) { 343 trueTestCases := []string{ 344 "d8a994ef243349f321568f9e36d5c3f444b99cae", 345 "abcdefabcdefabcdefabcdefabcdefabcdefabcd", 346 "(abcdefabcdefabcdefabcdefabcdefabcdefabcd)", 347 "[abcdefabcdefabcdefabcdefabcdefabcdefabcd]", 348 "abcdefabcdefabcdefabcdefabcdefabcdefabcd.", 349 } 350 falseTestCases := []string{ 351 "test", 352 "abcdefg", 353 "e59ff077-2d03-4e6b-964d-63fbaea81f", 354 "abcdefghijklmnopqrstuvwxyzabcdefghijklmn", 355 "abcdefghijklmnopqrstuvwxyzabcdefghijklmO", 356 } 357 358 for _, testCase := range trueTestCases { 359 assert.True(t, sha1CurrentPattern.MatchString(testCase)) 360 } 361 for _, testCase := range falseTestCases { 362 assert.False(t, sha1CurrentPattern.MatchString(testCase)) 363 } 364 } 365 366 func TestRegExp_anySHA1Pattern(t *testing.T) { 367 testCases := map[string][]string{ 368 "https://github.com/jquery/jquery/blob/a644101ed04d0beacea864ce805e0c4f86ba1cd1/test/unit/event.js#L2703": { 369 "a644101ed04d0beacea864ce805e0c4f86ba1cd1", 370 "/test/unit/event.js", 371 "#L2703", 372 }, 373 "https://github.com/jquery/jquery/blob/a644101ed04d0beacea864ce805e0c4f86ba1cd1/test/unit/event.js": { 374 "a644101ed04d0beacea864ce805e0c4f86ba1cd1", 375 "/test/unit/event.js", 376 "", 377 }, 378 "https://github.com/jquery/jquery/commit/0705be475092aede1eddae01319ec931fb9c65fc": { 379 "0705be475092aede1eddae01319ec931fb9c65fc", 380 "", 381 "", 382 }, 383 "https://github.com/jquery/jquery/tree/0705be475092aede1eddae01319ec931fb9c65fc/src": { 384 "0705be475092aede1eddae01319ec931fb9c65fc", 385 "/src", 386 "", 387 }, 388 "https://try.gogs.io/gogs/gogs/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2": { 389 "d8a994ef243349f321568f9e36d5c3f444b99cae", 390 "", 391 "#diff-2", 392 }, 393 } 394 395 for k, v := range testCases { 396 assert.Equal(t, anySHA1Pattern.FindStringSubmatch(k)[1:], v) 397 } 398 } 399 400 func TestRegExp_shortLinkPattern(t *testing.T) { 401 trueTestCases := []string{ 402 "[[stuff]]", 403 "[[]]", 404 "[[stuff|title=Difficult name with spaces*!]]", 405 } 406 falseTestCases := []string{ 407 "test", 408 "abcdefg", 409 "[[]", 410 "[[", 411 "[]", 412 "]]", 413 "abcdefghijklmnopqrstuvwxyz", 414 } 415 416 for _, testCase := range trueTestCases { 417 assert.True(t, shortLinkPattern.MatchString(testCase)) 418 } 419 for _, testCase := range falseTestCases { 420 assert.False(t, shortLinkPattern.MatchString(testCase)) 421 } 422 }