|
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 => 4; |
| 22 |
use Test::Mojo; |
| 23 |
|
| 24 |
use t::lib::TestBuilder; |
| 25 |
use t::lib::Mocks; |
| 26 |
|
| 27 |
use Koha::Items; |
| 28 |
use Koha::Old::Items; |
| 29 |
use Koha::Database; |
| 30 |
|
| 31 |
my $schema = Koha::Database->new->schema; |
| 32 |
my $builder = t::lib::TestBuilder->new; |
| 33 |
|
| 34 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
| 35 |
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); |
| 36 |
|
| 37 |
subtest 'list() tests' => sub { |
| 38 |
|
| 39 |
plan tests => 9; |
| 40 |
|
| 41 |
$schema->storage->txn_begin; |
| 42 |
|
| 43 |
Koha::Old::Items->search->delete; |
| 44 |
|
| 45 |
my $librarian = $builder->build_object( |
| 46 |
{ |
| 47 |
class => 'Koha::Patrons', |
| 48 |
value => { flags => 2**2 } |
| 49 |
} |
| 50 |
); |
| 51 |
my $password = 'thePassword123'; |
| 52 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 53 |
my $userid = $librarian->userid; |
| 54 |
|
| 55 |
my $patron = $builder->build_object( |
| 56 |
{ |
| 57 |
class => 'Koha::Patrons', |
| 58 |
value => { flags => 0 } |
| 59 |
} |
| 60 |
); |
| 61 |
|
| 62 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 63 |
my $unauth_userid = $patron->userid; |
| 64 |
|
| 65 |
$t->get_ok("//$userid:$password@/api/v1/deleted/items")->status_is(200)->json_is( [] ); |
| 66 |
|
| 67 |
my $item = $builder->build_sample_item(); |
| 68 |
my $item_id = $item->itemnumber; |
| 69 |
my $item_data = $item->unblessed; |
| 70 |
my $deleted_item = Koha::Old::Item->new($item_data)->store; |
| 71 |
$item->delete; |
| 72 |
|
| 73 |
$t->get_ok("//$userid:$password@/api/v1/deleted/items")->status_is(200); |
| 74 |
|
| 75 |
my $expected = $deleted_item->to_api; |
| 76 |
|
| 77 |
$t->json_has('/0')->json_is( '/0' => $expected ); |
| 78 |
|
| 79 |
$t->get_ok("//$unauth_userid:$password@/api/v1/deleted/items")->status_is(403); |
| 80 |
|
| 81 |
$schema->storage->txn_rollback; |
| 82 |
}; |
| 83 |
|
| 84 |
subtest 'get() tests' => sub { |
| 85 |
|
| 86 |
plan tests => 8; |
| 87 |
|
| 88 |
$schema->storage->txn_begin; |
| 89 |
|
| 90 |
my $item = $builder->build_sample_item(); |
| 91 |
my $item_id = $item->itemnumber; |
| 92 |
my $item_data = $item->unblessed; |
| 93 |
my $deleted_item = Koha::Old::Item->new($item_data)->store; |
| 94 |
$item->delete; |
| 95 |
|
| 96 |
my $librarian = $builder->build_object( |
| 97 |
{ |
| 98 |
class => 'Koha::Patrons', |
| 99 |
value => { flags => 2**2 } |
| 100 |
} |
| 101 |
); |
| 102 |
my $password = 'thePassword123'; |
| 103 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 104 |
my $userid = $librarian->userid; |
| 105 |
|
| 106 |
my $patron = $builder->build_object( |
| 107 |
{ |
| 108 |
class => 'Koha::Patrons', |
| 109 |
value => { flags => 0 } |
| 110 |
} |
| 111 |
); |
| 112 |
|
| 113 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 114 |
my $unauth_userid = $patron->userid; |
| 115 |
|
| 116 |
$t->get_ok("//$userid:$password@/api/v1/deleted/items/$item_id")->status_is(200)->json_is( $deleted_item->to_api ); |
| 117 |
|
| 118 |
$t->get_ok("//$unauth_userid:$password@/api/v1/deleted/items/$item_id")->status_is(403); |
| 119 |
|
| 120 |
my $non_existent_id = $item_id + 99999; |
| 121 |
|
| 122 |
$t->get_ok("//$userid:$password@/api/v1/deleted/items/$non_existent_id")->status_is(404) |
| 123 |
->json_is( '/error' => 'Item not found' ); |
| 124 |
|
| 125 |
$schema->storage->txn_rollback; |
| 126 |
}; |
| 127 |
|
| 128 |
subtest 'restore() tests' => sub { |
| 129 |
|
| 130 |
plan tests => 11; |
| 131 |
|
| 132 |
$schema->storage->txn_begin; |
| 133 |
|
| 134 |
my $librarian = $builder->build_object( |
| 135 |
{ |
| 136 |
class => 'Koha::Patrons', |
| 137 |
value => { flags => 0 } |
| 138 |
} |
| 139 |
); |
| 140 |
my $password = 'thePassword123'; |
| 141 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 142 |
my $userid = $librarian->userid; |
| 143 |
|
| 144 |
$builder->build( |
| 145 |
{ |
| 146 |
source => 'UserPermission', |
| 147 |
value => { |
| 148 |
borrowernumber => $librarian->borrowernumber, |
| 149 |
module_bit => 9, |
| 150 |
code => 'restore_records', |
| 151 |
} |
| 152 |
} |
| 153 |
); |
| 154 |
|
| 155 |
my $patron = $builder->build_object( |
| 156 |
{ |
| 157 |
class => 'Koha::Patrons', |
| 158 |
value => { flags => 0 } |
| 159 |
} |
| 160 |
); |
| 161 |
|
| 162 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 163 |
my $unauth_userid = $patron->userid; |
| 164 |
|
| 165 |
my $item = $builder->build_sample_item( { barcode => 'TEST_RESTORE_ITEM' } ); |
| 166 |
my $item_id = $item->itemnumber; |
| 167 |
my $item_data = $item->unblessed; |
| 168 |
my $deleted_item = Koha::Old::Item->new($item_data)->store; |
| 169 |
$item->delete; |
| 170 |
|
| 171 |
is( Koha::Items->find($item_id), undef, 'Item deleted successfully' ); |
| 172 |
ok( $deleted_item, 'Item found in deleted table' ); |
| 173 |
|
| 174 |
$t->put_ok("//$unauth_userid:$password@/api/v1/deleted/items/$item_id")->status_is(403); |
| 175 |
|
| 176 |
$t->put_ok("//$userid:$password@/api/v1/deleted/items/$item_id")->status_is(200); |
| 177 |
|
| 178 |
my $restored_item = Koha::Items->find($item_id); |
| 179 |
ok( $restored_item, 'Item restored successfully' ); |
| 180 |
is( $restored_item->barcode, 'TEST_RESTORE_ITEM', 'Item barcode preserved' ); |
| 181 |
|
| 182 |
is( Koha::Old::Items->find($item_id), undef, 'Item removed from deleted table' ); |
| 183 |
|
| 184 |
$t->put_ok("//$userid:$password@/api/v1/deleted/items/$item_id")->status_is(404); |
| 185 |
|
| 186 |
$schema->storage->txn_rollback; |
| 187 |
}; |