Lines 18-29
Link Here
|
18 |
use Modern::Perl; |
18 |
use Modern::Perl; |
19 |
|
19 |
|
20 |
use Test::NoWarnings; |
20 |
use Test::NoWarnings; |
21 |
use Test::More tests => 5; |
21 |
use Test::More tests => 7; |
22 |
use Test::MockModule; |
22 |
use Test::MockModule; |
23 |
use Test::Exception; |
23 |
use Test::Exception; |
24 |
use Test::Warn; |
24 |
use Test::Warn; |
25 |
|
25 |
|
26 |
use t::lib::TestBuilder; |
26 |
use t::lib::TestBuilder; |
|
|
27 |
use t::lib::Mocks; |
27 |
|
28 |
|
28 |
BEGIN { |
29 |
BEGIN { |
29 |
use_ok('Koha::ApiKeys'); |
30 |
use_ok('Koha::ApiKeys'); |
Lines 121-127
subtest 'store() tests' => sub {
Link Here
|
121 |
|
122 |
|
122 |
subtest 'validate_secret() tests' => sub { |
123 |
subtest 'validate_secret() tests' => sub { |
123 |
|
124 |
|
124 |
plan tests => 2; |
125 |
plan tests => 12; |
125 |
|
126 |
|
126 |
$schema->storage->txn_begin; |
127 |
$schema->storage->txn_begin; |
127 |
|
128 |
|
Lines 138-143
subtest 'validate_secret() tests' => sub {
Link Here
|
138 |
is( $api_key->validate_secret($secret), 1, 'Valid secret returns true' ); |
139 |
is( $api_key->validate_secret($secret), 1, 'Valid secret returns true' ); |
139 |
is( $api_key->validate_secret('Wrong secret'), 0, 'Invalid secret returns false' ); |
140 |
is( $api_key->validate_secret('Wrong secret'), 0, 'Invalid secret returns false' ); |
140 |
|
141 |
|
|
|
142 |
# Test CREATE logging when ApiKeyLog is enabled |
143 |
t::lib::Mocks::mock_preference( 'ApiKeyLog', 1 ); |
144 |
my $test_patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
145 |
my $logs_before = $schema->resultset('ActionLog')->search( { module => 'APIKEYS', action => 'CREATE' } )->count; |
146 |
my $logged_key = Koha::ApiKey->new( { description => 'logged', patron_id => $test_patron->id } )->store; |
147 |
my $logs_after = $schema->resultset('ActionLog')->search( { module => 'APIKEYS', action => 'CREATE' } )->count; |
148 |
is( $logs_after, $logs_before + 1, 'CREATE action logged when ApiKeyLog enabled' ); |
149 |
|
150 |
# Verify log entry parameters |
151 |
my $log_entry = $schema->resultset('ActionLog')->search( |
152 |
{ module => 'APIKEYS', action => 'CREATE', object => $test_patron->id }, |
153 |
{ order_by => { -desc => 'action_id' } } |
154 |
)->first; |
155 |
ok( $log_entry, 'CREATE log entry found' ); |
156 |
is( $log_entry->object, $test_patron->id, 'Log object is patron_id' ); |
157 |
like( $log_entry->info, qr/Client ID: .+, Description: logged/, 'Log info contains client_id and description' ); |
158 |
|
159 |
# Test no CREATE logging when ApiKeyLog is disabled |
160 |
t::lib::Mocks::mock_preference( 'ApiKeyLog', 0 ); |
161 |
$logs_before = $schema->resultset('ActionLog')->search( { module => 'APIKEYS', action => 'CREATE' } )->count; |
162 |
my $unlogged_key = Koha::ApiKey->new( { description => 'unlogged', patron_id => $test_patron->id } )->store; |
163 |
$logs_after = $schema->resultset('ActionLog')->search( { module => 'APIKEYS', action => 'CREATE' } )->count; |
164 |
is( $logs_after, $logs_before, 'CREATE action not logged when ApiKeyLog disabled' ); |
165 |
|
166 |
# Test DELETE logging when ApiKeyLog is enabled |
167 |
t::lib::Mocks::mock_preference( 'ApiKeyLog', 1 ); |
168 |
$logs_before = $schema->resultset('ActionLog')->search( { module => 'APIKEYS', action => 'DELETE' } )->count; |
169 |
my $client_id_to_delete = $logged_key->client_id; |
170 |
$logged_key->delete; |
171 |
$logs_after = $schema->resultset('ActionLog')->search( { module => 'APIKEYS', action => 'DELETE' } )->count; |
172 |
is( $logs_after, $logs_before + 1, 'DELETE action logged when ApiKeyLog enabled' ); |
173 |
|
174 |
# Verify DELETE log entry parameters |
175 |
my $delete_log = $schema->resultset('ActionLog')->search( |
176 |
{ module => 'APIKEYS', action => 'DELETE', object => $test_patron->id }, |
177 |
{ order_by => { -desc => 'action_id' } } |
178 |
)->first; |
179 |
ok( $delete_log, 'DELETE log entry found' ); |
180 |
is( $delete_log->object, $test_patron->id, 'DELETE log object is patron_id' ); |
181 |
like( |
182 |
$delete_log->info, qr/Client ID: $client_id_to_delete, Description: logged/, |
183 |
'DELETE log info contains client_id and description' |
184 |
); |
185 |
|
186 |
# Test no DELETE logging when ApiKeyLog is disabled |
187 |
t::lib::Mocks::mock_preference( 'ApiKeyLog', 0 ); |
188 |
$logs_before = $schema->resultset('ActionLog')->search( { module => 'APIKEYS', action => 'DELETE' } )->count; |
189 |
$unlogged_key->delete; |
190 |
$logs_after = $schema->resultset('ActionLog')->search( { module => 'APIKEYS', action => 'DELETE' } )->count; |
191 |
is( $logs_after, $logs_before, 'DELETE action not logged when ApiKeyLog disabled' ); |
192 |
|
141 |
$schema->storage->txn_rollback; |
193 |
$schema->storage->txn_rollback; |
142 |
}; |
194 |
}; |
143 |
|
195 |
|
Lines 158-160
subtest 'plain_text_secret() tests' => sub {
Link Here
|
158 |
|
210 |
|
159 |
$schema->storage->txn_rollback; |
211 |
$schema->storage->txn_rollback; |
160 |
}; |
212 |
}; |
161 |
- |
213 |
|
|
|
214 |
subtest 'revoke() tests' => sub { |
215 |
|
216 |
plan tests => 8; |
217 |
|
218 |
$schema->storage->txn_begin; |
219 |
|
220 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
221 |
my $api_key = Koha::ApiKey->new( { description => 'test', patron_id => $patron->id } )->store; |
222 |
|
223 |
# Test successful revoke |
224 |
is( $api_key->active, 1, 'API key starts as active' ); |
225 |
$api_key->revoke; |
226 |
is( $api_key->active, 0, 'API key is revoked' ); |
227 |
|
228 |
# Test exception when already revoked |
229 |
throws_ok { $api_key->revoke } 'Koha::Exceptions::ApiKey::AlreadyRevoked', |
230 |
'Exception thrown when trying to revoke already revoked key'; |
231 |
|
232 |
# Test logging when ApiKeyLog is enabled |
233 |
t::lib::Mocks::mock_preference( 'ApiKeyLog', 1 ); |
234 |
my $api_key2 = Koha::ApiKey->new( { description => 'test2', patron_id => $patron->id } )->store; |
235 |
|
236 |
my $logs_before = $schema->resultset('ActionLog')->search( { module => 'APIKEYS', action => 'REVOKE' } )->count; |
237 |
my $client_id_to_revoke = $api_key2->client_id; |
238 |
$api_key2->revoke; |
239 |
my $logs_after = $schema->resultset('ActionLog')->search( { module => 'APIKEYS', action => 'REVOKE' } )->count; |
240 |
is( $logs_after, $logs_before + 1, 'REVOKE action logged when ApiKeyLog enabled' ); |
241 |
|
242 |
# Verify REVOKE log entry parameters |
243 |
my $revoke_log = $schema->resultset('ActionLog')->search( |
244 |
{ module => 'APIKEYS', action => 'REVOKE', object => $patron->id }, |
245 |
{ order_by => { -desc => 'action_id' } } |
246 |
)->first; |
247 |
ok( $revoke_log, 'REVOKE log entry found' ); |
248 |
is( $revoke_log->object, $patron->id, 'REVOKE log object is patron_id' ); |
249 |
like( |
250 |
$revoke_log->info, qr/Client ID: $client_id_to_revoke, Description: test2/, |
251 |
'REVOKE log info contains client_id and description' |
252 |
); |
253 |
|
254 |
# Test no logging when ApiKeyLog is disabled |
255 |
t::lib::Mocks::mock_preference( 'ApiKeyLog', 0 ); |
256 |
my $api_key3 = Koha::ApiKey->new( { description => 'test3', patron_id => $patron->id } )->store; |
257 |
|
258 |
$logs_before = $schema->resultset('ActionLog')->search( { module => 'APIKEYS', action => 'REVOKE' } )->count; |
259 |
$api_key3->revoke; |
260 |
$logs_after = $schema->resultset('ActionLog')->search( { module => 'APIKEYS', action => 'REVOKE' } )->count; |
261 |
is( $logs_after, $logs_before, 'REVOKE action not logged when ApiKeyLog disabled' ); |
262 |
|
263 |
$schema->storage->txn_rollback; |
264 |
}; |
265 |
|
266 |
subtest 'activate() tests' => sub { |
267 |
|
268 |
plan tests => 8; |
269 |
|
270 |
$schema->storage->txn_begin; |
271 |
|
272 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
273 |
my $api_key = Koha::ApiKey->new( { description => 'test', patron_id => $patron->id } )->store; |
274 |
$api_key->active(0)->store; # Start as revoked |
275 |
|
276 |
# Test successful activate |
277 |
is( $api_key->active, 0, 'API key starts as revoked' ); |
278 |
$api_key->activate; |
279 |
is( $api_key->active, 1, 'API key is activated' ); |
280 |
|
281 |
# Test exception when already active |
282 |
throws_ok { $api_key->activate } 'Koha::Exceptions::ApiKey::AlreadyActive', |
283 |
'Exception thrown when trying to activate already active key'; |
284 |
|
285 |
# Test logging when ApiKeyLog is enabled |
286 |
t::lib::Mocks::mock_preference( 'ApiKeyLog', 1 ); |
287 |
my $api_key2 = Koha::ApiKey->new( { description => 'test2', patron_id => $patron->id } )->store; |
288 |
$api_key2->active(0)->store( { skip_log => 1 } ); # Start as revoked |
289 |
|
290 |
my $logs_before = $schema->resultset('ActionLog')->search( { module => 'APIKEYS', action => 'ACTIVATE' } )->count; |
291 |
my $client_id_to_activate = $api_key2->client_id; |
292 |
$api_key2->activate; |
293 |
my $logs_after = $schema->resultset('ActionLog')->search( { module => 'APIKEYS', action => 'ACTIVATE' } )->count; |
294 |
is( $logs_after, $logs_before + 1, 'ACTIVATE action logged when ApiKeyLog enabled' ); |
295 |
|
296 |
# Verify ACTIVATE log entry parameters |
297 |
my $activate_log = $schema->resultset('ActionLog')->search( |
298 |
{ module => 'APIKEYS', action => 'ACTIVATE', object => $patron->id }, |
299 |
{ order_by => { -desc => 'action_id' } } |
300 |
)->first; |
301 |
ok( $activate_log, 'ACTIVATE log entry found' ); |
302 |
is( $activate_log->object, $patron->id, 'ACTIVATE log object is patron_id' ); |
303 |
like( |
304 |
$activate_log->info, qr/Client ID: $client_id_to_activate, Description: test2/, |
305 |
'ACTIVATE log info contains client_id and description' |
306 |
); |
307 |
|
308 |
# Test no logging when ApiKeyLog is disabled |
309 |
t::lib::Mocks::mock_preference( 'ApiKeyLog', 0 ); |
310 |
my $api_key3 = Koha::ApiKey->new( { description => 'test3', patron_id => $patron->id } )->store; |
311 |
$api_key3->active(0)->store( { skip_log => 1 } ); # Start as revoked |
312 |
|
313 |
$logs_before = $schema->resultset('ActionLog')->search( { module => 'APIKEYS', action => 'ACTIVATE' } )->count; |
314 |
$api_key3->activate; |
315 |
$logs_after = $schema->resultset('ActionLog')->search( { module => 'APIKEYS', action => 'ACTIVATE' } )->count; |
316 |
is( $logs_after, $logs_before, 'ACTIVATE action not logged when ApiKeyLog disabled' ); |
317 |
|
318 |
$schema->storage->txn_rollback; |
319 |
}; |