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 {
45 $schema->storage->txn_begin;
47 my ($borrowernumber, $session_id) = create_user_and_session();
49 # disable the /public namespace
50 t::lib::Mocks::mock_preference( 'RESTPublicAPI', 0 );
51 $tx = $t->ua->build_tx( POST => "/api/v1/public/patrons/$borrowernumber/password" );
52 $tx->req->env( { REMOTE_ADDR => $remote_address } );
55 ->json_is('/error', 'Configuration prevents the usage of this endpoint by unprivileged users');
57 # enable the /public namespace
58 t::lib::Mocks::mock_preference( 'RESTPublicAPI', 1 );
59 $tx = $t->ua->build_tx( GET => "/api/v1/public/patrons/$borrowernumber/password" );
60 $tx->req->env( { REMOTE_ADDR => $remote_address } );
61 $t->request_ok($tx)->status_is(404);
63 # 401 (no authentication)
64 $tx = $t->ua->build_tx( GET => "/api/v1/patrons" );
65 $tx->req->env( { REMOTE_ADDR => $remote_address } );
68 ->json_is('/error', 'Authentication failure.');
71 $tx = $t->ua->build_tx( GET => "/api/v1/patrons" );
73 { name => 'CGISESSID', value => $session_id } );
74 $tx->req->env( { REMOTE_ADDR => $remote_address } );
77 ->json_is('/error', 'Authorization failure. Missing required permission(s).');
79 # 401 (session expired)
80 t::lib::Mocks::mock_preference( 'timeout', '1' );
81 ($borrowernumber, $session_id) = create_user_and_session();
82 $tx = $t->ua->build_tx( GET => "/api/v1/patrons" );
84 { name => 'CGISESSID', value => $session_id } );
85 $tx->req->env( { REMOTE_ADDR => $remote_address } );
89 ->json_is('/error', 'Session has been expired.');
91 # 503 (under maintenance & pending update)
92 t::lib::Mocks::mock_preference('Version', 1);
93 $tx = $t->ua->build_tx( GET => "/api/v1/patrons" );
94 $tx->req->env( { REMOTE_ADDR => $remote_address } );
97 ->json_is('/error', 'System is under maintenance.');
99 # 503 (under maintenance & database not installed)
100 t::lib::Mocks::mock_preference('Version', undef);
101 $tx = $t->ua->build_tx( GET => "/api/v1/patrons" );
102 $tx->req->env( { REMOTE_ADDR => $remote_address } );
105 ->json_is('/error', 'System is under maintenance.');
107 $schema->storage->txn_rollback;
110 sub create_user_and_session {
111 my $user = $builder->build(
113 source => 'Borrower',
120 # Create a session for the authorized user
121 my $session = C4::Auth::get_session('');
122 $session->param( 'number', $user->{borrowernumber} );
123 $session->param( 'id', $user->{userid} );
124 $session->param( 'ip', '127.0.0.1' );
125 $session->param( 'lasttime', time() );
128 return ( $user->{borrowernumber}, $session->id );