Bug 15517: Change wording for tests
[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 => 13;
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             test/does_not_finished_by_tt_t
116         )
117     ) {
118         eval {
119             ( $template, $loggedinuser, $cookies ) = get_template_and_user(
120                 {
121                     template_name   => $template_name,
122                     query           => $query,
123                     type            => "intranet",
124                     authnotrequired => 1,
125                     flagsrequired   => { catalogue => 1 },
126                 }
127             );
128         };
129         like ( $@, qr(^bad template path), 'The file $template_name should not be accessible' );
130     }
131     ( $template, $loggedinuser, $cookies ) = get_template_and_user(
132         {
133             template_name   => 'errors/500.tt',
134             query           => $query,
135             type            => "intranet",
136             authnotrequired => 1,
137             flagsrequired   => { catalogue => 1 },
138         }
139     );
140     my $file_exists = ( -f $template->{filename} ) ? 1 : 0;
141     is ( $file_exists, 1, 'The file errors/500.tt should be accessible (contains integers)' );
142 }
143
144 # Check that there is always an OPACBaseURL set.
145 my $input = CGI->new();
146 my ( $template1, $borrowernumber, $cookie );
147 ( $template1, $borrowernumber, $cookie ) = get_template_and_user(
148     {
149         template_name => "opac-detail.tt",
150         type => "opac",
151         query => $input,
152         authnotrequired => 1,
153     }
154 );
155
156 ok( ( any { 'OPACBaseURL' eq $_ } keys %{$template1->{VARS}} ),
157     'OPACBaseURL is in OPAC template' );
158
159 my ( $template2 );
160 ( $template2, $borrowernumber, $cookie ) = get_template_and_user(
161     {
162         template_name => "catalogue/detail.tt",
163         type => "intranet",
164         query => $input,
165         authnotrequired => 1,
166     }
167 );
168
169 ok( ( any { 'OPACBaseURL' eq $_ } keys %{$template2->{VARS}} ),
170     'OPACBaseURL is in Staff template' );
171
172 my $hash1 = hash_password('password');
173 my $hash2 = hash_password('password');
174
175 ok(C4::Auth::checkpw_hash('password', $hash1), 'password validates with first hash');
176 ok(C4::Auth::checkpw_hash('password', $hash2), 'password validates with second hash');
177
178 $dbh->rollback;