Bug 21835: (QA follow-up) Fix failing test
[koha.git] / t / db_dependent / api / v1 / illrequests.t
1 #!/usr/bin/env perl
2
3 # This file is part of Koha.
4 #
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
8 # version.
9 #
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.
13 #
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.
17
18 use Modern::Perl;
19
20 use Test::More tests => 1;
21 use Test::MockModule;
22 use Test::MockObject;
23 use Test::Mojo;
24 use Test::Warn;
25
26 use t::lib::TestBuilder;
27 use t::lib::Mocks;
28
29 use C4::Auth;
30 use Koha::Illrequests;
31
32 my $schema  = Koha::Database->new->schema;
33 my $builder = t::lib::TestBuilder->new;
34
35 t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' );
36
37 my $remote_address = '127.0.0.1';
38 my $t              = Test::Mojo->new('Koha::REST::V1');
39
40 subtest 'list() tests' => sub {
41
42     plan tests => 18;
43
44     # Mock ILLBackend (as object)
45     my $backend = Test::MockObject->new;
46     $backend->set_isa('Koha::Illbackends::Mock');
47     $backend->set_always('name', 'Mock');
48     $backend->set_always('capabilities', sub { return 'bar'; } );
49     $backend->mock(
50         'metadata',
51         sub {
52             my ( $self, $rq ) = @_;
53             return {
54                 ID => $rq->illrequest_id,
55                 Title => $rq->patron->borrowernumber
56             }
57         }
58     );
59     $backend->mock(
60         'status_graph', sub {},
61     );
62
63     # Mock Koha::Illrequest::load_backend (to load Mocked Backend)
64     my $illreqmodule = Test::MockModule->new('Koha::Illrequest');
65     $illreqmodule->mock( 'load_backend',
66         sub { my $self = shift; $self->{_my_backend} = $backend; return $self }
67     );
68
69     $schema->storage->txn_begin;
70
71     Koha::Illrequests->search->delete;
72     # ill => 22 (userflags.sql)
73     my ( $borrowernumber, $session_id ) = create_user_and_session({ authorized => 22 });
74
75     ## Authorized user tests
76     # No requests, so empty array should be returned
77     my $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( [] );
81
82     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
83     my $patron  = $builder->build_object( { class => 'Koha::Patrons' } );
84
85     # Create an ILL request
86     my $illrequest = $builder->build_object(
87         {
88             class => 'Koha::Illrequests',
89             value => {
90                 backend        => 'Mock',
91                 branchcode     => $library->branchcode,
92                 borrowernumber => $patron->borrowernumber
93             }
94         }
95     );
96
97     # The api response is always augmented with the id_prefix
98     my $response = $illrequest->unblessed;
99     $response->{id_prefix} = $illrequest->id_prefix;
100
101     # One illrequest created, should get returned
102     $tx = $t->ua->build_tx( GET => '/api/v1/illrequests' );
103     $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
104     $tx->req->env( { REMOTE_ADDR => $remote_address } );
105     $t->request_ok($tx)->status_is(200)->json_is( [$response] );
106
107     # One illrequest created, returned with augmented data
108     $tx = $t->ua->build_tx( GET =>
109           '/api/v1/illrequests?embed=patron,library,capabilities,metadata' );
110     $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
111     $tx->req->env( { REMOTE_ADDR => $remote_address } );
112     $t->request_ok($tx)->status_is(200)
113         ->json_has( '/0/patron', 'patron embedded' )
114         ->json_has( '/0/capabilities', 'capabilities embedded' )
115         ->json_has( '/0/library', 'library embedded'  )
116         ->json_has( '/0/metadata', 'metadata embedded'  );
117
118     # Create another ILL request
119     my $illrequest2 = $builder->build_object(
120         {
121             class => 'Koha::Illrequests',
122             value => {
123                 backend        => 'Mock',
124                 branchcode     => $library->branchcode,
125                 borrowernumber => $patron->borrowernumber
126             }
127         }
128     );
129
130     # The api response is always augmented with the id_prefix
131     my $response2 = $illrequest2->unblessed;
132     $response2->{id_prefix} = $illrequest2->id_prefix;
133
134     # Two illrequest created, should get returned
135     $tx = $t->ua->build_tx( GET => '/api/v1/illrequests' );
136     $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
137     $tx->req->env( { REMOTE_ADDR => $remote_address } );
138     $t->request_ok($tx)->status_is(200)
139       ->json_is( [ $response, $response2 ] );
140
141     # Warn on unsupported query parameter
142     $tx = $t->ua->build_tx( GET => '/api/v1/illrequests?request_blah=blah' );
143     $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
144     $tx->req->env( { REMOTE_ADDR => $remote_address } );
145     $t->request_ok($tx)->status_is(400)->json_is(
146         [{ path => '/query/request_blah', message => 'Malformed query string'}]
147     );
148
149     $schema->storage->txn_rollback;
150 };
151
152 sub create_user_and_session {
153
154     my $args = shift;
155     my $dbh  = C4::Context->dbh;
156
157     my $flags = ( $args->{authorized} ) ? 2**$args->{authorized} : 0;
158
159     my $user = $builder->build(
160         {
161             source => 'Borrower',
162             value  => {
163                 flags => $flags
164             }
165         }
166     );
167
168     # Create a session for the authorized user
169     my $session = C4::Auth::get_session('');
170     $session->param( 'number',   $user->{borrowernumber} );
171     $session->param( 'id',       $user->{userid} );
172     $session->param( 'ip',       '127.0.0.1' );
173     $session->param( 'lasttime', time() );
174     $session->flush;
175
176     return ( $user->{borrowernumber}, $session->id );
177 }
178
179 1;