|
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 <https://www.gnu.org/licenses>. |
| 17 |
|
| 18 |
use Modern::Perl; |
| 19 |
|
| 20 |
use Test::More tests => 4; |
| 21 |
use Test::NoWarnings; |
| 22 |
|
| 23 |
use t::lib::TestBuilder; |
| 24 |
use t::lib::Mocks; |
| 25 |
|
| 26 |
use Koha::Database; |
| 27 |
use Koha::PatronPasswordHistory; |
| 28 |
use Koha::PatronPasswordHistories; |
| 29 |
use Koha::AuthUtils; |
| 30 |
|
| 31 |
my $schema = Koha::Database->new->schema; |
| 32 |
my $builder = t::lib::TestBuilder->new; |
| 33 |
|
| 34 |
subtest 'cleanup_old_password_history' => sub { |
| 35 |
plan tests => 6; |
| 36 |
|
| 37 |
$schema->storage->txn_begin; |
| 38 |
|
| 39 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 40 |
|
| 41 |
# Create 5 password history entries |
| 42 |
my @passwords = (); |
| 43 |
for my $i ( 1 .. 5 ) { |
| 44 |
my $password_history = Koha::PatronPasswordHistory->new( |
| 45 |
{ |
| 46 |
borrowernumber => $patron->borrowernumber, |
| 47 |
password => Koha::AuthUtils::hash_password("password_$i") |
| 48 |
} |
| 49 |
); |
| 50 |
$password_history->store; |
| 51 |
push @passwords, $password_history; |
| 52 |
sleep(1); # Ensure different timestamps |
| 53 |
} |
| 54 |
|
| 55 |
my $count = Koha::PatronPasswordHistories->search( { borrowernumber => $patron->borrowernumber } )->count; |
| 56 |
is( $count, 5, 'Initially have 5 password history entries' ); |
| 57 |
|
| 58 |
# Keep 3 most recent |
| 59 |
my $deleted = Koha::PatronPasswordHistories->cleanup_old_password_history( $patron->borrowernumber, 3 ); |
| 60 |
is( $deleted, 2, 'Deleted 2 old entries' ); |
| 61 |
|
| 62 |
$count = Koha::PatronPasswordHistories->search( { borrowernumber => $patron->borrowernumber } )->count; |
| 63 |
is( $count, 3, 'Now have 3 password history entries' ); |
| 64 |
|
| 65 |
# Keep 1 most recent |
| 66 |
$deleted = Koha::PatronPasswordHistories->cleanup_old_password_history( $patron->borrowernumber, 1 ); |
| 67 |
is( $deleted, 2, 'Deleted 2 more entries' ); |
| 68 |
|
| 69 |
$count = Koha::PatronPasswordHistories->search( { borrowernumber => $patron->borrowernumber } )->count; |
| 70 |
is( $count, 1, 'Now have 1 password history entry' ); |
| 71 |
|
| 72 |
# Delete all (count = 0) |
| 73 |
$deleted = Koha::PatronPasswordHistories->cleanup_old_password_history( $patron->borrowernumber, 0 ); |
| 74 |
is( $deleted, 1, 'Deleted remaining entry' ); |
| 75 |
|
| 76 |
$schema->storage->txn_rollback; |
| 77 |
}; |
| 78 |
|
| 79 |
subtest 'has_used_password with category settings' => sub { |
| 80 |
plan tests => 10; |
| 81 |
|
| 82 |
$schema->storage->txn_begin; |
| 83 |
|
| 84 |
my $category = $builder->build_object( |
| 85 |
{ |
| 86 |
class => 'Koha::Patron::Categories', |
| 87 |
value => { password_history_count => 3 } |
| 88 |
} |
| 89 |
); |
| 90 |
my $patron = $builder->build_object( |
| 91 |
{ |
| 92 |
class => 'Koha::Patrons', |
| 93 |
value => { |
| 94 |
categorycode => $category->categorycode, |
| 95 |
password => Koha::AuthUtils::hash_password('current_password') |
| 96 |
} |
| 97 |
} |
| 98 |
); |
| 99 |
|
| 100 |
# Create password history |
| 101 |
my $old_password1 = 'old_password_1'; |
| 102 |
my $old_password2 = 'old_password_2'; |
| 103 |
|
| 104 |
my $history1 = Koha::PatronPasswordHistory->new( |
| 105 |
{ |
| 106 |
borrowernumber => $patron->borrowernumber, |
| 107 |
password => Koha::AuthUtils::hash_password($old_password1) |
| 108 |
} |
| 109 |
); |
| 110 |
$history1->store; |
| 111 |
sleep(1); |
| 112 |
|
| 113 |
my $history2 = Koha::PatronPasswordHistory->new( |
| 114 |
{ |
| 115 |
borrowernumber => $patron->borrowernumber, |
| 116 |
password => Koha::AuthUtils::hash_password($old_password2) |
| 117 |
} |
| 118 |
); |
| 119 |
$history2->store; |
| 120 |
|
| 121 |
# Test checking against current password |
| 122 |
my $is_used = Koha::PatronPasswordHistories->has_used_password( |
| 123 |
{ |
| 124 |
borrowernumber => $patron->borrowernumber, |
| 125 |
password => 'current_password', |
| 126 |
current_password => $patron->password |
| 127 |
} |
| 128 |
); |
| 129 |
ok( $is_used, 'Current password detected as already used' ); |
| 130 |
|
| 131 |
# Test checking against history |
| 132 |
$is_used = Koha::PatronPasswordHistories->has_used_password( |
| 133 |
{ |
| 134 |
borrowernumber => $patron->borrowernumber, |
| 135 |
password => $old_password1, |
| 136 |
current_password => $patron->password |
| 137 |
} |
| 138 |
); |
| 139 |
ok( $is_used, 'Old password from history detected as already used' ); |
| 140 |
|
| 141 |
$is_used = Koha::PatronPasswordHistories->has_used_password( |
| 142 |
{ |
| 143 |
borrowernumber => $patron->borrowernumber, |
| 144 |
password => $old_password2, |
| 145 |
current_password => $patron->password |
| 146 |
} |
| 147 |
); |
| 148 |
ok( $is_used, 'Another old password from history detected as already used' ); |
| 149 |
|
| 150 |
# Test new password |
| 151 |
$is_used = Koha::PatronPasswordHistories->has_used_password( |
| 152 |
{ |
| 153 |
borrowernumber => $patron->borrowernumber, |
| 154 |
password => 'brand_new_password', |
| 155 |
current_password => $patron->password |
| 156 |
} |
| 157 |
); |
| 158 |
ok( !$is_used, 'New password not detected as used' ); |
| 159 |
|
| 160 |
# Test with category history_count = 1 (only checks current) |
| 161 |
$category->password_history_count(1)->store; |
| 162 |
|
| 163 |
$is_used = Koha::PatronPasswordHistories->has_used_password( |
| 164 |
{ |
| 165 |
borrowernumber => $patron->borrowernumber, |
| 166 |
password => $old_password1, |
| 167 |
current_password => $patron->password |
| 168 |
} |
| 169 |
); |
| 170 |
ok( !$is_used, 'With history_count=1, old passwords not checked against history' ); |
| 171 |
|
| 172 |
$is_used = Koha::PatronPasswordHistories->has_used_password( |
| 173 |
{ |
| 174 |
borrowernumber => $patron->borrowernumber, |
| 175 |
password => 'current_password', |
| 176 |
current_password => $patron->password |
| 177 |
} |
| 178 |
); |
| 179 |
ok( $is_used, 'With history_count=1, current password still checked' ); |
| 180 |
|
| 181 |
# Test with category history_count = 0 (no checking) |
| 182 |
$category->password_history_count(0)->store; |
| 183 |
|
| 184 |
$is_used = Koha::PatronPasswordHistories->has_used_password( |
| 185 |
{ |
| 186 |
borrowernumber => $patron->borrowernumber, |
| 187 |
password => 'current_password', |
| 188 |
current_password => $patron->password |
| 189 |
} |
| 190 |
); |
| 191 |
ok( !$is_used, 'With history_count=0, no password checking performed' ); |
| 192 |
|
| 193 |
# Test fallback to system preference |
| 194 |
$category->password_history_count(undef)->store; |
| 195 |
t::lib::Mocks::mock_preference( 'PasswordHistoryCount', 2 ); |
| 196 |
|
| 197 |
$is_used = Koha::PatronPasswordHistories->has_used_password( |
| 198 |
{ |
| 199 |
borrowernumber => $patron->borrowernumber, |
| 200 |
password => $old_password2, |
| 201 |
current_password => $patron->password |
| 202 |
} |
| 203 |
); |
| 204 |
ok( $is_used, 'Falls back to system preference when category setting is null' ); |
| 205 |
|
| 206 |
# Test without current password parameter |
| 207 |
$is_used = Koha::PatronPasswordHistories->has_used_password( |
| 208 |
{ |
| 209 |
borrowernumber => $patron->borrowernumber, |
| 210 |
password => $old_password1 |
| 211 |
} |
| 212 |
); |
| 213 |
ok( $is_used, 'Can check password history without current_password parameter' ); |
| 214 |
|
| 215 |
# Test with patron that has no password history |
| 216 |
my $new_patron = $builder->build_object( |
| 217 |
{ |
| 218 |
class => 'Koha::Patrons', |
| 219 |
value => { |
| 220 |
categorycode => $category->categorycode, |
| 221 |
password => Koha::AuthUtils::hash_password('new_patron_password') |
| 222 |
} |
| 223 |
} |
| 224 |
); |
| 225 |
|
| 226 |
$is_used = Koha::PatronPasswordHistories->has_used_password( |
| 227 |
{ |
| 228 |
borrowernumber => $new_patron->borrowernumber, |
| 229 |
password => 'some_password', |
| 230 |
current_password => $new_patron->password |
| 231 |
} |
| 232 |
); |
| 233 |
ok( !$is_used, 'Returns false for patron with no password history' ); |
| 234 |
|
| 235 |
$schema->storage->txn_rollback; |
| 236 |
}; |
| 237 |
|
| 238 |
subtest 'edge cases and validation' => sub { |
| 239 |
plan tests => 4; |
| 240 |
|
| 241 |
$schema->storage->txn_begin; |
| 242 |
|
| 243 |
# Test with missing parameters |
| 244 |
my $is_used = Koha::PatronPasswordHistories->has_used_password( {} ); |
| 245 |
ok( !$is_used, 'Returns false with no parameters' ); |
| 246 |
|
| 247 |
$is_used = Koha::PatronPasswordHistories->has_used_password( { borrowernumber => 99999 } ); |
| 248 |
ok( !$is_used, 'Returns false with missing password' ); |
| 249 |
|
| 250 |
$is_used = Koha::PatronPasswordHistories->has_used_password( { password => 'test' } ); |
| 251 |
ok( !$is_used, 'Returns false with missing borrowernumber' ); |
| 252 |
|
| 253 |
# Test with non-existent patron |
| 254 |
$is_used = Koha::PatronPasswordHistories->has_used_password( |
| 255 |
{ |
| 256 |
borrowernumber => 99999, |
| 257 |
password => 'test_password' |
| 258 |
} |
| 259 |
); |
| 260 |
ok( !$is_used, 'Returns false for non-existent patron' ); |
| 261 |
|
| 262 |
$schema->storage->txn_rollback; |
| 263 |
}; |