|
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 => 5; |
| 21 |
use Test::Mojo; |
| 22 |
use Test::Warn; |
| 23 |
|
| 24 |
use t::lib::TestBuilder; |
| 25 |
use t::lib::Mocks; |
| 26 |
|
| 27 |
use List::Util qw(min); |
| 28 |
|
| 29 |
use Koha::Biblio::ItemGroups; |
| 30 |
use Koha::Libraries; |
| 31 |
use Koha::Database; |
| 32 |
|
| 33 |
my $schema = Koha::Database->new->schema; |
| 34 |
my $builder = t::lib::TestBuilder->new; |
| 35 |
|
| 36 |
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); |
| 37 |
t::lib::Mocks::mock_preference( 'EnableItemGroups', 1 ); |
| 38 |
|
| 39 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
| 40 |
|
| 41 |
subtest 'list() tests' => sub { |
| 42 |
plan tests => 9; |
| 43 |
|
| 44 |
$schema->storage->txn_begin; |
| 45 |
|
| 46 |
my $patron = $builder->build_object({ |
| 47 |
class => 'Koha::Patrons', |
| 48 |
value => { flags => 4 } |
| 49 |
}); |
| 50 |
my $password = 'thePassword123'; |
| 51 |
$patron->set_password({ password => $password, skip_validation => 1 }); |
| 52 |
my $userid = $patron->userid; |
| 53 |
|
| 54 |
my $biblio = $builder->build_sample_biblio(); |
| 55 |
my $biblio_id = $biblio->id; |
| 56 |
|
| 57 |
$t->get_ok( "//$userid:$password@/api/v1/biblios/$biblio_id/item_groups" ) |
| 58 |
->status_is( 200, 'SWAGGER3.2.2' ); |
| 59 |
my $response_count = scalar @{ $t->tx->res->json }; |
| 60 |
is( $response_count, 0, 'Results count is 2'); |
| 61 |
|
| 62 |
my $item_group_1 = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id, display_order => 1, description => "Vol 1" } )->store(); |
| 63 |
|
| 64 |
$t->get_ok( "//$userid:$password@/api/v1/biblios/$biblio_id/item_groups" ) |
| 65 |
->status_is( 200, 'SWAGGER3.2.2' ); |
| 66 |
$response_count = scalar @{ $t->tx->res->json }; |
| 67 |
is( $response_count, 1, 'Results count is 2'); |
| 68 |
|
| 69 |
my $item_group_2 = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id, display_order => 2, description => "Vol 2" } )->store(); |
| 70 |
|
| 71 |
$t->get_ok( "//$userid:$password@/api/v1/biblios/$biblio_id/item_groups" ) |
| 72 |
->status_is( 200, 'SWAGGER3.2.2' ); |
| 73 |
|
| 74 |
$response_count = scalar @{ $t->tx->res->json }; |
| 75 |
is( $response_count, 2, 'Results count is 2'); |
| 76 |
|
| 77 |
$schema->storage->txn_rollback; |
| 78 |
}; |
| 79 |
|
| 80 |
subtest 'add() tests' => sub { |
| 81 |
|
| 82 |
plan tests => 6; |
| 83 |
|
| 84 |
$schema->storage->txn_begin; |
| 85 |
|
| 86 |
my $authorized_patron = $builder->build_object({ |
| 87 |
class => 'Koha::Patrons', |
| 88 |
value => { flags => 1 } |
| 89 |
}); |
| 90 |
my $password = 'thePassword123'; |
| 91 |
$authorized_patron->set_password({ password => $password, skip_validation => 1 }); |
| 92 |
my $auth_userid = $authorized_patron->userid; |
| 93 |
|
| 94 |
my $unauthorized_patron = $builder->build_object({ |
| 95 |
class => 'Koha::Patrons', |
| 96 |
value => { flags => 0 } |
| 97 |
}); |
| 98 |
$unauthorized_patron->set_password({ password => $password, skip_validation => 1 }); |
| 99 |
my $unauth_userid = $unauthorized_patron->userid; |
| 100 |
|
| 101 |
my $biblio = $builder->build_sample_biblio(); |
| 102 |
my $biblio_id = $biblio->id; |
| 103 |
my $item_group = { description => 'Vol 1', display_order => 1 }; |
| 104 |
|
| 105 |
# Unauthorized attempt |
| 106 |
$t->post_ok( "//$unauth_userid:$password@/api/v1/biblios/$biblio_id/item_groups" => json => $item_group ) |
| 107 |
->status_is(403); |
| 108 |
|
| 109 |
# Authorized attempt |
| 110 |
$t->post_ok( "//$auth_userid:$password@/api/v1/biblios/$biblio_id/item_groups" => json => $item_group ) |
| 111 |
->status_is( 201, 'SWAGGER3.2.1' ); |
| 112 |
|
| 113 |
# Invalid biblio id |
| 114 |
{ # hide useless warnings |
| 115 |
local *STDERR; |
| 116 |
open STDERR, '>', '/dev/null'; |
| 117 |
$t->post_ok( "//$auth_userid:$password@/api/v1/biblios/XXX/item_groups" => json => $item_group ) |
| 118 |
->status_is( 404 ); |
| 119 |
close STDERR; |
| 120 |
} |
| 121 |
|
| 122 |
$schema->storage->txn_rollback; |
| 123 |
}; |
| 124 |
|
| 125 |
subtest 'update() tests' => sub { |
| 126 |
plan tests => 9; |
| 127 |
|
| 128 |
$schema->storage->txn_begin; |
| 129 |
|
| 130 |
my $authorized_patron = $builder->build_object({ |
| 131 |
class => 'Koha::Patrons', |
| 132 |
value => { flags => 1 } |
| 133 |
}); |
| 134 |
my $password = 'thePassword123'; |
| 135 |
$authorized_patron->set_password({ password => $password, skip_validation => 1 }); |
| 136 |
my $auth_userid = $authorized_patron->userid; |
| 137 |
|
| 138 |
my $unauthorized_patron = $builder->build_object({ |
| 139 |
class => 'Koha::Patrons', |
| 140 |
value => { flags => 0 } |
| 141 |
}); |
| 142 |
$unauthorized_patron->set_password({ password => $password, skip_validation => 1 }); |
| 143 |
my $unauth_userid = $unauthorized_patron->userid; |
| 144 |
|
| 145 |
my $biblio = $builder->build_sample_biblio(); |
| 146 |
my $biblio_id = $biblio->id; |
| 147 |
my $item_group = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id, display_order => 1, description => "Vol 1" } )->store(); |
| 148 |
my $item_group_id = $item_group->id; |
| 149 |
|
| 150 |
# Unauthorized attempt |
| 151 |
$t->put_ok( "//$unauth_userid:$password@/api/v1/biblios/$biblio_id/item_groups/$item_group_id" |
| 152 |
=> json => { description => 'New unauthorized desc change' } ) |
| 153 |
->status_is(403); |
| 154 |
|
| 155 |
# Authorized attempt |
| 156 |
$t->put_ok( "//$auth_userid:$password@/api/v1/biblios/$biblio_id/item_groups/$item_group_id" => json => { description => "Vol A" } ) |
| 157 |
->status_is(200, 'SWAGGER3.2.1') |
| 158 |
->json_has( '/description' => "Vol A", 'SWAGGER3.3.3' ); |
| 159 |
|
| 160 |
# Invalid biblio id |
| 161 |
$t->put_ok( "//$auth_userid:$password@/api/v1/biblios/XXX/item_groups/$item_group_id" => json => { description => "Vol A" } ) |
| 162 |
->status_is(404); |
| 163 |
|
| 164 |
# Invalid item group id |
| 165 |
$t->put_ok( "//$auth_userid:$password@/api/v1/biblios/$biblio_id/item_groups/XXX" => json => { description => "Vol A" } ) |
| 166 |
->status_is(404); |
| 167 |
|
| 168 |
$schema->storage->txn_rollback; |
| 169 |
}; |
| 170 |
|
| 171 |
subtest 'delete() tests' => sub { |
| 172 |
plan tests => 9; |
| 173 |
|
| 174 |
$schema->storage->txn_begin; |
| 175 |
|
| 176 |
my $authorized_patron = $builder->build_object({ |
| 177 |
class => 'Koha::Patrons', |
| 178 |
value => { flags => 1 } |
| 179 |
}); |
| 180 |
my $password = 'thePassword123'; |
| 181 |
$authorized_patron->set_password({ password => $password, skip_validation => 1 }); |
| 182 |
my $auth_userid = $authorized_patron->userid; |
| 183 |
|
| 184 |
my $unauthorized_patron = $builder->build_object({ |
| 185 |
class => 'Koha::Patrons', |
| 186 |
value => { flags => 0 } |
| 187 |
}); |
| 188 |
$unauthorized_patron->set_password({ password => $password, skip_validation => 1 }); |
| 189 |
my $unauth_userid = $unauthorized_patron->userid; |
| 190 |
|
| 191 |
my $biblio = $builder->build_sample_biblio(); |
| 192 |
my $biblio_id = $biblio->id; |
| 193 |
my $item_group = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id, display_order => 1, description => "Vol 1" } )->store(); |
| 194 |
my $item_groupid = $item_group->id; |
| 195 |
|
| 196 |
$t->delete_ok( "//$auth_userid:$password@/api/v1/biblios/$biblio_id/item_groups/$item_groupid" ) |
| 197 |
->status_is(204, 'SWAGGER3.2.4') |
| 198 |
->content_is('', 'SWAGGER3.3.4'); |
| 199 |
|
| 200 |
# Unauthorized attempt to delete |
| 201 |
$t->delete_ok( "//$unauth_userid:$password@/api/v1/biblios/$biblio_id/item_groups/$item_groupid" ) |
| 202 |
->status_is(403); |
| 203 |
|
| 204 |
$t->delete_ok( "//$auth_userid:$password@/api/v1/biblios/XXX/item_groups/$item_groupid" ) |
| 205 |
->status_is(404); |
| 206 |
|
| 207 |
$t->delete_ok( "//$auth_userid:$password@/api/v1/biblios/$biblio_id/item_groups/XXX" ) |
| 208 |
->status_is(404); |
| 209 |
|
| 210 |
$schema->storage->txn_rollback; |
| 211 |
}; |
| 212 |
|
| 213 |
subtest 'volume items add() + delete() tests' => sub { |
| 214 |
plan tests => 14; |
| 215 |
|
| 216 |
$schema->storage->txn_begin; |
| 217 |
|
| 218 |
my $patron = $builder->build_object({ |
| 219 |
class => 'Koha::Patrons', |
| 220 |
value => { flags => 4 } |
| 221 |
}); |
| 222 |
my $password = 'thePassword123'; |
| 223 |
$patron->set_password({ password => $password, skip_validation => 1 }); |
| 224 |
my $userid = $patron->userid; |
| 225 |
|
| 226 |
my $biblio = $builder->build_sample_biblio(); |
| 227 |
my $biblio_id = $biblio->id; |
| 228 |
|
| 229 |
my $item_group = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id, display_order => 1, description => "Vol 1" } )->store(); |
| 230 |
my $item_groupid = $item_group->id; |
| 231 |
|
| 232 |
my @items = $item_group->items; |
| 233 |
is( scalar(@items), 0, 'Item group has no items'); |
| 234 |
|
| 235 |
my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber }); |
| 236 |
my $item_1_id = $item_1->id; |
| 237 |
|
| 238 |
$t->post_ok( "//$userid:$password@/api/v1/biblios/XXX/item_groups/$item_groupid/items" => json => { item_id => $item_1->id } ) |
| 239 |
->status_is( 409 ) |
| 240 |
->json_is( { error => 'Item group does not belong to passed biblio_id' } ); |
| 241 |
|
| 242 |
$t->post_ok( "//$userid:$password@/api/v1/biblios/$biblio_id/item_groups/$item_groupid/items" => json => { item_id => $item_1->id } ) |
| 243 |
->status_is( 201, 'SWAGGER3.2.1' ); |
| 244 |
|
| 245 |
@items = $item_group->items; |
| 246 |
is( scalar(@items), 1, 'Item group now has one item'); |
| 247 |
|
| 248 |
my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber }); |
| 249 |
my $item_2_id = $item_2->id; |
| 250 |
|
| 251 |
$t->post_ok( "//$userid:$password@/api/v1/biblios/$biblio_id/item_groups/$item_groupid/items" => json => { item_id => $item_2->id } ) |
| 252 |
->status_is( 201, 'SWAGGER3.2.1' ); |
| 253 |
|
| 254 |
@items = $item_group->items; |
| 255 |
is( scalar(@items), 2, 'Item group now has two items'); |
| 256 |
|
| 257 |
$t->delete_ok( "//$userid:$password@/api/v1/biblios/$biblio_id/item_groups/$item_groupid/items/$item_1_id" ) |
| 258 |
->status_is(204, 'SWAGGER3.2.4') |
| 259 |
->content_is('', 'SWAGGER3.3.4'); |
| 260 |
|
| 261 |
@items = $item_group->items; |
| 262 |
is( scalar(@items), 1, 'Item group now has one item'); |
| 263 |
|
| 264 |
$schema->storage->txn_rollback; |
| 265 |
}; |