Bug 9611: (follow-up) move new password hashing routines to separate module
[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;
9 use Test::MockModule;
10 use List::MoreUtils qw/all any none/;
11 use Test::More tests => 6;
12 use C4::Members;
13 use Koha::AuthUtils qw/hash_password/;
14
15 BEGIN {
16         use_ok('C4::Auth');
17 }
18
19 my $dbh = C4::Context->dbh;
20
21 # Start transaction
22 $dbh->{AutoCommit} = 0;
23 $dbh->{RaiseError} = 1;
24
25
26 # get_template_and_user tests
27
28 {   # Tests for the language URL parameter
29
30     sub MockedCheckauth {
31         my ($query,$authnotrequired,$flagsrequired,$type) = @_;
32         # return vars
33         my $userid = 'cobain';
34         my $sessionID = 234;
35         # we don't need to bother about permissions for this test
36         my $flags = {
37             superlibrarian    => 1, acquisition       => 0,
38             borrow            => 0, borrowers         => 0,
39             catalogue         => 1, circulate         => 0,
40             coursereserves    => 0, editauthorities   => 0,
41             editcatalogue     => 0, management        => 0,
42             parameters        => 0, permissions       => 0,
43             plugins           => 0, reports           => 0,
44             reserveforothers  => 0, serials           => 0,
45             staffaccess       => 0, tools             => 0,
46             updatecharges     => 0
47         };
48
49         my $session_cookie = $query->cookie(
50             -name => 'CGISESSID',
51             -value    => 'nirvana',
52             -HttpOnly => 1
53         );
54
55         return ( $userid, $session_cookie, $sessionID, $flags );
56     }
57
58     # Mock checkauth, build the scenario
59     my $auth = new Test::MockModule( 'C4::Auth' );
60     $auth->mock( 'checkauth', \&MockedCheckauth );
61
62     # Make sure 'EnableOpacSearchHistory' is set
63     C4::Context->set_preference('EnableOpacSearchHistory',1);
64     # Enable es-ES for the OPAC and staff interfaces
65     C4::Context->set_preference('opaclanguages','en,es-ES');
66     C4::Context->set_preference('language','en,es-ES');
67
68     # we need a session cookie and have some anonymous search history
69     $ENV{"SERVER_PORT"} = 80;
70     $ENV{"HTTP_COOKIE"} = 'CGISESSID=nirvana; KohaOpacRecentSearches=%255B%257B%2522time%2522%253A1378313124%252C%2522query_cgi%2522%253A%2522idx%253D%2526q%253Dhistory%2526branch_group_limit%253D%2522%252C%2522total%2522%253A3%252C%2522query_desc%2522%253A%2522kw%252Cwrdl%253A%2520history%252C%2520%2522%257D%252C%257B%2522time%2522%253A1378313137%252C%2522query_cgi%2522%253A%2522idx%253D%2526q%253D%2525D8%2525B9%2525D8%2525B1%2525D8%2525A8%2525D9%25258A%25252F%2525D8%2525B9%2525D8%2525B1%2525D8%2525A8%2525D9%252589%2526branch_group_limit%253D%2522%252C%2522total%2522%253A2%252C%2522query_desc%2522%253A%2522kw%252Cwrdl%253A%2520%25D8%25B9%25D8%25B1%25D8%25A8%25D9%258A%252F%25D8%25B9%25D8%25B1%25D8%25A8%25D9%2589%252C%2520%2522%257D%255D';
71
72     my $query = new CGI;
73     $query->param('language','es-ES');
74
75     my ( $template, $loggedinuser, $cookies ) = get_template_and_user(
76         {
77             template_name   => "about.tmpl",
78             query           => $query,
79             type            => "opac",
80             authnotrequired => 1,
81             flagsrequired   => { catalogue => 1 },
82             debug           => 1
83         }
84     );
85
86     ok ( ( all { ref($_) eq 'CGI::Cookie' } @$cookies ),
87             'BZ9735: the cookies array is flat' );
88
89     # new query, with non-existent language (we only have en and es-ES)
90     $query->param('language','tomas');
91
92     ( $template, $loggedinuser, $cookies ) = get_template_and_user(
93         {
94             template_name   => "about.tmpl",
95             query           => $query,
96             type            => "opac",
97             authnotrequired => 1,
98             flagsrequired   => { catalogue => 1 },
99             debug           => 1
100         }
101     );
102
103     ok( ( none { $_->name eq 'KohaOpacLanguage' and $_->value eq 'tomas' } @$cookies ),
104         'BZ9735: invalid language, it is not set');
105
106     ok( ( any { $_->name eq 'KohaOpacLanguage' and $_->value eq 'en' } @$cookies ),
107         'BZ9735: invalid language, then default to en');
108 }
109
110 my $hash1 = hash_password('password');
111 my $hash2 = hash_password('password');
112
113 ok(C4::Auth::checkpw_hash('password', $hash1), 'password validates with first hash');
114 ok(C4::Auth::checkpw_hash('password', $hash2), 'password validates with second hash');
115
116 $dbh->rollback;