Bug 22132: (QA follow-up) set_password now expects a hashref - Revert
[koha.git] / t / db_dependent / api / v1 / auth_basic.t
1 #!/usr/bin/env perl
2
3 # This file is part of Koha.
4 #
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
8 # version.
9 #
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.
13 #
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.
17
18 use Modern::Perl;
19
20 use Test::More tests => 2;
21 use Test::Mojo;
22
23 use t::lib::TestBuilder;
24 use t::lib::Mocks;
25
26 my $schema  = Koha::Database->new->schema;
27 my $builder = t::lib::TestBuilder->new;
28
29 my $t = Test::Mojo->new('Koha::REST::V1');
30
31 subtest 'success tests' => sub {
32
33     plan tests => 5;
34
35     $schema->storage->txn_begin;
36
37     t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
38
39     my $password = 'AbcdEFG123';
40
41     my $patron = $builder->build_object(
42         { class => 'Koha::Patrons', value => { userid => 'tomasito', flags => 2**4 } } );
43     $patron->set_password($password);
44     my $userid = $patron->userid;
45
46     $t->get_ok("//$userid:$password@/api/v1/patrons")
47       ->status_is( 200, 'Successful authentication and permissions check' );
48
49     $patron->flags(undef)->store;
50
51     $t->get_ok("//$userid:$password@/api/v1/patrons")
52       ->status_is( 403, 'Successful authentication and not enough permissions' )
53       ->json_is(
54         '/error' => 'Authorization failure. Missing required permission(s).',
55         'Error message returned'
56       );
57
58     $schema->storage->txn_rollback;
59 };
60
61 subtest 'failure tests' => sub {
62
63     plan tests => 8;
64
65     $schema->storage->txn_begin;
66
67     my $password     = 'AbcdEFG123';
68     my $bad_password = '123456789';
69
70     t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
71
72     my $patron = $builder->build_object(
73         { class => 'Koha::Patrons', value => { userid => 'tomasito', flags => 2**4 } } );
74     $patron->set_password($password);
75     my $userid = $patron->userid;
76
77     $t->get_ok("//@/api/v1/patrons")
78       ->status_is( 401, 'No credentials passed' );
79
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' );
83
84     t::lib::Mocks::mock_preference( 'RESTBasicAuth', 0 );
85
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' );
89
90     $schema->storage->txn_rollback;
91 };
92
93 1;