Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/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::Mojo; |
22 |
|
23 |
use t::lib::TestBuilder; |
24 |
use t::lib::Mocks; |
25 |
|
26 |
use Koha::Patrons; |
27 |
|
28 |
my $schema = Koha::Database->new->schema; |
29 |
my $builder = t::lib::TestBuilder->new; |
30 |
|
31 |
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); |
32 |
|
33 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
34 |
|
35 |
subtest 'Image tests' => sub { |
36 |
|
37 |
plan tests => 16; |
38 |
|
39 |
$schema->storage->txn_begin; |
40 |
|
41 |
my $privileged_patron = $builder->build_object( |
42 |
{ |
43 |
class => 'Koha::Patrons', |
44 |
value => { flags => 1 } |
45 |
} |
46 |
); |
47 |
my $password = 'thePassword123'; |
48 |
$privileged_patron->set_password( |
49 |
{ password => $password, skip_validation => 1 } ); |
50 |
my $userid = $privileged_patron->userid; |
51 |
my $borrowernumber = $privileged_patron->borrowernumber; |
52 |
|
53 |
$t->get_ok( "//$userid:$password@/api/v1/patrons/$borrowernumber/default_image" ) |
54 |
->status_is(404)->json_is('/error' => 'Patron image not found.'); |
55 |
|
56 |
my $non_existent_patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
57 |
my $non_existent_patron_borrower_number = $non_existent_patron->borrowernumber; |
58 |
$non_existent_patron->delete; |
59 |
|
60 |
$t->get_ok( "//$userid:$password@/api/v1/patrons/" . $non_existent_patron_borrower_number . '/checkouts' ) |
61 |
->status_is(404)->json_is('/error' => 'Patron not found'); |
62 |
|
63 |
my $new_image = Koha::Patron::Image->new({ |
64 |
borrowernumber => $privileged_patron->borrowernumber, |
65 |
mimetype => 'image/png', |
66 |
imagefile => 'lot of binary content', |
67 |
})->store; |
68 |
|
69 |
$t->get_ok( "//$userid:$password@/api/v1/patrons/$borrowernumber/default_image" ) |
70 |
->status_is(200)->content_type_is('image/png')->content_is('lot of binary content'); |
71 |
|
72 |
my $unauthorized_patron = $builder->build_object( |
73 |
{ |
74 |
class => 'Koha::Patrons', |
75 |
value => { flags => 0 } |
76 |
} |
77 |
); |
78 |
my $unauthorized_password = 'thePassword456'; |
79 |
|
80 |
$unauthorized_patron->set_password( { password => $unauthorized_password, skip_validation => 1 } ); |
81 |
my $unauthorized_userid = $unauthorized_patron->userid; |
82 |
|
83 |
$t->get_ok( "//$unauthorized_userid:$unauthorized_password@/api/v1/patrons/" . $borrowernumber . "/default_image" ) |
84 |
->status_is(403)->json_is('/error' => "Authorization failure. Missing required permission(s)."); |
85 |
|
86 |
$t->get_ok( "//@/api/v1/patrons/" . $borrowernumber . "/default_image" ) |
87 |
->status_is(401)->json_is('/error' => 'Authentication failure.'); |
88 |
}; |