blob: eff2a61607ea362a26cf89b9fa4e0dae5c469087 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/bin/sh

# Checks about-render.lua, the shipped about-filter. The unit checks drive
# the script under every suitable standalone Lua through the harness in
# extensions/, once as installed and once with lpeg forced away so the
# plain text fallback is proven everywhere. A repository with a markdown
# readme is then rendered through cgit itself, where the outcome depends on
# whether the Lua inside the binary has lpeg, so a probe decides which of
# the two right answers to expect.

test_description='Check the shipped about-render extension'
CGIT_TEST_NO_CREATE_REPOS=YesPlease
. ./setup.sh
. "$TEST_OUTPUT_DIRECTORY/extensions/lib.sh"

interpreters=$(ext_lua_interpreters 4)
test -z "$interpreters" &&
	say 'no standalone lua on the path, unit checks skipped'

for lua in $interpreters
do
	test_expect_success "unit checks under $lua" "
		'$lua' '$EXT_TEST_DIRECTORY/test-about-render.lua' \
			'$EXTENSIONS_DIRECTORY/about-render.lua'
	"

	test_expect_success "fallback checks without lpeg under $lua" "
		'$lua' '$EXT_TEST_DIRECTORY/test-about-render.lua' \
			'$EXTENSIONS_DIRECTORY/about-render.lua' nolpeg
	"
done

test_expect_success 'create a repository with a markdown readme' '
	test_create_repo repos/md &&
	(
		cd repos/md &&
		cat >README.md <<-\EOF &&
		# The Title

		A **bold** claim and a <script>alert(1)</script> attempt.
		EOF
		git add README.md &&
		git commit -m "add readme"
	)
'

test_expect_success 'point cgit at it through the about filter' '
	cat >cgitrc <<-EOF
	virtual-root=/
	cache-size=0
	enable-filter-overrides=1
	repo.url=md
	repo.path=$PWD/repos/md/.git
	repo.readme=master:README.md
	repo.about-filter=lua:$EXTENSIONS_DIRECTORY/about-render.lua
	EOF
'

if test "$CGIT_HAS_LUA" -eq 1
then
	if cgit_lua_probe "$PWD/repos/md/.git" lpeg
	then
		test_set_prereq CGIT_LUA_LPEG
	else
		test_set_prereq CGIT_LUA_NOLPEG
		say 'cgit lua has no lpeg, expecting the plain text fallback'
	fi
else
	say 'cgit built without lua, filter checks through cgit skipped'
fi

test_expect_success CGIT_LUA_LPEG 'the about page renders the markdown' '
	cgit_url "md/about/" >tmp &&
	grep "<h1>The Title</h1>" tmp &&
	grep "<strong>bold</strong>" tmp
'

test_expect_success CGIT_LUA_LPEG 'readme markup reaches the page escaped' '
	cgit_url "md/about/" >tmp &&
	grep "&lt;script&gt;alert(1)&lt;/script&gt;" tmp &&
	! grep "<script>alert(1)" tmp
'

test_expect_success CGIT_LUA_NOLPEG 'without lpeg the readme is served escaped' '
	cgit_url "md/about/" >tmp &&
	grep "<pre class=.plaintext.>" tmp &&
	grep "# The Title" tmp
'

test_done