Bug 22483: Explicitly ban 'undef' as a valid $flagsrequired
[koha.git] / t / db_dependent / Auth / haspermission.t
1 #!/usr/bin/perl
2
3 # Tests for C4::Auth::haspermission
4
5 # This file is part of Koha.
6 #
7 # Copyright 2016 Rijksmuseum
8 #
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.
13 #
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.
18 #
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>.
21
22 use Modern::Perl;
23 use Test::More tests => 4;
24 use Test::Exception;
25
26 use Koha::Database;
27 use t::lib::TestBuilder;
28 use C4::Auth qw(haspermission);
29
30 my $schema = Koha::Database->new->schema;
31 $schema->storage->txn_begin;
32
33 # Adding two borrowers and granular permissions for the second borrower
34 my $builder = t::lib::TestBuilder->new();
35 my $borr1   = $builder->build(
36     {
37         source => 'Borrower',
38         value  => {
39             surname => 'Superlib',
40             flags   => 1,
41         },
42     }
43 );
44 my $borr2 = $builder->build(
45     {
46         source => 'Borrower',
47         value  => {
48             surname => 'Bor2',
49             flags   => 2 + 4 + 2**11,    # circulate, catalogue, acquisition
50         },
51     }
52 );
53 $builder->build(
54     {
55         source => 'UserPermission',
56         value  => {
57             borrowernumber => $borr2->{borrowernumber},
58             module_bit     => 13,                            # tools
59             code           => 'upload_local_cover_images',
60         },
61     }
62 );
63 $builder->build(
64     {
65         source => 'UserPermission',
66         value  => {
67             borrowernumber => $borr2->{borrowernumber},
68             module_bit     => 13,                             # tools
69             code           => 'batch_upload_patron_images',
70         },
71     }
72 );
73
74 subtest 'undef top level tests' => sub {
75
76     plan tests => 2;
77
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';
83 };
84
85 subtest 'scalar top level tests' => sub {
86
87     plan tests => 3;
88
89     # Check top level permission for superlibrarian
90     my $r = haspermission( $borr1->{userid}, 'circulate' );
91     is( ref($r), 'HASH', 'Superlibrarian/circulate' );
92
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' );
98 };
99
100 subtest 'hashref top level AND tests' => sub {
101
102     plan tests => 15;
103
104     # Check top level permission for superlibrarian
105     my $r =
106       haspermission( $borr1->{userid}, { circulate => 1 } );
107     is( ref($r), 'HASH', 'Superlibrarian/circulate' );
108
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' );
114
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' );
120
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' );
130
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' );
139     $r = haspermission(
140         $borr2->{userid},
141         {
142             tools => {
143                 'upload_local_cover_images'  => 1,
144                 'batch_upload_patron_images' => 1
145             },
146         }
147     );
148     is( ref($r), 'HASH', 'Borrower2 (/tools|upload_local_cover_image AND /tools|batch_upload_patron_images) granular' );
149     $r = haspermission(
150         $borr2->{userid},
151         {
152             tools => {
153                 'upload_local_cover_images'  => 1,
154                 'edit_news' => 1
155             },
156         }
157     );
158     is( $r, 0, 'Borrower2 (/tools|upload_local_cover_image AND /tools|edit_news) granular' );
159     $r = haspermission(
160         $borr2->{userid},
161         {
162             tools => [ 'upload_local_cover_images', 'edit_news'],
163         }
164     );
165     is( ref($r), 'HASH', 'Borrower2 (/tools|upload_local_cover_image OR /tools|edit_news) granular' );
166 };
167
168 subtest 'arrayref top level OR tests' => sub {
169
170     plan tests => 13;
171
172     # Check top level permission for superlibrarian
173     my $r =
174       haspermission( $borr1->{userid}, [ 'circulate', 'editcatalogue' ] );
175     is( ref($r), 'HASH', 'Superlibrarian/circulate' );
176
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' );
182
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' );
188
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' );
198
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' );
202     $r =
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' );
208     $r = haspermission(
209         $borr2->{userid},
210         [
211             { tools => ['upload_local_cover_images'] },
212             { tools => ['edit_news'] }
213         ]
214     );
215     is( ref($r), 'HASH', 'Borrower2/tools granular OR subperms' );
216 };
217
218 $schema->storage->txn_rollback;