Bug 7317: (QA followup) Rename 'branch' for 'library'
[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::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 => 15;
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                 branchcode     => $library->branchcode,
71                 borrowernumber => $patron->borrowernumber
72             }
73         }
74     );
75
76     # One illrequest created, should get returned
77     $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( [ $illrequest->TO_JSON ] );
81
82     # One illrequest created, returned with augmented data
83     $tx = $t->ua->build_tx( GET =>
84           '/api/v1/illrequests?embed=patron,library,capabilities,metadata' );
85     $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
86     $tx->req->env( { REMOTE_ADDR => $remote_address } );
87     $t->request_ok($tx)->status_is(200)->json_is(
88         [
89             $illrequest->TO_JSON(
90                 { patron => 1, library => 1, capabilities => 1, metadata => 1 }
91             )
92         ]
93     );
94
95     # Create another ILL request
96     my $illrequest2 = $builder->build_object(
97         {
98             class => 'Koha::Illrequests',
99             value => {
100                 branchcode     => $library->branchcode,
101                 borrowernumber => $patron->borrowernumber
102             }
103         }
104     );
105
106     # Two illrequest created, should get returned
107     $tx = $t->ua->build_tx( GET => '/api/v1/illrequests' );
108     $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
109     $tx->req->env( { REMOTE_ADDR => $remote_address } );
110     $t->request_ok($tx)->status_is(200)
111       ->json_is( [ $illrequest->TO_JSON, $illrequest2->TO_JSON ] );
112
113     # Warn on unsupported query parameter
114     $tx = $t->ua->build_tx( GET => '/api/v1/illrequests?request_blah=blah' );
115     $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
116     $tx->req->env( { REMOTE_ADDR => $remote_address } );
117     $t->request_ok($tx)->status_is(400)->json_is(
118         [{ path => '/query/request_blah', message => 'Malformed query string'}]
119     );
120
121     $schema->storage->txn_rollback;
122 };
123
124 sub create_user_and_session {
125
126     my $args = shift;
127     my $dbh  = C4::Context->dbh;
128
129     my $flags = ( $args->{authorized} ) ? 2**$args->{authorized} : 0;
130
131     my $user = $builder->build(
132         {
133             source => 'Borrower',
134             value  => {
135                 flags => $flags
136             }
137         }
138     );
139
140     # Create a session for the authorized user
141     my $session = C4::Auth::get_session('');
142     $session->param( 'number',   $user->{borrowernumber} );
143     $session->param( 'id',       $user->{userid} );
144     $session->param( 'ip',       '127.0.0.1' );
145     $session->param( 'lasttime', time() );
146     $session->flush;
147
148     return ( $user->{borrowernumber}, $session->id );
149 }
150
151 1;