1// SPDX-License-Identifier: AGPL-3.0-or-later
2
3package web
4
5import (
6 "bytes"
7 "context"
8 "testing"
9
10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
12)
13
14// Plain text stays text; only http(s) addresses become links, without the
15// punctuation that ends a sentence; invisible characters are marked.
16func TestPlainParts(t *testing.T) {
17 tests := []struct {
18 name, in string
19 want []TextPart
20 }{
21 {"plain", "nothing to see", []TextPart{{Text: "nothing to see"}}},
22 {"empty", "", nil},
23 {"link", "see https://example.org/a/b?x=1#y now", []TextPart{{Text: "see "}, {Text: "https://example.org/a/b?x=1#y", Href: "https://example.org/a/b?x=1#y"}, {Text: " now"}}},
24 {"sentence end", "go to http://x.test/a.", []TextPart{{Text: "go to "}, {Text: "http://x.test/a", Href: "http://x.test/a"}, {Text: "."}}},
25 {"closing bracket", "(see https://x.test/a)", []TextPart{{Text: "(see "}, {Text: "https://x.test/a", Href: "https://x.test/a"}, {Text: ")"}}},
26 {"balanced bracket", "https://x.test/a_(b)", []TextPart{{Text: "https://x.test/a_(b)", Href: "https://x.test/a_(b)"}}},
27 {"other schemes stay text", "javascript:alert(1) ftp://x.test data:text/html,x", []TextPart{{Text: "javascript:alert(1) ftp://x.test data:text/html,x"}}},
28 {"no host", "https:// and http:///x", []TextPart{{Text: "https:// and http:///x"}}},
29 {"quote ends the address", `http://x.test/"onmouseover="y`, []TextPart{{Text: "http://x.test/", Href: "http://x.test/"}, {Text: `"onmouseover="y`}}},
30 {"crlf", "a\r\nb", []TextPart{{Text: "a\nb"}}},
31 {"invisible", "a\u202eb", []TextPart{{Text: "a<U+202E>b"}}},
32 }
33 for _, tt := range tests {
34 t.Run(tt.name, func(t *testing.T) {
35 assert.Equal(t, tt.want, plainParts(tt.in))
36 })
37 }
38}
39
40// userText escapes the text and the address, and marks the link as untrusted.
41func TestUserTextEscapes(t *testing.T) {
42 var b bytes.Buffer
43 require.NoError(t, userText(plainParts(`<b>x</b> & http://x.test/?a=1&b=2`)).Render(context.Background(), &b))
44 assert.Equal(t, `<b>x</b> & <a href="http://x.test/?a=1&b=2" rel="noopener nofollow ugc">http://x.test/?a=1&b=2</a>`, b.String())
45}
46
47func TestParseOn(t *testing.T) {
48 assert.Equal(t, &lineRef{side: "new", line: 12, path: "a/b:c.go"}, parseOn("new:12:a/b:c.go"))
49 assert.Equal(t, &lineRef{side: "old", line: 1, path: "x"}, parseOn("old:1:x"))
50 for _, s := range []string{"", "new", "new:12", "new:12:", "new:0:x", "new:-1:x", "new:x:y", "up:1:x", "new::x"} {
51 assert.Nil(t, parseOn(s), s)
52 }
53}
54
55func TestShellWord(t *testing.T) {
56 assert.Equal(t, "feat/search", shellWord("feat/search"))
57 assert.Equal(t, `'a b'`, shellWord("a b"))
58 assert.Equal(t, `'it'\''s'`, shellWord("it's"))
59 assert.Equal(t, `'$(x)'`, shellWord("$(x)"))
60}
61
62// The notes of a split row: a context line counts once.
63func TestSplitRowNotes(t *testing.T) {
64 a, b := CommentView{ID: "1"}, CommentView{ID: "2"}
65 ctx := &DiffLine{Notes: []CommentView{a}}
66 assert.Equal(t, []CommentView{a}, SplitRow{ctx, ctx}.Notes())
67 del, add := &DiffLine{Notes: []CommentView{a}}, &DiffLine{Notes: []CommentView{b}}
68 assert.Equal(t, []CommentView{a, b}, SplitRow{del, add}.Notes())
69 assert.Equal(t, []CommentView{a}, SplitRow{del, nil}.Notes())
70 assert.Equal(t, []CommentView{b}, SplitRow{nil, add}.Notes())
71 assert.Nil(t, SplitRow{}.Notes())
72}