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;
25 use t::lib::TestBuilder;
29 use Koha::Illrequests;
31 my $schema = Koha::Database->new->schema;
32 my $builder = t::lib::TestBuilder->new;
34 t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' );
36 my $remote_address = '127.0.0.1';
37 my $t = Test::Mojo->new('Koha::REST::V1');
39 subtest 'list() tests' => sub {
43 my $illreqmodule = Test::MockModule->new('Koha::Illrequest');
45 $illreqmodule->mock( 'capabilities', sub { return 'capable'; } );
47 $illreqmodule->mock( 'metadata', sub { return 'metawhat?'; } );
49 $schema->storage->txn_begin;
51 Koha::Illrequests->search->delete;
52 # ill => 22 (userflags.sql)
53 my ( $borrowernumber, $session_id ) = create_user_and_session({ authorized => 22 });
55 ## Authorized user tests
56 # No requests, so empty array should be returned
57 my $tx = $t->ua->build_tx( GET => '/api/v1/illrequests' );
58 $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
59 $tx->req->env( { REMOTE_ADDR => $remote_address } );
60 $t->request_ok($tx)->status_is(200)->json_is( [] );
62 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
63 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
65 # Create an ILL request
66 my $illrequest = $builder->build_object(
68 class => 'Koha::Illrequests',
70 branchcode => $library->branchcode,
71 borrowernumber => $patron->borrowernumber
76 # One illrequest created, should get returned
77 $tx = $t->ua->build_tx( GET => '/api/v1/illrequests' );
78 $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
79 $tx->req->env( { REMOTE_ADDR => $remote_address } );
80 $t->request_ok($tx)->status_is(200)->json_is( [ $illrequest->TO_JSON ] );
82 # One illrequest created, returned with augmented data
83 $tx = $t->ua->build_tx( GET =>
84 '/api/v1/illrequests?embed=patron,library,capabilities,metadata' );
85 $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
86 $tx->req->env( { REMOTE_ADDR => $remote_address } );
87 $t->request_ok($tx)->status_is(200)->json_is(
90 { patron => 1, library => 1, capabilities => 1, metadata => 1 }
95 # Create another ILL request
96 my $illrequest2 = $builder->build_object(
98 class => 'Koha::Illrequests',
100 branchcode => $library->branchcode,
101 borrowernumber => $patron->borrowernumber
106 # Two illrequest created, should get returned
107 $tx = $t->ua->build_tx( GET => '/api/v1/illrequests' );
108 $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
109 $tx->req->env( { REMOTE_ADDR => $remote_address } );
110 $t->request_ok($tx)->status_is(200)
111 ->json_is( [ $illrequest->TO_JSON, $illrequest2->TO_JSON ] );
113 # Warn on unsupported query parameter
114 $tx = $t->ua->build_tx( GET => '/api/v1/illrequests?request_blah=blah' );
115 $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
116 $tx->req->env( { REMOTE_ADDR => $remote_address } );
117 $t->request_ok($tx)->status_is(400)->json_is(
118 [{ path => '/query/request_blah', message => 'Malformed query string'}]
121 $schema->storage->txn_rollback;
124 sub create_user_and_session {
127 my $dbh = C4::Context->dbh;
129 my $flags = ( $args->{authorized} ) ? 2**$args->{authorized} : 0;
131 my $user = $builder->build(
133 source => 'Borrower',
140 # Create a session for the authorized user
141 my $session = C4::Auth::get_session('');
142 $session->param( 'number', $user->{borrowernumber} );
143 $session->param( 'id', $user->{userid} );
144 $session->param( 'ip', '127.0.0.1' );
145 $session->param( 'lasttime', time() );
148 return ( $user->{borrowernumber}, $session->id );