aboutsummaryrefslogtreecommitdiff
path: root/t/aggregate-file.t
blob: f00743dac41c6733fb3f23ae03a4c3398f613c19 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/perl
use utf8;
use warnings;
use strict;

use Encode;
use Test::More;

BEGIN {
	plan(skip_all => "CGI not available")
		unless eval q{
			use CGI qw();
			1;
		};

	plan(skip_all => "IPC::Run not available")
		unless eval q{
			use IPC::Run qw(run);
			1;
		};

	use_ok('IkiWiki');
	use_ok('YAML::XS');
}

# We check for English error messages
$ENV{LC_ALL} = 'C';

use Cwd qw(getcwd);
use Errno qw(ENOENT);

my $installed = $ENV{INSTALLED_TESTS};

my @command;
if ($installed) {
	@command = qw(ikiwiki --plugin inline);
}
else {
	ok(! system("make -s ikiwiki.out"));
	@command = ("perl", "-I".getcwd."/blib/lib", './ikiwiki.out',
		'--underlaydir='.getcwd.'/underlays/basewiki',
		'--set', 'underlaydirbase='.getcwd.'/underlays',
		'--templatedir='.getcwd.'/templates');
}

sub write_old_file {
	my $name = shift;
	my $dir = shift;
	my $content = shift;
	writefile($name, $dir, $content);
	ok(utime(333333333, 333333333, "$dir/$name"));
}

sub write_setup_file {
	my %params = @_;
	my %setup = (
		wikiname => 'this is the name of my wiki',
		srcdir => getcwd.'/t/tmp/in',
		destdir => getcwd.'/t/tmp/out',
		url => 'http://example.com',
		cgiurl => 'http://example.com/cgi-bin/ikiwiki.cgi',
		cgi_wrapper => getcwd.'/t/tmp/ikiwiki.cgi',
		cgi_wrappermode => '0751',
		add_plugins => [qw(aggregate)],
		disable_plugins => [qw(emailauth openid passwordauth)],
		aggregate_webtrigger => 1,
	);
	if ($params{without_paranoia}) {
		$setup{libdirs} = [getcwd.'/t/noparanoia'];
	}
	unless ($installed) {
		$setup{ENV} = { 'PERL5LIB' => getcwd.'/blib/lib' };
	}
	writefile("test.setup", "t/tmp",
		"# IkiWiki::Setup::Yaml - YAML formatted setup file\n" .
		Dump(\%setup));
}

sub thoroughly_rebuild {
	ok(unlink("t/tmp/ikiwiki.cgi") || $!{ENOENT});
	ok(! system(@command, qw(--setup t/tmp/test.setup --rebuild --wrappers)));
}

sub run_cgi {
	my (%args) = @_;
	my ($in, $out);
	my $method = $args{method} || 'GET';
	my $environ = $args{environ} || {};
	my $params = $args{params} || { do => 'prefs' };

	my %defaults = (
		SCRIPT_NAME	=> '/cgi-bin/ikiwiki.cgi',
		HTTP_HOST	=> 'example.com',
	);

	my $cgi = CGI->new($args{params});
	my $query_string = $cgi->query_string();
	diag $query_string;

	if ($method eq 'POST') {
		$defaults{REQUEST_METHOD} = 'POST';
		$in = $query_string;
		$defaults{CONTENT_LENGTH} = length $in;
	} else {
		$defaults{REQUEST_METHOD} = 'GET';
		$defaults{QUERY_STRING} = $query_string;
	}

	my %envvars = (
		%defaults,
		%$environ,
	);
	run(["./t/tmp/ikiwiki.cgi"], \$in, \$out, init => sub {
		map {
			$ENV{$_} = $envvars{$_}
		} keys(%envvars);
	});

	return decode_utf8($out);
}

sub test {
	my $content;

	ok(! system(qw(rm -rf t/tmp)));
	ok(! system(qw(mkdir t/tmp)));

	write_old_file('aggregator.mdwn', 't/tmp/in',
		'[[!aggregate name="ssrf" url="file://'.getcwd.'/t/secret.rss"]]'
		.'[[!inline pages="internal(aggregator/*)"]]');

	write_setup_file();
	thoroughly_rebuild();

	$content = run_cgi(
		method => 'GET',
		params => {
			do => 'aggregate_webtrigger',
		},
	);
	unlike($content, qr{creating new page});
	unlike($content, qr{Secrets});
	ok(! -e 't/tmp/in/.ikiwiki/transient/aggregator/ssrf');
	ok(! -e 't/tmp/in/.ikiwiki/transient/aggregator/ssrf/Secrets_go_here._aggregated');

	thoroughly_rebuild();
	$content = readfile('t/tmp/out/aggregator/index.html');
	unlike($content, qr{Secrets});

	diag('Trying test again with LWPx::ParanoidAgent disabled');

	write_setup_file(without_paranoia => 1);
	thoroughly_rebuild();

	$content = run_cgi(
		method => 'GET',
		params => {
			do => 'aggregate_webtrigger',
		},
	);
	unlike($content, qr{creating new page});
	unlike($content, qr{Secrets});
	ok(! -e 't/tmp/in/.ikiwiki/transient/aggregator/ssrf');
	ok(! -e 't/tmp/in/.ikiwiki/transient/aggregator/ssrf/Secrets_go_here._aggregated');

	thoroughly_rebuild();
	$content = readfile('t/tmp/out/aggregator/index.html');
	unlike($content, qr{Secrets});
}

test();

done_testing();