3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use Test::More tests => 2;
23 use t::lib::TestBuilder;
26 my $schema = Koha::Database->new->schema;
27 my $builder = t::lib::TestBuilder->new;
29 my $t = Test::Mojo->new('Koha::REST::V1');
31 subtest 'success tests' => sub {
35 $schema->storage->txn_begin;
37 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
39 my $password = 'AbcdEFG123';
41 my $patron = $builder->build_object(
42 { class => 'Koha::Patrons', value => { userid => 'tomasito', flags => 2**4 } } );
43 $patron->set_password({ password => $password });
44 my $userid = $patron->userid;
46 $t->get_ok("//$userid:$password@/api/v1/patrons")
47 ->status_is( 200, 'Successful authentication and permissions check' );
49 $patron->flags(undef)->store;
51 $t->get_ok("//$userid:$password@/api/v1/patrons")
52 ->status_is( 403, 'Successful authentication and not enough permissions' )
54 '/error' => 'Authorization failure. Missing required permission(s).',
55 'Error message returned'
58 $schema->storage->txn_rollback;
61 subtest 'failure tests' => sub {
65 $schema->storage->txn_begin;
67 my $password = 'AbcdEFG123';
68 my $bad_password = '123456789';
70 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
72 my $patron = $builder->build_object(
73 { class => 'Koha::Patrons', value => { userid => 'tomasito', flags => 2**4 } } );
74 $patron->set_password({ password => $password });
75 my $userid = $patron->userid;
77 $t->get_ok("//@/api/v1/patrons")
78 ->status_is( 401, 'No credentials passed' );
80 $t->get_ok("//$userid:$bad_password@/api/v1/patrons")
81 ->status_is( 403, 'Failed authentication, invalid password' )
82 ->json_is( '/error' => 'Invalid password', 'Error message returned' );
84 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 0 );
86 $t->get_ok("//$userid:$password@/api/v1/patrons")
87 ->status_is( 401, 'Basic authentication is disabled' )
88 ->json_is( '/error' => 'Basic authentication disabled', 'Expected error message rendered' );
90 $schema->storage->txn_rollback;