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 => 21;
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 $schema->storage->txn_begin;
34 subtest 'checkauth() tests' => sub {
38 my $patron = $builder->build({ source => 'Borrower', value => { flags => undef } })->{userid};
40 # Mock a CGI object with real userid param
41 my $cgi = Test::MockObject->new();
42 $cgi->mock( 'param', sub { return $patron; } );
43 $cgi->mock( 'cookie', sub { return; } );
45 my $authnotrequired = 1;
46 my ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth( $cgi, $authnotrequired );
48 is( $userid, undef, 'checkauth() returns undef for userid if no logged in user (Bug 18275)' );
50 my $db_user_id = C4::Context->config('user');
51 my $db_user_pass = C4::Context->config('pass');
52 $cgi = Test::MockObject->new();
53 $cgi->mock( 'cookie', sub { return; } );
54 $cgi->mock( 'param', sub {
55 my ( $self, $param ) = @_;
56 if ( $param eq 'userid' ) { return $db_user_id; }
57 elsif ( $param eq 'password' ) { return $db_user_pass; }
60 ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth( $cgi, $authnotrequired );
61 is ( $userid, $db_user_id, 'If DB user is logging in, it should be considered as logged in, i.e. checkauth return the relevant userid' );
62 C4::Context->_new_userenv; # For next tests
66 my $hash1 = hash_password('password');
67 my $hash2 = hash_password('password');
69 { # tests no_set_userenv parameter
70 my $patron = $builder->build( { source => 'Borrower' } );
71 Koha::Patrons->find( $patron->{borrowernumber} )->update_password( $patron->{userid}, $hash1 );
72 my $library = $builder->build(
78 ok( checkpw( $dbh, $patron->{userid}, 'password', undef, undef, 1 ), 'checkpw returns true' );
79 is( C4::Context->userenv, undef, 'Userenv should be undef as required' );
80 C4::Context->_new_userenv('DUMMY SESSION');
81 C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, 'Library 1', 0, '', '');
82 is( C4::Context->userenv->{branch}, $library->{branchcode}, 'Userenv gives correct branch' );
83 ok( checkpw( $dbh, $patron->{userid}, 'password', undef, undef, 1 ), 'checkpw returns true' );
84 is( C4::Context->userenv->{branch}, $library->{branchcode}, 'Userenv branch is preserved if no_set_userenv is true' );
85 ok( checkpw( $dbh, $patron->{userid}, 'password', undef, undef, 0 ), 'checkpw still returns true' );
86 isnt( C4::Context->userenv->{branch}, $library->{branchcode}, 'Userenv branch is overwritten if no_set_userenv is false' );
89 # get_template_and_user tests
91 { # Tests for the language URL parameter
94 my ($query,$authnotrequired,$flagsrequired,$type) = @_;
96 my $userid = 'cobain';
98 # we don't need to bother about permissions for this test
100 superlibrarian => 1, acquisition => 0,
102 catalogue => 1, circulate => 0,
103 coursereserves => 0, editauthorities => 0,
104 editcatalogue => 0, management => 0,
105 parameters => 0, permissions => 0,
106 plugins => 0, reports => 0,
107 reserveforothers => 0, serials => 0,
108 staffaccess => 0, tools => 0,
112 my $session_cookie = $query->cookie(
113 -name => 'CGISESSID',
118 return ( $userid, $session_cookie, $sessionID, $flags );
121 # Mock checkauth, build the scenario
122 my $auth = new Test::MockModule( 'C4::Auth' );
123 $auth->mock( 'checkauth', \&MockedCheckauth );
125 # Make sure 'EnableOpacSearchHistory' is set
126 t::lib::Mocks::mock_preference('EnableOpacSearchHistory',1);
127 # Enable es-ES for the OPAC and staff interfaces
128 t::lib::Mocks::mock_preference('opaclanguages','en,es-ES');
129 t::lib::Mocks::mock_preference('language','en,es-ES');
131 # we need a session cookie
132 $ENV{"SERVER_PORT"} = 80;
133 $ENV{"HTTP_COOKIE"} = 'CGISESSID=nirvana';
136 $query->param('language','es-ES');
138 my ( $template, $loggedinuser, $cookies ) = get_template_and_user(
140 template_name => "about.tt",
143 authnotrequired => 1,
144 flagsrequired => { catalogue => 1 },
149 ok ( ( all { ref($_) eq 'CGI::Cookie' } @$cookies ),
150 'BZ9735: the cookies array is flat' );
152 # new query, with non-existent language (we only have en and es-ES)
153 $query->param('language','tomas');
155 ( $template, $loggedinuser, $cookies ) = get_template_and_user(
157 template_name => "about.tt",
160 authnotrequired => 1,
161 flagsrequired => { catalogue => 1 },
166 ok( ( none { $_->name eq 'KohaOpacLanguage' and $_->value eq 'tomas' } @$cookies ),
167 'BZ9735: invalid language, it is not set');
169 ok( ( any { $_->name eq 'KohaOpacLanguage' and $_->value eq 'en' } @$cookies ),
170 'BZ9735: invalid language, then default to en');
172 for my $template_name (
174 ../../../../../../../../../../../../../../../etc/passwd
175 test/../../../../../../../../../../../../../../etc/passwd
177 test/does_not_finished_by_tt_t
181 ( $template, $loggedinuser, $cookies ) = get_template_and_user(
183 template_name => $template_name,
186 authnotrequired => 1,
187 flagsrequired => { catalogue => 1 },
191 like ( $@, qr(^bad template path), 'The file $template_name should not be accessible' );
193 ( $template, $loggedinuser, $cookies ) = get_template_and_user(
195 template_name => 'errors/errorpage.tt',
198 authnotrequired => 1,
199 flagsrequired => { catalogue => 1 },
202 my $file_exists = ( -f $template->{filename} ) ? 1 : 0;
203 is ( $file_exists, 1, 'The file errors/errorpage.tt should be accessible (contains integers)' );
206 # Check that there is always an OPACBaseURL set.
207 my $input = CGI->new();
208 my ( $template1, $borrowernumber, $cookie );
209 ( $template1, $borrowernumber, $cookie ) = get_template_and_user(
211 template_name => "opac-detail.tt",
214 authnotrequired => 1,
218 ok( ( any { 'OPACBaseURL' eq $_ } keys %{$template1->{VARS}} ),
219 'OPACBaseURL is in OPAC template' );
222 ( $template2, $borrowernumber, $cookie ) = get_template_and_user(
224 template_name => "catalogue/detail.tt",
227 authnotrequired => 1,
231 ok( ( any { 'OPACBaseURL' eq $_ } keys %{$template2->{VARS}} ),
232 'OPACBaseURL is in Staff template' );
234 ok(C4::Auth::checkpw_hash('password', $hash1), 'password validates with first hash');
235 ok(C4::Auth::checkpw_hash('password', $hash2), 'password validates with second hash');