Bug 25898: Prohibit indirect object notation
[koha.git] / t / db_dependent / api / v1 / auth_basic.t
blob49fdfe93d2eae77912ace05693fba906c1ceff5b
1 #!/usr/bin/env perl
3 # This file is part of Koha.
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.
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.
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>.
18 use Modern::Perl;
20 use Test::More tests => 2;
21 use Test::Mojo;
23 use t::lib::TestBuilder;
24 use t::lib::Mocks;
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 {
33 plan tests => 10;
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 my $stash;
47 my $interface;
48 my $userenv;
50 $t->app->hook(after_dispatch => sub {
51 $stash = shift->stash;
52 $interface = C4::Context->interface;
53 $userenv = C4::Context->userenv;
54 });
56 $t->get_ok("//$userid:$password@/api/v1/patrons")
57 ->status_is( 200, 'Successful authentication and permissions check' );
59 my $user = $stash->{'koha.user'};
60 ok( defined $user, 'The \'koha.user\' object is defined in the stash') and
61 is( ref($user), 'Koha::Patron', 'Stashed koha.user object type is Koha::Patron') and
62 is( $user->borrowernumber, $patron->borrowernumber, 'The stashed user is the right one' );
63 is( $userenv->{number}, $patron->borrowernumber, 'userenv set correctly' );
64 is( $interface, 'api', "Interface correctly set to \'api\'" );
66 $patron->flags(undef)->store;
68 $t->get_ok("//$userid:$password@/api/v1/patrons")
69 ->status_is( 403, 'Successful authentication and not enough permissions' )
70 ->json_is(
71 '/error' => 'Authorization failure. Missing required permission(s).',
72 'Error message returned'
75 $schema->storage->txn_rollback;
78 subtest 'failure tests' => sub {
80 plan tests => 8;
82 $schema->storage->txn_begin;
84 my $password = 'AbcdEFG123';
85 my $bad_password = '123456789';
87 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
89 my $patron = $builder->build_object(
90 { class => 'Koha::Patrons', value => { userid => 'tomasito', flags => 2**4 } } );
91 $patron->set_password({ password => $password });
92 my $userid = $patron->userid;
94 $t->get_ok("//@/api/v1/patrons")
95 ->status_is( 401, 'No credentials passed' );
97 $t->get_ok("//$userid:$bad_password@/api/v1/patrons")
98 ->status_is( 403, 'Failed authentication, invalid password' )
99 ->json_is( '/error' => 'Invalid password', 'Error message returned' );
101 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 0 );
103 $t->get_ok("//$userid:$password@/api/v1/patrons")
104 ->status_is( 401, 'Basic authentication is disabled' )
105 ->json_is( '/error' => 'Basic authentication disabled', 'Expected error message rendered' );
107 $schema->storage->txn_rollback;