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 |
|
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 |
|
34 |
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); |
35 |
|
36 |
subtest 'Patron checkouts list() tests' => sub { |
37 |
|
38 |
plan tests => 13; |
39 |
|
40 |
$schema->storage->txn_begin; |
41 |
|
42 |
my $librarian = $builder->build_object({ |
43 |
class => 'Koha::Patrons', |
44 |
value => { flags => 2 } |
45 |
}); |
46 |
my $password = 'thePassword123'; |
47 |
$librarian->set_password({ password => $password, skip_validation => 1 }); |
48 |
my $userid = $librarian->userid; |
49 |
|
50 |
my $patron = $builder->build_object({ |
51 |
class => 'Koha::Patrons', |
52 |
value => { flags => 0 } |
53 |
}); |
54 |
|
55 |
$t->get_ok("//$userid:$password@/api/v1/patrons/" . $patron->id . '/checkouts') |
56 |
->status_is(200) |
57 |
->json_is([]); |
58 |
|
59 |
my $date_due = DateTime->now->add(weeks => 2); |
60 |
my $item1 = $builder->build_sample_item; |
61 |
my $item2 = $builder->build_sample_item; |
62 |
|
63 |
my $issue1 = C4::Circulation::AddIssue($patron, $item1->barcode, $date_due); |
64 |
my $issue2 = C4::Circulation::AddIssue($patron, $item2->barcode, $date_due); |
65 |
|
66 |
$t->get_ok("//$userid:$password@/api/v1/patrons/" . $patron->id . '/checkouts') |
67 |
->status_is(200) |
68 |
->json_is('/0/item_id' => $item1->itemnumber) |
69 |
->json_is('/1/item_id' => $item2->itemnumber); |
70 |
|
71 |
my $non_existent_patron = $builder->build_object({ class => 'Koha::Patrons' }); |
72 |
my $non_existent_patron_id = $non_existent_patron->id; |
73 |
$non_existent_patron->delete; |
74 |
|
75 |
$t->get_ok("//$userid:$password@/api/v1/patrons/" . $non_existent_patron_id . '/checkouts') |
76 |
->status_is( 404 ) |
77 |
->json_is( '/error' => 'Patron not found' ); |
78 |
|
79 |
my $unauthorized_patron = $builder->build_object({ |
80 |
class => 'Koha::Patrons', |
81 |
value => { flags => 0 } |
82 |
}); |
83 |
my $unauthorized_password = 'thePassword456'; |
84 |
|
85 |
$unauthorized_patron->set_password({ |
86 |
password => $unauthorized_password, skip_validation => 1 |
87 |
}); |
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 ) |
92 |
->json_is( '/error' => 'Authorization failure. Missing required permission(s).' ); |
93 |
} |