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