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 backend => 'FreeForm',
71 branchcode => $library->branchcode,
72 borrowernumber => $patron->borrowernumber
77 # One illrequest created, should get returned
78 $tx = $t->ua->build_tx( GET => '/api/v1/illrequests' );
79 $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
80 $tx->req->env( { REMOTE_ADDR => $remote_address } );
81 $t->request_ok($tx)->status_is(200)->json_is( [ $illrequest->unblessed ] );
83 # One illrequest created, returned with augmented data
84 $tx = $t->ua->build_tx( GET =>
85 '/api/v1/illrequests?embed=patron,library,capabilities,metadata' );
86 $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
87 $tx->req->env( { REMOTE_ADDR => $remote_address } );
88 $t->request_ok($tx)->status_is(200)
89 ->json_has( '/0/patron', $patron->unblessed )
90 ->json_has( '/0/capabilities', 'capable' )
91 ->json_has( '/0/library', $library->unblessed )
92 ->json_has( '/0/metadata', 'metawhat?' );
94 # Create another ILL request
95 my $illrequest2 = $builder->build_object(
97 class => 'Koha::Illrequests',
99 branchcode => $library->branchcode,
100 borrowernumber => $patron->borrowernumber
105 # Two illrequest created, should get returned
106 $tx = $t->ua->build_tx( GET => '/api/v1/illrequests' );
107 $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
108 $tx->req->env( { REMOTE_ADDR => $remote_address } );
109 $t->request_ok($tx)->status_is(200)
110 ->json_is( [ $illrequest->unblessed, $illrequest2->unblessed ] );
112 # Warn on unsupported query parameter
113 $tx = $t->ua->build_tx( GET => '/api/v1/illrequests?request_blah=blah' );
114 $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
115 $tx->req->env( { REMOTE_ADDR => $remote_address } );
116 $t->request_ok($tx)->status_is(400)->json_is(
117 [{ path => '/query/request_blah', message => 'Malformed query string'}]
120 $schema->storage->txn_rollback;
123 sub create_user_and_session {
126 my $dbh = C4::Context->dbh;
128 my $flags = ( $args->{authorized} ) ? 2**$args->{authorized} : 0;
130 my $user = $builder->build(
132 source => 'Borrower',
139 # Create a session for the authorized user
140 my $session = C4::Auth::get_session('');
141 $session->param( 'number', $user->{borrowernumber} );
142 $session->param( 'id', $user->{userid} );
143 $session->param( 'ip', '127.0.0.1' );
144 $session->param( 'lasttime', time() );
147 return ( $user->{borrowernumber}, $session->id );