Bug 16853: Move changepassword to Koha::Patron->update_password
[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 => 20;
12 use Test::Warn;
13 use t::lib::Mocks;
14 use t::lib::TestBuilder;
15
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;
21
22 BEGIN {
23     use_ok('C4::Auth');
24 }
25
26 my $schema = Koha::Database->schema;
27 $schema->storage->txn_begin;
28 my $builder = t::lib::TestBuilder->new;
29 my $dbh     = C4::Context->dbh;
30
31 my $hash1 = hash_password('password');
32 my $hash2 = hash_password('password');
33
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(
38         {
39             source => 'Branch',
40         }
41     );
42
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' );
52 }
53
54 # get_template_and_user tests
55
56 {   # Tests for the language URL parameter
57
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
75         };
76
77         my $session_cookie = $query->cookie(
78             -name => 'CGISESSID',
79             -value    => 'nirvana',
80             -HttpOnly => 1
81         );
82
83         return ( $userid, $session_cookie, $sessionID, $flags );
84     }
85
86     # Mock checkauth, build the scenario
87     my $auth = new Test::MockModule( 'C4::Auth' );
88     $auth->mock( 'checkauth', \&MockedCheckauth );
89
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');
95
96     # we need a session cookie
97     $ENV{"SERVER_PORT"} = 80;
98     $ENV{"HTTP_COOKIE"} = 'CGISESSID=nirvana';
99
100     my $query = new CGI;
101     $query->param('language','es-ES');
102
103     my ( $template, $loggedinuser, $cookies ) = get_template_and_user(
104         {
105             template_name   => "about.tt",
106             query           => $query,
107             type            => "opac",
108             authnotrequired => 1,
109             flagsrequired   => { catalogue => 1 },
110             debug           => 1
111         }
112     );
113
114     ok ( ( all { ref($_) eq 'CGI::Cookie' } @$cookies ),
115             'BZ9735: the cookies array is flat' );
116
117     # new query, with non-existent language (we only have en and es-ES)
118     $query->param('language','tomas');
119
120     ( $template, $loggedinuser, $cookies ) = get_template_and_user(
121         {
122             template_name   => "about.tt",
123             query           => $query,
124             type            => "opac",
125             authnotrequired => 1,
126             flagsrequired   => { catalogue => 1 },
127             debug           => 1
128         }
129     );
130
131     ok( ( none { $_->name eq 'KohaOpacLanguage' and $_->value eq 'tomas' } @$cookies ),
132         'BZ9735: invalid language, it is not set');
133
134     ok( ( any { $_->name eq 'KohaOpacLanguage' and $_->value eq 'en' } @$cookies ),
135         'BZ9735: invalid language, then default to en');
136
137     for my $template_name (
138         qw(
139             ../../../../../../../../../../../../../../../etc/passwd
140             test/../../../../../../../../../../../../../../etc/passwd
141             /etc/passwd
142             test/does_not_finished_by_tt_t
143         )
144     ) {
145         eval {
146             ( $template, $loggedinuser, $cookies ) = get_template_and_user(
147                 {
148                     template_name   => $template_name,
149                     query           => $query,
150                     type            => "intranet",
151                     authnotrequired => 1,
152                     flagsrequired   => { catalogue => 1 },
153                 }
154             );
155         };
156         like ( $@, qr(^bad template path), 'The file $template_name should not be accessible' );
157     }
158     ( $template, $loggedinuser, $cookies ) = get_template_and_user(
159         {
160             template_name   => 'errors/errorpage.tt',
161             query           => $query,
162             type            => "intranet",
163             authnotrequired => 1,
164             flagsrequired   => { catalogue => 1 },
165         }
166     );
167     my $file_exists = ( -f $template->{filename} ) ? 1 : 0;
168     is ( $file_exists, 1, 'The file errors/errorpage.tt should be accessible (contains integers)' );
169 }
170
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(
175     {
176         template_name => "opac-detail.tt",
177         type => "opac",
178         query => $input,
179         authnotrequired => 1,
180     }
181 );
182
183 ok( ( any { 'OPACBaseURL' eq $_ } keys %{$template1->{VARS}} ),
184     'OPACBaseURL is in OPAC template' );
185
186 my ( $template2 );
187 ( $template2, $borrowernumber, $cookie ) = get_template_and_user(
188     {
189         template_name => "catalogue/detail.tt",
190         type => "intranet",
191         query => $input,
192         authnotrequired => 1,
193     }
194 );
195
196 ok( ( any { 'OPACBaseURL' eq $_ } keys %{$template2->{VARS}} ),
197     'OPACBaseURL is in Staff template' );
198
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');