3 # This Koha test module is a stub!
4 # Add more tests here!!!
12 use List
::MoreUtils qw
/all any none/;
13 use Test
::More tests
=> 22;
16 use t
::lib
::TestBuilder
;
18 use C4
::Auth
qw(checkpw);
20 use Koha
::AuthUtils qw
/hash_password/;
28 my $schema = Koha
::Database
->schema;
29 my $builder = t
::lib
::TestBuilder
->new;
30 my $dbh = C4
::Context
->dbh;
32 # FIXME: SessionStorage defaults to mysql, but it seems to break transaction
34 t
::lib
::Mocks
::mock_preference
( 'SessionStorage', 'tmp' );
35 t
::lib
::Mocks
::mock_preference
( 'GDPR_Policy', '' ); # Disabled
37 $schema->storage->txn_begin;
39 subtest
'checkauth() tests' => sub {
43 my $patron = $builder->build({ source
=> 'Borrower', value
=> { flags
=> undef } })->{userid
};
45 # Mock a CGI object with real userid param
46 my $cgi = Test
::MockObject
->new();
47 $cgi->mock( 'param', sub { return $patron; } );
48 $cgi->mock( 'cookie', sub { return; } );
50 my $authnotrequired = 1;
51 my ( $userid, $cookie, $sessionID, $flags ) = C4
::Auth
::checkauth
( $cgi, $authnotrequired );
53 is
( $userid, undef, 'checkauth() returns undef for userid if no logged in user (Bug 18275)' );
55 my $db_user_id = C4
::Context
->config('user');
56 my $db_user_pass = C4
::Context
->config('pass');
57 $cgi = Test
::MockObject
->new();
58 $cgi->mock( 'cookie', sub { return; } );
59 $cgi->mock( 'param', sub {
60 my ( $self, $param ) = @_;
61 if ( $param eq 'userid' ) { return $db_user_id; }
62 elsif ( $param eq 'password' ) { return $db_user_pass; }
65 ( $userid, $cookie, $sessionID, $flags ) = C4
::Auth
::checkauth
( $cgi, $authnotrequired );
66 is
( $userid, undef, 'If DB user is used, it should not be logged in' );
68 my $is_allowed = C4
::Auth
::haspermission
( $db_user_id, { can_do
=> 'everything' } );
70 # FIXME This belongs to t/db_dependent/Auth/haspermission.t but we do not want to c/p the pervious mock statements
71 ok
( !$is_allowed, 'DB user should not have any permissions');
73 C4
::Context
->_new_userenv; # For next tests
77 subtest
'track_login_daily tests' => sub {
81 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
82 my $userid = $patron->userid;
84 $patron->lastseen( undef );
87 my $cache = Koha
::Caches
->get_instance();
88 my $cache_key = "track_login_" . $patron->userid;
89 $cache->clear_from_cache($cache_key);
91 t
::lib
::Mocks
::mock_preference
( 'TrackLastPatronActivity', '1' );
93 is
( $patron->lastseen, undef, 'Patron should have not last seen when newly created' );
95 C4
::Auth
::track_login_daily
( $userid );
96 $patron->_result()->discard_changes();
97 isnt
( $patron->lastseen, undef, 'Patron should have last seen set when TrackLastPatronActivity = 1' );
99 sleep(1); # We need to wait a tiny bit to make sure the timestamp will be different
100 my $last_seen = $patron->lastseen;
101 C4
::Auth
::track_login_daily
( $userid );
102 $patron->_result()->discard_changes();
103 is
( $patron->lastseen, $last_seen, 'Patron last seen should still be unchanged' );
105 $cache->clear_from_cache($cache_key);
106 C4
::Auth
::track_login_daily
( $userid );
107 $patron->_result()->discard_changes();
108 isnt
( $patron->lastseen, $last_seen, 'Patron last seen should be changed if we cleared the cache' );
110 t
::lib
::Mocks
::mock_preference
( 'TrackLastPatronActivity', '0' );
111 $patron->lastseen( undef )->store;
112 $cache->clear_from_cache($cache_key);
113 C4
::Auth
::track_login_daily
( $userid );
114 $patron->_result()->discard_changes();
115 is
( $patron->lastseen, undef, 'Patron should still have last seen unchanged when TrackLastPatronActivity = 0' );
119 subtest
'no_set_userenv parameter tests' => sub {
123 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
124 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
125 my $password = 'password';
127 t
::lib
::Mocks
::mock_preference
( 'RequireStrongPassword', 0 );
128 $patron->set_password({ password
=> $password });
130 ok
( checkpw
( $dbh, $patron->userid, $password, undef, undef, 1 ), 'checkpw returns true' );
131 is
( C4
::Context
->userenv, undef, 'Userenv should be undef as required' );
132 C4
::Context
->_new_userenv('DUMMY SESSION');
133 C4
::Context
->set_userenv(0,0,0,'firstname','surname', $library->branchcode, 'Library 1', 0, '', '');
134 is
( C4
::Context
->userenv->{branch
}, $library->branchcode, 'Userenv gives correct branch' );
135 ok
( checkpw
( $dbh, $patron->userid, $password, undef, undef, 1 ), 'checkpw returns true' );
136 is
( C4
::Context
->userenv->{branch
}, $library->branchcode, 'Userenv branch is preserved if no_set_userenv is true' );
137 ok
( checkpw
( $dbh, $patron->userid, $password, undef, undef, 0 ), 'checkpw still returns true' );
138 isnt
( C4
::Context
->userenv->{branch
}, $library->branchcode, 'Userenv branch is overwritten if no_set_userenv is false' );
141 subtest
'checkpw lockout tests' => sub {
145 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
146 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
147 my $password = 'password';
148 t
::lib
::Mocks
::mock_preference
( 'RequireStrongPassword', 0 );
149 t
::lib
::Mocks
::mock_preference
( 'FailedLoginAttempts', 1 );
150 $patron->set_password({ password
=> $password });
152 my ( $checkpw, undef, undef ) = checkpw
( $dbh, $patron->cardnumber, $password, undef, undef, 1 );
153 ok
( $checkpw, 'checkpw returns true with right password when logging in via cardnumber' );
154 ( $checkpw, undef, undef ) = checkpw
( $dbh, $patron->userid, "wrong_password", undef, undef, 1 );
155 is
( $checkpw, 0, 'checkpw returns false when given wrong password' );
156 $patron = $patron->get_from_storage;
157 is
( $patron->account_locked, 1, "Account is locked from failed login");
158 ( $checkpw, undef, undef ) = checkpw
( $dbh, $patron->userid, $password, undef, undef, 1 );
159 is
( $checkpw, undef, 'checkpw returns undef with right password when account locked' );
160 ( $checkpw, undef, undef ) = checkpw
( $dbh, $patron->cardnumber, $password, undef, undef, 1 );
161 is
( $checkpw, undef, 'checkpw returns undefwith right password when logging in via cardnumber if account locked' );
165 # get_template_and_user tests
167 { # Tests for the language URL parameter
169 sub MockedCheckauth
{
170 my ($query,$authnotrequired,$flagsrequired,$type) = @_;
172 my $userid = 'cobain';
174 # we don't need to bother about permissions for this test
176 superlibrarian
=> 1, acquisition
=> 0,
178 catalogue
=> 1, circulate
=> 0,
179 coursereserves
=> 0, editauthorities
=> 0,
181 parameters
=> 0, permissions
=> 0,
182 plugins
=> 0, reports
=> 0,
183 reserveforothers
=> 0, serials
=> 0,
184 staffaccess
=> 0, tools
=> 0,
188 my $session_cookie = $query->cookie(
189 -name
=> 'CGISESSID',
194 return ( $userid, $session_cookie, $sessionID, $flags );
197 # Mock checkauth, build the scenario
198 my $auth = new Test
::MockModule
( 'C4::Auth' );
199 $auth->mock( 'checkauth', \
&MockedCheckauth
);
201 # Make sure 'EnableOpacSearchHistory' is set
202 t
::lib
::Mocks
::mock_preference
('EnableOpacSearchHistory',1);
203 # Enable es-ES for the OPAC and staff interfaces
204 t
::lib
::Mocks
::mock_preference
('opaclanguages','en,es-ES');
205 t
::lib
::Mocks
::mock_preference
('language','en,es-ES');
207 # we need a session cookie
208 $ENV{"SERVER_PORT"} = 80;
209 $ENV{"HTTP_COOKIE"} = 'CGISESSID=nirvana';
212 $query->param('language','es-ES');
214 my ( $template, $loggedinuser, $cookies ) = get_template_and_user
(
216 template_name
=> "about.tt",
219 authnotrequired
=> 1,
220 flagsrequired
=> { catalogue
=> 1 },
225 ok
( ( all
{ ref($_) eq 'CGI::Cookie' } @
$cookies ),
226 'BZ9735: the cookies array is flat' );
228 # new query, with non-existent language (we only have en and es-ES)
229 $query->param('language','tomas');
231 ( $template, $loggedinuser, $cookies ) = get_template_and_user
(
233 template_name
=> "about.tt",
236 authnotrequired
=> 1,
237 flagsrequired
=> { catalogue
=> 1 },
242 ok
( ( none
{ $_->name eq 'KohaOpacLanguage' and $_->value eq 'tomas' } @
$cookies ),
243 'BZ9735: invalid language, it is not set');
245 ok
( ( any
{ $_->name eq 'KohaOpacLanguage' and $_->value eq 'en' } @
$cookies ),
246 'BZ9735: invalid language, then default to en');
248 for my $template_name (
250 ../../../../../../../../../../../../../../../etc/passwd
251 test/../../../../../../../../../../../../../../etc/passwd
253 test/does_not_finished_by_tt_t
257 ( $template, $loggedinuser, $cookies ) = get_template_and_user
(
259 template_name
=> $template_name,
262 authnotrequired
=> 1,
263 flagsrequired
=> { catalogue
=> 1 },
267 like
( $@
, qr
(^bad template path
), "The file $template_name should not be accessible" );
269 ( $template, $loggedinuser, $cookies ) = get_template_and_user
(
271 template_name
=> 'errors/errorpage.tt',
274 authnotrequired
=> 1,
275 flagsrequired
=> { catalogue
=> 1 },
278 my $file_exists = ( -f
$template->{filename
} ) ?
1 : 0;
279 is
( $file_exists, 1, 'The file errors/errorpage.tt should be accessible (contains integers)' );
281 # Regression test for env opac search limit override
282 $ENV{"OPAC_SEARCH_LIMIT"} = "branch:CPL";
283 $ENV{"OPAC_LIMIT_OVERRIDE"} = 1;
285 ( $template, $loggedinuser, $cookies) = get_template_and_user
(
287 template_name
=> 'opac-main.tt',
290 authnotrequired
=> 1,
293 is
($template->{VARS
}->{'opac_name'}, "CPL", "Opac name was set correctly");
294 is
($template->{VARS
}->{'opac_search_limit'}, "branch:CPL", "Search limit was set correctly");
296 $ENV{"OPAC_SEARCH_LIMIT"} = "branch:multibranch-19";
298 ( $template, $loggedinuser, $cookies) = get_template_and_user
(
300 template_name
=> 'opac-main.tt',
303 authnotrequired
=> 1,
306 is
($template->{VARS
}->{'opac_name'}, "multibranch-19", "Opac name was set correctly");
307 is
($template->{VARS
}->{'opac_search_limit'}, "branch:multibranch-19", "Search limit was set correctly");
310 # Check that there is always an OPACBaseURL set.
311 my $input = CGI
->new();
312 my ( $template1, $borrowernumber, $cookie );
313 ( $template1, $borrowernumber, $cookie ) = get_template_and_user
(
315 template_name
=> "opac-detail.tt",
318 authnotrequired
=> 1,
322 ok
( ( any
{ 'OPACBaseURL' eq $_ } keys %{$template1->{VARS
}} ),
323 'OPACBaseURL is in OPAC template' );
326 ( $template2, $borrowernumber, $cookie ) = get_template_and_user
(
328 template_name
=> "catalogue/detail.tt",
331 authnotrequired
=> 1,
335 ok
( ( any
{ 'OPACBaseURL' eq $_ } keys %{$template2->{VARS
}} ),
336 'OPACBaseURL is in Staff template' );
338 my $hash1 = hash_password
('password');
339 my $hash2 = hash_password
('password');
341 ok
(C4
::Auth
::checkpw_hash
('password', $hash1), 'password validates with first hash');
342 ok
(C4
::Auth
::checkpw_hash
('password', $hash2), 'password validates with second hash');
344 subtest
'Check value of login_attempts in checkpw' => sub {
347 t
::lib
::Mocks
::mock_preference
('FailedLoginAttempts', 3);
349 # Only interested here in regular login
353 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
354 $patron->login_attempts(2);
355 $patron->password('123')->store; # yes, deliberately not hashed
357 is
( $patron->account_locked, 0, 'Patron not locked' );
358 my @test = checkpw
( $dbh, $patron->userid, '123', undef, 'opac', 1 );
359 # Note: 123 will not be hashed to 123 !
360 is
( $test[0], 0, 'checkpw should have failed' );
361 $patron->discard_changes; # refresh
362 is
( $patron->login_attempts, 3, 'Login attempts increased' );
363 is
( $patron->account_locked, 1, 'Check locked status' );
365 # And another try to go over the limit: different return value!
366 @test = checkpw
( $dbh, $patron->userid, '123', undef, 'opac', 1 );
367 is
( @test, 0, 'checkpw failed again and returns nothing now' );
368 $patron->discard_changes; # refresh
369 is
( $patron->login_attempts, 3, 'Login attempts not increased anymore' );
371 # Administrative lockout cannot be undone?
372 # Pass the right password now (or: add a nice mock).
373 my $auth = Test
::MockModule
->new( 'C4::Auth' );
374 $auth->mock( 'checkpw_hash', sub { return 1; } ); # not for production :)
375 $patron->login_attempts(0)->store;
376 @test = checkpw
( $dbh, $patron->userid, '123', undef, 'opac', 1 );
377 is
( $test[0], 1, 'Build confidence in the mock' );
378 $patron->login_attempts(-1)->store;
379 is
( $patron->account_locked, 1, 'Check administrative lockout' );
380 @test = checkpw
( $dbh, $patron->userid, '123', undef, 'opac', 1 );
381 is
( @test, 0, 'checkpw gave red' );
382 $patron->discard_changes; # refresh
383 is
( $patron->login_attempts, -1, 'Still locked out' );
384 t
::lib
::Mocks
::mock_preference
('FailedLoginAttempts', ''); # disable
385 is
( $patron->account_locked, 1, 'Check administrative lockout without pref' );
388 $schema->storage->txn_rollback;