Bug 36908: Additional unit tests to identify flaw when two branches have same IP
[koha.git] / t / db_dependent / Auth.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use CGI qw ( -utf8 );
6
7 use Test::MockObject;
8 use Test::MockModule;
9 use List::MoreUtils qw/all any none/;
10 use Test::More tests => 22;
11 use Test::Warn;
12 use t::lib::Mocks;
13 use t::lib::TestBuilder;
14
15 use C4::Auth;
16 use C4::Members;
17 use Koha::AuthUtils qw( hash_password );
18 use Koha::DateUtils qw( dt_from_string );
19 use Koha::Database;
20 use Koha::Patrons;
21 use Koha::Auth::TwoFactorAuth;
22
23 BEGIN {
24     use_ok(
25         'C4::Auth',
26         qw( checkauth haspermission checkpw get_template_and_user checkpw_hash get_cataloguing_page_permissions )
27     );
28 }
29
30 my $schema  = Koha::Database->schema;
31 my $builder = t::lib::TestBuilder->new;
32
33 # FIXME: SessionStorage defaults to mysql, but it seems to break transaction
34 # handling
35 t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' );
36 t::lib::Mocks::mock_preference( 'PrivacyPolicyConsent', '' ); # Disabled
37
38 # To silence useless warnings
39 $ENV{REMOTE_ADDR} = '127.0.0.1';
40
41 $schema->storage->txn_begin;
42
43 subtest 'checkauth() tests' => sub {
44
45     plan tests => 11;
46
47     my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { flags => undef } });
48
49     # Mock a CGI object with real userid param
50     my $cgi = Test::MockObject->new();
51     $cgi->mock(
52         'param',
53         sub {
54             my $var = shift;
55             if ( $var eq 'userid' ) { return $patron->userid; }
56         }
57     );
58     $cgi->mock( 'cookie', sub { return; } );
59     $cgi->mock( 'request_method', sub { return 'POST' } );
60
61     my $authnotrequired = 1;
62     my ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth( $cgi, $authnotrequired );
63
64     is( $userid, undef, 'checkauth() returns undef for userid if no logged in user (Bug 18275)' );
65
66     my $db_user_id = C4::Context->config('user');
67     my $db_user_pass = C4::Context->config('pass');
68     $cgi = Test::MockObject->new();
69     $cgi->mock( 'cookie', sub { return; } );
70     $cgi->mock( 'param', sub {
71             my ( $self, $param ) = @_;
72             if ( $param eq 'login_userid' ) { return $db_user_id; }
73             elsif ( $param eq 'login_password' ) { return $db_user_pass; }
74             else { return; }
75         });
76     $cgi->mock( 'request_method', sub { return 'POST' } );
77     ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth( $cgi, $authnotrequired );
78     is ( $userid, undef, 'If DB user is used, it should not be logged in' );
79
80     my $is_allowed = C4::Auth::haspermission( $db_user_id, { can_do => 'everything' } );
81
82     # FIXME This belongs to t/db_dependent/Auth/haspermission.t but we do not want to c/p the pervious mock statements
83     ok( !$is_allowed, 'DB user should not have any permissions');
84
85     subtest 'Prevent authentication when sending credential via GET' => sub {
86
87         plan tests => 2;
88
89         my $patron = $builder->build_object(
90             { class => 'Koha::Patrons', value => { flags => 1 } } );
91         my $password = set_weak_password($patron);
92         $cgi = Test::MockObject->new();
93         $cgi->mock( 'cookie', sub { return; } );
94         $cgi->mock(
95             'param',
96             sub {
97                 my ( $self, $param ) = @_;
98                 if    ( $param eq 'login_userid' )   { return $patron->userid; }
99                 elsif ( $param eq 'login_password' ) { return $password; }
100                 else                           { return; }
101             }
102         );
103
104         $cgi->mock( 'request_method', sub { return 'POST' } );
105         ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth( $cgi, 'authrequired' );
106         is( $userid, $patron->userid, 'If librarian user is used and password with POST, they should be logged in' );
107
108         $cgi->mock( 'request_method', sub { return 'GET' } );
109         ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth( $cgi, 'authrequired' );
110         is( $userid, undef, 'If librarian user is used and password with GET, they should not be logged in' );
111     };
112
113     subtest 'cas_ticket must be empty in session' => sub {
114
115         plan tests => 2;
116
117         my $patron   = $builder->build_object( { class => 'Koha::Patrons', value => { flags => 1 } } );
118         my $password = 'password';
119         t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 );
120         $patron->set_password( { password => $password } );
121         $cgi = Test::MockObject->new();
122         $cgi->mock( 'cookie', sub { return; } );
123         $cgi->mock(
124             'param',
125             sub {
126                 my ( $self, $param ) = @_;
127                 if    ( $param eq 'login_userid' )   { return $patron->userid; }
128                 elsif ( $param eq 'login_password' ) { return $password; }
129                 else                           { return; }
130             }
131         );
132
133         $cgi->mock( 'request_method', sub { return 'POST' } );
134         ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth( $cgi, 'authrequired' );
135         is( $userid, $patron->userid, 'If librarian user is used and password with POST, they should be logged in' );
136         my $session = C4::Auth::get_session($sessionID);
137         is( $session->param('cas_ticket'), undef );
138
139     };
140
141     subtest 'sessionID should be passed to the template for auth' => sub {
142
143         plan tests => 1;
144
145         subtest 'hit auth.tt' => sub {
146
147             plan tests => 1;
148
149             my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { flags => 0 } } );
150
151             my $password = set_weak_password($patron);
152
153             my $cgi_mock = Test::MockModule->new('CGI');
154             $cgi_mock->mock( 'request_method', sub { return 'POST' } );
155             my $cgi = CGI->new;
156
157             # Simulating the login form submission
158             $cgi->param( 'login_userid',   $patron->userid );
159             $cgi->param( 'login_password', $password );
160
161             my ( $userid, $cookie, $sessionID, $flags, $template ) =
162                 C4::Auth::checkauth( $cgi, 0, { catalogue => 1 }, 'intranet', undef, undef, { do_not_print => 1 } );
163             ok( $template->{VARS}->{sessionID} );
164         };
165     };
166
167     subtest 'Template params tests (password_expired)' => sub {
168
169         plan tests => 1;
170
171         my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
172
173         my $password = set_weak_password($patron);
174         $patron->password_expiration_date( dt_from_string->subtract(days => 1) )->store;
175
176         my $cgi_mock = Test::MockModule->new('CGI');
177         $cgi_mock->mock( 'request_method', sub { return 'POST' } );
178         my $cgi  = CGI->new;
179         # Simulating the login form submission
180         $cgi->param( 'login_userid',   $patron->userid );
181         $cgi->param( 'login_password', $password );
182
183         my ( $userid, $cookie, $sessionID, $flags, $template ) = C4::Auth::checkauth( $cgi, 0, { catalogue => 1 }, 'intranet', undef, undef, { do_not_print => 1 } );
184         is( $template->{VARS}->{password_has_expired}, 1 );
185     };
186
187     subtest 'Reset auth state when changing users' => sub {
188
189         #NOTE: It's easiest to detect this when changing to a non-existent user, since
190         #that should trigger a redirect to login (instead of returning a session cookie)
191         plan tests => 2;
192         my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { flags => undef } } );
193
194         my $session = C4::Auth::get_session();
195         $session->param( 'number',    $patron->id );
196         $session->param( 'id',        $patron->userid );
197         $session->param( 'ip',        '1.2.3.4' );
198         $session->param( 'lasttime',  time() );
199         $session->param( 'interface', 'intranet' );
200         $session->flush;
201         my $sessionID = $session->id;
202         C4::Context->_new_userenv($sessionID);
203
204         my ($return) =
205             C4::Auth::check_cookie_auth( $sessionID, undef, { skip_version_check => 1, remote_addr => '1.2.3.4' } );
206         is( $return, 'ok', 'Patron authenticated' );
207
208         my $mock2 = Test::MockModule->new('CGI');
209         $mock2->mock( 'request_method', 'POST' );
210         $mock2->mock( 'cookie',         sub { return $sessionID; } );    # oversimplified..
211         my $cgi = CGI->new;
212
213         $cgi->param( -name => 'login_userid',             -value => 'Bond' );
214         $cgi->param( -name => 'login_password',           -value => 'James Bond' );
215         $cgi->param( -name => 'koha_login_context', -value => 1 );
216         my ( $userid, $cookie, $flags, $template );
217         ( $userid, $cookie, $sessionID, $flags, $template ) =
218             C4::Auth::checkauth( $cgi, 0, { catalogue => 1 }, 'intranet', undef, undef, { do_not_print => 1 } );
219         is( $template->{VARS}->{loginprompt}, 1, 'Changing to non-existent user causes a redirect to login' );
220     };
221
222     subtest 'While still logged in, relogin with another user' => sub {
223         plan tests => 5;
224
225         my $patron = $builder->build_object({ class => 'Koha::Patrons', value => {} });
226         my $patron2 = $builder->build_object({ class => 'Koha::Patrons', value => {} });
227         # Create 'former' session
228         my $session = C4::Auth::get_session();
229         $session->param( 'number',       $patron->id );
230         $session->param( 'id',           $patron->userid );
231         $session->param( 'ip',           '1.2.3.4' );
232         $session->param( 'lasttime',     time() );
233         $session->param( 'interface',    'opac' );
234         $session->flush;
235         my $previous_sessionID = $session->id;
236         C4::Context->_new_userenv($previous_sessionID);
237
238         my ( $return ) = C4::Auth::check_cookie_auth( $previous_sessionID, undef, { skip_version_check => 1, remote_addr => '1.2.3.4' } );
239         is( $return, 'ok', 'Former session in shape now' );
240
241         my $mock1 = Test::MockModule->new('C4::Auth');
242         $mock1->mock( 'safe_exit', sub {} );
243         my $mock2 = Test::MockModule->new('CGI');
244         $mock2->mock( 'request_method', 'POST' );
245         $mock2->mock( 'cookie', sub { return $previous_sessionID; } ); # oversimplified..
246         my $cgi = CGI->new;
247         my $password = 'Incr3d1blyZtr@ng93$';
248         $patron2->set_password({ password => $password });
249         $cgi->param( -name => 'login_userid',             -value => $patron2->userid );
250         $cgi->param( -name => 'login_password',           -value => $password );
251         $cgi->param( -name => 'koha_login_context', -value => 1 );
252         my ( $userid, $cookie, $sessionID, $flags, $template ) =
253             C4::Auth::checkauth( $cgi, 0, {}, 'opac', undef, undef, { do_not_print => 1 } );
254         is( $userid, $patron2->userid, 'Login of patron2 approved' );
255         isnt( $sessionID, $previous_sessionID, 'Did not return previous session ID' );
256         ok( $sessionID, 'New session ID not empty' );
257
258         # Similar situation: Relogin with former session of $patron, new user $patron2 has no permissions
259         $patron2->flags(undef)->store;
260         $session->param( 'number',       $patron->id );
261         $session->param( 'id',           $patron->userid );
262         $session->param( 'interface',    'intranet' );
263         $session->flush;
264         $previous_sessionID = $session->id;
265         C4::Context->_new_userenv($previous_sessionID);
266         $cgi->param( -name => 'login_userid',             -value => $patron2->userid );
267         $cgi->param( -name => 'login_password',           -value => $password );
268         $cgi->param( -name => 'koha_login_context', -value => 1 );
269         ( $userid, $cookie, $sessionID, $flags, $template ) =
270             C4::Auth::checkauth( $cgi, 0, { catalogue => 1 }, 'intranet', undef, undef, { do_not_print => 1 } );
271         is( $template->{VARS}->{nopermission}, 1, 'No permission response' );
272     };
273
274     subtest 'Two-factor authentication' => sub {
275         plan tests => 18;
276
277         my $patron = $builder->build_object(
278             { class => 'Koha::Patrons', value => { flags => 1 } } );
279         my $password = 'password';
280         $patron->set_password( { password => $password } );
281         $cgi = Test::MockObject->new();
282
283         my $otp_token;
284         our ( $logout, $sessionID, $verified );
285         $cgi->mock(
286             'param',
287             sub {
288                 my ( $self, $param ) = @_;
289                 if    ( $param eq 'login_userid' )    { return $patron->userid; }
290                 elsif ( $param eq 'login_password' )  { return $password; }
291                 elsif ( $param eq 'otp_token' ) { return $otp_token; }
292                 elsif ( $param eq 'logout.x' )  { return $logout; }
293                 else                            { return; }
294             }
295         );
296         $cgi->mock( 'request_method', sub { return 'POST' } );
297         $cgi->mock( 'cookie', sub { return $sessionID } );
298
299         my $two_factor_auth = Test::MockModule->new( 'Koha::Auth::TwoFactorAuth' );
300         $two_factor_auth->mock( 'verify', sub {$verified} );
301
302         my ( $userid, $cookie, $flags );
303         ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth( $cgi, 'authrequired', undef, 'intranet' );
304
305         sub logout {
306             my $cgi = shift;
307             $logout = 1;
308             undef $sessionID;
309             C4::Auth::checkauth( $cgi, 'authrequired', undef, 'intranet' );
310             $logout = 0;
311         }
312
313         t::lib::Mocks::mock_preference( 'TwoFactorAuthentication', 'disabled' );
314         $patron->auth_method('password')->store;
315         ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth( $cgi, 'authrequired', undef, 'intranet' );
316         is( $userid, $patron->userid, 'Succesful login' );
317         is( C4::Auth::get_session($sessionID)->param('waiting-for-2FA'), undef, 'Second auth not required' );
318         logout($cgi);
319
320         $patron->auth_method('two-factor')->store;
321         ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth( $cgi, 'authrequired', undef, 'intranet' );
322         is( $userid, $patron->userid, 'Succesful login' );
323         is( C4::Auth::get_session($sessionID)->param('waiting-for-2FA'), undef, 'Second auth not required' );
324         logout($cgi);
325
326         t::lib::Mocks::mock_preference( 'TwoFactorAuthentication', 'enabled' );
327         t::lib::Mocks::mock_config('encryption_key', '1234tH1s=t&st');
328         $patron->auth_method('password')->store;
329         ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth( $cgi, 'authrequired', undef, 'intranet' );
330         is( $userid, $patron->userid, 'Succesful login' );
331         is( C4::Auth::get_session($sessionID)->param('waiting-for-2FA'), undef, 'Second auth not required' );
332         logout($cgi);
333
334         $patron->encode_secret('one_secret');
335         $patron->auth_method('two-factor');
336         $patron->store;
337         ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth( $cgi, 'authrequired', undef, 'intranet' );
338         is( $userid, $patron->userid, 'Succesful login' );
339         my $session = C4::Auth::get_session($sessionID);
340         is( C4::Auth::get_session($sessionID)->param('waiting-for-2FA'), 1, 'Second auth required' );
341
342         # Wrong OTP token
343         $otp_token = "wrong";
344         $verified = 0;
345         $patron->auth_method('two-factor')->store;
346         ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth( $cgi, 'authrequired', undef, 'intranet' );
347         is( $userid, $patron->userid, 'Succesful login' );
348         is( C4::Auth::get_session($sessionID)->param('waiting-for-2FA'), 1, 'Second auth still required after wrong OTP token' );
349
350         $otp_token = "good";
351         $verified = 1;
352         ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth( $cgi, 'authrequired', undef, 'intranet' );
353         is( $userid, $patron->userid, 'Succesful login' );
354         is( C4::Auth::get_session($sessionID)->param('waiting-for-2FA'), 0, 'Second auth no longer required if OTP token has been verified' );
355         logout($cgi);
356
357         t::lib::Mocks::mock_preference( 'TwoFactorAuthentication', 'enforced' );
358         $patron->auth_method('password')->store;
359         ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth( $cgi, 'authrequired', undef, 'intranet' );
360         is( $userid, $patron->userid, 'Succesful login' );
361         is( C4::Auth::get_session($sessionID)->param('waiting-for-2FA-setup'), 1, 'Setup 2FA required' );
362         logout($cgi);
363
364         ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth( $cgi, 'authrequired', undef, 'opac' );
365         is( $userid, $patron->userid, 'Succesful login at the OPAC' );
366         is( C4::Auth::get_session($sessionID)->param('waiting-for-2FA'), undef, 'No second auth required at the OPAC' );
367
368         #
369         t::lib::Mocks::mock_preference( 'TwoFactorAuthentication', 'disabled' );
370         $session = C4::Auth::get_session($sessionID);
371         $session->param('waiting-for-2FA', 1);
372         $session->flush;
373         my ($auth_status, undef ) = C4::Auth::check_cookie_auth($sessionID, undef );
374         is( $auth_status, 'ok', 'User authenticated, pref was disabled, access OK' );
375         $session->param('waiting-for-2FA', 0);
376         $session->param('waiting-for-2FA-setup', 1);
377         $session->flush;
378         ($auth_status, undef ) = C4::Auth::check_cookie_auth($sessionID, undef );
379         is( $auth_status, 'ok', 'User waiting for 2FA setup, pref was disabled, access OK' );
380     };
381
382     subtest 'loggedinlibrary permission tests' => sub {
383
384         plan tests => 3;
385         my $staff_user = $builder->build_object(
386             { class => 'Koha::Patrons', value => { flags => 536870916 } } );
387
388         my $branch = $builder->build_object({ class => 'Koha::Libraries' });
389
390         my $password = set_weak_password($staff_user);
391         my $cgi = Test::MockObject->new();
392         $cgi->mock( 'cookie', sub { return; } );
393         $cgi->mock(
394             'param',
395             sub {
396                 my ( $self, $param ) = @_;
397                 if    ( $param eq 'login_userid' )   { return $staff_user->userid; }
398                 elsif ( $param eq 'login_password' ) { return $password; }
399                 elsif ( $param eq 'branch' )   { return $branch->branchcode; }
400                 else                           { return; }
401             }
402         );
403
404         $cgi->mock( 'request_method', sub { return 'POST' } );
405         my ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth( $cgi, 'authrequired' );
406         my $sesh = C4::Auth::get_session($sessionID);
407         is( $sesh->param('branch'), $branch->branchcode, "If user has permission, they should be able to choose a branch" );
408
409         $staff_user->flags(4)->store->discard_changes;
410         ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth( $cgi, 'authrequired' );
411         $sesh = C4::Auth::get_session($sessionID);
412         is( $sesh->param('branch'), $staff_user->branchcode, "If user has not permission, they should not be able to choose a branch" );
413
414         $staff_user->flags(1)->store->discard_changes;
415         ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth( $cgi, 'authrequired' );
416         $sesh = C4::Auth::get_session($sessionID);
417         is( $sesh->param('branch'), $branch->branchcode, "If user is superlibrarian, they should be able to choose a branch" );
418
419     };
420     C4::Context->_new_userenv; # For next tests
421 };
422
423 subtest 'no_set_userenv parameter tests' => sub {
424
425     plan tests => 7;
426
427     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
428     my $patron  = $builder->build_object( { class => 'Koha::Patrons' } );
429
430     my $password = set_weak_password($patron);
431
432     ok( checkpw( $patron->userid, $password, undef, undef, 1 ), 'checkpw returns true' );
433     is( C4::Context->userenv, undef, 'Userenv should be undef as required' );
434     C4::Context->_new_userenv('DUMMY SESSION');
435     C4::Context->set_userenv(0,0,0,'firstname','surname', $library->branchcode, 'Library 1', 0, '', '');
436     is( C4::Context->userenv->{branch}, $library->branchcode, 'Userenv gives correct branch' );
437     ok( checkpw( $patron->userid, $password, undef, undef, 1 ), 'checkpw returns true' );
438     is( C4::Context->userenv->{branch}, $library->branchcode, 'Userenv branch is preserved if no_set_userenv is true' );
439     ok( checkpw( $patron->userid, $password, undef, undef, 0 ), 'checkpw still returns true' );
440     isnt( C4::Context->userenv->{branch}, $library->branchcode, 'Userenv branch is overwritten if no_set_userenv is false' );
441 };
442
443 subtest 'checkpw lockout tests' => sub {
444
445     plan tests => 5;
446
447     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
448     my $patron  = $builder->build_object( { class => 'Koha::Patrons' } );
449     my $password = set_weak_password($patron);
450     t::lib::Mocks::mock_preference( 'FailedLoginAttempts', 1 );
451
452     my ( $checkpw, undef, undef ) = checkpw( $patron->cardnumber, $password, undef, undef, 1 );
453     ok( $checkpw, 'checkpw returns true with right password when logging in via cardnumber' );
454     ( $checkpw, undef, undef ) = checkpw( $patron->userid, "wrong_password", undef, undef, 1 );
455     is( $checkpw, 0, 'checkpw returns false when given wrong password' );
456     $patron = $patron->get_from_storage;
457     is( $patron->account_locked, 1, "Account is locked from failed login");
458     ( $checkpw, undef, undef ) = checkpw( $patron->userid, $password, undef, undef, 1 );
459     is( $checkpw, undef, 'checkpw returns undef with right password when account locked' );
460     ( $checkpw, undef, undef ) = checkpw( $patron->cardnumber, $password, undef, undef, 1 );
461     is( $checkpw, undef, 'checkpw returns undefwith right password when logging in via cardnumber if account locked' );
462
463 };
464
465 # get_template_and_user tests
466
467 subtest 'get_template_and_user' => sub {   # Tests for the language URL parameter
468
469     sub MockedCheckauth {
470         my ($query,$authnotrequired,$flagsrequired,$type) = @_;
471         # return vars
472         my $userid = 'cobain';
473         my $sessionID = 234;
474         # we don't need to bother about permissions for this test
475         my $flags = {
476             superlibrarian    => 1, acquisition       => 0,
477             borrowers         => 0,
478             catalogue         => 1, circulate         => 0,
479             coursereserves    => 0, editauthorities   => 0,
480             editcatalogue     => 0,
481             parameters        => 0, permissions       => 0,
482             plugins           => 0, reports           => 0,
483             reserveforothers  => 0, serials           => 0,
484             staffaccess       => 0, tools             => 0,
485             updatecharges     => 0
486         };
487
488         my $session_cookie = $query->cookie(
489             -name => 'CGISESSID',
490             -value    => 'nirvana',
491             -HttpOnly => 1
492         );
493
494         return ( $userid, [ $session_cookie ], $sessionID, $flags );
495     }
496
497     # Mock checkauth, build the scenario
498     my $auth = Test::MockModule->new( 'C4::Auth' );
499     $auth->mock( 'checkauth', \&MockedCheckauth );
500
501     # Make sure 'EnableOpacSearchHistory' is set
502     t::lib::Mocks::mock_preference('EnableOpacSearchHistory',1);
503     # Enable es-ES for the OPAC and staff interfaces
504     t::lib::Mocks::mock_preference('OPACLanguages','en,es-ES');
505     t::lib::Mocks::mock_preference('language','en,es-ES');
506
507     # we need a session cookie
508     $ENV{"SERVER_PORT"} = 80;
509     $ENV{"HTTP_COOKIE"} = 'CGISESSID=nirvana';
510
511     my $query = CGI->new;
512     $query->param('language','es-ES');
513
514     my ( $template, $loggedinuser, $cookies ) = get_template_and_user(
515         {
516             template_name   => "about.tt",
517             query           => $query,
518             type            => "opac",
519             authnotrequired => 1,
520             flagsrequired   => { catalogue => 1 },
521             debug           => 1
522         }
523     );
524
525     ok ( ( all { ref($_) eq 'CGI::Cookie' } @$cookies ),
526             'BZ9735: the cookies array is flat' );
527
528     # new query, with non-existent language (we only have en and es-ES)
529     $query->param('language','tomas');
530
531     ( $template, $loggedinuser, $cookies ) = get_template_and_user(
532         {
533             template_name   => "about.tt",
534             query           => $query,
535             type            => "opac",
536             authnotrequired => 1,
537             flagsrequired   => { catalogue => 1 },
538             debug           => 1
539         }
540     );
541
542     ok( ( none { $_->name eq 'KohaOpacLanguage' and $_->value eq 'tomas' } @$cookies ),
543         'BZ9735: invalid language, it is not set');
544
545     ok( ( any { $_->name eq 'KohaOpacLanguage' and $_->value eq 'en' } @$cookies ),
546         'BZ9735: invalid language, then default to en');
547
548     for my $template_name (
549         qw(
550             ../../../../../../../../../../../../../../../etc/passwd
551             test/../../../../../../../../../../../../../../etc/passwd
552             /etc/passwd
553             test/does_not_finished_by_tt_t
554         )
555     ) {
556         eval {
557             ( $template, $loggedinuser, $cookies ) = get_template_and_user(
558                 {
559                     template_name   => $template_name,
560                     query           => $query,
561                     type            => "intranet",
562                     authnotrequired => 1,
563                     flagsrequired   => { catalogue => 1 },
564                 }
565             );
566         };
567         like ( $@, qr(bad template path), "The file $template_name should not be accessible" );
568     }
569     ( $template, $loggedinuser, $cookies ) = get_template_and_user(
570         {
571             template_name   => 'errors/errorpage.tt',
572             query           => $query,
573             type            => "intranet",
574             authnotrequired => 1,
575             flagsrequired   => { catalogue => 1 },
576         }
577     );
578     my $file_exists = ( -f $template->{filename} ) ? 1 : 0;
579     is ( $file_exists, 1, 'The file errors/errorpage.tt should be accessible (contains integers)' );
580
581     # Regression test for env opac search limit override
582     $ENV{"OPAC_SEARCH_LIMIT"} = "branch:CPL";
583     $ENV{"OPAC_LIMIT_OVERRIDE"} = 1;
584
585     ( $template, $loggedinuser, $cookies) = get_template_and_user(
586         {
587             template_name => 'opac-main.tt',
588             query => $query,
589             type => 'opac',
590             authnotrequired => 1,
591         }
592     );
593     is($template->{VARS}->{'opac_name'}, "CPL", "Opac name was set correctly");
594     is($template->{VARS}->{'opac_search_limit'}, "branch:CPL", "Search limit was set correctly");
595
596     $ENV{"OPAC_SEARCH_LIMIT"} = "branch:multibranch-19";
597
598     ( $template, $loggedinuser, $cookies) = get_template_and_user(
599         {
600             template_name => 'opac-main.tt',
601             query => $query,
602             type => 'opac',
603             authnotrequired => 1,
604         }
605     );
606     is($template->{VARS}->{'opac_name'}, "multibranch-19", "Opac name was set correctly");
607     is($template->{VARS}->{'opac_search_limit'}, "branch:multibranch-19", "Search limit was set correctly");
608
609     delete $ENV{"HTTP_COOKIE"};
610 };
611
612 # Check that there is always an OPACBaseURL set.
613 my $input = CGI->new();
614 my ( $template1, $borrowernumber, $cookie );
615 ( $template1, $borrowernumber, $cookie ) = get_template_and_user(
616     {
617         template_name => "opac-detail.tt",
618         type => "opac",
619         query => $input,
620         authnotrequired => 1,
621     }
622 );
623
624 ok( ( any { 'OPACBaseURL' eq $_ } keys %{$template1->{VARS}} ),
625     'OPACBaseURL is in OPAC template' );
626
627 my ( $template2 );
628 ( $template2, $borrowernumber, $cookie ) = get_template_and_user(
629     {
630         template_name => "catalogue/detail.tt",
631         type => "intranet",
632         query => $input,
633         authnotrequired => 1,
634     }
635 );
636
637 ok( ( any { 'OPACBaseURL' eq $_ } keys %{$template2->{VARS}} ),
638     'OPACBaseURL is in Staff template' );
639
640 my $hash1 = hash_password('password');
641 my $hash2 = hash_password('password');
642
643 ok(C4::Auth::checkpw_hash('password', $hash1), 'password validates with first hash');
644 ok(C4::Auth::checkpw_hash('password', $hash2), 'password validates with second hash');
645
646 subtest 'Check value of login_attempts in checkpw' => sub {
647     plan tests => 11;
648
649     t::lib::Mocks::mock_preference('FailedLoginAttempts', 3);
650
651     # Only interested here in regular login
652     $C4::Auth::cas  = 0;
653     $C4::Auth::ldap = 0;
654
655     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
656     $patron->login_attempts(2);
657     $patron->password('123')->store; # yes, deliberately not hashed
658
659     is( $patron->account_locked, 0, 'Patron not locked' );
660     my @test = checkpw( $patron->userid, '123', undef, 'opac', 1 );
661         # Note: 123 will not be hashed to 123 !
662     is( $test[0], 0, 'checkpw should have failed' );
663     $patron->discard_changes; # refresh
664     is( $patron->login_attempts, 3, 'Login attempts increased' );
665     is( $patron->account_locked, 1, 'Check locked status' );
666
667     # And another try to go over the limit: different return value!
668     @test = checkpw( $patron->userid, '123', undef, 'opac', 1 );
669     is( @test, 0, 'checkpw failed again and returns nothing now' );
670     $patron->discard_changes; # refresh
671     is( $patron->login_attempts, 3, 'Login attempts not increased anymore' );
672
673     # Administrative lockout cannot be undone?
674     # Pass the right password now (or: add a nice mock).
675     my $auth = Test::MockModule->new( 'C4::Auth' );
676     $auth->mock( 'checkpw_hash', sub { return 1; } ); # not for production :)
677     $patron->login_attempts(0)->store;
678     @test = checkpw( $patron->userid, '123', undef, 'opac', 1 );
679     is( $test[0], 1, 'Build confidence in the mock' );
680     $patron->login_attempts(-1)->store;
681     is( $patron->account_locked, 1, 'Check administrative lockout' );
682     @test = checkpw( $patron->userid, '123', undef, 'opac', 1 );
683     is( @test, 0, 'checkpw gave red' );
684     $patron->discard_changes; # refresh
685     is( $patron->login_attempts, -1, 'Still locked out' );
686     t::lib::Mocks::mock_preference('FailedLoginAttempts', ''); # disable
687     is( $patron->account_locked, 1, 'Check administrative lockout without pref' );
688 };
689
690 subtest 'Check value of login_attempts in checkpw' => sub {
691     plan tests => 2;
692
693     t::lib::Mocks::mock_preference('FailedLoginAttempts', 3);
694     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
695     $patron->set_password({ password => '123', skip_validation => 1 });
696
697     my @test = checkpw( $patron->userid, '123', undef, 'opac', 1 );
698     is( $test[0], 1, 'Patron authenticated correctly' );
699
700     $patron->password_expiration_date('2020-01-01')->store;
701     @test = checkpw( $patron->userid, '123', undef, 'opac', 1 );
702     is( $test[0], -2, 'Patron returned as expired correctly' );
703
704 };
705
706 subtest '_timeout_syspref' => sub {
707
708     plan tests => 6;
709
710     t::lib::Mocks::mock_preference('timeout', "100");
711     is( C4::Auth::_timeout_syspref, 100, );
712
713     t::lib::Mocks::mock_preference('timeout', "2d");
714     is( C4::Auth::_timeout_syspref, 2*86400, );
715
716     t::lib::Mocks::mock_preference('timeout', "2D");
717     is( C4::Auth::_timeout_syspref, 2*86400, );
718
719     t::lib::Mocks::mock_preference('timeout', "10h");
720     is( C4::Auth::_timeout_syspref, 10*3600, );
721
722     t::lib::Mocks::mock_preference('timeout', "10x");
723     warning_is
724         { is( C4::Auth::_timeout_syspref, 600, ); }
725         "The value of the system preference 'timeout' is not correct, defaulting to 600",
726         'Bad values throw a warning and fallback to 600';
727 };
728
729 subtest 'check_cookie_auth' => sub {
730     plan tests => 4;
731
732     t::lib::Mocks::mock_preference('timeout', "1d"); # back to default
733
734     my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { flags => 1 } });
735
736     # Mock a CGI object with real userid param
737     my $cgi = Test::MockObject->new();
738     $cgi->mock(
739         'param',
740         sub {
741             my $var = shift;
742             if ( $var eq 'userid' ) { return $patron->userid; }
743         }
744     );
745     $cgi->mock('multi_param', sub {return q{}} );
746     $cgi->mock( 'cookie', sub { return; } );
747     $cgi->mock( 'request_method', sub { return 'POST' } );
748
749     $ENV{REMOTE_ADDR} = '127.0.0.1';
750
751     # Setting authnotrequired=1 or we wont' hit the return but the end of the sub that prints headers
752     my ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth( $cgi, 1 );
753
754     my ($auth_status, $session) = C4::Auth::check_cookie_auth($sessionID);
755     isnt( $auth_status, 'ok', 'check_cookie_auth should not return ok if the user has not been authenticated before if no permissions needed' );
756     is( $auth_status, 'anon', 'check_cookie_auth should return anon if the user has not been authenticated before and no permissions needed' );
757
758     ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth( $cgi, 1 );
759
760     ($auth_status, $session) = C4::Auth::check_cookie_auth($sessionID, {catalogue => 1});
761     isnt( $auth_status, 'ok', 'check_cookie_auth should not return ok if the user has not been authenticated before and permissions needed' );
762     is( $auth_status, 'anon', 'check_cookie_auth should return anon if the user has not been authenticated before and permissions needed' );
763
764     #FIXME We should have a test to cover 'failed' status when a user has logged in, but doesn't have permission
765 };
766
767 subtest 'checkauth & check_cookie_auth' => sub {
768     plan tests => 34;
769
770     # flags = 4 => { catalogue => 1 }
771     my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { flags => 4 } });
772     my $password = set_weak_password($patron);
773
774     my $cgi_mock = Test::MockModule->new('CGI');
775     $cgi_mock->mock( 'request_method', sub { return 'POST' } );
776
777     my $cgi = CGI->new;
778
779     my $auth = Test::MockModule->new( 'C4::Auth' );
780     # Tests will fail if we hit safe_exit
781     $auth->mock( 'safe_exit', sub { return } );
782
783     my ( $userid, $cookie, $sessionID, $flags );
784     {
785         # checkauth will redirect and safe_exit if not authenticated and not authorized
786         local *STDOUT;
787         my $stdout;
788         open STDOUT, '>', \$stdout;
789         C4::Auth::checkauth($cgi, 0, {catalogue => 1});
790         like( $stdout, qr{<title>\s*Log in to your account} );
791         $sessionID = ( $stdout =~ m{Set-Cookie: CGISESSID=((\d|\w)+);} ) ? $1 : undef;
792         ok($sessionID);
793         close STDOUT;
794     };
795
796     my $first_sessionID = $sessionID;
797
798     $ENV{"HTTP_COOKIE"} = "CGISESSID=$sessionID";
799     # Not authenticated yet, the login form is displayed
800     my $template;
801     ( $userid, $cookie, $sessionID, $flags, $template ) = C4::Auth::checkauth($cgi, 0, {catalogue => 1}, 'intranet', undef, undef, { do_not_print => 1 } );
802     is( $template->{VARS}->{loginprompt}, 1, );
803
804     # Sending undefined fails obviously
805     my ( $auth_status, $session ) = C4::Auth::check_cookie_auth($sessionID, {catalogue => 1} );
806     is( $auth_status, 'failed' );
807     is( $session, undef );
808
809     # Simulating the login form submission
810     $cgi->param('login_userid', $patron->userid);
811     $cgi->param('login_password', $password);
812
813     # Logged in!
814     ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth($cgi, 0, {catalogue => 1});
815     is( $sessionID, $first_sessionID );
816     is( $userid, $patron->userid );
817
818     ( $auth_status, $session ) = C4::Auth::check_cookie_auth($sessionID, {catalogue => 1});
819     is( $auth_status, 'ok' );
820     is( $session->id, $first_sessionID );
821
822     my $patron_to_delete = $builder->build_object({ class => 'Koha::Patrons' });
823     my $fresh_userid = $patron_to_delete->userid;
824     $patron_to_delete->delete;
825     my $old_userid   = $patron->userid;
826
827     # change the current session user's userid
828     $patron->userid( $fresh_userid )->store;
829     ( $auth_status, $session ) = C4::Auth::check_cookie_auth($sessionID, {catalogue => 1});
830     is( $auth_status, 'expired' );
831     is( $session, undef );
832
833     # restore userid and generate a new session
834     $patron->userid($old_userid)->store;
835     ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth($cgi, 0, {catalogue => 1});
836     is( $sessionID, $first_sessionID );
837     is( $userid, $patron->userid );
838
839     # Logging out!
840     $cgi->param('logout.x', 1);
841     $cgi->delete( 'login_userid', 'login_password' );
842     ( $userid, $cookie, $sessionID, $flags, $template ) =
843         C4::Auth::checkauth( $cgi, 0, { catalogue => 1 }, 'intranet', undef, undef, { do_not_print => 1 } );
844
845     is( $sessionID, undef );
846     is( $ENV{"HTTP_COOKIE"}, "CGISESSID=$first_sessionID", 'HTTP_COOKIE not unset' );
847     ( $auth_status, $session) = C4::Auth::check_cookie_auth( $first_sessionID, {catalogue => 1} );
848     is( $auth_status, "expired");
849     is( $session, undef );
850
851     {
852         # Trying to access without sessionID
853         $cgi = CGI->new;
854         ( $auth_status, $session) = C4::Auth::check_cookie_auth(undef, {catalogue => 1});
855         is( $auth_status, 'failed' );
856         is( $session, undef );
857
858         # This will fail on permissions
859         undef $ENV{"HTTP_COOKIE"};
860         {
861             local *STDOUT;
862             my $stdout;
863             open STDOUT, '>', \$stdout;
864             ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth($cgi, 0, {catalogue => 1} );
865             close STDOUT;
866         }
867         is( $userid, undef );
868         is( $sessionID, undef );
869     }
870
871     {
872         # First logging in
873         $cgi = CGI->new;
874         $cgi->param('login_userid', $patron->userid);
875         $cgi->param('login_password', $password);
876         ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth($cgi, 0, {catalogue => 1});
877         is( $userid, $patron->userid );
878         $first_sessionID = $sessionID;
879
880         # Patron does not have the borrowers permission
881         # $ENV{"HTTP_COOKIE"} = "CGISESSID=$sessionID"; # not needed, we use $cgi here
882         {
883             local *STDOUT;
884             my $stdout;
885             open STDOUT, '>', \$stdout;
886             ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth($cgi, 0, {borrowers => 1} );
887             close STDOUT;
888         }
889         is( $userid, undef );
890         is( $sessionID, undef );
891
892         # When calling check_cookie_auth, the session will be deleted
893         ( $auth_status, $session) = C4::Auth::check_cookie_auth( $first_sessionID, { borrowers => 1 } );
894         is( $auth_status, "failed" );
895         is( $session, undef );
896         ( $auth_status, $session) = C4::Auth::check_cookie_auth( $first_sessionID, { borrowers => 1 } );
897         is( $auth_status, 'expired', 'Session no longer exists' );
898
899         # NOTE: It is not what the UI is doing.
900         # From the UI we are allowed to hit an unauthorized page then reuse the session to hit back authorized area.
901         # It is because check_cookie_auth is ALWAYS called from checkauth WITHOUT $flagsrequired
902         # It then return "ok", when the previous called got "failed"
903
904         # Try reusing the deleted session: since it does not exist, we should get a new one now when passing correct permissions
905         $cgi->cookie( -name => 'CGISESSID', value => $first_sessionID );
906         ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth($cgi, 0, {catalogue => 1});
907         is( $userid, $patron->userid );
908         isnt( $sessionID, undef, 'Check if we have a sessionID' );
909         isnt( $sessionID, $first_sessionID, 'New value expected' );
910         ( $auth_status, $session) = C4::Auth::check_cookie_auth( $sessionID, {catalogue => 1} );
911         is( $auth_status, "ok" );
912         is( $session->id, $sessionID, 'Same session' );
913         # Two additional tests on userenv
914         is( $C4::Context::context->{activeuser}, $session->id, 'Check if environment has been setup for session' );
915         is( C4::Context->userenv->{id}, $userid, 'Check userid in userenv' );
916     }
917 };
918
919 subtest 'Userenv clearing in check_cookie_auth' => sub {
920     # Note: We did already test userenv for a logged-in user in previous subtest
921     plan tests => 9;
922
923     t::lib::Mocks::mock_preference( 'timeout', 600 );
924     my $cgi = CGI->new;
925
926     # Create a new anonymous session by passing a fake session ID
927     $cgi->cookie( -name => 'CGISESSID', -value => 'fake_sessionID' );
928     my ($userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth($cgi, 1);
929     my ( $auth_status, $session) = C4::Auth::check_cookie_auth( $sessionID );
930     is( $auth_status, 'anon', 'Should be anonymous' );
931     is( $C4::Context::context->{activeuser}, $session->id, 'Check activeuser' );
932     is( defined C4::Context->userenv, 1, 'There should be a userenv' );
933     is(  C4::Context->userenv->{id}, q{}, 'userid should be empty string' );
934
935     # Make the session expire now, check_cookie_auth will delete it
936     $session->param('lasttime', time() - 1200 );
937     $session->flush;
938     ( $auth_status, $session) = C4::Auth::check_cookie_auth( $sessionID );
939     is( $auth_status, 'expired', 'Should be expired' );
940     is( C4::Context->userenv, undef, 'Environment should be cleared too' );
941
942     # Show that we clear the userenv again: set up env and check deleted session
943     C4::Context->_new_userenv( $sessionID );
944     C4::Context->set_userenv; # empty
945     is( defined C4::Context->userenv, 1, 'There should be an empty userenv again' );
946     ( $auth_status, $session) = C4::Auth::check_cookie_auth( $sessionID );
947     is( $auth_status, 'expired', 'Should be expired already' );
948     is( C4::Context->userenv, undef, 'Environment should be cleared again' );
949 };
950
951 subtest 'create_basic_session tests' => sub {
952     plan tests => 13;
953
954     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
955
956     my $session = C4::Auth::create_basic_session({ patron => $patron, interface => 'opac' });
957
958     isnt($session->id, undef, 'A new sessionID was created');
959     is( $session->param('number'), $patron->borrowernumber, 'Session parameter number matches' );
960     is( $session->param('id'), $patron->userid, 'Session parameter id matches' );
961     is( $session->param('cardnumber'), $patron->cardnumber, 'Session parameter cardnumber matches' );
962     is( $session->param('firstname'), $patron->firstname, 'Session parameter firstname matches' );
963     is( $session->param('surname'), $patron->surname, 'Session parameter surname matches' );
964     is( $session->param('branch'), $patron->branchcode, 'Session parameter branch matches' );
965     is( $session->param('branchname'), $patron->library->branchname, 'Session parameter branchname matches' );
966     is( $session->param('flags'), $patron->flags, 'Session parameter flags matches' );
967     is( $session->param('emailaddress'), $patron->email, 'Session parameter emailaddress matches' );
968     is( $session->param('ip'), $session->remote_addr(), 'Session parameter ip matches' );
969     is( $session->param('interface'), 'opac', 'Session parameter interface matches' );
970
971     $session = C4::Auth::create_basic_session({ patron => $patron, interface => 'staff' });
972     is( $session->param('interface'), 'intranet', 'Staff interface gets converted to intranet' );
973 };
974
975 subtest 'check_cookie_auth overwriting interface already set' => sub {
976     plan tests => 2;
977
978     t::lib::Mocks::mock_preference( 'SessionRestrictionByIP', 0 );
979
980     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
981     my $session = C4::Auth::get_session();
982     $session->param( 'number',       $patron->id );
983     $session->param( 'id',           $patron->userid );
984     $session->param( 'ip',           '1.2.3.4' );
985     $session->param( 'lasttime',     time() );
986     $session->param( 'interface',    'opac' );
987     $session->flush;
988
989     C4::Context->interface('intranet');
990     C4::Auth::check_cookie_auth( $session->id );
991     is( C4::Context->interface, 'intranet', 'check_cookie_auth did not overwrite' );
992     delete $C4::Context::context->{interface}; # clear context interface
993     C4::Auth::check_cookie_auth( $session->id );
994     is( C4::Context->interface, 'opac', 'check_cookie_auth used interface from session when context interface was empty' );
995
996     t::lib::Mocks::mock_preference( 'SessionRestrictionByIP', 1 );
997 };
998
999 $schema->storage->txn_rollback;
1000
1001 subtest 'get_cataloguing_page_permissions() tests' => sub {
1002
1003     plan tests => 6;
1004
1005     $schema->storage->txn_begin;
1006
1007     my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { flags => 2**2 } } );    # catalogue
1008
1009     ok(
1010         !C4::Auth::haspermission( $patron->userid, get_cataloguing_page_permissions() ),
1011         '"catalogue" is not enough to see the cataloguing page'
1012     );
1013
1014     $builder->build(
1015         {
1016             source => 'UserPermission',
1017             value  => {
1018                 borrowernumber => $patron->id,
1019                 module_bit     => 24,               # stockrotation
1020                 code           => 'manage_rotas',
1021             },
1022         }
1023     );
1024
1025     t::lib::Mocks::mock_preference( 'StockRotation', 1 );
1026     ok(
1027         C4::Auth::haspermission( $patron->userid, get_cataloguing_page_permissions() ),
1028         '"stockrotation => manage_rotas" is enough'
1029     );
1030
1031     t::lib::Mocks::mock_preference( 'StockRotation', 0 );
1032     ok(
1033         !C4::Auth::haspermission( $patron->userid, get_cataloguing_page_permissions() ),
1034         '"stockrotation => manage_rotas" is not enough when `StockRotation` is disabled'
1035     );
1036
1037     $builder->build(
1038         {
1039             source => 'UserPermission',
1040             value  => {
1041                 borrowernumber => $patron->id,
1042                 module_bit     => 13,                     # tools
1043                 code           => 'manage_staged_marc',
1044             },
1045         }
1046     );
1047
1048     ok(
1049         C4::Auth::haspermission( $patron->userid, get_cataloguing_page_permissions() ),
1050         'Having one of the listed `tools` subperm is enough'
1051     );
1052
1053     $schema->resultset('UserPermission')->search( { borrowernumber => $patron->id } )->delete;
1054
1055     ok(
1056         !C4::Auth::haspermission( $patron->userid, get_cataloguing_page_permissions() ),
1057         'Permission removed, no access'
1058     );
1059
1060     $builder->build(
1061         {
1062             source => 'UserPermission',
1063             value  => {
1064                 borrowernumber => $patron->id,
1065                 module_bit     => 9,                    # editcatalogue
1066                 code           => 'delete_all_items',
1067             },
1068         }
1069     );
1070
1071     ok(
1072         C4::Auth::haspermission( $patron->userid, get_cataloguing_page_permissions() ),
1073         'Having any `editcatalogue` subperm is enough'
1074     );
1075
1076     $schema->storage->txn_rollback;
1077 };
1078
1079 subtest 'checkpw() return values tests' => sub {
1080
1081     plan tests => 3;
1082
1083     subtest 'Internal check tests' => sub {
1084
1085         plan tests => 25;
1086
1087         $schema->storage->txn_begin;
1088
1089         my $account_locked;
1090         my $password_expired;
1091
1092         my $mock_patron = Test::MockModule->new('Koha::Patron');
1093         $mock_patron->mock( 'account_locked',   sub { return $account_locked; } );
1094         $mock_patron->mock( 'password_expired', sub { return $password_expired; } );
1095
1096         # Only interested here in regular login
1097         t::lib::Mocks::mock_config( 'useshibboleth', undef );
1098         $C4::Auth::cas  = 0;
1099         $C4::Auth::ldap = 0;
1100
1101         my $patron   = $builder->build_object( { class => 'Koha::Patrons' } );
1102         my $password = set_weak_password($patron);
1103
1104         my $patron_to_delete  = $builder->build_object( { class => 'Koha::Patrons' } );
1105         my $unused_userid     = $patron_to_delete->userid;
1106         my $unused_cardnumber = $patron_to_delete->cardnumber;
1107         $patron_to_delete->delete;
1108
1109         $account_locked = 1;
1110         my @return = checkpw( $patron->userid, $password, undef, );
1111         is_deeply( \@return, [], 'If the account is locked, empty list is returned' );
1112
1113         $account_locked = 0;
1114
1115         my @matchpoints = qw(userid cardnumber);
1116         foreach my $matchpoint (@matchpoints) {
1117
1118             @return = checkpw( $patron->$matchpoint, $password, undef, );
1119
1120             is( $return[0],        1,                   "Password validation successful returns 1 ($matchpoint)" );
1121             is( $return[1],        $patron->cardnumber, '`cardnumber` returned' );
1122             is( $return[2],        $patron->userid,     '`userid` returned' );
1123             is( ref( $return[3] ), 'Koha::Patron',      'Koha::Patron object reference returned' );
1124             is( $return[3]->id,    $patron->id,         'Correct patron returned' );
1125         }
1126
1127         @return = checkpw( $patron->userid, $password . 'hey', undef, );
1128
1129         is( scalar @return,    2, "Two results on invalid password scenario" );
1130         is( $return[0],        0, '0 returned on invalid password' );
1131         is( ref( $return[1] ), 'Koha::Patron' );
1132         is( $return[1]->id,    $patron->id, 'Patron matched correctly' );
1133
1134         $password_expired = 1;
1135         @return           = checkpw( $patron->userid, $password, undef, );
1136
1137         is( scalar @return,    2,  "Two results on expired password scenario" );
1138         is( $return[0],        -2, '-2 returned' );
1139         is( ref( $return[1] ), 'Koha::Patron' );
1140         is( $return[1]->id,    $patron->id, 'Patron matched correctly' );
1141
1142         @return = checkpw( $unused_userid, $password, undef, );
1143
1144         is( scalar @return, 2,     "Two results on non-existing userid scenario" );
1145         is( $return[0],     0,     '0 returned' );
1146         is( $return[1],     undef, 'Undef returned, representing no match' );
1147
1148         @return = checkpw( $unused_cardnumber, $password, undef, );
1149
1150         is( scalar @return, 2,     "Only one result on non-existing cardnumber scenario" );
1151         is( $return[0],     0,     '0 returned' );
1152         is( $return[1],     undef, 'Undef returned, representing no match' );
1153
1154         $schema->storage->txn_rollback;
1155     };
1156
1157     subtest 'CAS check (mocked) tests' => sub {
1158
1159         plan tests => 25;
1160
1161         $schema->storage->txn_begin;
1162
1163         my $account_locked;
1164         my $password_expired;
1165
1166         my $mock_patron = Test::MockModule->new('Koha::Patron');
1167         $mock_patron->mock( 'account_locked',   sub { return $account_locked; } );
1168         $mock_patron->mock( 'password_expired', sub { return $password_expired; } );
1169
1170         # Only interested here in regular login
1171         t::lib::Mocks::mock_config( 'useshibboleth', undef );
1172         $C4::Auth::cas  = 1;
1173         $C4::Auth::ldap = 0;
1174
1175         my $patron   = $builder->build_object( { class => 'Koha::Patrons' } );
1176         my $password = 'thePassword123';
1177         $patron->set_password( { password => $password, skip_validation => 1 } );
1178
1179         my $patron_to_delete  = $builder->build_object( { class => 'Koha::Patrons' } );
1180         my $unused_userid     = $patron_to_delete->userid;
1181         my $unused_cardnumber = $patron_to_delete->cardnumber;
1182         $patron_to_delete->delete;
1183
1184         my $ticket = '123456';
1185         my $query  = CGI->new;
1186         $query->param( -name => 'ticket', -value => $ticket );
1187
1188         my @cas_return = ( 1, $patron->cardnumber, $patron->userid, $ticket, Koha::Patrons->find( $patron->id ) );
1189
1190         my $cas_mock = Test::MockModule->new('C4::Auth');
1191         $cas_mock->mock( 'checkpw_cas', sub { return @cas_return; } );
1192
1193         $account_locked = 1;
1194         my @return = checkpw( $patron->userid, $password, $query, );
1195         is_deeply( \@return, [], 'If the account is locked, empty list is returned' );
1196
1197         $account_locked = 0;
1198
1199         my @matchpoints = qw(userid cardnumber);
1200         foreach my $matchpoint (@matchpoints) {
1201
1202             @return = checkpw( $patron->$matchpoint, $password, $query, );
1203
1204             is( $return[0],        1,                   "Password validation successful returns 1 ($matchpoint)" );
1205             is( $return[1],        $patron->cardnumber, '`cardnumber` returned' );
1206             is( $return[2],        $patron->userid,     '`userid` returned' );
1207             is( ref( $return[3] ), 'Koha::Patron',      'Koha::Patron object reference returned' );
1208             is( $return[3]->id,    $patron->id,         'Correct patron returned' );
1209         }
1210
1211         @return = checkpw( $patron->userid, $password . 'hey', $query, );
1212
1213         is( scalar @return,    2, "Two results on invalid password scenario" );
1214         is( $return[0],        0, '0 returned on invalid password' );
1215         is( ref( $return[1] ), 'Koha::Patron' );
1216         is( $return[1]->id,    $patron->id, 'Patron matched correctly' );
1217
1218         $password_expired = 1;
1219         @return           = checkpw( $patron->userid, $password, $query, );
1220
1221         is( scalar @return,    2,  "Two results on expired password scenario" );
1222         is( $return[0],        -2, '-2 returned' );
1223         is( ref( $return[1] ), 'Koha::Patron' );
1224         is( $return[1]->id,    $patron->id, 'Patron matched correctly' );
1225
1226         @return = checkpw( $unused_userid, $password, $query, );
1227
1228         is( scalar @return, 2,     "Two results on non-existing userid scenario" );
1229         is( $return[0],     0,     '0 returned' );
1230         is( $return[1],     undef, 'Undef returned, representing no match' );
1231
1232         @return = checkpw( $unused_cardnumber, $password, $query, );
1233
1234         is( scalar @return, 2,     "Only one result on non-existing cardnumber scenario" );
1235         is( $return[0],     0,     '0 returned' );
1236         is( $return[1],     undef, 'Undef returned, representing no match' );
1237
1238         $schema->storage->txn_rollback;
1239     };
1240
1241     subtest 'Shibboleth check (mocked) tests' => sub {
1242
1243         plan tests => 6;
1244
1245         $schema->storage->txn_begin;
1246
1247         my $account_locked;
1248         my $password_expired;
1249
1250         my $mock_patron = Test::MockModule->new('Koha::Patron');
1251         $mock_patron->mock( 'account_locked',   sub { return $account_locked; } );
1252         $mock_patron->mock( 'password_expired', sub { return $password_expired; } );
1253
1254         # Only interested here in regular login
1255         t::lib::Mocks::mock_config( 'useshibboleth', 1 );
1256         $C4::Auth::cas  = 0;
1257         $C4::Auth::ldap = 0;
1258
1259         my $patron   = $builder->build_object( { class => 'Koha::Patrons' } );
1260         my $password = 'thePassword123';
1261         $patron->set_password( { password => $password, skip_validation => 1 } );
1262
1263         my $patron_to_delete  = $builder->build_object( { class => 'Koha::Patrons' } );
1264         my $unused_userid     = $patron_to_delete->userid;
1265         my $unused_cardnumber = $patron_to_delete->cardnumber;
1266         $patron_to_delete->delete;
1267
1268         my @shib_return = ( 1, $patron->cardnumber, $patron->userid, Koha::Patrons->find( $patron->id ) );
1269
1270         my $auth_mock = Test::MockModule->new('C4::Auth');
1271         $auth_mock->mock( 'shib_ok',        1 );
1272         $auth_mock->mock( 'get_login_shib', 1 );
1273
1274         my $shib_mock = Test::MockModule->new('C4::Auth_with_shibboleth');
1275         $shib_mock->mock( 'checkpw_shib', sub { return @shib_return; } );
1276
1277         $account_locked = 1;
1278         my @return = checkpw( $patron->userid );
1279         is_deeply( \@return, [], 'If the account is locked, empty list is returned' );
1280
1281         $account_locked = 0;
1282
1283         @return = checkpw();
1284
1285         is( $return[0],        1,                   "Password validation successful returns 1" );
1286         is( $return[1],        $patron->cardnumber, '`cardnumber` returned' );
1287         is( $return[2],        $patron->userid,     '`userid` returned' );
1288         is( ref( $return[3] ), 'Koha::Patron',      'Koha::Patron object reference returned' );
1289         is( $return[3]->id,    $patron->id,         'Correct patron returned' );
1290
1291         $schema->storage->txn_rollback;
1292     };
1293 };
1294
1295 subtest 'StaffLoginBranchBasedOnIP' => sub {
1296
1297     plan tests => 7;
1298
1299     $schema->storage->txn_begin;
1300
1301     t::lib::Mocks::mock_preference( 'AutoLocation',              0 );
1302     t::lib::Mocks::mock_preference( 'StaffLoginBranchBasedOnIP', 0 );
1303
1304     my $patron   = $builder->build_object( { class => 'Koha::Patrons',   value => { flags    => 1 } } );
1305     my $branch   = $builder->build_object( { class => 'Koha::Libraries', value => { branchip => "127.0.0.1" } } );
1306     my $password = 'password';
1307     t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 );
1308     $patron->set_password( { password => $password } );
1309
1310     my $cgi_mock = Test::MockModule->new('CGI');
1311     $cgi_mock->mock( 'request_method', sub { return 'POST' } );
1312     my $cgi  = CGI->new;
1313     my $auth = Test::MockModule->new('C4::Auth');
1314
1315     # Simulating the login form submission
1316     $cgi->param( 'login_userid',   $patron->userid );
1317     $cgi->param( 'login_password', $password );
1318
1319     $ENV{REMOTE_ADDR} = '127.0.0.1';
1320     my ( $userid, $cookie, $sessionID, $flags ) =
1321         C4::Auth::checkauth( $cgi, 0, { catalogue => 1 }, 'intranet' );
1322     is( $userid, $patron->userid, "User successfully logged in" );
1323     my $session = C4::Auth::get_session($sessionID);
1324     is( $session->param('branch'), $patron->branchcode, "Logged in branch is set to the patron's branchcode" );
1325
1326     my $template;
1327     t::lib::Mocks::mock_preference( 'StaffLoginBranchBasedOnIP', 1 );
1328
1329     ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth( $cgi, 0, { catalogue => 1 }, 'intranet' );
1330     is( $userid, $patron->userid, "User successfully logged in" );
1331     $session = C4::Auth::get_session($sessionID);
1332     is( $session->param('branch'), $branch->branchcode, "Logged in branch is set based on the IP from REMOTE_ADDR " );
1333
1334     # AutoLocation overrides StaffLoginBranchBasedOnIP
1335     t::lib::Mocks::mock_preference( 'AutoLocation', 1 );
1336     ( $userid, $cookie, $sessionID, $flags, $template ) =
1337         C4::Auth::checkauth( $cgi, 0, { catalogue => 1 }, 'intranet', undef, undef, { do_not_print => 1 } );
1338     is(
1339         $template->{VARS}->{wrongip}, 1,
1340         "AutoLocation prevents StaffLoginBranchBasedOnIP from logging user in to another branch"
1341     );
1342
1343     t::lib::Mocks::mock_preference( 'AutoLocation', 0 );
1344     my $other_branch   = $builder->build_object( { class => 'Koha::Libraries', value => { branchip => "127.0.0.1" } } );
1345     ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth( $cgi, 0, { catalogue => 1 }, 'intranet' );
1346     is( $userid, $patron->userid, "User successfully logged in" );
1347     $session = C4::Auth::get_session($sessionID);
1348     is( $session->param('branch'), $branch->branchcode, "Logged in branch is set based which branch when two libraries have same IP?" );
1349
1350 };
1351
1352 subtest 'AutoLocation' => sub {
1353
1354     plan tests => 11;
1355
1356     $schema->storage->txn_begin;
1357
1358     t::lib::Mocks::mock_preference( 'AutoLocation', 0 );
1359
1360     my $patron   = $builder->build_object( { class => 'Koha::Patrons', value => { flags => 1 } } );
1361     my $password = 'password';
1362     t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 );
1363     $patron->set_password( { password => $password } );
1364
1365     my $cgi_mock = Test::MockModule->new('CGI');
1366     $cgi_mock->mock( 'request_method', sub { return 'POST' } );
1367     my $cgi  = CGI->new;
1368     my $auth = Test::MockModule->new('C4::Auth');
1369
1370     # Simulating the login form submission
1371     $cgi->param( 'login_userid',   $patron->userid );
1372     $cgi->param( 'login_password', $password );
1373
1374     $ENV{REMOTE_ADDR} = '127.0.0.1';
1375     my ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth( $cgi, 0, { catalogue => 1 }, 'intranet' );
1376     is( $userid, $patron->userid, "Standard login without AutoLocation" );
1377
1378     my $template;
1379     t::lib::Mocks::mock_preference( 'AutoLocation', 1 );
1380
1381     # AutoLocation: "Require staff to log in from a computer in the IP address range specified by their library (if any)"
1382     $patron->library->branchip('')->store;    # There is none, allow access from anywhere
1383     ( $userid, $cookie, $sessionID, $flags, $template ) =
1384         C4::Auth::checkauth( $cgi, 0, { catalogue => 1 }, 'intranet' );
1385     is( $userid,   $patron->userid, "Login is successful when patron's branch does not have an IP" );
1386     is( $template, undef,           "Template is undef as none passed and not sent to error page" );
1387
1388     $patron->library->branchip('1.2.3.4')->store;
1389     ( $userid, $cookie, $sessionID, $flags, $template ) =
1390         C4::Auth::checkauth( $cgi, 0, { catalogue => 1 }, 'intranet', undef, undef, { do_not_print => 1 } );
1391     is(
1392         $template->{VARS}->{wrongip}, 1,
1393         "Login denied when no branch specified and IP does not match patron's branch IP"
1394     );
1395
1396     $patron->library->branchip('127.0.0.1')->store;
1397     ( $userid, $cookie, $sessionID, $flags, $template ) =
1398         C4::Auth::checkauth( $cgi, 0, { catalogue => 1 }, 'intranet' );
1399     is( $userid,   $patron->userid, "Login is successful when patron IP and branch IP match" );
1400     is( $template, undef,           "Template is undef as none passed and not sent to error page" );
1401
1402     my $other_library = $builder->build_object( { class => 'Koha::Libraries', value => { branchip => '127.0.0.1' } } );
1403     $patron->library->branchip('127.0.0.1')->store;
1404     ( $userid, $cookie, $sessionID, $flags, $template ) =
1405         C4::Auth::checkauth( $cgi, 0, { catalogue => 1 }, 'intranet' );
1406     my $session = C4::Auth::get_session($sessionID);
1407     is(
1408         $session->param('branch'), $patron->branchcode,
1409         "If no branch specified, and IP matches patron branch, login is successful at patron branch even if another branch IP matches"
1410     );
1411
1412     $cgi->param( 'branch', $other_library->branchcode );
1413     ( $userid, $cookie, $sessionID, $flags, $template ) =
1414         C4::Auth::checkauth( $cgi, 0, { catalogue => 1 }, 'intranet', undef, undef, { do_not_print => 1 } );
1415     $session = C4::Auth::get_session($sessionID);
1416     is(
1417         $session->param('branch'), $other_library->branchcode,
1418         "AutoLocation allows specifying a branch as long as the IP matches"
1419     );
1420
1421     $other_library->branchip('129.0.0.1')->store;
1422     ( $userid, $cookie, $sessionID, $flags, $template ) =
1423         C4::Auth::checkauth( $cgi, 0, { catalogue => 1 }, 'intranet', undef, undef, { do_not_print => 1 } );
1424     is( $template->{VARS}->{wrongip}, 1, "Login denied when branch specified and IP does not match branch IP" );
1425
1426     my $noip_library = $builder->build_object( { class => 'Koha::Libraries', value => { branchip => '' } } );
1427     $cgi->param( 'branch', $noip_library->branchcode );
1428     ( $userid, $cookie, $sessionID, $flags, $template ) =
1429         C4::Auth::checkauth( $cgi, 0, { catalogue => 1 }, 'intranet' );
1430     $session = C4::Auth::get_session($sessionID);
1431     is( $session->param('branch'), $noip_library->branchcode, "When a branch with no IP set is chosen, we respect the choice regardless of current IP" );
1432
1433     $ENV{REMOTE_ADDR} = '129.0.0.1';          # Set current IP to match other_branch
1434     $cgi->param( 'branch', undef );           # Do not pass a branch
1435     $patron->library->branchip('')->store;    # Unset user branch IP, to allow IP matching on any branch
1436     # Add a second branch with same IP
1437     my $another_library = $builder->build_object( { class => 'Koha::Libraries', value => { branchip => '129.0.0.1' } } );
1438     ( $userid, $cookie, $sessionID, $flags, $template ) =
1439         C4::Auth::checkauth( $cgi, 0, { catalogue => 1 }, 'intranet', undef, undef, { do_not_print => 1 } );
1440     $session = C4::Auth::get_session($sessionID);
1441     is(
1442         $session->param('branch'), $other_library->branchcode,
1443         "Which branch is chosen when home branch has no IP and more than 1 library matches?"
1444     );
1445
1446     $schema->storage->txn_rollback;
1447
1448 };
1449
1450 subtest 'AutoSelfCheckAllowed' => sub {
1451     plan tests => 5;
1452
1453     my $query = CGI->new;
1454     my $auth  = Test::MockModule->new('C4::Auth');
1455     $auth->mock( 'safe_exit', sub { return } );
1456
1457     t::lib::Mocks::mock_preference( 'AutoSelfCheckAllowed', 0 );
1458
1459     # Pref is off, cannot access sco
1460     {
1461         # checkauth will redirect and safe_exit if not authenticated and not authorized
1462         local *STDOUT;
1463         my $stdout;
1464         open STDOUT, '>', \$stdout;
1465         my ( $template, $loggedinuser, $cookies ) = get_template_and_user(
1466             {
1467                 template_name => "sco/sco-main.tt",
1468                 query         => $query,
1469                 type          => "opac",
1470                 flagsrequired => { self_check => "self_checkout_module" },
1471             }
1472         );
1473         like( $stdout, qr{<title>\s*Log in to your account} );
1474         close STDOUT;
1475     };
1476
1477     # Pref is on from here
1478     t::lib::Mocks::mock_preference( 'AutoSelfCheckAllowed', 1 );
1479
1480     t::lib::Mocks::mock_preference( 'AutoSelfCheckID',   '' );
1481     t::lib::Mocks::mock_preference( 'AutoSelfCheckPass', '' );
1482
1483     # Credential prefs are empty, cannot access sco
1484     {
1485         # checkauth will redirect and safe_exit if not authenticated and not authorized
1486         local *STDOUT;
1487         my $stdout;
1488         open STDOUT, '>', \$stdout;
1489         my ( $template, $loggedinuser, $cookies ) = get_template_and_user(
1490             {
1491                 template_name => "sco/sco-main.tt",
1492                 query         => $query,
1493                 type          => "opac",
1494                 flagsrequired => { self_check => "self_checkout_module" },
1495             }
1496         );
1497         like( $stdout, qr{<title>\s*Log in to your account} );
1498         close STDOUT;
1499     };
1500
1501     my $sco_patron = $builder->build_object( { class => 'Koha::Patrons', value => { flags => 0 } } );
1502     my $password   = set_weak_password($sco_patron);
1503     t::lib::Mocks::mock_preference( 'AutoSelfCheckID',   $sco_patron->userid );
1504     t::lib::Mocks::mock_preference( 'AutoSelfCheckPass', $password );
1505
1506     # Credential pref are good but patron does not have the self_checkout_module subpermission
1507     {
1508         # checkauth will redirect and safe_exit if not authenticated and not authorized
1509         local *STDOUT;
1510         my $stdout;
1511         open STDOUT, '>', \$stdout;
1512         my ( $template, $loggedinuser, $cookies ) = get_template_and_user(
1513             {
1514                 template_name => "sco/sco-main.tt",
1515                 query         => $query,
1516                 type          => "opac",
1517                 flagsrequired => { self_check => "self_checkout_module" },
1518             }
1519         );
1520         like( $stdout, qr{<title>\s*Log in to your account} );
1521         close STDOUT;
1522     };
1523
1524     # All good from now
1525     C4::Context->dbh->do(
1526         q|
1527             INSERT INTO user_permissions (borrowernumber, module_bit, code) VALUES (?, ?, ?)
1528         |, undef, $sco_patron->borrowernumber, 23, 'self_checkout_module'
1529     );
1530     my ( $template, $loggedinuser, $cookies ) = get_template_and_user(
1531         {
1532             template_name => "sco/sco-main.tt",
1533             query         => $query,
1534             type          => "opac",
1535             flagsrequired => { self_check => "self_checkout_module" },
1536         }
1537     );
1538     is( $template->{VARS}->{logged_in_user}->id, $sco_patron->id );
1539     is( $loggedinuser,                           $sco_patron->id );
1540 };
1541
1542 sub set_weak_password {
1543     my ($patron) = @_;
1544     my $password = 'password';
1545     t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 );
1546     $patron->set_password( { password => $password } );
1547     return $password;
1548 }