Bug 21063: Add "Columns settings" for ILL
[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 => 20;
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  = $builder->build_object( { class => 'Koha::Patrons' } );
85
86     # Create an ILL request
87     my $illrequest = $builder->build_object(
88         {
89             class => 'Koha::Illrequests',
90             value => {
91                 backend        => 'Mock',
92                 branchcode     => $library->branchcode,
93                 borrowernumber => $patron->borrowernumber
94             }
95         }
96     );
97
98     # The api response is always augmented with the id_prefix
99     my $response = $illrequest->unblessed;
100     $response->{id_prefix} = $illrequest->id_prefix;
101
102     my $req_formatted = add_formatted($response);
103
104     # One illrequest created, should get returned
105     $tx = $t->ua->build_tx( GET => '/api/v1/illrequests' );
106     $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
107     $tx->req->env( { REMOTE_ADDR => $remote_address } );
108     $t->request_ok($tx)->status_is(200)->json_is( [ $req_formatted ] );
109
110     # One illrequest created, returned with augmented data
111     $tx = $t->ua->build_tx( GET =>
112           '/api/v1/illrequests?embed=patron,library,capabilities,metadata' );
113     $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
114     $tx->req->env( { REMOTE_ADDR => $remote_address } );
115     $t->request_ok($tx)->status_is(200)
116         ->json_has( '/0/patron', 'patron embedded' )
117         ->json_is( '/0/patron/patron_id', $patron->borrowernumber, 'The right patron is embeded')
118         ->json_has( '/0/capabilities', 'capabilities embedded' )
119         ->json_has( '/0/library', 'library embedded'  )
120         ->json_has( '/0/metadata', 'metadata embedded'  )
121         ->json_hasnt( '/1', 'Only one request was created' );
122
123     # Create another ILL request
124     my $illrequest2 = $builder->build_object(
125         {
126             class => 'Koha::Illrequests',
127             value => {
128                 backend        => 'Mock',
129                 branchcode     => $library->branchcode,
130                 borrowernumber => $patron->borrowernumber
131             }
132         }
133     );
134
135     # The api response is always augmented with the id_prefix
136     my $response2 = $illrequest2->unblessed;
137     $response2->{id_prefix} = $illrequest2->id_prefix;
138
139     my $req2_formatted = add_formatted($response2);
140
141     # Two illrequest created, should get returned
142     $tx = $t->ua->build_tx( GET => '/api/v1/illrequests' );
143     $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
144     $tx->req->env( { REMOTE_ADDR => $remote_address } );
145     $t->request_ok($tx)->status_is(200)
146       ->json_is( [ $req_formatted, $req2_formatted ] );
147
148     # Warn on unsupported query parameter
149     $tx = $t->ua->build_tx( GET => '/api/v1/illrequests?request_blah=blah' );
150     $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
151     $tx->req->env( { REMOTE_ADDR => $remote_address } );
152     $t->request_ok($tx)->status_is(400)->json_is(
153         [{ path => '/query/request_blah', message => 'Malformed query string'}]
154     );
155
156     $schema->storage->txn_rollback;
157 };
158
159 sub add_formatted {
160     my $req = shift;
161     my @format_dates = ( 'placed', 'updated', 'completed' );
162     # We need to embellish the request with properties that the API
163     # controller calculates on the fly
164     # Create new "formatted" columns for each date column
165     # that needs formatting
166     foreach my $field(@format_dates) {
167         if (defined $req->{$field}) {
168             $req->{$field . "_formatted"} = format_sqldatetime(
169                 $req->{$field},
170                 undef,
171                 undef,
172                 1
173             );
174         }
175     }
176     return $req;
177 }
178
179 sub create_user_and_session {
180
181     my $args = shift;
182     my $dbh  = C4::Context->dbh;
183
184     my $flags = ( $args->{authorized} ) ? 2**$args->{authorized} : 0;
185
186     my $user = $builder->build(
187         {
188             source => 'Borrower',
189             value  => {
190                 flags => $flags
191             }
192         }
193     );
194
195     # Create a session for the authorized user
196     my $session = C4::Auth::get_session('');
197     $session->param( 'number',   $user->{borrowernumber} );
198     $session->param( 'id',       $user->{userid} );
199     $session->param( 'ip',       '127.0.0.1' );
200     $session->param( 'lasttime', time() );
201     $session->flush;
202
203     return ( $user->{borrowernumber}, $session->id );
204 }
205
206 1;