Bug 18442: Add a test
[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
10 use Test::MockObject;
11 use Test::MockModule;
12 use List::MoreUtils qw/all any none/;
13 use Test::More tests => 21;
14 use Test::Warn;
15 use t::lib::Mocks;
16 use t::lib::TestBuilder;
17
18 use C4::Auth qw(checkpw);
19 use C4::Members;
20 use Koha::AuthUtils qw/hash_password/;
21 use Koha::Database;
22 use Koha::Patrons;
23
24 BEGIN {
25     use_ok('C4::Auth');
26 }
27
28 my $schema  = Koha::Database->schema;
29 my $builder = t::lib::TestBuilder->new;
30 my $dbh     = C4::Context->dbh;
31
32 $schema->storage->txn_begin;
33
34 subtest 'checkauth() tests' => sub {
35
36     plan tests => 2;
37
38     my $patron = $builder->build({ source => 'Borrower', value => { flags => undef } })->{userid};
39
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; } );
44
45     my $authnotrequired = 1;
46     my ( $userid, $cookie, $sessionID, $flags ) = C4::Auth::checkauth( $cgi, $authnotrequired );
47
48     is( $userid, undef, 'checkauth() returns undef for userid if no logged in user (Bug 18275)' );
49
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; }
58             else { return; }
59         });
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
63
64 };
65
66 my $hash1 = hash_password('password');
67 my $hash2 = hash_password('password');
68
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(
73         {
74             source => 'Branch',
75         }
76     );
77
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' );
87 }
88
89 # get_template_and_user tests
90
91 {   # Tests for the language URL parameter
92
93     sub MockedCheckauth {
94         my ($query,$authnotrequired,$flagsrequired,$type) = @_;
95         # return vars
96         my $userid = 'cobain';
97         my $sessionID = 234;
98         # we don't need to bother about permissions for this test
99         my $flags = {
100             superlibrarian    => 1, acquisition       => 0,
101             borrowers         => 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,
109             updatecharges     => 0
110         };
111
112         my $session_cookie = $query->cookie(
113             -name => 'CGISESSID',
114             -value    => 'nirvana',
115             -HttpOnly => 1
116         );
117
118         return ( $userid, $session_cookie, $sessionID, $flags );
119     }
120
121     # Mock checkauth, build the scenario
122     my $auth = new Test::MockModule( 'C4::Auth' );
123     $auth->mock( 'checkauth', \&MockedCheckauth );
124
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');
130
131     # we need a session cookie
132     $ENV{"SERVER_PORT"} = 80;
133     $ENV{"HTTP_COOKIE"} = 'CGISESSID=nirvana';
134
135     my $query = new CGI;
136     $query->param('language','es-ES');
137
138     my ( $template, $loggedinuser, $cookies ) = get_template_and_user(
139         {
140             template_name   => "about.tt",
141             query           => $query,
142             type            => "opac",
143             authnotrequired => 1,
144             flagsrequired   => { catalogue => 1 },
145             debug           => 1
146         }
147     );
148
149     ok ( ( all { ref($_) eq 'CGI::Cookie' } @$cookies ),
150             'BZ9735: the cookies array is flat' );
151
152     # new query, with non-existent language (we only have en and es-ES)
153     $query->param('language','tomas');
154
155     ( $template, $loggedinuser, $cookies ) = get_template_and_user(
156         {
157             template_name   => "about.tt",
158             query           => $query,
159             type            => "opac",
160             authnotrequired => 1,
161             flagsrequired   => { catalogue => 1 },
162             debug           => 1
163         }
164     );
165
166     ok( ( none { $_->name eq 'KohaOpacLanguage' and $_->value eq 'tomas' } @$cookies ),
167         'BZ9735: invalid language, it is not set');
168
169     ok( ( any { $_->name eq 'KohaOpacLanguage' and $_->value eq 'en' } @$cookies ),
170         'BZ9735: invalid language, then default to en');
171
172     for my $template_name (
173         qw(
174             ../../../../../../../../../../../../../../../etc/passwd
175             test/../../../../../../../../../../../../../../etc/passwd
176             /etc/passwd
177             test/does_not_finished_by_tt_t
178         )
179     ) {
180         eval {
181             ( $template, $loggedinuser, $cookies ) = get_template_and_user(
182                 {
183                     template_name   => $template_name,
184                     query           => $query,
185                     type            => "intranet",
186                     authnotrequired => 1,
187                     flagsrequired   => { catalogue => 1 },
188                 }
189             );
190         };
191         like ( $@, qr(^bad template path), 'The file $template_name should not be accessible' );
192     }
193     ( $template, $loggedinuser, $cookies ) = get_template_and_user(
194         {
195             template_name   => 'errors/errorpage.tt',
196             query           => $query,
197             type            => "intranet",
198             authnotrequired => 1,
199             flagsrequired   => { catalogue => 1 },
200         }
201     );
202     my $file_exists = ( -f $template->{filename} ) ? 1 : 0;
203     is ( $file_exists, 1, 'The file errors/errorpage.tt should be accessible (contains integers)' );
204 }
205
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(
210     {
211         template_name => "opac-detail.tt",
212         type => "opac",
213         query => $input,
214         authnotrequired => 1,
215     }
216 );
217
218 ok( ( any { 'OPACBaseURL' eq $_ } keys %{$template1->{VARS}} ),
219     'OPACBaseURL is in OPAC template' );
220
221 my ( $template2 );
222 ( $template2, $borrowernumber, $cookie ) = get_template_and_user(
223     {
224         template_name => "catalogue/detail.tt",
225         type => "intranet",
226         query => $input,
227         authnotrequired => 1,
228     }
229 );
230
231 ok( ( any { 'OPACBaseURL' eq $_ } keys %{$template2->{VARS}} ),
232     'OPACBaseURL is in Staff template' );
233
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');