3 # Tests for C4::Auth::haspermission
5 # This file is part of Koha.
7 # Copyright 2016 Rijksmuseum
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
23 use Test::More tests => 4;
27 use t::lib::TestBuilder;
28 use C4::Auth qw(haspermission);
30 my $schema = Koha::Database->new->schema;
31 $schema->storage->txn_begin;
33 # Adding two borrowers and granular permissions for the second borrower
34 my $builder = t::lib::TestBuilder->new();
35 my $borr1 = $builder->build(
39 surname => 'Superlib',
44 my $borr2 = $builder->build(
49 flags => 2 + 4 + 2**11, # circulate, catalogue, acquisition
55 source => 'UserPermission',
57 borrowernumber => $borr2->{borrowernumber},
58 module_bit => 13, # tools
59 code => 'upload_local_cover_images',
65 source => 'UserPermission',
67 borrowernumber => $borr2->{borrowernumber},
68 module_bit => 13, # tools
69 code => 'batch_upload_patron_images',
74 subtest 'undef top level tests' => sub {
78 throws_ok { my $r = haspermission( $borr1->{userid} ); }
79 'Koha::Exceptions::WrongParameter',
80 'Exception thrown when missing $requiredflags';
81 throws_ok { my $r = haspermission( $borr1->{userid}, undef ); }
82 'Koha::Exceptions::WrongParameter', 'Exception thrown when explicit undef';
85 subtest 'scalar top level tests' => sub {
89 # Check top level permission for superlibrarian
90 my $r = haspermission( $borr1->{userid}, 'circulate' );
91 is( ref($r), 'HASH', 'Superlibrarian/circulate' );
93 # Check specific top level permission(s) for borr2
94 $r = haspermission( $borr2->{userid}, 'circulate' );
95 is( ref($r), 'HASH', 'Borrower2/circulate' );
96 $r = haspermission( $borr2->{userid}, 'updatecharges' );
97 is( $r, 0, 'Borrower2/updatecharges should fail' );
100 subtest 'hashref top level AND tests' => sub {
104 # Check top level permission for superlibrarian
106 haspermission( $borr1->{userid}, { circulate => 1 } );
107 is( ref($r), 'HASH', 'Superlibrarian/circulate' );
109 # Check specific top level permission(s) for borr2
110 $r = haspermission( $borr2->{userid}, { circulate => 1, catalogue => 1 } );
111 is( ref($r), 'HASH', 'Borrower2/circulate' );
112 $r = haspermission( $borr2->{userid}, { updatecharges => 1 } );
113 is( $r, 0, 'Borrower2/updatecharges should fail' );
115 # Check granular permission with 1: means all subpermissions
116 $r = haspermission( $borr1->{userid}, { tools => 1 } );
117 is( ref($r), 'HASH', 'Superlibrarian/tools granular all' );
118 $r = haspermission( $borr2->{userid}, { tools => 1 } );
119 is( $r, 0, 'Borrower2/tools granular all should fail' );
121 # Check granular permission with *: means at least one subpermission
122 $r = haspermission( $borr1->{userid}, { tools => '*' } );
123 is( ref($r), 'HASH', 'Superlibrarian/tools granular *' );
124 $r = haspermission( $borr2->{userid}, { acquisition => '*' } );
125 is( ref($r), 'HASH', 'Borrower2/acq granular *' );
126 $r = haspermission( $borr2->{userid}, { tools => '*' } );
127 is( ref($r), 'HASH', 'Borrower2/tools granular *' );
128 $r = haspermission( $borr2->{userid}, { serials => '*' } );
129 is( $r, 0, 'Borrower2/serials granular * should fail' );
131 # Check granular permission with one or more specific subperms
132 $r = haspermission( $borr1->{userid}, { tools => 'edit_news' } );
133 is( ref($r), 'HASH', 'Superlibrarian/tools edit_news' );
134 $r = haspermission( $borr2->{userid}, { acquisition => 'budget_manage' } );
135 is( ref($r), 'HASH', 'Borrower2/acq budget_manage' );
136 $r = haspermission( $borr2->{userid},
137 { acquisition => 'budget_manage', tools => 'edit_news' } );
138 is( $r, 0, 'Borrower2 (/acquisition|budget_manage AND /tools|edit_news) should fail' );
143 'upload_local_cover_images' => 1,
144 'batch_upload_patron_images' => 1
148 is( ref($r), 'HASH', 'Borrower2 (/tools|upload_local_cover_image AND /tools|batch_upload_patron_images) granular' );
153 'upload_local_cover_images' => 1,
158 is( $r, 0, 'Borrower2 (/tools|upload_local_cover_image AND /tools|edit_news) granular' );
162 tools => [ 'upload_local_cover_images', 'edit_news'],
165 is( ref($r), 'HASH', 'Borrower2 (/tools|upload_local_cover_image OR /tools|edit_news) granular' );
168 subtest 'arrayref top level OR tests' => sub {
172 # Check top level permission for superlibrarian
174 haspermission( $borr1->{userid}, [ 'circulate', 'editcatalogue' ] );
175 is( ref($r), 'HASH', 'Superlibrarian/circulate' );
177 # Check specific top level permission(s) for borr2
178 $r = haspermission( $borr2->{userid}, [ 'circulate', 'updatecharges' ] );
179 is( ref($r), 'HASH', 'Borrower2/circulate OR Borrower2/updatecharges' );
180 $r = haspermission( $borr2->{userid}, ['updatecharges', 'serials' ] );
181 is( $r, 0, 'Borrower2/updatecharges OR Borrower2/serials should fail' );
183 # Check granular permission with 1: means all subpermissions
184 $r = haspermission( $borr1->{userid}, [ 'tools' ] );
185 is( ref($r), 'HASH', 'Superlibrarian/tools granular all' );
186 $r = haspermission( $borr2->{userid}, [ 'tools' ] );
187 is( $r, 0, 'Borrower2/tools granular all should fail' );
189 # Check granular permission with *: means at least one subpermission
190 $r = haspermission( $borr1->{userid}, [ { tools => '*' } ] );
191 is( ref($r), 'HASH', 'Superlibrarian/tools granular *' );
192 $r = haspermission( $borr2->{userid}, [ { acquisition => '*' } ] );
193 is( ref($r), 'HASH', 'Borrower2/acq granular *' );
194 $r = haspermission( $borr2->{userid}, [ { tools => '*' } ] );
195 is( ref($r), 'HASH', 'Borrower2/tools granular *' );
196 $r = haspermission( $borr2->{userid}, [ { serials => '*' } ] );
197 is( $r, 0, 'Borrower2/serials granular * should fail' );
199 # Check granular permission with one or more specific subperms
200 $r = haspermission( $borr1->{userid}, [ { tools => 'edit_news' } ] );
201 is( ref($r), 'HASH', 'Superlibrarian/tools edit_news' );
203 haspermission( $borr2->{userid}, [ { acquisition => 'budget_manage' } ] );
204 is( ref($r), 'HASH', 'Borrower2/acq budget_manage' );
205 $r = haspermission( $borr2->{userid},
206 [ { acquisition => 'budget_manage'}, { tools => 'edit_news' } ] );
207 is( ref($r), 'HASH', 'Borrower2/two granular OR should pass' );
211 { tools => ['upload_local_cover_images'] },
212 { tools => ['edit_news'] }
215 is( ref($r), 'HASH', 'Borrower2/tools granular OR subperms' );
218 $schema->storage->txn_rollback;