|
Lines 22-27
use Test::More tests => 2;
Link Here
|
| 22 |
use Test::Mojo; |
22 |
use Test::Mojo; |
| 23 |
use Test::Warn; |
23 |
use Test::Warn; |
| 24 |
|
24 |
|
|
|
25 |
use Carp qw( carp ); |
| 26 |
use Log::Log4perl; |
| 27 |
|
| 25 |
use t::lib::Mocks; |
28 |
use t::lib::Mocks; |
| 26 |
|
29 |
|
| 27 |
use Koha::Database; |
30 |
use Koha::Database; |
|
Lines 33-39
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
Link Here
|
| 33 |
|
36 |
|
| 34 |
subtest 'add() tests' => sub { |
37 |
subtest 'add() tests' => sub { |
| 35 |
|
38 |
|
| 36 |
plan tests => 6; |
39 |
plan tests => 7; |
| 37 |
|
40 |
|
| 38 |
$schema->storage->txn_begin; |
41 |
$schema->storage->txn_begin; |
| 39 |
|
42 |
|
|
Lines 74-79
subtest 'add() tests' => sub {
Link Here
|
| 74 |
$t->post_ok( '/api/v1/public/csp-reports' => json => $minimal_report ) |
77 |
$t->post_ok( '/api/v1/public/csp-reports' => json => $minimal_report ) |
| 75 |
->status_is( 204, 'Minimal CSP report accepted' ); |
78 |
->status_is( 204, 'Minimal CSP report accepted' ); |
| 76 |
|
79 |
|
|
|
80 |
subtest 'make sure log entries are being written' => sub { |
| 81 |
plan tests => 17; |
| 82 |
|
| 83 |
my $log4perl_conf_file = C4::Context->config('intranetdir') . '/etc/log4perl.conf'; |
| 84 |
open my $fh, '<:encoding(UTF-8)', $log4perl_conf_file or do { |
| 85 |
carp "Cannot read $log4perl_conf_file: $!"; |
| 86 |
return; |
| 87 |
}; |
| 88 |
my $log4perl_conf_content = ""; |
| 89 |
while (<$fh>) { |
| 90 |
my $line = $_; |
| 91 |
next unless $line =~ /^(log4perl.logger.(plack-)?csp)|(log4perl.appender.(PLACK)?CSP)/; |
| 92 |
$log4perl_conf_content .= "$line"; |
| 93 |
} |
| 94 |
close $fh; |
| 95 |
like( $log4perl_conf_content, qr/__LOG_DIR__\/csp-violations\.log/, 'csp-violations.log is defined' ); |
| 96 |
like( |
| 97 |
$log4perl_conf_content, qr/__LOG_DIR__\/plack-csp-violations\.log/, |
| 98 |
'plack-csp-violations.log is defined' |
| 99 |
); |
| 100 |
like( |
| 101 |
$log4perl_conf_content, qr/log4perl.appender.CSP=Log::Log4perl::Appender::File/, |
| 102 |
'CSP: File appender exists' |
| 103 |
); |
| 104 |
like( |
| 105 |
$log4perl_conf_content, qr/log4perl.appender.PLACKCSP=Log::Log4perl::Appender::File/, |
| 106 |
'PLACKCSP: File appender exists' |
| 107 |
); |
| 108 |
$log4perl_conf_content =~ s/Log::Log4perl::Appender::File/Log::Log4perl::Appender::TestBuffer/g; |
| 109 |
Log::Log4perl::init( \$log4perl_conf_content ); |
| 110 |
my $appenders = Log::Log4perl->appenders; |
| 111 |
my $appender = Log::Log4perl->appenders->{CSP}; |
| 112 |
my $appenderplack = Log::Log4perl->appenders->{PLACKCSP}; |
| 113 |
|
| 114 |
is( $appender->buffer, '', 'Nothing in log buffer yet' ); |
| 115 |
is( $appenderplack->buffer, '', 'Nothing in plack log buffer yet' ); |
| 116 |
|
| 117 |
$t->post_ok( |
| 118 |
'/api/v1/public/csp-reports' => { 'Content-Type' => 'application/csp-report' } => json => $csp_report ) |
| 119 |
->status_is( 204, 'CSP report accepted' ); |
| 120 |
|
| 121 |
my $expected_log_entry = sprintf( |
| 122 |
"CSP violation: '%s' blocked '%s' on page '%s'%s", |
| 123 |
$csp_report->{'csp-report'}->{'violated-directive'}, |
| 124 |
$csp_report->{'csp-report'}->{'blocked-uri'}, |
| 125 |
$csp_report->{'csp-report'}->{'document-uri'}, |
| 126 |
' at ' |
| 127 |
. $csp_report->{'csp-report'}->{'source-file'} . ':' |
| 128 |
. $csp_report->{'csp-report'}->{'line-number'} . ':' |
| 129 |
. $csp_report->{'csp-report'}->{'column-number'} |
| 130 |
); |
| 131 |
|
| 132 |
like( |
| 133 |
$appender->buffer, qr/$expected_log_entry/ |
| 134 |
, 'Log contains expected log entry' |
| 135 |
); |
| 136 |
is( $appenderplack->buffer, '', 'Nothing in plack log buffer yet' ); |
| 137 |
$appender->clear(); |
| 138 |
|
| 139 |
$ENV{'plack.is.enabled.for.this.test'} = 1; # tricking C4::Context->psgi_env |
| 140 |
$t->post_ok( |
| 141 |
'/api/v1/public/csp-reports' => { 'Content-Type' => 'application/csp-report' } => json => $csp_report ) |
| 142 |
->status_is( 204, 'CSP report accepted' ); |
| 143 |
like( |
| 144 |
$appenderplack->buffer, qr/$expected_log_entry/ |
| 145 |
, 'Plack log contains expected log entry' |
| 146 |
); |
| 147 |
is( $appender->buffer, '', 'Nothing in log buffer yet' ); |
| 148 |
|
| 149 |
Log::Log4perl::init( |
| 150 |
\<<"HERE" |
| 151 |
log4perl.logger.api = WARN, API |
| 152 |
log4perl.appender.API=Log::Log4perl::Appender::TestBuffer |
| 153 |
log4perl.appender.API.mode=append |
| 154 |
log4perl.appender.API.layout=PatternLayout |
| 155 |
log4perl.appender.API.layout.ConversionPattern=[%d] [%p] %m %l%n |
| 156 |
log4perl.appender.API.utf8=1 |
| 157 |
HERE |
| 158 |
); |
| 159 |
$appender = Log::Log4perl->appenders()->{API}; |
| 160 |
$t->post_ok( |
| 161 |
'/api/v1/public/csp-reports' => { 'Content-Type' => 'application/csp-report' } => json => $csp_report ) |
| 162 |
->status_is( 204, 'CSP report returns 204 even when no CSP loggers are defined' ); |
| 163 |
is( $appender->buffer, '', 'Nothing in the only defined log buffer, because it is unrelated to CSP' ); |
| 164 |
|
| 165 |
}; |
| 166 |
|
| 77 |
$schema->storage->txn_rollback; |
167 |
$schema->storage->txn_rollback; |
| 78 |
}; |
168 |
}; |
| 79 |
|
169 |
|
| 80 |
- |
|
|