3 # This file is part of Koha.
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.
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.
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>.
20 use Test::More tests => 2;
23 use t::lib::TestBuilder;
26 my $schema = Koha::Database->schema;
27 my $builder = t::lib::TestBuilder->new;
29 $schema->storage->txn_begin;
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}});
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';
43 subtest 'is_password_valid for category' => sub {
46 my ( $is_valid, $error );
48 t::lib::Mocks::mock_preference('RequireStrongPassword', 0);
49 t::lib::Mocks::mock_preference('minPasswordLength', 3);
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');
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');
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');
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');
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');
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');
76 ( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( $p_3l_strong, $category3 );
77 is($is_valid, 1, 'password should be ok');
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');
84 subtest 'generate_password for category' => sub {
87 my ( $is_valid, $error );
89 t::lib::Mocks::mock_preference('RequireStrongPassword', 0);
90 t::lib::Mocks::mock_preference('minPasswordLength', 3);
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');
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');
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');
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');
114 $schema->storage->txn_rollback;