3 # This file is part of Koha.
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
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.
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.
20 use Test::More tests => 1;
24 use t::lib::TestBuilder;
30 my $schema = Koha::Database->new->schema;
31 my $builder = t::lib::TestBuilder->new;
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' );
37 my $remote_address = '127.0.0.1';
38 my $t = Test::Mojo->new('Koha::REST::V1');
41 subtest 'under() tests' => sub {
44 $schema->storage->txn_begin;
46 my ($borrowernumber, $session_id) = create_user_and_session();
48 # 401 (no authentication)
49 $tx = $t->ua->build_tx( GET => "/api/v1/patrons" );
50 $tx->req->env( { REMOTE_ADDR => $remote_address } );
53 ->json_is('/error', 'Authentication failure.');
56 $tx = $t->ua->build_tx( GET => "/api/v1/patrons" );
58 { name => 'CGISESSID', value => $session_id } );
59 $tx->req->env( { REMOTE_ADDR => $remote_address } );
62 ->json_is('/error', 'Authorization failure. Missing required permission(s).');
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" );
69 { name => 'CGISESSID', value => $session_id } );
70 $tx->req->env( { REMOTE_ADDR => $remote_address } );
74 ->json_is('/error', 'Session has been expired.');
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 } );
82 ->json_is('/error', 'System is under maintenance.');
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 } );
90 ->json_is('/error', 'System is under maintenance.');
92 $schema->storage->txn_rollback;
95 sub create_user_and_session {
96 my $user = $builder->build(
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() );
113 return ( $user->{borrowernumber}, $session->id );