|
Lines 20-25
use Modern::Perl;
Link Here
|
| 20 |
use Test::More tests => 3; |
20 |
use Test::More tests => 3; |
| 21 |
|
21 |
|
| 22 |
use t::lib::Mocks; |
22 |
use t::lib::Mocks; |
|
|
23 |
use t::lib::TestBuilder; |
| 23 |
use Koha::AuthUtils qw/hash_password/; |
24 |
use Koha::AuthUtils qw/hash_password/; |
| 24 |
|
25 |
|
| 25 |
my $hash1 = hash_password('password'); |
26 |
my $hash1 = hash_password('password'); |
|
Lines 27-73
my $hash2 = hash_password('password');
Link Here
|
| 27 |
|
28 |
|
| 28 |
ok($hash1 ne $hash2, 'random salts used when generating password hash'); |
29 |
ok($hash1 ne $hash2, 'random salts used when generating password hash'); |
| 29 |
|
30 |
|
|
|
31 |
my $schema = Koha::Database->schema; |
| 32 |
my $dbh = C4::Context->dbh; |
| 33 |
$schema->storage->txn_begin; |
| 34 |
|
| 35 |
my $builder = t::lib::TestBuilder->new; |
| 36 |
|
| 37 |
# Password policy blank |
| 38 |
my $category = $builder->build({ |
| 39 |
source => 'Category', |
| 40 |
value => { |
| 41 |
categorycode => 'XYZ4', |
| 42 |
passwordpolicy => '' |
| 43 |
}, |
| 44 |
}); |
| 45 |
|
| 46 |
# Password policy simplenumeric |
| 47 |
my $category_simple = $builder->build({ |
| 48 |
source => 'Category', |
| 49 |
value => { |
| 50 |
categorycode => 'XYZ1', |
| 51 |
passwordpolicy => 'simplenumeric' |
| 52 |
}, |
| 53 |
}); |
| 54 |
|
| 55 |
# Password policy alphanumeric |
| 56 |
my $category_alpha = $builder->build({ |
| 57 |
source => 'Category', |
| 58 |
value => { |
| 59 |
categorycode => 'XYZ2', |
| 60 |
passwordpolicy => 'alphanumeric' |
| 61 |
}, |
| 62 |
}); |
| 63 |
|
| 64 |
# Password policy complex |
| 65 |
my $category_complex = $builder->build({ |
| 66 |
source => 'Category', |
| 67 |
value => { |
| 68 |
categorycode => 'XYZ3', |
| 69 |
passwordpolicy => 'complex' |
| 70 |
}, |
| 71 |
}); |
| 72 |
|
| 73 |
t::lib::Mocks::mock_preference('minAlnumPasswordLength', 5); |
| 74 |
t::lib::Mocks::mock_preference('minComplexPasswordLength', 6); |
| 75 |
|
| 30 |
subtest 'is_password_valid' => sub { |
76 |
subtest 'is_password_valid' => sub { |
| 31 |
plan tests => 12; |
77 |
plan tests => 13; |
| 32 |
|
78 |
|
| 33 |
my ( $is_valid, $error ); |
79 |
my ( $is_valid, $error ); |
| 34 |
|
80 |
|
| 35 |
t::lib::Mocks::mock_preference('RequireStrongPassword', 0); |
81 |
t::lib::Mocks::mock_preference('RequireStrongPassword', 0); |
| 36 |
t::lib::Mocks::mock_preference('minPasswordLength', 0); |
82 |
t::lib::Mocks::mock_preference('minPasswordLength', 0); |
| 37 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( '12' ); |
83 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( '12', $category->{categorycode} ); |
| 38 |
is( $is_valid, 0, 'min password size should be 3' ); |
84 |
is( $is_valid, 0, 'min password size should be 3' ); |
| 39 |
is( $error, 'too_short', 'min password size should be 3' ); |
85 |
is( $error, 'too_short', 'min password size should be 3' ); |
| 40 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( ' 123' ); |
86 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( ' 123', $category->{categorycode} ); |
| 41 |
is( $is_valid, 0, 'password should not contain leading spaces' ); |
87 |
is( $is_valid, 0, 'password should not contain leading spaces' ); |
| 42 |
is( $error, 'has_whitespaces', 'password should not contain leading spaces' ); |
88 |
is( $error, 'has_whitespaces', 'password should not contain leading spaces' ); |
| 43 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( '123 ' ); |
89 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( '123 ', $category->{categorycode} ); |
| 44 |
is( $is_valid, 0, 'password should not contain trailing spaces' ); |
90 |
is( $is_valid, 0, 'password should not contain trailing spaces' ); |
| 45 |
is( $error, 'has_whitespaces', 'password should not contain trailing spaces' ); |
91 |
is( $error, 'has_whitespaces', 'password should not contain trailing spaces' ); |
| 46 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( '123' ); |
92 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( '123', $category->{categorycode} ); |
| 47 |
is( $is_valid, 1, 'min password size should be 3' ); |
93 |
is( $is_valid, 1, 'min password size should be 3' ); |
| 48 |
|
94 |
|
| 49 |
t::lib::Mocks::mock_preference('RequireStrongPassword', 1); |
95 |
t::lib::Mocks::mock_preference('RequireStrongPassword', 1); |
| 50 |
t::lib::Mocks::mock_preference('minPasswordLength', 8); |
96 |
t::lib::Mocks::mock_preference('minPasswordLength', 8); |
| 51 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( '12345678' ); |
97 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( '12345678', $category->{categorycode} ); |
| 52 |
is( $is_valid, 0, 'password should be strong' ); |
98 |
is( $is_valid, 0, 'password should be strong' ); |
| 53 |
is( $error, 'too_weak', 'password should be strong' ); |
99 |
is( $error, 'too_weak', 'password should be strong' ); |
| 54 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( 'abcd1234' ); |
100 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( 'abcd1234', $category->{categorycode} ); |
| 55 |
is( $is_valid, 0, 'strong password should contain uppercase' ); |
101 |
is( $is_valid, 0, 'strong password should contain uppercase' ); |
| 56 |
is( $error, 'too_weak', 'strong password should contain uppercase' ); |
102 |
is( $error, 'too_weak', 'strong password should contain uppercase' ); |
| 57 |
|
103 |
|
| 58 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( 'abcD1234' ); |
104 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( 'abcD1234', $category->{categorycode} ); |
| 59 |
is( $is_valid, 1, 'strong password should contain uppercase' ); |
105 |
is( $is_valid, 1, 'strong password should contain uppercase' ); |
|
|
106 |
|
| 107 |
subtest 'password policies' => sub { |
| 108 |
|
| 109 |
t::lib::Mocks::mock_preference('RequireStrongPassword', 0); |
| 110 |
t::lib::Mocks::mock_preference('minPasswordLength', 4); |
| 111 |
|
| 112 |
#test simplenumeric password policy |
| 113 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( '1234', $category_simple->{categorycode} ); |
| 114 |
is ( $is_valid, 1, 'simplenumeric password should contain only numbers' ); |
| 115 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( 'A123', $category_simple->{categorycode} ); |
| 116 |
is ( $is_valid, 0, 'simplenumeric password should not contain alphabets' ); |
| 117 |
is($error, 'simple_policy_mismatch', 'error "simple_policy_mismatch" raised'); |
| 118 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( '!234', $category_simple->{categorycode} ); |
| 119 |
is ( $is_valid, 0, 'simplenumeric password should not contain non-special characters' ); |
| 120 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( '123', $category_simple->{categorycode} ); |
| 121 |
is ( $is_valid, 0, 'simplenumeric password follows minPasswordLength syspref' ); |
| 122 |
|
| 123 |
#test alphanumeric |
| 124 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( 'A1234', $category_alpha->{categorycode} ); |
| 125 |
is ( $is_valid, 1, 'alphanumeric password should contain both numbers and non-special characters' ); |
| 126 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( '12345', $category_alpha->{categorycode} ); |
| 127 |
is ( $is_valid, 0, 'alphanumeric password must contain at least one uppercase character' ); |
| 128 |
is($error, 'alpha_policy_mismatch', 'error "alpha_policy_mismatch" raised'); |
| 129 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( 'A123', $category_alpha->{categorycode} ); |
| 130 |
is ( $is_valid, 0, 'alphanumeric password follows minAlnumPasswordLength syspref'); |
| 131 |
|
| 132 |
#test complex |
| 133 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( 'As!123', $category_complex->{categorycode} ); |
| 134 |
is ( $is_valid, 1, 'complex password should contain numbers, lower and uppercase characters and special characters' ); |
| 135 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( 'A12345', $category_complex->{categorycode}); |
| 136 |
is ( $is_valid, 0, 'complex password must contain numbers, lower and uppercase characters and special characters' ); |
| 137 |
is($error, 'complex_policy_mismatch', 'error "complex_policy_mismatch" raised'); |
| 138 |
( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( 'As!12', $category_complex->{categorycode}); |
| 139 |
is ( $is_valid, 0, 'complex password follows minComplexPasswordLength syspref' ); |
| 140 |
} |
| 60 |
}; |
141 |
}; |
| 61 |
|
142 |
|
| 62 |
subtest 'generate_password' => sub { |
143 |
subtest 'generate_password' => sub { |
| 63 |
plan tests => 1; |
144 |
plan tests => 2; |
| 64 |
t::lib::Mocks::mock_preference('RequireStrongPassword', 1); |
145 |
t::lib::Mocks::mock_preference('RequireStrongPassword', 1); |
| 65 |
t::lib::Mocks::mock_preference('minPasswordLength', 8); |
146 |
t::lib::Mocks::mock_preference('minPasswordLength', 8); |
| 66 |
my $all_valid = 1; |
147 |
my $all_valid = 1; |
| 67 |
for ( 1 .. 10 ) { |
148 |
for ( 1 .. 10 ) { |
| 68 |
my $password = Koha::AuthUtils::generate_password; |
149 |
my $password = Koha::AuthUtils::generate_password( $category->{categorycode} ); |
| 69 |
my ( $is_valid, undef ) = Koha::AuthUtils::is_password_valid( $password ); |
150 |
my ( $is_valid, undef ) = Koha::AuthUtils::is_password_valid( $password, $category->{categorycode} ); |
| 70 |
$all_valid = 0 unless $is_valid; |
151 |
$all_valid = 0 unless $is_valid; |
| 71 |
} |
152 |
} |
| 72 |
is ( $all_valid, 1, 'generate_password should generate valid passwords' ); |
153 |
is ( $all_valid, 1, 'generate_password should generate valid passwords' ); |
|
|
154 |
|
| 155 |
subtest 'generate_password with password policies' => sub { |
| 156 |
|
| 157 |
t::lib::Mocks::mock_preference('RequireStrongPassword', 0); |
| 158 |
t::lib::Mocks::mock_preference('minPasswordLength', 4); |
| 159 |
|
| 160 |
#simplenumeric |
| 161 |
for ( 1 .. 10 ) { |
| 162 |
my $password = Koha::AuthUtils::generate_password( $category_simple->{categorycode} );; |
| 163 |
my ( $is_valid, undef ) = Koha::AuthUtils::is_password_valid( $password, $category_simple->{categorycode} ); |
| 164 |
$all_valid = 0 unless $is_valid; |
| 165 |
} |
| 166 |
is ( $all_valid, 1, 'generate_password should generate valid passwords with simplenumeric policy' ); |
| 167 |
|
| 168 |
#alphanumeric |
| 169 |
for ( 1 .. 10 ) { |
| 170 |
my $password = Koha::AuthUtils::generate_password( $category_alpha->{categorycode} ); |
| 171 |
my ( $is_valid, undef ) = Koha::AuthUtils::is_password_valid( $password, $category_alpha->{categorycode} ); |
| 172 |
$all_valid = 0 unless $is_valid; |
| 173 |
} |
| 174 |
is ( $all_valid, 1, 'generate_password should generate valid passwords with alphanumeric policy' ); |
| 175 |
|
| 176 |
#complex |
| 177 |
for ( 1 .. 10 ) { |
| 178 |
my $password = Koha::AuthUtils::generate_password( $category_complex->{categorycode} ); |
| 179 |
my ( $is_valid, undef ) = Koha::AuthUtils::is_password_valid( $password, $category_complex->{categorycode} ); |
| 180 |
$all_valid = 0 unless $is_valid; |
| 181 |
} |
| 182 |
is ( $all_valid, 1, 'generate_password should generate valid passwords with complex policy' ); |
| 183 |
} |
| 184 |
|
| 73 |
}; |
185 |
}; |
|
|
186 |
|
| 187 |
$schema->storage->txn_rollback; |