|
Line 0
Link Here
|
|
|
1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
# This file is part of Koha. |
| 4 |
# |
| 5 |
# Koha is free software; you can redistribute it and/or modify it |
| 6 |
# under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation; either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# Koha is distributed in the hope that it will be useful, but |
| 11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 17 |
|
| 18 |
use Modern::Perl; |
| 19 |
|
| 20 |
use Test::More tests => 2; |
| 21 |
use Test::MockModule; |
| 22 |
use t::lib::Mocks; |
| 23 |
use t::lib::TestBuilder; |
| 24 |
use Koha::AuthUtils; |
| 25 |
|
| 26 |
my $schema = Koha::Database->schema; |
| 27 |
my $builder = t::lib::TestBuilder->new; |
| 28 |
|
| 29 |
$schema->storage->txn_begin; |
| 30 |
|
| 31 |
my $category1 = $builder->build_object({class=>'Koha::Patron::Categories', value=>{min_password_length => 15, require_strong_password => 1}}); |
| 32 |
my $category2 = $builder->build_object({class=>'Koha::Patron::Categories', value=>{min_password_length => 5, require_strong_password => undef}}); |
| 33 |
my $category3 = $builder->build_object({class=>'Koha::Patron::Categories', value=>{min_password_length => undef, require_strong_password => 1}}); |
| 34 |
my $category4 = $builder->build_object({class=>'Koha::Patron::Categories', value=>{min_password_length => undef, require_strong_password => undef}}); |
| 35 |
|
| 36 |
my $p_3l_weak = '123'; |
| 37 |
my $p_3l_strong = '1Ab'; |
| 38 |
my $p_5l_weak = 'abcde'; |
| 39 |
my $p_15l_weak = '0123456789abcdf'; |
| 40 |
my $p_5l_strong = 'Abc12'; |
| 41 |
my $p_15l_strong = '0123456789AbCdF'; |
| 42 |
|
| 43 |
subtest 'is_password_valid for category' => sub { |
| 44 |
plan tests => 12; |
| 45 |
|
| 46 |
my ( $is_valid, $error ); |
| 47 |
|
| 48 |
t::lib::Mocks::mock_preference('RequireStrongPassword', 0); |
| 49 |
t::lib::Mocks::mock_preference('minPasswordLength', 3); |
| 50 |
|
| 51 |
#Category 1 - override=>1, length=>15, strong=>1 |
| 52 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( $p_5l_strong, $category1 ); |
| 53 |
is($is_valid, 0, 'min password length for this category is 15'); |
| 54 |
is($error, 'too_short', 'min password length for this category is 15'); |
| 55 |
|
| 56 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( $p_15l_weak, $category1 ); |
| 57 |
is($is_valid, 0, 'password should be strong for this category'); |
| 58 |
is($error, 'too_weak', 'password should be strong for this category'); |
| 59 |
|
| 60 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( $p_15l_strong, $category1 ); |
| 61 |
is($is_valid, 1, 'password should be ok for this category'); |
| 62 |
|
| 63 |
#Category 2 - override=>1, length=>5, strong=>0 |
| 64 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( $p_3l_strong, $category2 ); |
| 65 |
is($is_valid, 0, 'min password length for this category is 5'); |
| 66 |
is($error, 'too_short', 'min password length for this category is 5'); |
| 67 |
|
| 68 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( $p_5l_weak, $category2 ); |
| 69 |
is($is_valid, 1, 'password should be ok for this category'); |
| 70 |
|
| 71 |
#Category 3 - override=>0, length=>20, strong=>0 |
| 72 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( $p_3l_weak, $category3 ); |
| 73 |
is($is_valid, 0, 'password should be strong'); |
| 74 |
is($error, 'too_weak', 'password should be strong'); |
| 75 |
|
| 76 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( $p_3l_strong, $category3 ); |
| 77 |
is($is_valid, 1, 'password should be ok'); |
| 78 |
|
| 79 |
#Category 4 - default settings - override=>undef, length=>undef, strong=>undef |
| 80 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( $p_3l_weak, $category4 ); |
| 81 |
is($is_valid, 1, 'password should be ok'); |
| 82 |
}; |
| 83 |
|
| 84 |
subtest 'generate_password for category' => sub { |
| 85 |
plan tests => 4; |
| 86 |
|
| 87 |
my ( $is_valid, $error ); |
| 88 |
|
| 89 |
t::lib::Mocks::mock_preference('RequireStrongPassword', 0); |
| 90 |
t::lib::Mocks::mock_preference('minPasswordLength', 3); |
| 91 |
|
| 92 |
#Category 4 |
| 93 |
my $password = Koha::AuthUtils::generate_password($category4); |
| 94 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( $password, $category4 ); |
| 95 |
is($is_valid, 1, 'password should be ok'); |
| 96 |
|
| 97 |
#Category 3 |
| 98 |
$password = Koha::AuthUtils::generate_password($category3); |
| 99 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( $password, $category3 ); |
| 100 |
is($is_valid, 1, 'password should be ok'); |
| 101 |
|
| 102 |
#Category 2 |
| 103 |
$password = Koha::AuthUtils::generate_password($category2); |
| 104 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( $password, $category2 ); |
| 105 |
is($is_valid, 1, 'password should be ok'); |
| 106 |
|
| 107 |
#Category 1 |
| 108 |
$password = Koha::AuthUtils::generate_password($category1); |
| 109 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( $password, $category1 ); |
| 110 |
is($is_valid, 1, 'password should be ok'); |
| 111 |
|
| 112 |
}; |
| 113 |
|
| 114 |
$schema->storage->txn_rollback; |