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 t.Parallel()
18 tests := []struct {
19 name, in string
20 want []TextPart
21 }{
22 {"plain", "nothing to see", []TextPart{{Text: "nothing to see"}}},
23 {"empty", "", nil},
24 {"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"}}},
25 {"sentence end", "go to http://x.test/a.", []TextPart{{Text: "go to "}, {Text: "http://x.test/a", Href: "http://x.test/a"}, {Text: "."}}},
26 {"closing bracket", "(see https://x.test/a)", []TextPart{{Text: "(see "}, {Text: "https://x.test/a", Href: "https://x.test/a"}, {Text: ")"}}},
27 {"balanced bracket", "https://x.test/a_(b)", []TextPart{{Text: "https://x.test/a_(b)", Href: "https://x.test/a_(b)"}}},
28 {"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"}}},
29 {"no host", "https:// and http:///x", []TextPart{{Text: "https:// and http:///x"}}},
30 {"quote ends the address", `http://x.test/"onmouseover="y`, []TextPart{{Text: "http://x.test/", Href: "http://x.test/"}, {Text: `"onmouseover="y`}}},
31 {"crlf", "a\r\nb", []TextPart{{Text: "a\nb"}}},
32 {"invisible", "a\u202eb", []TextPart{{Text: "a<U+202E>b"}}},
33 }
34 for _, tt := range tests {
35 t.Run(tt.name, func(t *testing.T) {
36 t.Parallel()
37 assert.Equal(t, tt.want, plainParts(tt.in))
38 })
39 }
40}
41
42// userText escapes the text and the address, and marks the link as untrusted.
43func TestUserTextEscapes(t *testing.T) {
44 t.Parallel()
45 var b bytes.Buffer
46 require.NoError(t, userText(plainParts(`<b>x</b> & http://x.test/?a=1&b=2`)).Render(context.Background(), &b))
47 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())
48}
49
50func TestParseOn(t *testing.T) {
51 t.Parallel()
52 assert.Equal(t, &lineRef{side: "new", line: 12, path: "a/b:c.go"}, parseOn("new:12:a/b:c.go"))
53 assert.Equal(t, &lineRef{side: "old", line: 1, path: "x"}, parseOn("old:1:x"))
54 for _, s := range []string{"", "new", "new:12", "new:12:", "new:0:x", "new:-1:x", "new:x:y", "up:1:x", "new::x"} {
55 assert.Nil(t, parseOn(s), s)
56 }
57}
58
59func TestShellWord(t *testing.T) {
60 t.Parallel()
61 assert.Equal(t, "feat/search", shellWord("feat/search"))
62 assert.Equal(t, `'a b'`, shellWord("a b"))
63 assert.Equal(t, `'it'\''s'`, shellWord("it's"))
64 assert.Equal(t, `'$(x)'`, shellWord("$(x)"))
65}
66
67// The notes of a split row: a context line counts once.
68func TestSplitRowNotes(t *testing.T) {
69 t.Parallel()
70 a, b := CommentView{ID: "1"}, CommentView{ID: "2"}
71 ctx := &DiffLine{Notes: []CommentView{a}}
72 assert.Equal(t, []CommentView{a}, SplitRow{ctx, ctx}.Notes())
73 del, add := &DiffLine{Notes: []CommentView{a}}, &DiffLine{Notes: []CommentView{b}}
74 assert.Equal(t, []CommentView{a, b}, SplitRow{del, add}.Notes())
75 assert.Equal(t, []CommentView{a}, SplitRow{del, nil}.Notes())
76 assert.Equal(t, []CommentView{b}, SplitRow{nil, add}.Notes())
77 assert.Nil(t, SplitRow{}.Notes())
78}