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;
26 use t::lib::TestBuilder;
30 use Koha::Illrequests;
31 use Koha::DateUtils qw( format_sqldatetime );
33 my $schema = Koha::Database->new->schema;
34 my $builder = t::lib::TestBuilder->new;
36 t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' );
38 my $remote_address = '127.0.0.1';
39 my $t = Test::Mojo->new('Koha::REST::V1');
41 subtest 'list() tests' => sub {
45 # Mock ILLBackend (as object)
46 my $backend = Test::MockObject->new;
47 $backend->set_isa('Koha::Illbackends::Mock');
48 $backend->set_always('name', 'Mock');
49 $backend->set_always('capabilities', sub { return 'bar'; } );
53 my ( $self, $rq ) = @_;
55 ID => $rq->illrequest_id,
56 Title => $rq->patron->borrowernumber
61 'status_graph', sub {},
64 # Mock Koha::Illrequest::load_backend (to load Mocked Backend)
65 my $illreqmodule = Test::MockModule->new('Koha::Illrequest');
66 $illreqmodule->mock( 'load_backend',
67 sub { my $self = shift; $self->{_my_backend} = $backend; return $self }
70 $schema->storage->txn_begin;
72 Koha::Illrequests->search->delete;
73 # ill => 22 (userflags.sql)
74 my ( $borrowernumber, $session_id ) = create_user_and_session({ authorized => 22 });
76 ## Authorized user tests
77 # No requests, so empty array should be returned
78 my $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( [] );
83 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
84 my $patron_1 = $builder->build_object( { class => 'Koha::Patrons' } );
85 my $patron_2 = $builder->build_object( { class => 'Koha::Patrons' } );
87 # Create an ILL request
88 my $illrequest = $builder->build_object(
90 class => 'Koha::Illrequests',
93 branchcode => $library->branchcode,
94 borrowernumber => $patron_1->borrowernumber
99 # The api response is always augmented with the id_prefix
100 my $response = $illrequest->unblessed;
101 $response->{id_prefix} = $illrequest->id_prefix;
103 my $req_formatted = add_formatted($response);
105 # One 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)->json_is( [ $req_formatted ] );
111 # One illrequest created, returned with augmented data
112 $tx = $t->ua->build_tx( GET =>
113 '/api/v1/illrequests?embed=patron,library,capabilities,metadata,requested_partners' );
114 $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
115 $tx->req->env( { REMOTE_ADDR => $remote_address } );
116 $t->request_ok($tx)->status_is(200)
117 ->json_has( '/0/patron', 'patron embedded' )
118 ->json_is( '/0/patron/patron_id', $patron_1->borrowernumber, 'The right patron is embeded')
119 ->json_has( '/0/requested_partners', 'requested_partners embedded' )
120 ->json_has( '/0/capabilities', 'capabilities embedded' )
121 ->json_has( '/0/library', 'library embedded' )
122 ->json_has( '/0/metadata', 'metadata embedded' )
123 ->json_hasnt( '/1', 'Only one request was created' );
125 # Create another ILL request
126 my $illrequest2 = $builder->build_object(
128 class => 'Koha::Illrequests',
131 branchcode => $library->branchcode,
132 borrowernumber => $patron_2->borrowernumber
137 # The api response is always augmented with the id_prefix
138 my $response2 = $illrequest2->unblessed;
139 $response2->{id_prefix} = $illrequest2->id_prefix;
141 my $req2_formatted = add_formatted($response2);
143 # Two illrequest created, should get returned
144 $tx = $t->ua->build_tx( GET => '/api/v1/illrequests' );
145 $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
146 $tx->req->env( { REMOTE_ADDR => $remote_address } );
147 $t->request_ok($tx)->status_is(200)
148 ->json_is( [ $req_formatted, $req2_formatted ] );
150 # Warn on unsupported query parameter
151 $tx = $t->ua->build_tx( GET => '/api/v1/illrequests?request_blah=blah' );
152 $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
153 $tx->req->env( { REMOTE_ADDR => $remote_address } );
154 $t->request_ok($tx)->status_is(400)->json_is(
155 [{ path => '/query/request_blah', message => 'Malformed query string'}]
158 # Test the borrowernumber parameter
159 $tx = $t->ua->build_tx( GET => '/api/v1/illrequests?borrowernumber=' .
160 $patron_2->borrowernumber );
161 $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
162 $tx->req->env( { REMOTE_ADDR => $remote_address } );
163 $t->request_ok($tx)->status_is(200)->json_is( [ $response2 ] );
165 $schema->storage->txn_rollback;
170 my @format_dates = ( 'placed', 'updated', 'completed' );
171 # We need to embellish the request with properties that the API
172 # controller calculates on the fly
173 # Create new "formatted" columns for each date column
174 # that needs formatting
175 foreach my $field(@format_dates) {
176 if (defined $req->{$field}) {
177 $req->{$field . "_formatted"} = format_sqldatetime(
188 sub create_user_and_session {
191 my $dbh = C4::Context->dbh;
193 my $flags = ( $args->{authorized} ) ? 2**$args->{authorized} : 0;
195 my $user = $builder->build(
197 source => 'Borrower',
204 # Create a session for the authorized user
205 my $session = C4::Auth::get_session('');
206 $session->param( 'number', $user->{borrowernumber} );
207 $session->param( 'id', $user->{userid} );
208 $session->param( 'ip', '127.0.0.1' );
209 $session->param( 'lasttime', time() );
212 return ( $user->{borrowernumber}, $session->id );