From 2c584f1f90638d8e48fd6816f9cd5f381bb61767 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Fri, 29 Aug 2025 09:57:49 -0300 Subject: [PATCH] Bug 36536: Unit tests This patch introduces tests for the feature enabling patron's `lastseen` attribute be updated when a successful password validation happens through the API. To test: 1. Apply this patch 2. Run: $ ktd --shell k$ prove t/db_dependent/api/v1/password_validation.t => FAIL: Tests fail! The feature is not implemented! Signed-off-by: Tomas Cohen Arazi --- t/db_dependent/api/v1/password_validation.t | 64 ++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/api/v1/password_validation.t b/t/db_dependent/api/v1/password_validation.t index 6e59d52a51c..3a3e6a05857 100755 --- a/t/db_dependent/api/v1/password_validation.t +++ b/t/db_dependent/api/v1/password_validation.t @@ -129,7 +129,7 @@ subtest 'password validation - unauthenticated user' => sub { subtest 'Password validation - authorized requests tests' => sub { - plan tests => 24; + plan tests => 25; $schema->storage->txn_begin; @@ -216,6 +216,68 @@ subtest 'Password validation - authorized requests tests' => sub { ->status_is( 400, 'Passing both parameters forbidden' ) ->json_is( { error => 'Bad request. Only one identifier attribute can be passed.' } ); + subtest 'TrackLastPatronActivityTriggers tests' => sub { + + plan tests => 12; + + my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { lastseen => undef } } ); + my $patron_password = 'thePassword123'; + $patron->set_password( { password => $patron_password, skip_validation => 1 } ); + + # to avoid interference + t::lib::Mocks::mock_preference( 'FailedLoginAttempts', 99 ); + + # All good but TrackLastPatronActivity disabled + t::lib::Mocks::mock_preference( 'TrackLastPatronActivityTriggers', 'api_verify' ); + t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', 0 ); + + $t->post_ok( "//$userid:$password@/api/v1/auth/password/validation" => json => + { identifier => $patron->userid, password => $patron_password } )->status_is( 201, 'Validation works' ); + + # read patron from the DB + $patron->discard_changes(); + is( $patron->lastseen, undef, "'lastseen' flag untouched if TrackLastPatronActivity is disabled" ); + + # TrackLastPatronActivity enabled but api_verify disabled + t::lib::Mocks::mock_preference( 'TrackLastPatronActivityTriggers', 'login' ); + t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', 1 ); + + $patron->lastseen(undef)->store(); + $t->post_ok( "//$userid:$password@/api/v1/auth/password/validation" => json => + { identifier => $patron->userid, password => $patron_password } )->status_is( 201, 'Validation works' ); + + # read patron from the DB + $patron->discard_changes(); + is( + $patron->lastseen, undef, + "'lastseen' flag untouched if TrackLastPatronActivity enabled but no 'api_verify' in TrackLastPatronActivityTriggers" + ); + + # good scenario + t::lib::Mocks::mock_preference( 'TrackLastPatronActivityTriggers', 'api_verify' ); + t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', 1 ); + + $patron->lastseen(undef)->store(); + $t->post_ok( "//$userid:$password@/api/v1/auth/password/validation" => json => + { identifier => $patron->userid, password => 'wrong password :-D' } ) + ->status_is( 400, 'Validation failed due to bad password' ); + + # read patron from the DB + $patron->discard_changes(); + is( $patron->lastseen, undef, "'lastseen' flag not updated as validation failed" ); + + $patron->lastseen(undef)->store(); + $t->post_ok( "//$userid:$password@/api/v1/auth/password/validation" => json => + { identifier => $patron->userid, password => $patron_password } )->status_is( 201, 'Validation works' ); + + # read patron from the DB + $patron->discard_changes(); + ok( + $patron->lastseen, + "'lastseen' flag updated if TrackLastPatronActivity and TrackLastPatronActivityTriggers includes 'api_verify'" + ); + }; + $schema->storage->txn_rollback; }; -- 2.51.0