3 # This file is part of Koha.
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.
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.
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>.
20 use Test
::More tests
=> 3;
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 subtest
'CORS support' => sub {
114 t
::lib
::Mocks
::mock_preference
('AccessControlAllowOrigin','');
115 $t->get_ok("/api/v1/patrons")
116 ->header_is( 'Access-control-allow-origin', undef, 'Header not returned' );
117 # FIXME: newer Test::Mojo has header_exists_not
119 t
::lib
::Mocks
::mock_preference
('AccessControlAllowOrigin',undef);
120 $t->get_ok("/api/v1/patrons")
121 ->header_is( 'Access-control-allow-origin', undef, 'Header not returned' );
122 # FIXME: newer Test::Mojo has header_exists_not
124 t
::lib
::Mocks
::mock_preference
('AccessControlAllowOrigin','*');
125 $t->get_ok("/api/v1/patrons")
126 ->header_is( 'Access-control-allow-origin', '*', 'Header set' );
129 subtest
'spec retrieval tests' => sub {
133 $t->get_ok("/api/v1/")
136 $t->get_ok("/api/v1/.html")
140 sub create_user_and_session
{
141 my $user = $builder->build(
143 source
=> 'Borrower',
150 # Create a session for the authorized user
151 my $session = C4
::Auth
::get_session
('');
152 $session->param( 'number', $user->{borrowernumber
} );
153 $session->param( 'id', $user->{userid
} );
154 $session->param( 'ip', '127.0.0.1' );
155 $session->param( 'lasttime', time() );
158 return ( $user->{borrowernumber
}, $session->id );