From 58f12efbd4b0d8043a26398294af4549b3ca830c Mon Sep 17 00:00:00 2001 From: CJ Lynce Date: Fri, 29 Aug 2025 21:26:53 +0000 Subject: [PATCH] Bug 29900: Unit tests This patch adds unit tests for the feature allowing to update 'lastseen' for an API service user both through auth_basic and oauth2 To test: 1. Apply Patch 2. Run: #> prove t/db_dependent/api/v1/auth_basic.t Verify all tests PASS 3. Run: #> prove prove t/db_dependent/api/v1/authenticate_api_request.t Verify all tests PASS --- .../api/v1/auth_authenticate_api_request.t | 47 ++++++++++++++++++- t/db_dependent/api/v1/auth_basic.t | 44 ++++++++++++++++- 2 files changed, 89 insertions(+), 2 deletions(-) diff --git a/t/db_dependent/api/v1/auth_authenticate_api_request.t b/t/db_dependent/api/v1/auth_authenticate_api_request.t index c314561fb9..ca2900932e 100755 --- a/t/db_dependent/api/v1/auth_authenticate_api_request.t +++ b/t/db_dependent/api/v1/auth_authenticate_api_request.t @@ -45,7 +45,7 @@ t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' ); subtest 'token-based tests' => sub { if ( can_load( modules => { 'Net::OAuth2::AuthorizationServer' => undef } ) ) { - plan tests => 15; + plan tests => 16; } else { plan skip_all => 'Net::OAuth2::AuthorizationServer not available'; } @@ -110,6 +110,51 @@ subtest 'token-based tests' => sub { ok( defined $embed, 'The embed hashref is generated and stashed' ); is_deeply( $embed, { fund => {} }, 'The embed data structure is correct' ); + subtest 'TrackLastPatronActivityTriggers tests for api_oauth2' => sub { + + plan tests => 6; + + $patron->lastseen(undef)->store; + + my $tx = $t->ua->build_tx( GET => '/api/v1/patrons' ); + $tx->req->headers->authorization("Bearer $access_token"); + $t->request_ok($tx); + + is( + $patron->lastseen, undef, + "'lastseen' is undefined" + ); + + #set login in TrackLastPatronActivity_Triggers + t::lib::Mocks::mock_preference( 'TrackLastPatronActivityTriggers', 'login' ); + + $patron->lastseen(undef)->store; + $tx = $t->ua->build_tx( GET => '/api/v1/patrons' ); + $tx->req->headers->authorization("Bearer $access_token"); + $t->request_ok($tx); + + $patron->discard_changes(); + is( + $patron->lastseen, undef, + "'lastseen' untouched if 'api_oauth2' is not enabled in TrackLastPatronActivityTriggers" + ); + + #set api_oauth2 in in TrackLastPatronActivity_Triggers + t::lib::Mocks::mock_preference( 'TrackLastPatronActivityTriggers', 'api_oauth2' ); + + $patron->lastseen(undef)->store; + $tx = $t->ua->build_tx( GET => '/api/v1/patrons' ); + $tx->req->headers->authorization("Bearer $access_token"); + $t->request_ok($tx); + + $patron->discard_changes(); + ok( + $patron->lastseen, + "'lastseen' flag updated TrackLastPatronActivityTriggers includes 'api_oauth2'" + ); + + }; + $schema->storage->txn_rollback; }; diff --git a/t/db_dependent/api/v1/auth_basic.t b/t/db_dependent/api/v1/auth_basic.t index 4d23949b4f..c5da793573 100755 --- a/t/db_dependent/api/v1/auth_basic.t +++ b/t/db_dependent/api/v1/auth_basic.t @@ -33,7 +33,7 @@ my $t = Test::Mojo->new('Koha::REST::V1'); subtest 'success tests' => sub { - plan tests => 2; + plan tests => 3; $schema->storage->txn_begin; @@ -109,6 +109,48 @@ subtest 'success tests' => sub { ); }; + subtest 'TrackLastPatronActivityTriggers tests for api_basic_auth' => sub { + + plan tests => 7; + + $patron->flags( 2**4 )->store; + + #set login in TrackLastPatronActivity_Triggers + t::lib::Mocks::mock_preference( 'TrackLastPatronActivityTriggers', 'login' ); + + $patron->lastseen(undef)->store; + $t->get_ok("//$cardnumber:$password@/api/v1/patrons"); + + $patron->discard_changes(); + is( + $patron->lastseen, undef, + "'lastseen' untouched if 'api_basic_auth' is not enabled in TrackLastPatronActivityTriggers" + ); + + #set api_oauth2 in in TrackLastPatronActivity_Triggers + t::lib::Mocks::mock_preference( 'TrackLastPatronActivityTriggers', 'api_basic_auth' ); + + $patron->lastseen(undef)->store; + $t->get_ok("//$cardnumber:$password@/api/v1/patrons"); + $patron->discard_changes(); + + ok( + $patron->lastseen, + "'lastseen' flag updated TrackLastPatronActivityTriggers includes 'api_basic_auth'" + ); + + $patron->lastseen(undef)->store; + $t->get_ok("//$cardnumber:basspassword@/api/v1/patrons") + ->status_is( 403, 'Basic Auth fails due to bad password' ); + $patron->discard_changes(); + + is( + $patron->lastseen, undef, + "'lastseen' flag remains untouch due to Basic Auth authentication failure'" + ); + + }; + $schema->storage->txn_rollback; }; -- 2.39.5