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