|
Lines 18-23
Link Here
|
| 18 |
use Modern::Perl; |
18 |
use Modern::Perl; |
| 19 |
|
19 |
|
| 20 |
use Test::More tests => 2; |
20 |
use Test::More tests => 2; |
|
|
21 |
use Test::Exception; |
| 21 |
use Test::MockModule; |
22 |
use Test::MockModule; |
| 22 |
use t::lib::Mocks; |
23 |
use t::lib::Mocks; |
| 23 |
use t::lib::TestBuilder; |
24 |
use t::lib::TestBuilder; |
|
Lines 28-114
my $builder = t::lib::TestBuilder->new;
Link Here
|
| 28 |
|
29 |
|
| 29 |
$schema->storage->txn_begin; |
30 |
$schema->storage->txn_begin; |
| 30 |
|
31 |
|
| 31 |
my $category1 = $builder->build_object({class=>'Koha::Patron::Categories', value=>{min_password_length => 15, require_strong_password => 1}}); |
32 |
my $category1 = $builder->build_object( |
| 32 |
my $category2 = $builder->build_object({class=>'Koha::Patron::Categories', value=>{min_password_length => 5, require_strong_password => undef}}); |
33 |
{ |
| 33 |
my $category3 = $builder->build_object({class=>'Koha::Patron::Categories', value=>{min_password_length => undef, require_strong_password => 1}}); |
34 |
class => 'Koha::Patron::Categories', |
| 34 |
my $category4 = $builder->build_object({class=>'Koha::Patron::Categories', value=>{min_password_length => undef, require_strong_password => undef}}); |
35 |
value => { min_password_length => 15, require_strong_password => 1 } |
| 35 |
|
36 |
} |
| 36 |
my $p_3l_weak = '123'; |
37 |
); |
| 37 |
my $p_3l_strong = '1Ab'; |
38 |
my $category2 = $builder->build_object( |
| 38 |
my $p_5l_weak = 'abcde'; |
39 |
{ |
| 39 |
my $p_15l_weak = '0123456789abcdf'; |
40 |
class => 'Koha::Patron::Categories', |
| 40 |
my $p_5l_strong = 'Abc12'; |
41 |
value => { min_password_length => 5, require_strong_password => undef } |
|
|
42 |
} |
| 43 |
); |
| 44 |
my $category3 = $builder->build_object( |
| 45 |
{ |
| 46 |
class => 'Koha::Patron::Categories', |
| 47 |
value => { min_password_length => undef, require_strong_password => 1 } |
| 48 |
} |
| 49 |
); |
| 50 |
my $category4 = $builder->build_object( |
| 51 |
{ |
| 52 |
class => 'Koha::Patron::Categories', |
| 53 |
value => |
| 54 |
{ min_password_length => undef, require_strong_password => undef } |
| 55 |
} |
| 56 |
); |
| 57 |
|
| 58 |
my $p_2l = '1A'; |
| 59 |
my $p_3l_weak = '123'; |
| 60 |
my $p_3l_strong = '1Ab'; |
| 61 |
my $p_5l_weak = 'abcde'; |
| 62 |
my $p_15l_weak = '0123456789abcdf'; |
| 63 |
my $p_5l_strong = 'Abc12'; |
| 41 |
my $p_15l_strong = '0123456789AbCdF'; |
64 |
my $p_15l_strong = '0123456789AbCdF'; |
| 42 |
|
65 |
|
| 43 |
subtest 'is_password_valid for category' => sub { |
66 |
subtest 'is_password_valid for category' => sub { |
| 44 |
plan tests => 12; |
67 |
plan tests => 15; |
| 45 |
|
68 |
|
| 46 |
my ( $is_valid, $error ); |
69 |
my ( $is_valid, $error ); |
| 47 |
|
70 |
|
| 48 |
t::lib::Mocks::mock_preference('RequireStrongPassword', 0); |
71 |
t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 ); |
| 49 |
t::lib::Mocks::mock_preference('minPasswordLength', 3); |
72 |
t::lib::Mocks::mock_preference( 'minPasswordLength', 3 ); |
| 50 |
|
73 |
|
| 51 |
#Category 1 - override=>1, length=>15, strong=>1 |
74 |
#Category 1 - override=>1, length=>15, strong=>1 |
| 52 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( $p_5l_strong, $category1 ); |
75 |
( $is_valid, $error ) = |
| 53 |
is($is_valid, 0, 'min password length for this category is 15'); |
76 |
Koha::AuthUtils::is_password_valid( $p_5l_strong, $category1 ); |
| 54 |
is($error, 'too_short', 'min password length for this category is 15'); |
77 |
is( $is_valid, 0, 'min password length for this category is 15' ); |
|
|
78 |
is( $error, 'too_short', 'min password length for this category is 15' ); |
| 55 |
|
79 |
|
| 56 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( $p_15l_weak, $category1 ); |
80 |
( $is_valid, $error ) = |
| 57 |
is($is_valid, 0, 'password should be strong for this category'); |
81 |
Koha::AuthUtils::is_password_valid( $p_15l_weak, $category1 ); |
| 58 |
is($error, 'too_weak', 'password should be strong for this category'); |
82 |
is( $is_valid, 0, 'password should be strong for this category' ); |
|
|
83 |
is( $error, 'too_weak', 'password should be strong for this category' ); |
| 59 |
|
84 |
|
| 60 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( $p_15l_strong, $category1 ); |
85 |
( $is_valid, $error ) = |
| 61 |
is($is_valid, 1, 'password should be ok for this category'); |
86 |
Koha::AuthUtils::is_password_valid( $p_15l_strong, $category1 ); |
|
|
87 |
is( $is_valid, 1, 'password should be ok for this category' ); |
| 62 |
|
88 |
|
| 63 |
#Category 2 - override=>1, length=>5, strong=>0 |
89 |
#Category 2 - override=>1, length=>5, strong=>0 |
| 64 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( $p_3l_strong, $category2 ); |
90 |
( $is_valid, $error ) = |
| 65 |
is($is_valid, 0, 'min password length for this category is 5'); |
91 |
Koha::AuthUtils::is_password_valid( $p_3l_strong, $category2 ); |
| 66 |
is($error, 'too_short', 'min password length for this category is 5'); |
92 |
is( $is_valid, 0, 'min password length for this category is 5' ); |
|
|
93 |
is( $error, 'too_short', 'min password length for this category is 5' ); |
| 67 |
|
94 |
|
| 68 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( $p_5l_weak, $category2 ); |
95 |
( $is_valid, $error ) = |
| 69 |
is($is_valid, 1, 'password should be ok for this category'); |
96 |
Koha::AuthUtils::is_password_valid( $p_5l_weak, $category2 ); |
|
|
97 |
is( $is_valid, 1, 'password should be ok for this category' ); |
| 70 |
|
98 |
|
| 71 |
#Category 3 - override=>0, length=>20, strong=>0 |
99 |
#Category 3 - override=>0, length=>20, strong=>0 |
| 72 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( $p_3l_weak, $category3 ); |
100 |
( $is_valid, $error ) = |
| 73 |
is($is_valid, 0, 'password should be strong'); |
101 |
Koha::AuthUtils::is_password_valid( $p_3l_weak, $category3 ); |
| 74 |
is($error, 'too_weak', 'password should be strong'); |
102 |
is( $is_valid, 0, 'password should be strong' ); |
|
|
103 |
is( $error, 'too_weak', 'password should be strong' ); |
| 75 |
|
104 |
|
| 76 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( $p_3l_strong, $category3 ); |
105 |
( $is_valid, $error ) = |
| 77 |
is($is_valid, 1, 'password should be ok'); |
106 |
Koha::AuthUtils::is_password_valid( $p_3l_strong, $category3 ); |
|
|
107 |
is( $is_valid, 1, 'password should be ok' ); |
| 78 |
|
108 |
|
| 79 |
#Category 4 - default settings - override=>undef, length=>undef, strong=>undef |
109 |
#Category 4 - default settings - override=>undef, length=>undef, strong=>undef |
| 80 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( $p_3l_weak, $category4 ); |
110 |
( $is_valid, $error ) = |
| 81 |
is($is_valid, 1, 'password should be ok'); |
111 |
Koha::AuthUtils::is_password_valid( $p_3l_weak, $category4 ); |
|
|
112 |
is( $is_valid, 1, 'password should be ok' ); |
| 113 |
|
| 114 |
t::lib::Mocks::mock_preference( 'minPasswordLength', 0 ); |
| 115 |
( $is_valid, $error ) = |
| 116 |
Koha::AuthUtils::is_password_valid( $p_2l, $category4 ); |
| 117 |
is( $is_valid, 0, '3 is absolute minimum password' ); |
| 118 |
is( $error, 'too_short', '3 is absolute minimum password' ); |
| 119 |
|
| 120 |
throws_ok { Koha::AuthUtils::is_password_valid($p_2l); } |
| 121 |
'Koha::Exceptions::Password::NoCategoryProvided', |
| 122 |
'Category should always be provided'; |
| 123 |
|
| 82 |
}; |
124 |
}; |
| 83 |
|
125 |
|
| 84 |
subtest 'generate_password for category' => sub { |
126 |
subtest 'generate_password for category' => sub { |
| 85 |
plan tests => 4; |
127 |
plan tests => 5; |
| 86 |
|
128 |
|
| 87 |
my ( $is_valid, $error ); |
129 |
my ( $is_valid, $error ); |
| 88 |
|
130 |
|
| 89 |
t::lib::Mocks::mock_preference('RequireStrongPassword', 0); |
131 |
t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 ); |
| 90 |
t::lib::Mocks::mock_preference('minPasswordLength', 3); |
132 |
t::lib::Mocks::mock_preference( 'minPasswordLength', 3 ); |
| 91 |
|
133 |
|
| 92 |
#Category 4 |
134 |
#Category 4 |
| 93 |
my $password = Koha::AuthUtils::generate_password($category4); |
135 |
my $password = Koha::AuthUtils::generate_password($category4); |
| 94 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( $password, $category4 ); |
136 |
( $is_valid, $error ) = |
| 95 |
is($is_valid, 1, 'password should be ok'); |
137 |
Koha::AuthUtils::is_password_valid( $password, $category4 ); |
|
|
138 |
is( $is_valid, 1, 'password should be ok' ); |
| 96 |
|
139 |
|
| 97 |
#Category 3 |
140 |
#Category 3 |
| 98 |
$password = Koha::AuthUtils::generate_password($category3); |
141 |
$password = Koha::AuthUtils::generate_password($category3); |
| 99 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( $password, $category3 ); |
142 |
( $is_valid, $error ) = |
| 100 |
is($is_valid, 1, 'password should be ok'); |
143 |
Koha::AuthUtils::is_password_valid( $password, $category3 ); |
|
|
144 |
is( $is_valid, 1, 'password should be ok' ); |
| 101 |
|
145 |
|
| 102 |
#Category 2 |
146 |
#Category 2 |
| 103 |
$password = Koha::AuthUtils::generate_password($category2); |
147 |
$password = Koha::AuthUtils::generate_password($category2); |
| 104 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( $password, $category2 ); |
148 |
( $is_valid, $error ) = |
| 105 |
is($is_valid, 1, 'password should be ok'); |
149 |
Koha::AuthUtils::is_password_valid( $password, $category2 ); |
|
|
150 |
is( $is_valid, 1, 'password should be ok' ); |
| 106 |
|
151 |
|
| 107 |
#Category 1 |
152 |
#Category 1 |
| 108 |
$password = Koha::AuthUtils::generate_password($category1); |
153 |
$password = Koha::AuthUtils::generate_password($category1); |
| 109 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( $password, $category1 ); |
154 |
( $is_valid, $error ) = |
| 110 |
is($is_valid, 1, 'password should be ok'); |
155 |
Koha::AuthUtils::is_password_valid( $password, $category1 ); |
|
|
156 |
is( $is_valid, 1, 'password should be ok' ); |
| 157 |
|
| 158 |
throws_ok { Koha::AuthUtils::generate_password(); } |
| 159 |
'Koha::Exceptions::Password::NoCategoryProvided', |
| 160 |
'Category should always be provided'; |
| 111 |
|
161 |
|
| 112 |
}; |
162 |
}; |
| 113 |
|
163 |
|
| 114 |
$schema->storage->txn_rollback; |
164 |
$schema->storage->txn_rollback; |
| 115 |
- |
|
|