diff --git a/Koha/REST/V1/Patrons/Image.pm b/Koha/REST/V1/Patrons/Image.pm index 3cba4612a9..53bc68698f 100644 --- a/Koha/REST/V1/Patrons/Image.pm +++ b/Koha/REST/V1/Patrons/Image.pm @@ -23,8 +23,6 @@ use Koha::Patrons; use Try::Tiny qw( catch try ); -use MIME::Base64 qw( encode_base64 ); - =head1 NAME Koha::REST::V1::Patrons::Image @@ -42,6 +40,13 @@ Controller method that gets a patron's image sub get { my $c = shift->openapi->valid_input or return; + unless ( C4::Context->preference('patronimages') ) { + return $c->render( + status => 403, + openapi => { error => "Patron images are disabled." } + ); + } + my $patron = Koha::Patrons->find( $c->param('patron_id') ); unless ($patron) { diff --git a/api/v1/swagger/paths/patrons_image.yaml b/api/v1/swagger/paths/patrons_image.yaml index 38fb7b6b90..d9bd563fb1 100644 --- a/api/v1/swagger/paths/patrons_image.yaml +++ b/api/v1/swagger/paths/patrons_image.yaml @@ -12,7 +12,10 @@ - image/png responses: "200": - description: Patron's image + description: Patron image + schema: + type: string + format: binary "401": description: Authentication required schema: diff --git a/t/db_dependent/api/v1/patron_images.t b/t/db_dependent/api/v1/patron_images.t index eb00276d59..b6af566ec5 100755 --- a/t/db_dependent/api/v1/patron_images.t +++ b/t/db_dependent/api/v1/patron_images.t @@ -35,14 +35,34 @@ my $t = Test::Mojo->new('Koha::REST::V1'); subtest 'Image tests' => sub { - plan tests => 16; + plan tests => 19; $schema->storage->txn_begin; my $privileged_patron = $builder->build_object( { class => 'Koha::Patrons', - value => { flags => 1 } + value => { flags => 0 } + } + ); + $builder->build( + { + source => 'UserPermission', + value => { + borrowernumber => $privileged_patron->borrowernumber, + module_bit => 4, + code => 'list_borrowers', + }, + } + ); + $builder->build( + { + source => 'UserPermission', + value => { + borrowernumber => $privileged_patron->borrowernumber, + module_bit => 4, + code => 'edit_borrowers', + }, } ); my $password = 'thePassword123'; @@ -50,6 +70,12 @@ subtest 'Image tests' => sub { my $userid = $privileged_patron->userid; my $borrowernumber = $privileged_patron->borrowernumber; + $t->get_ok("//$userid:$password@/api/v1/patrons/$borrowernumber/default_image") + ->status_is(403) + ->json_is( '/error' => 'Patron images are disabled.' ); + + t::lib::Mocks::mock_preference( 'patronimages', 1 ); + $t->get_ok("//$userid:$password@/api/v1/patrons/$borrowernumber/default_image") ->status_is(404) ->json_is( '/error' => 'Patron image not found.' ); @@ -58,9 +84,9 @@ subtest 'Image tests' => sub { my $non_existent_patron_borrower_number = $non_existent_patron->borrowernumber; $non_existent_patron->delete; - $t->get_ok( "//$userid:$password@/api/v1/patrons/" . $non_existent_patron_borrower_number . '/checkouts' ) + $t->get_ok("//$userid:$password@/api/v1/patrons/$non_existent_patron_borrower_number/default_image") ->status_is(404) - ->json_is( '/error' => 'Patron not found' ); + ->json_is( '/error' => 'Patron not found.' ); my $new_image = Koha::Patron::Image->new( {