Bug 18589: (follow-up) Add borrowernumber 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 use Koha::DateUtils qw( format_sqldatetime );
32
33 my $schema  = Koha::Database->new->schema;
34 my $builder = t::lib::TestBuilder->new;
35
36 t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' );
37
38 my $remote_address = '127.0.0.1';
39 my $t              = Test::Mojo->new('Koha::REST::V1');
40
41 subtest 'list() tests' => sub {
42
43     plan tests => 24;
44
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'; } );
50     $backend->mock(
51         'metadata',
52         sub {
53             my ( $self, $rq ) = @_;
54             return {
55                 ID => $rq->illrequest_id,
56                 Title => $rq->patron->borrowernumber
57             }
58         }
59     );
60     $backend->mock(
61         'status_graph', sub {},
62     );
63
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 }
68     );
69
70     $schema->storage->txn_begin;
71
72     Koha::Illrequests->search->delete;
73     # ill => 22 (userflags.sql)
74     my ( $borrowernumber, $session_id ) = create_user_and_session({ authorized => 22 });
75
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( [] );
82
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' } );
86
87     # Create an ILL request
88     my $illrequest = $builder->build_object(
89         {
90             class => 'Koha::Illrequests',
91             value => {
92                 backend        => 'Mock',
93                 branchcode     => $library->branchcode,
94                 borrowernumber => $patron_1->borrowernumber
95             }
96         }
97     );
98
99     # The api response is always augmented with the id_prefix
100     my $response = $illrequest->unblessed;
101     $response->{id_prefix} = $illrequest->id_prefix;
102
103     my $req_formatted = add_formatted($response);
104
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 ] );
110
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' );
124
125     # Create another ILL request
126     my $illrequest2 = $builder->build_object(
127         {
128             class => 'Koha::Illrequests',
129             value => {
130                 backend        => 'Mock',
131                 branchcode     => $library->branchcode,
132                 borrowernumber => $patron_2->borrowernumber
133             }
134         }
135     );
136
137     # The api response is always augmented with the id_prefix
138     my $response2 = $illrequest2->unblessed;
139     $response2->{id_prefix} = $illrequest2->id_prefix;
140
141     my $req2_formatted = add_formatted($response2);
142
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 ] );
149
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'}]
156     );
157
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 ] );
164
165     $schema->storage->txn_rollback;
166 };
167
168 sub add_formatted {
169     my $req = shift;
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(
178                 $req->{$field},
179                 undef,
180                 undef,
181                 1
182             );
183         }
184     }
185     return $req;
186 }
187
188 sub create_user_and_session {
189
190     my $args = shift;
191     my $dbh  = C4::Context->dbh;
192
193     my $flags = ( $args->{authorized} ) ? 2**$args->{authorized} : 0;
194
195     my $user = $builder->build(
196         {
197             source => 'Borrower',
198             value  => {
199                 flags => $flags
200             }
201         }
202     );
203
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() );
210     $session->flush;
211
212     return ( $user->{borrowernumber}, $session->id );
213 }
214
215 1;