Bug 14408: Allow integers in template paths
[koha.git] / t / db_dependent / Auth.t
1 #!/usr/bin/perl
2 #
3 # This Koha test module is a stub!  
4 # Add more tests here!!!
5
6 use Modern::Perl;
7
8 use CGI qw ( -utf8 );
9 use Test::MockModule;
10 use List::MoreUtils qw/all any none/;
11 use Test::More tests => 12;
12 use Test::Warn;
13 use C4::Members;
14 use Koha::AuthUtils qw/hash_password/;
15
16 BEGIN {
17         use_ok('C4::Auth');
18 }
19
20 my $dbh = C4::Context->dbh;
21
22 # Start transaction
23 $dbh->{AutoCommit} = 0;
24 $dbh->{RaiseError} = 1;
25
26
27 # get_template_and_user tests
28
29 {   # Tests for the language URL parameter
30
31     sub MockedCheckauth {
32         my ($query,$authnotrequired,$flagsrequired,$type) = @_;
33         # return vars
34         my $userid = 'cobain';
35         my $sessionID = 234;
36         # we don't need to bother about permissions for this test
37         my $flags = {
38             superlibrarian    => 1, acquisition       => 0,
39             borrow            => 0, borrowers         => 0,
40             catalogue         => 1, circulate         => 0,
41             coursereserves    => 0, editauthorities   => 0,
42             editcatalogue     => 0, management        => 0,
43             parameters        => 0, permissions       => 0,
44             plugins           => 0, reports           => 0,
45             reserveforothers  => 0, serials           => 0,
46             staffaccess       => 0, tools             => 0,
47             updatecharges     => 0
48         };
49
50         my $session_cookie = $query->cookie(
51             -name => 'CGISESSID',
52             -value    => 'nirvana',
53             -HttpOnly => 1
54         );
55
56         return ( $userid, $session_cookie, $sessionID, $flags );
57     }
58
59     # Mock checkauth, build the scenario
60     my $auth = new Test::MockModule( 'C4::Auth' );
61     $auth->mock( 'checkauth', \&MockedCheckauth );
62
63     # Make sure 'EnableOpacSearchHistory' is set
64     C4::Context->set_preference('EnableOpacSearchHistory',1);
65     # Enable es-ES for the OPAC and staff interfaces
66     C4::Context->set_preference('opaclanguages','en,es-ES');
67     C4::Context->set_preference('language','en,es-ES');
68
69     # we need a session cookie
70     $ENV{"SERVER_PORT"} = 80;
71     $ENV{"HTTP_COOKIE"} = 'CGISESSID=nirvana';
72
73     my $query = new CGI;
74     $query->param('language','es-ES');
75
76     my ( $template, $loggedinuser, $cookies ) = get_template_and_user(
77         {
78             template_name   => "about.tt",
79             query           => $query,
80             type            => "opac",
81             authnotrequired => 1,
82             flagsrequired   => { catalogue => 1 },
83             debug           => 1
84         }
85     );
86
87     ok ( ( all { ref($_) eq 'CGI::Cookie' } @$cookies ),
88             'BZ9735: the cookies array is flat' );
89
90     # new query, with non-existent language (we only have en and es-ES)
91     $query->param('language','tomas');
92
93     ( $template, $loggedinuser, $cookies ) = get_template_and_user(
94         {
95             template_name   => "about.tt",
96             query           => $query,
97             type            => "opac",
98             authnotrequired => 1,
99             flagsrequired   => { catalogue => 1 },
100             debug           => 1
101         }
102     );
103
104     ok( ( none { $_->name eq 'KohaOpacLanguage' and $_->value eq 'tomas' } @$cookies ),
105         'BZ9735: invalid language, it is not set');
106
107     ok( ( any { $_->name eq 'KohaOpacLanguage' and $_->value eq 'en' } @$cookies ),
108         'BZ9735: invalid language, then default to en');
109
110     for my $template_name (
111         qw(
112             ../../../../../../../../../../../../../../../etc/passwd
113             test/../../../../../../../../../../../../../../etc/passwd
114             /etc/passwd
115         )
116     ) {
117         eval {
118             ( $template, $loggedinuser, $cookies ) = get_template_and_user(
119                 {
120                     template_name   => $template_name,
121                     query           => $query,
122                     type            => "intranet",
123                     authnotrequired => 1,
124                     flagsrequired   => { catalogue => 1 },
125                 }
126             );
127         };
128         like ( $@, qr(^bad template path), 'The file $template_name should not be accessible' );
129     }
130     ( $template, $loggedinuser, $cookies ) = get_template_and_user(
131         {
132             template_name   => 'errors/500.tt',
133             query           => $query,
134             type            => "intranet",
135             authnotrequired => 1,
136             flagsrequired   => { catalogue => 1 },
137         }
138     );
139     my $file_exists = ( -f $template->{filename} ) ? 1 : 0;
140     is ( $file_exists, 1, 'The file errors/500.tt should be accessible (contains integers)' );
141 }
142
143 # Check that there is always an OPACBaseURL set.
144 my $input = CGI->new();
145 my ( $template1, $borrowernumber, $cookie );
146 ( $template1, $borrowernumber, $cookie ) = get_template_and_user(
147     {
148         template_name => "opac-detail.tt",
149         type => "opac",
150         query => $input,
151         authnotrequired => 1,
152     }
153 );
154
155 ok( ( any { 'OPACBaseURL' eq $_ } keys %{$template1->{VARS}} ),
156     'OPACBaseURL is in OPAC template' );
157
158 my ( $template2 );
159 ( $template2, $borrowernumber, $cookie ) = get_template_and_user(
160     {
161         template_name => "catalogue/detail.tt",
162         type => "intranet",
163         query => $input,
164         authnotrequired => 1,
165     }
166 );
167
168 ok( ( any { 'OPACBaseURL' eq $_ } keys %{$template2->{VARS}} ),
169     'OPACBaseURL is in Staff template' );
170
171 my $hash1 = hash_password('password');
172 my $hash2 = hash_password('password');
173
174 ok(C4::Auth::checkpw_hash('password', $hash1), 'password validates with first hash');
175 ok(C4::Auth::checkpw_hash('password', $hash2), 'password validates with second hash');
176
177 $dbh->rollback;