Bug 16974: Make koha-plack should check and fix log files permissions
[koha.git] / t / db_dependent / Auth.t
blob233b2beba212b7b32c600f7b6f6e45e502b150d5
1 #!/usr/bin/perl
3 # This Koha test module is a stub!
4 # Add more tests here!!!
6 use Modern::Perl;
8 use CGI qw ( -utf8 );
9 use Test::MockModule;
10 use List::MoreUtils qw/all any none/;
11 use Test::More tests => 20;
12 use Test::Warn;
13 use t::lib::Mocks;
14 use t::lib::TestBuilder;
16 use C4::Auth qw(checkpw);
17 use C4::Members;
18 use Koha::AuthUtils qw/hash_password/;
19 use Koha::Database;
20 use Koha::Patrons;
22 BEGIN {
23 use_ok('C4::Auth');
26 my $schema = Koha::Database->schema;
27 $schema->storage->txn_begin;
28 my $builder = t::lib::TestBuilder->new;
29 my $dbh = C4::Context->dbh;
31 my $hash1 = hash_password('password');
32 my $hash2 = hash_password('password');
34 { # tests no_set_userenv parameter
35 my $patron = $builder->build( { source => 'Borrower' } );
36 Koha::Patrons->find( $patron->{borrowernumber} )->update_password( $patron->{userid}, $hash1 );
37 my $library = $builder->build(
39 source => 'Branch',
43 ok( checkpw( $dbh, $patron->{userid}, 'password', undef, undef, 1 ), 'checkpw returns true' );
44 is( C4::Context->userenv, undef, 'Userenv should be undef as required' );
45 C4::Context->_new_userenv('DUMMY SESSION');
46 C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, 'Library 1', 0, '', '');
47 is( C4::Context->userenv->{branch}, $library->{branchcode}, 'Userenv gives correct branch' );
48 ok( checkpw( $dbh, $patron->{userid}, 'password', undef, undef, 1 ), 'checkpw returns true' );
49 is( C4::Context->userenv->{branch}, $library->{branchcode}, 'Userenv branch is preserved if no_set_userenv is true' );
50 ok( checkpw( $dbh, $patron->{userid}, 'password', undef, undef, 0 ), 'checkpw still returns true' );
51 isnt( C4::Context->userenv->{branch}, $library->{branchcode}, 'Userenv branch is overwritten if no_set_userenv is false' );
54 # get_template_and_user tests
56 { # Tests for the language URL parameter
58 sub MockedCheckauth {
59 my ($query,$authnotrequired,$flagsrequired,$type) = @_;
60 # return vars
61 my $userid = 'cobain';
62 my $sessionID = 234;
63 # we don't need to bother about permissions for this test
64 my $flags = {
65 superlibrarian => 1, acquisition => 0,
66 borrowers => 0,
67 catalogue => 1, circulate => 0,
68 coursereserves => 0, editauthorities => 0,
69 editcatalogue => 0, management => 0,
70 parameters => 0, permissions => 0,
71 plugins => 0, reports => 0,
72 reserveforothers => 0, serials => 0,
73 staffaccess => 0, tools => 0,
74 updatecharges => 0
77 my $session_cookie = $query->cookie(
78 -name => 'CGISESSID',
79 -value => 'nirvana',
80 -HttpOnly => 1
83 return ( $userid, $session_cookie, $sessionID, $flags );
86 # Mock checkauth, build the scenario
87 my $auth = new Test::MockModule( 'C4::Auth' );
88 $auth->mock( 'checkauth', \&MockedCheckauth );
90 # Make sure 'EnableOpacSearchHistory' is set
91 t::lib::Mocks::mock_preference('EnableOpacSearchHistory',1);
92 # Enable es-ES for the OPAC and staff interfaces
93 t::lib::Mocks::mock_preference('opaclanguages','en,es-ES');
94 t::lib::Mocks::mock_preference('language','en,es-ES');
96 # we need a session cookie
97 $ENV{"SERVER_PORT"} = 80;
98 $ENV{"HTTP_COOKIE"} = 'CGISESSID=nirvana';
100 my $query = new CGI;
101 $query->param('language','es-ES');
103 my ( $template, $loggedinuser, $cookies ) = get_template_and_user(
105 template_name => "about.tt",
106 query => $query,
107 type => "opac",
108 authnotrequired => 1,
109 flagsrequired => { catalogue => 1 },
110 debug => 1
114 ok ( ( all { ref($_) eq 'CGI::Cookie' } @$cookies ),
115 'BZ9735: the cookies array is flat' );
117 # new query, with non-existent language (we only have en and es-ES)
118 $query->param('language','tomas');
120 ( $template, $loggedinuser, $cookies ) = get_template_and_user(
122 template_name => "about.tt",
123 query => $query,
124 type => "opac",
125 authnotrequired => 1,
126 flagsrequired => { catalogue => 1 },
127 debug => 1
131 ok( ( none { $_->name eq 'KohaOpacLanguage' and $_->value eq 'tomas' } @$cookies ),
132 'BZ9735: invalid language, it is not set');
134 ok( ( any { $_->name eq 'KohaOpacLanguage' and $_->value eq 'en' } @$cookies ),
135 'BZ9735: invalid language, then default to en');
137 for my $template_name (
139 ../../../../../../../../../../../../../../../etc/passwd
140 test/../../../../../../../../../../../../../../etc/passwd
141 /etc/passwd
142 test/does_not_finished_by_tt_t
145 eval {
146 ( $template, $loggedinuser, $cookies ) = get_template_and_user(
148 template_name => $template_name,
149 query => $query,
150 type => "intranet",
151 authnotrequired => 1,
152 flagsrequired => { catalogue => 1 },
156 like ( $@, qr(^bad template path), 'The file $template_name should not be accessible' );
158 ( $template, $loggedinuser, $cookies ) = get_template_and_user(
160 template_name => 'errors/errorpage.tt',
161 query => $query,
162 type => "intranet",
163 authnotrequired => 1,
164 flagsrequired => { catalogue => 1 },
167 my $file_exists = ( -f $template->{filename} ) ? 1 : 0;
168 is ( $file_exists, 1, 'The file errors/errorpage.tt should be accessible (contains integers)' );
171 # Check that there is always an OPACBaseURL set.
172 my $input = CGI->new();
173 my ( $template1, $borrowernumber, $cookie );
174 ( $template1, $borrowernumber, $cookie ) = get_template_and_user(
176 template_name => "opac-detail.tt",
177 type => "opac",
178 query => $input,
179 authnotrequired => 1,
183 ok( ( any { 'OPACBaseURL' eq $_ } keys %{$template1->{VARS}} ),
184 'OPACBaseURL is in OPAC template' );
186 my ( $template2 );
187 ( $template2, $borrowernumber, $cookie ) = get_template_and_user(
189 template_name => "catalogue/detail.tt",
190 type => "intranet",
191 query => $input,
192 authnotrequired => 1,
196 ok( ( any { 'OPACBaseURL' eq $_ } keys %{$template2->{VARS}} ),
197 'OPACBaseURL is in Staff template' );
199 ok(C4::Auth::checkpw_hash('password', $hash1), 'password validates with first hash');
200 ok(C4::Auth::checkpw_hash('password', $hash2), 'password validates with second hash');