Bug 25570: (follow-up) Fix tests
[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 => 10;
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 => $password });
44     my $userid = $patron->userid;
45
46     my $stash;
47     my $interface;
48     my $userenv;
49
50     $t->app->hook(after_dispatch => sub {
51         $stash     = shift->stash;
52         $interface = C4::Context->interface;
53         $userenv   = C4::Context->userenv;
54     });
55
56     $t->get_ok("//$userid:$password@/api/v1/patrons")
57       ->status_is( 200, 'Successful authentication and permissions check' );
58
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\'" );
65
66     $patron->flags(undef)->store;
67
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'
73       );
74
75     $schema->storage->txn_rollback;
76 };
77
78 subtest 'failure tests' => sub {
79
80     plan tests => 8;
81
82     $schema->storage->txn_begin;
83
84     my $password     = 'AbcdEFG123';
85     my $bad_password = '123456789';
86
87     t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
88
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;
93
94     $t->get_ok("//@/api/v1/patrons")
95       ->status_is( 401, 'No credentials passed' );
96
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' );
100
101     t::lib::Mocks::mock_preference( 'RESTBasicAuth', 0 );
102
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' );
106
107     $schema->storage->txn_rollback;
108 };
109
110 1;