909e36e93ed0df531ead5bfc96c94cae30aef915
[koha.git] / t / db_dependent / api / v1 / patrons.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 => 21;
21 use Test::Mojo;
22 use t::lib::TestBuilder;
23 use t::lib::Mocks;
24
25 use C4::Auth;
26 use C4::Context;
27
28 use Koha::Database;
29 use Koha::Patron;
30
31 my $schema  = Koha::Database->new->schema;
32 my $builder = t::lib::TestBuilder->new();
33
34 $schema->storage->txn_begin;
35
36 # FIXME: sessionStorage defaults to mysql, but it seems to break transaction handling
37 # this affects the other REST api tests
38 t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' );
39
40 $ENV{REMOTE_ADDR} = '127.0.0.1';
41 my $t = Test::Mojo->new('Koha::REST::V1');
42
43 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
44 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
45 my $guarantor = $builder->build({
46     source => 'Borrower',
47     value => {
48         branchcode   => $branchcode,
49         categorycode => $categorycode,
50         flags        => 0,
51     }
52 });
53 my $borrower = $builder->build({
54     source => 'Borrower',
55     value => {
56         branchcode   => $branchcode,
57         categorycode => $categorycode,
58         flags        => 0,
59         lost         => 1,
60         guarantorid  => $guarantor->{borrowernumber},
61     }
62 });
63
64 $t->get_ok('/api/v1/patrons')
65   ->status_is(401);
66
67 $t->get_ok("/api/v1/patrons/" . $borrower->{ borrowernumber })
68   ->status_is(401);
69
70 my $session = C4::Auth::get_session('');
71 $session->param('number', $borrower->{ borrowernumber });
72 $session->param('id', $borrower->{ userid });
73 $session->param('ip', '127.0.0.1');
74 $session->param('lasttime', time());
75 $session->flush;
76
77 my $session2 = C4::Auth::get_session('');
78 $session2->param('number', $guarantor->{ borrowernumber });
79 $session2->param('id', $guarantor->{ userid });
80 $session2->param('ip', '127.0.0.1');
81 $session2->param('lasttime', time());
82 $session2->flush;
83
84 my $tx = $t->ua->build_tx(GET => '/api/v1/patrons');
85 $tx->req->cookies({name => 'CGISESSID', value => $session->id});
86 $t->request_ok($tx)
87   ->status_is(403);
88
89 $tx = $t->ua->build_tx(GET => "/api/v1/patrons/" . ($borrower->{ borrowernumber }-1));
90 $tx->req->cookies({name => 'CGISESSID', value => $session->id});
91 $t->request_ok($tx)
92   ->status_is(403)
93   ->json_is('/required_permissions', {"borrowers" => "1"});
94
95 # User without permissions, but is the owner of the object
96 $tx = $t->ua->build_tx(GET => "/api/v1/patrons/" . $borrower->{borrowernumber});
97 $tx->req->cookies({name => 'CGISESSID', value => $session->id});
98 $t->request_ok($tx)
99   ->status_is(200);
100
101 # User without permissions, but is the guarantor of the owner of the object
102 $tx = $t->ua->build_tx(GET => "/api/v1/patrons/" . $borrower->{borrowernumber});
103 $tx->req->cookies({name => 'CGISESSID', value => $session2->id});
104 $t->request_ok($tx)
105   ->status_is(200)
106   ->json_is('/guarantorid', $guarantor->{borrowernumber});
107
108 my $loggedinuser = $builder->build({
109     source => 'Borrower',
110     value => {
111         branchcode   => $branchcode,
112         categorycode => $categorycode,
113         flags        => 16 # borrowers flag
114     }
115 });
116
117 $session = C4::Auth::get_session('');
118 $session->param('number', $loggedinuser->{ borrowernumber });
119 $session->param('id', $loggedinuser->{ userid });
120 $session->param('ip', '127.0.0.1');
121 $session->param('lasttime', time());
122 $session->flush;
123
124 $tx = $t->ua->build_tx(GET => '/api/v1/patrons');
125 $tx->req->cookies({name => 'CGISESSID', value => $session->id});
126 $tx->req->env({REMOTE_ADDR => '127.0.0.1'});
127 $t->request_ok($tx)
128   ->status_is(200);
129
130 $tx = $t->ua->build_tx(GET => "/api/v1/patrons/" . $borrower->{ borrowernumber });
131 $tx->req->cookies({name => 'CGISESSID', value => $session->id});
132 $t->request_ok($tx)
133   ->status_is(200)
134   ->json_is('/borrowernumber' => $borrower->{ borrowernumber })
135   ->json_is('/surname' => $borrower->{ surname })
136   ->json_is('/lost' => 1 );
137
138 $schema->storage->txn_rollback;