Bug 24545: Fix license statements
[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
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.
9 #
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.
14 #
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>.
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 => $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 => $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;