Bug 18137: (QA followup) Make sure the session exists and is expired on expiration...
[koha-ffzg.git] / t / db_dependent / api / v1 / auth.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 => 1;
21 use Test::Mojo;
22 use Test::Warn;
23
24 use t::lib::TestBuilder;
25 use t::lib::Mocks;
26
27 use C4::Auth;
28 use Koha::Database;
29
30 my $schema  = Koha::Database->new->schema;
31 my $builder = t::lib::TestBuilder->new;
32
33 # FIXME: sessionStorage defaults to mysql, but it seems to break transaction handling
34 # this affects the other REST api tests
35 t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' );
36
37 my $remote_address = '127.0.0.1';
38 my $t              = Test::Mojo->new('Koha::REST::V1');
39 my $tx;
40
41 subtest 'under() tests' => sub {
42     plan tests => 15;
43
44     $schema->storage->txn_begin;
45
46     my ($borrowernumber, $session_id) = create_user_and_session();
47
48     # 401 (no authentication)
49     $tx = $t->ua->build_tx( GET => "/api/v1/patrons" );
50     $tx->req->env( { REMOTE_ADDR => $remote_address } );
51     $t->request_ok($tx)
52       ->status_is(401)
53       ->json_is('/error', 'Authentication failure.');
54
55     # 403 (no permission)
56     $tx = $t->ua->build_tx( GET => "/api/v1/patrons" );
57     $tx->req->cookies(
58         { name => 'CGISESSID', value => $session_id } );
59     $tx->req->env( { REMOTE_ADDR => $remote_address } );
60     $t->request_ok($tx)
61       ->status_is(403)
62       ->json_is('/error', 'Authorization failure. Missing required permission(s).');
63
64     # 401 (session expired)
65     t::lib::Mocks::mock_preference( 'timeout', '1' );
66     ($borrowernumber, $session_id) = create_user_and_session();
67     $tx = $t->ua->build_tx( GET => "/api/v1/patrons" );
68     $tx->req->cookies(
69         { name => 'CGISESSID', value => $session_id } );
70     $tx->req->env( { REMOTE_ADDR => $remote_address } );
71     sleep(2);
72     $t->request_ok($tx)
73       ->status_is(401)
74       ->json_is('/error', 'Session has been expired.');
75
76     # 503 (under maintenance & pending update)
77     t::lib::Mocks::mock_preference('Version', 1);
78     $tx = $t->ua->build_tx( GET => "/api/v1/patrons" );
79     $tx->req->env( { REMOTE_ADDR => $remote_address } );
80     $t->request_ok($tx)
81       ->status_is(503)
82       ->json_is('/error', 'System is under maintenance.');
83
84     # 503 (under maintenance & database not installed)
85     t::lib::Mocks::mock_preference('Version', undef);
86     $tx = $t->ua->build_tx( GET => "/api/v1/patrons" );
87     $tx->req->env( { REMOTE_ADDR => $remote_address } );
88     $t->request_ok($tx)
89       ->status_is(503)
90       ->json_is('/error', 'System is under maintenance.');
91
92     $schema->storage->txn_rollback;
93 };
94
95 sub create_user_and_session {
96     my $user = $builder->build(
97         {
98             source => 'Borrower',
99             value  => {
100                 flags => 0
101             }
102         }
103     );
104
105     # Create a session for the authorized user
106     my $session = C4::Auth::get_session('');
107     $session->param( 'number',   $user->{borrowernumber} );
108     $session->param( 'id',       $user->{userid} );
109     $session->param( 'ip',       '127.0.0.1' );
110     $session->param( 'lasttime', time() );
111     $session->flush;
112
113     return ( $user->{borrowernumber}, $session->id );
114 }
115
116 1;