View | Details | Raw Unified | Return to bug 35797
Collapse All | Expand All

(-)a/t/db_dependent/api/v1/image.t (-1 / +90 lines)
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 <https://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Test::NoWarnings;
21
use Test::More tests => 2;
22
use Test::Mojo;
23
24
use t::lib::TestBuilder;
25
use t::lib::Mocks;
26
27
use Koha::Patrons;
28
29
my $schema  = Koha::Database->new->schema;
30
my $builder = t::lib::TestBuilder->new;
31
32
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
33
34
my $t = Test::Mojo->new('Koha::REST::V1');
35
36
subtest 'Image tests' => sub {
37
38
    plan tests => 16;
39
40
    $schema->storage->txn_begin;
41
42
    my $privileged_patron = $builder->build_object(
43
        {
44
            class => 'Koha::Patrons',
45
            value => { flags => 1 }
46
        }
47
    );
48
    my $password = 'thePassword123';
49
    $privileged_patron->set_password( { 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")->status_is(404)
54
        ->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
        {
65
            borrowernumber => $privileged_patron->borrowernumber,
66
            mimetype       => 'image/png',
67
            imagefile      => 'lot of binary content',
68
        }
69
    )->store;
70
71
    $t->get_ok("//$userid:$password@/api/v1/patrons/$borrowernumber/default_image")->status_is(200)
72
        ->content_type_is('image/png')->content_is('lot of binary content');
73
74
    my $unauthorized_patron = $builder->build_object(
75
        {
76
            class => 'Koha::Patrons',
77
            value => { flags => 0 }
78
        }
79
    );
80
    my $unauthorized_password = 'thePassword456';
81
82
    $unauthorized_patron->set_password( { password => $unauthorized_password, skip_validation => 1 } );
83
    my $unauthorized_userid = $unauthorized_patron->userid;
84
85
    $t->get_ok( "//$unauthorized_userid:$unauthorized_password@/api/v1/patrons/" . $borrowernumber . "/default_image" )
86
        ->status_is(403)->json_is( '/error' => "Authorization failure. Missing required permission(s)." );
87
88
    $t->get_ok( "//@/api/v1/patrons/" . $borrowernumber . "/default_image" )->status_is(401)
89
        ->json_is( '/error' => 'Authentication failure.' );
90
};

Return to bug 35797