|
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 <http://www.gnu.org/licenses>. |
| 17 |
|
| 18 |
use Modern::Perl; |
| 19 |
|
| 20 |
use Test::More tests => 2; |
| 21 |
use Test::MockModule; |
| 22 |
use Test::Mojo; |
| 23 |
|
| 24 |
use t::lib::Mocks; |
| 25 |
use t::lib::TestBuilder; |
| 26 |
|
| 27 |
use Koha::Database; |
| 28 |
use Koha::Exceptions; |
| 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 |
subtest 'render_resource_not_found() tests' => sub { |
| 36 |
|
| 37 |
plan tests => 6; |
| 38 |
|
| 39 |
$schema->storage->txn_begin; |
| 40 |
|
| 41 |
my $authorized_patron = $builder->build_object( |
| 42 |
{ |
| 43 |
class => 'Koha::Patrons', |
| 44 |
value => { flags => 1 }, |
| 45 |
} |
| 46 |
); |
| 47 |
my $password = 'thePassword123'; |
| 48 |
$authorized_patron->set_password( { password => $password, skip_validation => 1 } ); |
| 49 |
my $userid = $authorized_patron->userid; |
| 50 |
|
| 51 |
my $mock_cities = Test::MockModule->new('Koha::REST::V1::Cities'); |
| 52 |
$mock_cities->mock( |
| 53 |
'get', |
| 54 |
sub { |
| 55 |
my $c = shift->openapi->valid_input or return; |
| 56 |
$c->render_resource_not_found; |
| 57 |
} |
| 58 |
); |
| 59 |
|
| 60 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
| 61 |
|
| 62 |
$t->get_ok("//$userid:$password@/api/v1/cities/1")->status_is('404')->json_is( |
| 63 |
{ |
| 64 |
error => 'Resource not found', |
| 65 |
error_code => 'not_found', |
| 66 |
} |
| 67 |
); |
| 68 |
|
| 69 |
$mock_cities->mock( |
| 70 |
'get', |
| 71 |
sub { |
| 72 |
my $c = shift->openapi->valid_input or return; |
| 73 |
$c->render_resource_not_found("Thing"); |
| 74 |
} |
| 75 |
); |
| 76 |
|
| 77 |
$t->get_ok("//$userid:$password@/api/v1/cities/1")->status_is('404')->json_is( |
| 78 |
{ |
| 79 |
error => 'Thing not found', |
| 80 |
error_code => 'not_found', |
| 81 |
} |
| 82 |
); |
| 83 |
|
| 84 |
$schema->storage->txn_rollback; |
| 85 |
}; |
| 86 |
|
| 87 |
subtest 'render_resource_deleted() tests' => sub { |
| 88 |
|
| 89 |
plan tests => 3; |
| 90 |
|
| 91 |
$schema->storage->txn_begin; |
| 92 |
|
| 93 |
my $authorized_patron = $builder->build_object( |
| 94 |
{ |
| 95 |
class => 'Koha::Patrons', |
| 96 |
value => { flags => 1 }, |
| 97 |
} |
| 98 |
); |
| 99 |
my $password = 'thePassword123'; |
| 100 |
$authorized_patron->set_password( { password => $password, skip_validation => 1 } ); |
| 101 |
my $userid = $authorized_patron->userid; |
| 102 |
|
| 103 |
my $mock_cities = Test::MockModule->new('Koha::REST::V1::Cities'); |
| 104 |
$mock_cities->mock( |
| 105 |
'delete', |
| 106 |
sub { |
| 107 |
my $c = shift->openapi->valid_input or return; |
| 108 |
return $c->render_resource_deleted; |
| 109 |
} |
| 110 |
); |
| 111 |
|
| 112 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
| 113 |
|
| 114 |
$t->delete_ok("//$userid:$password@/api/v1/cities/1")->status_is('204')->content_is(q{}); |
| 115 |
|
| 116 |
$schema->storage->txn_rollback; |
| 117 |
}; |