Bug 16492 - Checkouts ( and possibly checkins and other actions ) will use the patron...
[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 => 18;
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
21 BEGIN {
22     use_ok('C4::Auth');
23 }
24
25 my $schema = Koha::Database->schema;
26 $schema->storage->txn_begin;
27 my $builder = t::lib::TestBuilder->new;
28 my $dbh     = C4::Context->dbh;
29
30 # get_template_and_user tests
31
32 {   # Tests for the language URL parameter
33
34     sub MockedCheckauth {
35         my ($query,$authnotrequired,$flagsrequired,$type) = @_;
36         # return vars
37         my $userid = 'cobain';
38         my $sessionID = 234;
39         # we don't need to bother about permissions for this test
40         my $flags = {
41             superlibrarian    => 1, acquisition       => 0,
42             borrowers         => 0,
43             catalogue         => 1, circulate         => 0,
44             coursereserves    => 0, editauthorities   => 0,
45             editcatalogue     => 0, management        => 0,
46             parameters        => 0, permissions       => 0,
47             plugins           => 0, reports           => 0,
48             reserveforothers  => 0, serials           => 0,
49             staffaccess       => 0, tools             => 0,
50             updatecharges     => 0
51         };
52
53         my $session_cookie = $query->cookie(
54             -name => 'CGISESSID',
55             -value    => 'nirvana',
56             -HttpOnly => 1
57         );
58
59         return ( $userid, $session_cookie, $sessionID, $flags );
60     }
61
62     # Mock checkauth, build the scenario
63     my $auth = new Test::MockModule( 'C4::Auth' );
64     $auth->mock( 'checkauth', \&MockedCheckauth );
65
66     # Make sure 'EnableOpacSearchHistory' is set
67     t::lib::Mocks::mock_preference('EnableOpacSearchHistory',1);
68     # Enable es-ES for the OPAC and staff interfaces
69     t::lib::Mocks::mock_preference('opaclanguages','en,es-ES');
70     t::lib::Mocks::mock_preference('language','en,es-ES');
71
72     # we need a session cookie
73     $ENV{"SERVER_PORT"} = 80;
74     $ENV{"HTTP_COOKIE"} = 'CGISESSID=nirvana';
75
76     my $query = new CGI;
77     $query->param('language','es-ES');
78
79     my ( $template, $loggedinuser, $cookies ) = get_template_and_user(
80         {
81             template_name   => "about.tt",
82             query           => $query,
83             type            => "opac",
84             authnotrequired => 1,
85             flagsrequired   => { catalogue => 1 },
86             debug           => 1
87         }
88     );
89
90     ok ( ( all { ref($_) eq 'CGI::Cookie' } @$cookies ),
91             'BZ9735: the cookies array is flat' );
92
93     # new query, with non-existent language (we only have en and es-ES)
94     $query->param('language','tomas');
95
96     ( $template, $loggedinuser, $cookies ) = get_template_and_user(
97         {
98             template_name   => "about.tt",
99             query           => $query,
100             type            => "opac",
101             authnotrequired => 1,
102             flagsrequired   => { catalogue => 1 },
103             debug           => 1
104         }
105     );
106
107     ok( ( none { $_->name eq 'KohaOpacLanguage' and $_->value eq 'tomas' } @$cookies ),
108         'BZ9735: invalid language, it is not set');
109
110     ok( ( any { $_->name eq 'KohaOpacLanguage' and $_->value eq 'en' } @$cookies ),
111         'BZ9735: invalid language, then default to en');
112
113     for my $template_name (
114         qw(
115             ../../../../../../../../../../../../../../../etc/passwd
116             test/../../../../../../../../../../../../../../etc/passwd
117             /etc/passwd
118             test/does_not_finished_by_tt_t
119         )
120     ) {
121         eval {
122             ( $template, $loggedinuser, $cookies ) = get_template_and_user(
123                 {
124                     template_name   => $template_name,
125                     query           => $query,
126                     type            => "intranet",
127                     authnotrequired => 1,
128                     flagsrequired   => { catalogue => 1 },
129                 }
130             );
131         };
132         like ( $@, qr(^bad template path), 'The file $template_name should not be accessible' );
133     }
134     ( $template, $loggedinuser, $cookies ) = get_template_and_user(
135         {
136             template_name   => 'errors/errorpage.tt',
137             query           => $query,
138             type            => "intranet",
139             authnotrequired => 1,
140             flagsrequired   => { catalogue => 1 },
141         }
142     );
143     my $file_exists = ( -f $template->{filename} ) ? 1 : 0;
144     is ( $file_exists, 1, 'The file errors/errorpage.tt should be accessible (contains integers)' );
145 }
146
147 # Check that there is always an OPACBaseURL set.
148 my $input = CGI->new();
149 my ( $template1, $borrowernumber, $cookie );
150 ( $template1, $borrowernumber, $cookie ) = get_template_and_user(
151     {
152         template_name => "opac-detail.tt",
153         type => "opac",
154         query => $input,
155         authnotrequired => 1,
156     }
157 );
158
159 ok( ( any { 'OPACBaseURL' eq $_ } keys %{$template1->{VARS}} ),
160     'OPACBaseURL is in OPAC template' );
161
162 my ( $template2 );
163 ( $template2, $borrowernumber, $cookie ) = get_template_and_user(
164     {
165         template_name => "catalogue/detail.tt",
166         type => "intranet",
167         query => $input,
168         authnotrequired => 1,
169     }
170 );
171
172 ok( ( any { 'OPACBaseURL' eq $_ } keys %{$template2->{VARS}} ),
173     'OPACBaseURL is in Staff template' );
174
175 my $hash1 = hash_password('password');
176 my $hash2 = hash_password('password');
177
178 ok(C4::Auth::checkpw_hash('password', $hash1), 'password validates with first hash');
179 ok(C4::Auth::checkpw_hash('password', $hash2), 'password validates with second hash');
180
181 my $patron = $builder->build( { source => 'Borrower' } );
182 changepassword( $patron->{userid}, $patron->{borrowernumber}, $hash1 );
183 my $library = $builder->build(
184     {
185         source => 'Branch',
186     }
187 );
188
189 C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, 'Library 1', 0, '', '');
190 is( C4::Context->userenv->{branch}, $library->{branchcode}, 'Userenv gives correct branch' );
191 ok( checkpw( $dbh, $patron->{userid}, 'password', undef, undef, 1 ), 'checkpw returns true' );
192 is( C4::Context->userenv->{branch}, $library->{branchcode}, 'Userenv branch is preserved if no_set_userenv is true' );
193 ok( checkpw( $dbh, $patron->{userid}, 'password', undef, undef, 0 ), 'checkpw still returns true' );
194 isnt( C4::Context->userenv->{branch}, $library->{branchcode}, 'Userenv branch is overwritten if no_set_userenv is false' );