Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/env perl |
|
|
2 |
|
3 |
# Copyright 2016 Koha-Suomi |
4 |
# |
5 |
# This file is part of Koha. |
6 |
# |
7 |
# Koha is free software; you can redistribute it and/or modify it under the |
8 |
# terms of the GNU General Public License as published by the Free Software |
9 |
# Foundation; either version 3 of the License, or (at your option) any later |
10 |
# version. |
11 |
# |
12 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
13 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
14 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
15 |
# |
16 |
# You should have received a copy of the GNU General Public License along |
17 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
19 |
|
20 |
use Modern::Perl; |
21 |
|
22 |
use Test::More tests => 15; |
23 |
use Test::Mojo; |
24 |
|
25 |
use t::lib::TestBuilder; |
26 |
use t::lib::Mocks; |
27 |
|
28 |
use C4::Context; |
29 |
|
30 |
use Koha::Database; |
31 |
use Koha::Patron; |
32 |
|
33 |
my $builder = t::lib::TestBuilder->new(); |
34 |
|
35 |
my $dbh = C4::Context->dbh; |
36 |
$dbh->{AutoCommit} = 0; |
37 |
$dbh->{RaiseError} = 1; |
38 |
|
39 |
$ENV{REMOTE_ADDR} = '127.0.0.1'; |
40 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
41 |
|
42 |
my $categorycode = $builder->build({ source => 'Category' })->{ categorycode }; |
43 |
my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode }; |
44 |
my $guarantor = $builder->build({ |
45 |
source => 'Borrower', |
46 |
value => { |
47 |
branchcode => $branchcode, |
48 |
categorycode => $categorycode, |
49 |
flags => 0, |
50 |
} |
51 |
}); |
52 |
my $borrower = $builder->build({ |
53 |
source => 'Borrower', |
54 |
value => { |
55 |
branchcode => $branchcode, |
56 |
categorycode => $categorycode, |
57 |
flags => 0, |
58 |
guarantorid => $guarantor->{borrowernumber}, |
59 |
} |
60 |
}); |
61 |
my $librarian = $builder->build({ |
62 |
source => 'Borrower', |
63 |
value => { |
64 |
branchcode => $branchcode, |
65 |
categorycode => $categorycode, |
66 |
flags => 16, |
67 |
} |
68 |
}); |
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 $session_librarian = C4::Auth::get_session(''); |
85 |
$session_librarian->param('number', $librarian->{ borrowernumber }); |
86 |
$session_librarian->param('id', $librarian->{ userid }); |
87 |
$session_librarian->param('ip', '127.0.0.1'); |
88 |
$session_librarian->param('lasttime', time()); |
89 |
$session_librarian->flush; |
90 |
|
91 |
t::lib::Mocks::mock_preference("AccessOwnObjectsInAPI", 1); |
92 |
|
93 |
# User without permissions, but is the owner of the object |
94 |
my $tx = $t->ua->build_tx(GET => "/api/v1/patrons/" . $borrower->{borrowernumber}); |
95 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
96 |
$t->request_ok($tx) |
97 |
->status_is(200); |
98 |
|
99 |
# User without permissions, but is the guarantor of the owner of the object |
100 |
$tx = $t->ua->build_tx(GET => "/api/v1/patrons/" . $borrower->{borrowernumber}); |
101 |
$tx->req->cookies({name => 'CGISESSID', value => $session2->id}); |
102 |
$t->request_ok($tx) |
103 |
->status_is(200) |
104 |
->json_is('/guarantorid', $guarantor->{borrowernumber}); |
105 |
|
106 |
t::lib::Mocks::mock_preference("AccessOwnObjectsInAPI", 0); # disable access to own objects |
107 |
|
108 |
# User without permissions, accessing someone else |
109 |
$tx = $t->ua->build_tx(GET => "/api/v1/patrons/" . $librarian->{borrowernumber}); |
110 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
111 |
$t->request_ok($tx) |
112 |
->status_is(403) |
113 |
->json_like('/error', qr/^Authorization failure. Missing/); |
114 |
|
115 |
# Librarian with borrowers flag, should always work |
116 |
$tx = $t->ua->build_tx(GET => "/api/v1/patrons/" . $borrower->{borrowernumber}); |
117 |
$tx->req->cookies({name => 'CGISESSID', value => $session_librarian->id}); |
118 |
$t->request_ok($tx) |
119 |
->status_is(200); |
120 |
|
121 |
# User without permissions, but is the owner of the object |
122 |
$tx = $t->ua->build_tx(GET => "/api/v1/patrons/" . $borrower->{borrowernumber}); |
123 |
$tx->req->cookies({name => 'CGISESSID', value => $session->id}); |
124 |
$t->request_ok($tx) |
125 |
->status_is(403) |
126 |
->json_like('/error', qr/^Functionality for accessing own objects has been disabled/); |
127 |
|
128 |
# User without permissions, but is the guarantor of the owner of the object |
129 |
$tx = $t->ua->build_tx(GET => "/api/v1/patrons/" . $borrower->{borrowernumber}); |
130 |
$tx->req->cookies({name => 'CGISESSID', value => $session2->id}); |
131 |
$t->request_ok($tx) |
132 |
->status_is(403); |