|
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 |
|
| 23 |
use t::lib::TestBuilder; |
| 24 |
use t::lib::Mocks; |
| 25 |
|
| 26 |
use Koha::Tickets; |
| 27 |
use Koha::Database; |
| 28 |
|
| 29 |
my $schema = Koha::Database->new->schema; |
| 30 |
my $builder = t::lib::TestBuilder->new; |
| 31 |
|
| 32 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
| 33 |
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); |
| 34 |
|
| 35 |
subtest 'list() tests' => sub { |
| 36 |
|
| 37 |
plan tests => 14; |
| 38 |
|
| 39 |
$schema->storage->txn_begin; |
| 40 |
|
| 41 |
Koha::Tickets->search->delete; |
| 42 |
|
| 43 |
my $librarian = $builder->build_object( |
| 44 |
{ |
| 45 |
class => 'Koha::Patrons', |
| 46 |
value => { flags => 2**2 } # catalogue flag = 2 |
| 47 |
} |
| 48 |
); |
| 49 |
my $password = 'thePassword123'; |
| 50 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 51 |
my $userid = $librarian->userid; |
| 52 |
|
| 53 |
my $patron = $builder->build_object( |
| 54 |
{ |
| 55 |
class => 'Koha::Patrons', |
| 56 |
value => { flags => 0 } |
| 57 |
} |
| 58 |
); |
| 59 |
|
| 60 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 61 |
my $unauth_userid = $patron->userid; |
| 62 |
|
| 63 |
## Authorized user tests |
| 64 |
# No tickets, so empty array should be returned |
| 65 |
$t->get_ok("//$userid:$password@/api/v1/tickets")->status_is(200) |
| 66 |
->json_is( [] ); |
| 67 |
|
| 68 |
my $ticket = $builder->build_object( { class => 'Koha::Tickets' } ); |
| 69 |
|
| 70 |
# One ticket created, should get returned |
| 71 |
$t->get_ok("//$userid:$password@/api/v1/tickets")->status_is(200) |
| 72 |
->json_is( [ $ticket->to_api ] ); |
| 73 |
|
| 74 |
my $another_ticket = $builder->build_object( { class => 'Koha::Tickets' } ); |
| 75 |
my $and_another_ticket = |
| 76 |
$builder->build_object( { class => 'Koha::Tickets' } ); |
| 77 |
|
| 78 |
# Two tickets created, they should both be returned |
| 79 |
$t->get_ok("//$userid:$password@/api/v1/tickets")->status_is(200)->json_is( |
| 80 |
[ |
| 81 |
$ticket->to_api, $another_ticket->to_api, |
| 82 |
$and_another_ticket->to_api |
| 83 |
] |
| 84 |
); |
| 85 |
|
| 86 |
# Warn on unsupported query parameter |
| 87 |
$t->get_ok("//$userid:$password@/api/v1/tickets?ticket_blah=blah") |
| 88 |
->status_is(400)->json_is( |
| 89 |
[ |
| 90 |
{ |
| 91 |
path => '/query/ticket_blah', |
| 92 |
message => 'Malformed query string' |
| 93 |
} |
| 94 |
] |
| 95 |
); |
| 96 |
|
| 97 |
# Unauthorized access |
| 98 |
$t->get_ok("//$unauth_userid:$password@/api/v1/tickets")->status_is(403); |
| 99 |
|
| 100 |
$schema->storage->txn_rollback; |
| 101 |
}; |
| 102 |
|
| 103 |
subtest 'get() tests' => sub { |
| 104 |
|
| 105 |
plan tests => 8; |
| 106 |
|
| 107 |
$schema->storage->txn_begin; |
| 108 |
|
| 109 |
my $ticket = $builder->build_object( { class => 'Koha::Tickets' } ); |
| 110 |
my $librarian = $builder->build_object( |
| 111 |
{ |
| 112 |
class => 'Koha::Patrons', |
| 113 |
value => { flags => 2**2 } # catalogue flag = 2 |
| 114 |
} |
| 115 |
); |
| 116 |
my $password = 'thePassword123'; |
| 117 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 118 |
my $userid = $librarian->userid; |
| 119 |
|
| 120 |
my $patron = $builder->build_object( |
| 121 |
{ |
| 122 |
class => 'Koha::Patrons', |
| 123 |
value => { flags => 0 } |
| 124 |
} |
| 125 |
); |
| 126 |
|
| 127 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 128 |
my $unauth_userid = $patron->userid; |
| 129 |
|
| 130 |
$t->get_ok( "//$userid:$password@/api/v1/tickets/" . $ticket->id ) |
| 131 |
->status_is(200)->json_is( $ticket->to_api ); |
| 132 |
|
| 133 |
$t->get_ok( "//$unauth_userid:$password@/api/v1/tickets/" . $ticket->id ) |
| 134 |
->status_is(403); |
| 135 |
|
| 136 |
my $ticket_to_delete = |
| 137 |
$builder->build_object( { class => 'Koha::Tickets' } ); |
| 138 |
my $non_existent_id = $ticket_to_delete->id; |
| 139 |
$ticket_to_delete->delete; |
| 140 |
|
| 141 |
$t->get_ok("//$userid:$password@/api/v1/tickets/$non_existent_id") |
| 142 |
->status_is(404)->json_is( '/error' => 'Ticket not found' ); |
| 143 |
|
| 144 |
$schema->storage->txn_rollback; |
| 145 |
}; |
| 146 |
|
| 147 |
subtest 'add() tests' => sub { |
| 148 |
|
| 149 |
plan tests => 21; |
| 150 |
|
| 151 |
$schema->storage->txn_begin; |
| 152 |
|
| 153 |
my $librarian = $builder->build_object( |
| 154 |
{ |
| 155 |
class => 'Koha::Patrons', |
| 156 |
value => { flags => 2**2 } # catalogue flag = 2 |
| 157 |
} |
| 158 |
); |
| 159 |
my $password = 'thePassword123'; |
| 160 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 161 |
my $userid = $librarian->userid; |
| 162 |
|
| 163 |
my $patron = $builder->build_object( |
| 164 |
{ |
| 165 |
class => 'Koha::Patrons', |
| 166 |
value => { flags => 0 } |
| 167 |
} |
| 168 |
); |
| 169 |
|
| 170 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 171 |
my $unauth_userid = $patron->userid; |
| 172 |
|
| 173 |
my $biblio = $builder->build_sample_biblio(); |
| 174 |
my $ticket = { |
| 175 |
biblio_id => $biblio->id, |
| 176 |
title => "Test ticket", |
| 177 |
body => "Test ticket details", |
| 178 |
}; |
| 179 |
|
| 180 |
# Unauthorized attempt to write |
| 181 |
$t->post_ok( |
| 182 |
"//$unauth_userid:$password@/api/v1/tickets" => json => $ticket ) |
| 183 |
->status_is(403); |
| 184 |
|
| 185 |
# Authorized attempt to write invalid data |
| 186 |
my $ticket_with_invalid_field = { |
| 187 |
blah => "Something wrong", |
| 188 |
biblio_id => $biblio->id, |
| 189 |
title => "Test ticket", |
| 190 |
body => "Test ticket details", |
| 191 |
}; |
| 192 |
|
| 193 |
$t->post_ok( "//$userid:$password@/api/v1/tickets" => json => |
| 194 |
$ticket_with_invalid_field )->status_is(400)->json_is( |
| 195 |
"/errors" => [ |
| 196 |
{ |
| 197 |
message => "Properties not allowed: blah.", |
| 198 |
path => "/body" |
| 199 |
} |
| 200 |
] |
| 201 |
); |
| 202 |
|
| 203 |
# Authorized attempt to write |
| 204 |
my $ticket_id = |
| 205 |
$t->post_ok( "//$userid:$password@/api/v1/tickets" => json => $ticket ) |
| 206 |
->status_is( 201, 'SWAGGER3.2.1' )->header_like( |
| 207 |
Location => qr|^\/api\/v1\/tickets/\d*|, |
| 208 |
'SWAGGER3.4.1' |
| 209 |
)->json_is( '/biblio_id' => $ticket->{biblio_id} ) |
| 210 |
->json_is( '/title' => $ticket->{title} ) |
| 211 |
->json_is( '/body' => $ticket->{body} ) |
| 212 |
->json_is( '/reporter_id' => $librarian->id )->tx->res->json->{ticket_id}; |
| 213 |
|
| 214 |
# Authorized attempt to create with null id |
| 215 |
$ticket->{ticket_id} = undef; |
| 216 |
$t->post_ok( "//$userid:$password@/api/v1/tickets" => json => $ticket ) |
| 217 |
->status_is(400)->json_has('/errors'); |
| 218 |
|
| 219 |
# Authorized attempt to create with existing id |
| 220 |
$ticket->{ticket_id} = $ticket_id; |
| 221 |
$t->post_ok( "//$userid:$password@/api/v1/tickets" => json => $ticket ) |
| 222 |
->status_is(400)->json_is( |
| 223 |
"/errors" => [ |
| 224 |
{ |
| 225 |
message => "Read-only.", |
| 226 |
path => "/body/ticket_id" |
| 227 |
} |
| 228 |
] |
| 229 |
); |
| 230 |
|
| 231 |
# Authorized attempt to write missing data |
| 232 |
my $ticket_with_missing_field = { |
| 233 |
biblio_id => $biblio->id, |
| 234 |
body => "Test ticket details", |
| 235 |
}; |
| 236 |
|
| 237 |
$t->post_ok( "//$userid:$password@/api/v1/tickets" => json => |
| 238 |
$ticket_with_missing_field )->status_is(400)->json_is( |
| 239 |
"/errors" => [ |
| 240 |
{ |
| 241 |
message => "Missing property.", |
| 242 |
path => "/body/title" |
| 243 |
} |
| 244 |
] |
| 245 |
); |
| 246 |
|
| 247 |
$schema->storage->txn_rollback; |
| 248 |
}; |
| 249 |
|
| 250 |
subtest 'update() tests' => sub { |
| 251 |
|
| 252 |
plan tests => 15; |
| 253 |
|
| 254 |
$schema->storage->txn_begin; |
| 255 |
|
| 256 |
my $librarian = $builder->build_object( |
| 257 |
{ |
| 258 |
class => 'Koha::Patrons', |
| 259 |
value => { flags => 2**9 } # editcatalogue flag = 9 |
| 260 |
} |
| 261 |
); |
| 262 |
my $password = 'thePassword123'; |
| 263 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 264 |
my $userid = $librarian->userid; |
| 265 |
|
| 266 |
my $patron = $builder->build_object( |
| 267 |
{ |
| 268 |
class => 'Koha::Patrons', |
| 269 |
value => { flags => 0 } |
| 270 |
} |
| 271 |
); |
| 272 |
|
| 273 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 274 |
my $unauth_userid = $patron->userid; |
| 275 |
|
| 276 |
my $ticket_id = $builder->build_object( { class => 'Koha::Tickets' } )->id; |
| 277 |
|
| 278 |
# Unauthorized attempt to update |
| 279 |
$t->put_ok( |
| 280 |
"//$unauth_userid:$password@/api/v1/tickets/$ticket_id" => json => |
| 281 |
{ name => 'New unauthorized name change' } )->status_is(403); |
| 282 |
|
| 283 |
# Attempt partial update on a PUT |
| 284 |
my $ticket_with_missing_field = { |
| 285 |
body => "Test ticket details", |
| 286 |
}; |
| 287 |
|
| 288 |
$t->put_ok( "//$userid:$password@/api/v1/tickets/$ticket_id" => json => |
| 289 |
$ticket_with_missing_field )->status_is(400) |
| 290 |
->json_is( "/errors" => |
| 291 |
[ { message => "Missing property.", path => "/body/title" } ] ); |
| 292 |
|
| 293 |
# Full object update on PUT |
| 294 |
my $ticket_with_updated_field = { |
| 295 |
title => "Test ticket update", |
| 296 |
body => "Test ticket update details", |
| 297 |
}; |
| 298 |
|
| 299 |
$t->put_ok( "//$userid:$password@/api/v1/tickets/$ticket_id" => json => |
| 300 |
$ticket_with_updated_field )->status_is(200) |
| 301 |
->json_is( '/title' => 'Test ticket update' ); |
| 302 |
|
| 303 |
# Authorized attempt to write invalid data |
| 304 |
my $ticket_with_invalid_field = { |
| 305 |
blah => "Ticket Blah", |
| 306 |
title => "Test ticket update", |
| 307 |
body => "Test ticket update details", |
| 308 |
}; |
| 309 |
|
| 310 |
$t->put_ok( "//$userid:$password@/api/v1/tickets/$ticket_id" => json => |
| 311 |
$ticket_with_invalid_field )->status_is(400)->json_is( |
| 312 |
"/errors" => [ |
| 313 |
{ |
| 314 |
message => "Properties not allowed: blah.", |
| 315 |
path => "/body" |
| 316 |
} |
| 317 |
] |
| 318 |
); |
| 319 |
|
| 320 |
my $ticket_to_delete = |
| 321 |
$builder->build_object( { class => 'Koha::Tickets' } ); |
| 322 |
my $non_existent_id = $ticket_to_delete->id; |
| 323 |
$ticket_to_delete->delete; |
| 324 |
|
| 325 |
$t->put_ok( |
| 326 |
"//$userid:$password@/api/v1/tickets/$non_existent_id" => json => |
| 327 |
$ticket_with_updated_field )->status_is(404); |
| 328 |
|
| 329 |
# Wrong method (POST) |
| 330 |
$ticket_with_updated_field->{ticket_id} = 2; |
| 331 |
|
| 332 |
$t->post_ok( "//$userid:$password@/api/v1/tickets/$ticket_id" => json => |
| 333 |
$ticket_with_updated_field )->status_is(404); |
| 334 |
|
| 335 |
$schema->storage->txn_rollback; |
| 336 |
}; |
| 337 |
|
| 338 |
subtest 'delete() tests' => sub { |
| 339 |
|
| 340 |
plan tests => 7; |
| 341 |
|
| 342 |
$schema->storage->txn_begin; |
| 343 |
|
| 344 |
my $librarian = $builder->build_object( |
| 345 |
{ |
| 346 |
class => 'Koha::Patrons', |
| 347 |
value => { flags => 2**9 } # editcatalogue flag = 9 |
| 348 |
} |
| 349 |
); |
| 350 |
my $password = 'thePassword123'; |
| 351 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
| 352 |
my $userid = $librarian->userid; |
| 353 |
|
| 354 |
my $patron = $builder->build_object( |
| 355 |
{ |
| 356 |
class => 'Koha::Patrons', |
| 357 |
value => { flags => 0 } |
| 358 |
} |
| 359 |
); |
| 360 |
|
| 361 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 362 |
my $unauth_userid = $patron->userid; |
| 363 |
|
| 364 |
my $ticket_id = $builder->build_object( { class => 'Koha::Tickets' } )->id; |
| 365 |
|
| 366 |
# Unauthorized attempt to delete |
| 367 |
$t->delete_ok("//$unauth_userid:$password@/api/v1/tickets/$ticket_id") |
| 368 |
->status_is(403); |
| 369 |
|
| 370 |
$t->delete_ok("//$userid:$password@/api/v1/tickets/$ticket_id") |
| 371 |
->status_is( 204, 'SWAGGER3.2.4' )->content_is( '', 'SWAGGER3.3.4' ); |
| 372 |
|
| 373 |
$t->delete_ok("//$userid:$password@/api/v1/tickets/$ticket_id") |
| 374 |
->status_is(404); |
| 375 |
|
| 376 |
$schema->storage->txn_rollback; |
| 377 |
}; |