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 => 1; |
21 |
use Test::MockModule; |
22 |
use Test::Mojo; |
23 |
|
24 |
use t::lib::TestBuilder; |
25 |
use t::lib::Mocks; |
26 |
|
27 |
use Koha::Database; |
28 |
use Koha::DateUtils qw(dt_from_string); |
29 |
|
30 |
my $schema = Koha::Database->new->schema; |
31 |
my $builder = t::lib::TestBuilder->new(); |
32 |
|
33 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
34 |
|
35 |
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); |
36 |
|
37 |
subtest 'Patron checkouts list() tests' => sub { |
38 |
|
39 |
plan tests => 13; |
40 |
|
41 |
$schema->storage->txn_begin; |
42 |
|
43 |
my $librarian = $builder->build_object( |
44 |
{ |
45 |
class => 'Koha::Patrons', |
46 |
value => { flags => 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 |
$t->get_ok( "//$userid:$password@/api/v1/patrons/" . $patron->id . '/checkouts' )->status_is(200)->json_is( [] ); |
61 |
|
62 |
my $date_due = dt_from_string->add( weeks => 2 ); |
63 |
my $item1 = $builder->build_sample_item; |
64 |
my $item2 = $builder->build_sample_item; |
65 |
|
66 |
my $issue1 = C4::Circulation::AddIssue( $patron, $item1->barcode, $date_due ); |
67 |
my $issue2 = C4::Circulation::AddIssue( $patron, $item2->barcode, $date_due ); |
68 |
|
69 |
$t->get_ok( "//$userid:$password@/api/v1/patrons/" . $patron->id . '/checkouts' )->status_is(200) |
70 |
->json_is( '/0/item_id' => $item1->itemnumber )->json_is( '/1/item_id' => $item2->itemnumber ); |
71 |
|
72 |
my $non_existent_patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
73 |
my $non_existent_patron_id = $non_existent_patron->id; |
74 |
$non_existent_patron->delete; |
75 |
|
76 |
$t->get_ok( "//$userid:$password@/api/v1/patrons/" . $non_existent_patron_id . '/checkouts' )->status_is(404) |
77 |
->json_is( '/error' => 'Patron not found' ); |
78 |
|
79 |
my $unauthorized_patron = $builder->build_object( |
80 |
{ |
81 |
class => 'Koha::Patrons', |
82 |
value => { flags => 0 } |
83 |
} |
84 |
); |
85 |
my $unauthorized_password = 'thePassword456'; |
86 |
|
87 |
$unauthorized_patron->set_password( { password => $unauthorized_password, skip_validation => 1 } ); |
88 |
my $unauthorized_userid = $unauthorized_patron->userid; |
89 |
|
90 |
$t->get_ok( "//$unauthorized_userid:$unauthorized_password@/api/v1/patrons/" . $patron->id . '/checkouts' ) |
91 |
->status_is(403)->json_is( '/error' => 'Authorization failure. Missing required permission(s).' ); |
92 |
} |