6bd4e1a7ebd391659b3a0612682e6e33116d69ab
[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 => 10;
21 use Test::Mojo;
22 use t::lib::TestBuilder;
23
24 use C4::Auth;
25 use C4::Context;
26
27 use Koha::Database;
28 use Koha::Patron;
29
30 my $builder = t::lib::TestBuilder->new();
31
32 my $dbh = C4::Context->dbh;
33 $dbh->{AutoCommit} = 0;
34 $dbh->{RaiseError} = 1;
35
36 $ENV{REMOTE_ADDR} = '127.0.0.1';
37 my $t = Test::Mojo->new('Koha::REST::V1');
38
39 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
40 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
41 my $borrower = $builder->build({
42     source => 'Borrower',
43     value => {
44         branchcode   => $branchcode,
45         categorycode => $categorycode
46     }
47 });
48
49 $t->get_ok('/api/v1/patrons')
50   ->status_is(403);
51
52 $t->get_ok("/api/v1/patrons/" . $borrower->{ borrowernumber })
53   ->status_is(403);
54
55 my $loggedinuser = $builder->build({
56     source => 'Borrower',
57     value => {
58         branchcode   => $branchcode,
59         categorycode => $categorycode,
60         flags        => 16 # borrowers flag
61     }
62 });
63
64 my $session = C4::Auth::get_session('');
65 $session->param('number', $loggedinuser->{ borrowernumber });
66 $session->param('id', $loggedinuser->{ userid });
67 $session->param('ip', '127.0.0.1');
68 $session->param('lasttime', time());
69 $session->flush;
70
71 my $tx = $t->ua->build_tx(GET => '/api/v1/patrons');
72 $tx->req->cookies({name => 'CGISESSID', value => $session->id});
73 $tx->req->env({REMOTE_ADDR => '127.0.0.1'});
74 $t->request_ok($tx)
75   ->status_is(200);
76
77 $tx = $t->ua->build_tx(GET => "/api/v1/patrons/" . $borrower->{ borrowernumber });
78 $tx->req->cookies({name => 'CGISESSID', value => $session->id});
79 $t->request_ok($tx)
80   ->status_is(200)
81   ->json_is('/borrowernumber' => $borrower->{ borrowernumber })
82   ->json_is('/surname' => $borrower->{ surname });
83
84 $dbh->rollback;