|
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::More tests => 2; |
| 21 |
use Test::NoWarnings; |
| 22 |
use Test::Mojo; |
| 23 |
|
| 24 |
use t::lib::Mocks; |
| 25 |
use t::lib::TestBuilder; |
| 26 |
|
| 27 |
use Koha::Biblio::Metadata::Error; |
| 28 |
use Koha::Database; |
| 29 |
|
| 30 |
my $schema = Koha::Database->new->schema; |
| 31 |
my $builder = t::lib::TestBuilder->new; |
| 32 |
|
| 33 |
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); |
| 34 |
|
| 35 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
| 36 |
|
| 37 |
subtest 'delete() tests' => sub { |
| 38 |
|
| 39 |
plan tests => 13; |
| 40 |
|
| 41 |
$schema->storage->txn_begin; |
| 42 |
|
| 43 |
my $password = 'thePassword123'; |
| 44 |
|
| 45 |
my $unauthorized_patron = $builder->build_object( |
| 46 |
{ |
| 47 |
class => 'Koha::Patrons', |
| 48 |
value => { flags => 0 } |
| 49 |
} |
| 50 |
); |
| 51 |
$unauthorized_patron->set_password( { password => $password, skip_validation => 1 } ); |
| 52 |
my $unauth_userid = $unauthorized_patron->userid; |
| 53 |
|
| 54 |
my $authorized_patron = $builder->build_object( |
| 55 |
{ |
| 56 |
class => 'Koha::Patrons', |
| 57 |
value => { flags => 0 } |
| 58 |
} |
| 59 |
); |
| 60 |
$authorized_patron->set_password( { password => $password, skip_validation => 1 } ); |
| 61 |
my $auth_userid = $authorized_patron->userid; |
| 62 |
|
| 63 |
$builder->build( |
| 64 |
{ |
| 65 |
source => 'UserPermission', |
| 66 |
value => { |
| 67 |
borrowernumber => $authorized_patron->borrowernumber, |
| 68 |
module_bit => 9, |
| 69 |
code => 'edit_catalogue', |
| 70 |
}, |
| 71 |
} |
| 72 |
); |
| 73 |
|
| 74 |
my $biblio = $builder->build_sample_biblio; |
| 75 |
my $metadata_id = $biblio->metadata->id; |
| 76 |
|
| 77 |
my $error = Koha::Biblio::Metadata::Error->new( |
| 78 |
{ |
| 79 |
metadata_id => $metadata_id, |
| 80 |
error_type => 'nonxml_stripped', |
| 81 |
message => '245$a: invalid char value 31 (U+001F) at position 5', |
| 82 |
} |
| 83 |
)->store; |
| 84 |
my $error_id = $error->id; |
| 85 |
|
| 86 |
# Unauthenticated |
| 87 |
$t->delete_ok( "/api/v1/biblios/" . $biblio->biblionumber . "/metadata/errors/$error_id" ) |
| 88 |
->status_is( 401, 'Unauthenticated request returns 401' ); |
| 89 |
|
| 90 |
# Unauthorized (no permission) |
| 91 |
$t->delete_ok( |
| 92 |
"//$unauth_userid:$password@/api/v1/biblios/" . $biblio->biblionumber . "/metadata/errors/$error_id" ) |
| 93 |
->status_is( 403, 'Missing permission returns 403' ); |
| 94 |
|
| 95 |
# Biblio not found |
| 96 |
$t->delete_ok("//$auth_userid:$password@/api/v1/biblios/0/metadata/errors/$error_id") |
| 97 |
->status_is( 404, 'Non-existent biblio returns 404' ); |
| 98 |
|
| 99 |
# Error not found (wrong error_id for this biblio) |
| 100 |
my $other_biblio = $builder->build_sample_biblio; |
| 101 |
my $other_error = Koha::Biblio::Metadata::Error->new( |
| 102 |
{ |
| 103 |
metadata_id => $other_biblio->metadata->id, |
| 104 |
error_type => 'nonxml_stripped', |
| 105 |
message => '245$a: invalid char value 31 (U+001F) at position 5', |
| 106 |
} |
| 107 |
)->store; |
| 108 |
|
| 109 |
$t->delete_ok( |
| 110 |
"//$auth_userid:$password@/api/v1/biblios/" . $biblio->biblionumber . "/metadata/errors/" . $other_error->id ) |
| 111 |
->status_is( 404, 'Error belonging to a different biblio returns 404' ); |
| 112 |
|
| 113 |
# Successful delete |
| 114 |
$t->delete_ok( "//$auth_userid:$password@/api/v1/biblios/" . $biblio->biblionumber . "/metadata/errors/$error_id" ) |
| 115 |
->status_is( 204, 'Authorized delete of existing error returns 204' ) |
| 116 |
->content_is( '', 'No response body on successful delete' ); |
| 117 |
|
| 118 |
# Already deleted / not found |
| 119 |
$t->delete_ok( "//$auth_userid:$password@/api/v1/biblios/" . $biblio->biblionumber . "/metadata/errors/$error_id" ) |
| 120 |
->status_is( 404, 'Deleting an already-deleted error returns 404' ); |
| 121 |
|
| 122 |
$schema->storage->txn_rollback; |
| 123 |
}; |