|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/env perl |
|
|
2 |
|
| 3 |
# This file is part of Koha. |
| 4 |
# |
| 5 |
# Koha is free software; you can redistribute it and/or modify it |
| 6 |
# under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation; either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# Koha is distributed in the hope that it will be useful, but |
| 11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with Koha; if not, see <https://www.gnu.org/licenses>. |
| 17 |
|
| 18 |
use Modern::Perl; |
| 19 |
|
| 20 |
use Test::NoWarnings; |
| 21 |
use Test::More tests => 2; |
| 22 |
use Test::Mojo; |
| 23 |
use Test::Warn; |
| 24 |
|
| 25 |
use t::lib::Mocks; |
| 26 |
|
| 27 |
use Koha::Database; |
| 28 |
|
| 29 |
my $schema = Koha::Database->new->schema; |
| 30 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
| 31 |
|
| 32 |
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); |
| 33 |
|
| 34 |
subtest 'add() tests' => sub { |
| 35 |
|
| 36 |
plan tests => 6; |
| 37 |
|
| 38 |
$schema->storage->txn_begin; |
| 39 |
|
| 40 |
# Test with standard CSP report format |
| 41 |
my $csp_report = { |
| 42 |
'csp-report' => { |
| 43 |
'document-uri' => 'https://library.example.org/cgi-bin/koha/opac-main.pl', |
| 44 |
'referrer' => '', |
| 45 |
'violated-directive' => "script-src 'self' 'nonce-abc123'", |
| 46 |
'effective-directive' => 'script-src', |
| 47 |
'original-policy' => "default-src 'self'; script-src 'self' 'nonce-abc123'", |
| 48 |
'disposition' => 'enforce', |
| 49 |
'blocked-uri' => 'inline', |
| 50 |
'line-number' => 42, |
| 51 |
'column-number' => 10, |
| 52 |
'source-file' => 'https://library.example.org/cgi-bin/koha/opac-main.pl', |
| 53 |
'status-code' => 200, |
| 54 |
} |
| 55 |
}; |
| 56 |
|
| 57 |
# Anonymous request should work (browsers send these without auth) |
| 58 |
$t->post_ok( '/api/v1/public/csp-reports' => { 'Content-Type' => 'application/csp-report' } => json => $csp_report ) |
| 59 |
->status_is( 204, 'CSP report accepted' ); |
| 60 |
|
| 61 |
# Test with application/json content type (also valid) |
| 62 |
$t->post_ok( '/api/v1/public/csp-reports' => { 'Content-Type' => 'application/json' } => json => $csp_report ) |
| 63 |
->status_is( 204, 'CSP report accepted with application/json content type' ); |
| 64 |
|
| 65 |
# Test with minimal report |
| 66 |
my $minimal_report = { |
| 67 |
'csp-report' => { |
| 68 |
'document-uri' => 'https://library.example.org/', |
| 69 |
'violated-directive' => 'script-src', |
| 70 |
'blocked-uri' => 'https://evil.example.com/script.js', |
| 71 |
} |
| 72 |
}; |
| 73 |
|
| 74 |
$t->post_ok( '/api/v1/public/csp-reports' => json => $minimal_report ) |
| 75 |
->status_is( 204, 'Minimal CSP report accepted' ); |
| 76 |
|
| 77 |
$schema->storage->txn_rollback; |
| 78 |
}; |
| 79 |
|
| 80 |
1; |