|
Line 0
Link Here
|
|
|
1 |
#!/usr/bin/env perl |
| 2 |
|
| 3 |
# Copyright 2023 Theke Solutions |
| 4 |
# |
| 5 |
# This file is part of Koha. |
| 6 |
# |
| 7 |
# Koha is free software; you can redistribute it and/or modify it |
| 8 |
# under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation; either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# Koha is distributed in the hope that it will be useful, but |
| 13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 19 |
|
| 20 |
use Modern::Perl; |
| 21 |
|
| 22 |
use Test::More tests => 5; |
| 23 |
use Test::Mojo; |
| 24 |
use Test::Warn; |
| 25 |
|
| 26 |
use t::lib::TestBuilder; |
| 27 |
use t::lib::Mocks; |
| 28 |
|
| 29 |
use C4::Auth; |
| 30 |
use Koha::ImportSources; |
| 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 |
|
| 38 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
| 39 |
|
| 40 |
subtest 'list() tests' => sub { |
| 41 |
|
| 42 |
plan tests => 12; |
| 43 |
|
| 44 |
$schema->storage->txn_begin; |
| 45 |
|
| 46 |
my $source = $builder->build_object( |
| 47 |
{ |
| 48 |
class => 'Koha::ImportSources' |
| 49 |
} |
| 50 |
); |
| 51 |
my $patron = $builder->build_object( |
| 52 |
{ |
| 53 |
class => 'Koha::Patrons', |
| 54 |
value => { flags => 3 } |
| 55 |
} |
| 56 |
); |
| 57 |
|
| 58 |
for ( 1..10 ) { |
| 59 |
$builder->build_object( |
| 60 |
{ |
| 61 |
class => 'Koha::ImportSources' |
| 62 |
} |
| 63 |
); |
| 64 |
} |
| 65 |
|
| 66 |
my $nonprivilegedpatron = $builder->build_object( |
| 67 |
{ |
| 68 |
class => 'Koha::Patrons', |
| 69 |
value => { flags => 0 } |
| 70 |
} |
| 71 |
); |
| 72 |
|
| 73 |
my $password = 'thePassword123'; |
| 74 |
|
| 75 |
$nonprivilegedpatron->set_password( |
| 76 |
{ password => $password, skip_validation => 1 } ); |
| 77 |
my $userid = $nonprivilegedpatron->userid; |
| 78 |
|
| 79 |
$t->get_ok( "//$userid:$password@/api/v1/import_sources" ) |
| 80 |
->status_is(403) |
| 81 |
->json_is( |
| 82 |
'/error' => 'Authorization failure. Missing required permission(s).' ); |
| 83 |
|
| 84 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 85 |
$userid = $patron->userid; |
| 86 |
|
| 87 |
$t->get_ok( "//$userid:$password@/api/v1/import_sources?_per_page=10" ) |
| 88 |
->status_is( 200, 'SWAGGER3.2.2' ); |
| 89 |
|
| 90 |
my $response_count = scalar @{ $t->tx->res->json }; |
| 91 |
|
| 92 |
is( $response_count, 10, 'The API returns 10 sources' ); |
| 93 |
|
| 94 |
my $id = $source->import_source_id; |
| 95 |
$t->get_ok( "//$userid:$password@/api/v1/import_sources?q={\"import_source_id\": $id}" ) |
| 96 |
->status_is(200) |
| 97 |
->json_is( '' => [ $source->to_api ], 'SWAGGER3.3.2'); |
| 98 |
|
| 99 |
$source->delete; |
| 100 |
|
| 101 |
$t->get_ok( "//$userid:$password@/api/v1/import_sources?q={\"import_source_id\": $id}" ) |
| 102 |
->status_is(200) |
| 103 |
->json_is( '' => [] ); |
| 104 |
|
| 105 |
$schema->storage->txn_rollback; |
| 106 |
|
| 107 |
}; |
| 108 |
|
| 109 |
subtest 'get() tests' => sub { |
| 110 |
|
| 111 |
plan tests => 9; |
| 112 |
|
| 113 |
$schema->storage->txn_begin; |
| 114 |
|
| 115 |
my $source = $builder->build_object( |
| 116 |
{ |
| 117 |
class => 'Koha::ImportSources' |
| 118 |
} |
| 119 |
); |
| 120 |
my $patron = $builder->build_object( |
| 121 |
{ |
| 122 |
class => 'Koha::Patrons', |
| 123 |
value => { flags => 3 } |
| 124 |
} |
| 125 |
); |
| 126 |
|
| 127 |
my $nonprivilegedpatron = $builder->build_object( |
| 128 |
{ |
| 129 |
class => 'Koha::Patrons', |
| 130 |
value => { flags => 0 } |
| 131 |
} |
| 132 |
); |
| 133 |
|
| 134 |
my $password = 'thePassword123'; |
| 135 |
|
| 136 |
$nonprivilegedpatron->set_password( |
| 137 |
{ password => $password, skip_validation => 1 } ); |
| 138 |
my $userid = $nonprivilegedpatron->userid; |
| 139 |
|
| 140 |
my $id = $source->import_source_id; |
| 141 |
|
| 142 |
$t->get_ok( "//$userid:$password@/api/v1/import_sources/$id" ) |
| 143 |
->status_is(403) |
| 144 |
->json_is( |
| 145 |
'/error' => 'Authorization failure. Missing required permission(s).' ); |
| 146 |
|
| 147 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 148 |
$userid = $patron->userid; |
| 149 |
|
| 150 |
$t->get_ok( "//$userid:$password@/api/v1/import_sources/$id" ) |
| 151 |
->status_is( 200, 'SWAGGER3.2.2' ) |
| 152 |
->json_is( '' => $source->to_api, 'SWAGGER3.3.2' ); |
| 153 |
|
| 154 |
$source->delete; |
| 155 |
|
| 156 |
$t->get_ok( "//$userid:$password@/api/v1/import_sources/$id" ) |
| 157 |
->status_is(404) |
| 158 |
->json_is( '/error' => 'Import source not found' ); |
| 159 |
|
| 160 |
$schema->storage->txn_rollback; |
| 161 |
|
| 162 |
}; |
| 163 |
|
| 164 |
subtest 'delete() tests' => sub { |
| 165 |
|
| 166 |
plan tests => 6; |
| 167 |
|
| 168 |
$schema->storage->txn_begin; |
| 169 |
|
| 170 |
my $source = $builder->build_object( |
| 171 |
{ |
| 172 |
class => 'Koha::ImportSources' |
| 173 |
} |
| 174 |
); |
| 175 |
my $patron = $builder->build_object( |
| 176 |
{ |
| 177 |
class => 'Koha::Patrons', |
| 178 |
value => { flags => 3 } |
| 179 |
} |
| 180 |
); |
| 181 |
|
| 182 |
my $nonprivilegedpatron = $builder->build_object( |
| 183 |
{ |
| 184 |
class => 'Koha::Patrons', |
| 185 |
value => { flags => 0 } |
| 186 |
} |
| 187 |
); |
| 188 |
|
| 189 |
my $password = 'thePassword123'; |
| 190 |
|
| 191 |
$nonprivilegedpatron->set_password( |
| 192 |
{ password => $password, skip_validation => 1 } ); |
| 193 |
my $userid = $nonprivilegedpatron->userid; |
| 194 |
|
| 195 |
my $id = $source->import_source_id; |
| 196 |
|
| 197 |
$t->delete_ok( "//$userid:$password@/api/v1/import_sources/$id" ) |
| 198 |
->status_is(403) |
| 199 |
->json_is( |
| 200 |
'/error' => 'Authorization failure. Missing required permission(s).' ); |
| 201 |
|
| 202 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 203 |
$userid = $patron->userid; |
| 204 |
|
| 205 |
$t->delete_ok( "//$userid:$password@/api/v1/import_sources/$id" ) |
| 206 |
->status_is( 204, 'SWAGGER3.2.2' ); |
| 207 |
|
| 208 |
my $deleted_source = Koha::ImportSources->search({import_source_id => $id}); |
| 209 |
|
| 210 |
is($deleted_source->count, 0, 'No import source found'); |
| 211 |
|
| 212 |
$schema->storage->txn_rollback; |
| 213 |
|
| 214 |
}; |
| 215 |
|
| 216 |
subtest 'add() tests' => sub { |
| 217 |
|
| 218 |
plan tests => 8; |
| 219 |
|
| 220 |
$schema->storage->txn_begin; |
| 221 |
|
| 222 |
Koha::ImportSources->delete; |
| 223 |
|
| 224 |
my $patron = $builder->build_object( |
| 225 |
{ |
| 226 |
class => 'Koha::Patrons', |
| 227 |
value => { flags => 3 } |
| 228 |
} |
| 229 |
); |
| 230 |
|
| 231 |
my $nonprivilegedpatron = $builder->build_object( |
| 232 |
{ |
| 233 |
class => 'Koha::Patrons', |
| 234 |
value => { flags => 0 } |
| 235 |
} |
| 236 |
); |
| 237 |
|
| 238 |
my $password = 'thePassword123'; |
| 239 |
|
| 240 |
$nonprivilegedpatron->set_password( |
| 241 |
{ password => $password, skip_validation => 1 } ); |
| 242 |
my $userid = $nonprivilegedpatron->userid; |
| 243 |
my $patron_id = $nonprivilegedpatron->borrowernumber; |
| 244 |
|
| 245 |
$t->post_ok( "//$userid:$password@/api/v1/import_sources" => json => { name => 'test1', patron_id => $patron_id }) |
| 246 |
->status_is(403) |
| 247 |
->json_is( |
| 248 |
'/error' => 'Authorization failure. Missing required permission(s).' ); |
| 249 |
|
| 250 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 251 |
$userid = $patron->userid; |
| 252 |
|
| 253 |
$t->post_ok( "//$userid:$password@/api/v1/import_sources" => json => { name => 'test1', patron_id => $patron_id }) |
| 254 |
->status_is( 201, 'SWAGGER3.2.2' ) |
| 255 |
->json_is('/name', 'test1') |
| 256 |
->json_is('/patron_id', $patron_id); |
| 257 |
|
| 258 |
my $created_source = Koha::ImportSources->search->next; |
| 259 |
|
| 260 |
is($created_source->name, 'test1', 'Import source found'); |
| 261 |
|
| 262 |
$schema->storage->txn_rollback; |
| 263 |
|
| 264 |
}; |
| 265 |
|
| 266 |
subtest 'update() tests' => sub { |
| 267 |
|
| 268 |
plan tests => 7; |
| 269 |
|
| 270 |
$schema->storage->txn_begin; |
| 271 |
|
| 272 |
my $source = $builder->build_object( |
| 273 |
{ |
| 274 |
class => 'Koha::ImportSources', |
| 275 |
value => { name => 'Oldname' } |
| 276 |
} |
| 277 |
); |
| 278 |
my $patron = $builder->build_object( |
| 279 |
{ |
| 280 |
class => 'Koha::Patrons', |
| 281 |
value => { flags => 3 } |
| 282 |
} |
| 283 |
); |
| 284 |
|
| 285 |
my $nonprivilegedpatron = $builder->build_object( |
| 286 |
{ |
| 287 |
class => 'Koha::Patrons', |
| 288 |
value => { flags => 0 } |
| 289 |
} |
| 290 |
); |
| 291 |
|
| 292 |
my $password = 'thePassword123'; |
| 293 |
|
| 294 |
$nonprivilegedpatron->set_password( |
| 295 |
{ password => $password, skip_validation => 1 } ); |
| 296 |
my $userid = $nonprivilegedpatron->userid; |
| 297 |
|
| 298 |
my $id = $source->import_source_id; |
| 299 |
|
| 300 |
$t->put_ok( "//$userid:$password@/api/v1/import_sources/$id" => json => { name => 'Newname', patron_id => $source->patron_id } ) |
| 301 |
->status_is(403) |
| 302 |
->json_is( |
| 303 |
'/error' => 'Authorization failure. Missing required permission(s).' ); |
| 304 |
|
| 305 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 306 |
$userid = $patron->userid; |
| 307 |
|
| 308 |
$t->put_ok( "//$userid:$password@/api/v1/import_sources/$id" => json => { name => 'Newname', patron_id => $source->patron_id } ) |
| 309 |
->status_is( 200, 'SWAGGER3.2.2' ) |
| 310 |
->json_is('/name', 'Newname'); |
| 311 |
|
| 312 |
my $updated_source = Koha::ImportSources->find($id); |
| 313 |
|
| 314 |
is($updated_source->name, 'Newname', 'Import source updated'); |
| 315 |
|
| 316 |
$schema->storage->txn_rollback; |
| 317 |
|
| 318 |
}; |