1 # This file is part of Koha.
3 # Copyright (C) 2013 Equinox Software, Inc.
4 # Copyright 2017 Koha Development Team
6 # Koha is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # Koha is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Test::More tests => 3;
23 use Koha::AuthUtils qw/hash_password/;
25 my $hash1 = hash_password('password');
26 my $hash2 = hash_password('password');
28 ok($hash1 ne $hash2, 'random salts used when generating password hash');
30 subtest 'is_password_valid' => sub {
33 my ( $is_valid, $error );
35 t::lib::Mocks::mock_preference('RequireStrongPassword', 0);
36 t::lib::Mocks::mock_preference('minPasswordLength', 0);
37 ( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( '12' );
38 is( $is_valid, 0, 'min password size should be 3' );
39 is( $error, 'too_short', 'min password size should be 3' );
40 ( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( ' 123' );
41 is( $is_valid, 0, 'password should not contain leading spaces' );
42 is( $error, 'has_whitespaces', 'password should not contain leading spaces' );
43 ( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( '123 ' );
44 is( $is_valid, 0, 'password should not contain trailing spaces' );
45 is( $error, 'has_whitespaces', 'password should not contain trailing spaces' );
46 ( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( '123' );
47 is( $is_valid, 1, 'min password size should be 3' );
49 t::lib::Mocks::mock_preference('RequireStrongPassword', 1);
50 t::lib::Mocks::mock_preference('minPasswordLength', 8);
51 ( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( '12345678' );
52 is( $is_valid, 0, 'password should be strong' );
53 is( $error, 'too_weak', 'password should be strong' );
54 ( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( 'abcd1234' );
55 is( $is_valid, 0, 'strong password should contain uppercase' );
56 is( $error, 'too_weak', 'strong password should contain uppercase' );
58 ( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( 'abcD1234' );
59 is( $is_valid, 1, 'strong password should contain uppercase' );
62 subtest 'generate_password' => sub {
64 t::lib::Mocks::mock_preference('RequireStrongPassword', 1);
65 t::lib::Mocks::mock_preference('minPasswordLength', 8);
68 my $password = Koha::AuthUtils::generate_password;
69 my ( $is_valid, undef ) = Koha::AuthUtils::is_password_valid( $password );
70 $all_valid = 0 unless $is_valid;
72 is ( $all_valid, 1, 'generate_password should generate valid passwords' );