b58d529c08757524ba626cbe922ec44a5294ea64
[koha-ffzg.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::Mojo;
23 use Test::Warn;
24
25 use t::lib::TestBuilder;
26 use t::lib::Mocks;
27
28 use C4::Auth;
29 use Koha::Illrequests;
30
31 my $schema  = Koha::Database->new->schema;
32 my $builder = t::lib::TestBuilder->new;
33
34 t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' );
35
36 my $remote_address = '127.0.0.1';
37 my $t              = Test::Mojo->new('Koha::REST::V1');
38
39 subtest 'list() tests' => sub {
40
41     plan tests => 18;
42
43     my $illreqmodule = Test::MockModule->new('Koha::Illrequest');
44     # Mock ->capabilities
45     $illreqmodule->mock( 'capabilities', sub { return 'capable'; } );
46     # Mock ->metadata
47     $illreqmodule->mock( 'metadata', sub { return 'metawhat?'; } );
48
49     $schema->storage->txn_begin;
50
51     Koha::Illrequests->search->delete;
52     # ill => 22 (userflags.sql)
53     my ( $borrowernumber, $session_id ) = create_user_and_session({ authorized => 22 });
54
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( [] );
61
62     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
63     my $patron  = $builder->build_object( { class => 'Koha::Patrons' } );
64
65     # Create an ILL request
66     my $illrequest = $builder->build_object(
67         {
68             class => 'Koha::Illrequests',
69             value => {
70                 backend        => 'FreeForm',
71                 branchcode     => $library->branchcode,
72                 borrowernumber => $patron->borrowernumber
73             }
74         }
75     );
76
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 ] );
82
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?'  );
93
94     # Create another ILL request
95     my $illrequest2 = $builder->build_object(
96         {
97             class => 'Koha::Illrequests',
98             value => {
99                 branchcode     => $library->branchcode,
100                 borrowernumber => $patron->borrowernumber
101             }
102         }
103     );
104
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 ] );
111
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'}]
118     );
119
120     $schema->storage->txn_rollback;
121 };
122
123 sub create_user_and_session {
124
125     my $args = shift;
126     my $dbh  = C4::Context->dbh;
127
128     my $flags = ( $args->{authorized} ) ? 2**$args->{authorized} : 0;
129
130     my $user = $builder->build(
131         {
132             source => 'Borrower',
133             value  => {
134                 flags => $flags
135             }
136         }
137     );
138
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() );
145     $session->flush;
146
147     return ( $user->{borrowernumber}, $session->id );
148 }
149
150 1;