From 497127aef6324238067f48f7be5a35b2fc665cac Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Fri, 21 Jan 2022 17:11:39 +0000 Subject: [PATCH] Bug 29924: Unit tests Signed-off-by: Owen Leonard Signed-off-by: Bob Bennhoff Signed-off-by: Andrew Fuerste-Henry Signed-off-by: Tomas Cohen Arazi --- Koha/Patron.pm | 6 +- t/db_dependent/Koha/Patron.t | 97 ++++++++++++++++++++++++++- t/db_dependent/Koha/Patron/Category.t | 19 +++++- 3 files changed, 117 insertions(+), 5 deletions(-) diff --git a/Koha/Patron.pm b/Koha/Patron.pm index e2384ecf3f7..35809b180d5 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -268,7 +268,7 @@ sub store { $self->plain_text_password( $self->password ); $self->password_expiration_date( $self->password - ? $self->category->get_password_expiry_date + ? $self->category->get_password_expiry_date || undef : undef ); # Create a disabled account if no password provided $self->password( $self->password @@ -801,7 +801,7 @@ Returns 1 if the patron's password is expired or 0; sub password_expired { my ($self) = @_; return 0 unless $self->password_expiration_date; - return 1 if dt_from_string( $self->password_expiration_date ) < dt_from_string->truncate( to => 'day' ); + return 1 if dt_from_string( $self->password_expiration_date ) <= dt_from_string->truncate( to => 'day' ); return 0; } @@ -904,7 +904,7 @@ sub set_password { my $digest = Koha::AuthUtils::hash_password($password); - $self->password_expiration_date( $self->category->get_password_expiry_date ); + $self->password_expiration_date( $self->category->get_password_expiry_date || undef ); # We do not want to call $self->store and retrieve password from DB $self->password($digest); diff --git a/t/db_dependent/Koha/Patron.t b/t/db_dependent/Koha/Patron.t index 6418d6066f1..d339244d3ef 100755 --- a/t/db_dependent/Koha/Patron.t +++ b/t/db_dependent/Koha/Patron.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 16; +use Test::More tests => 17; use Test::Exception; use Test::Warn; @@ -882,6 +882,101 @@ subtest 'can_patron_change_staff_only_lists() tests' => sub { $schema->storage->txn_rollback; }; +subtest 'password expiration tests' => sub { + + plan tests => 5; + + $schema->storage->txn_begin; + my $date = dt_from_string(); + my $category = $builder->build_object({ class => 'Koha::Patron::Categories', value => { + password_expiry_days => 10, + require_strong_password => 0, + } + }); + my $patron = $builder->build_object({ class=> 'Koha::Patrons', value => { + categorycode => $category->categorycode, + password => 'hats' + } + }); + + $patron->delete()->store()->discard_changes(); # Make sure we are storing a 'new' patron + + is( $patron->password_expiration_date(), $date->add( days => 10 )->ymd() , "Password expiration date set correctly on patron creation"); + + $patron = $builder->build_object({ class => 'Koha::Patrons', value => { + categorycode => $category->categorycode, + password => undef + } + }); + $patron->delete()->store()->discard_changes(); + + is( $patron->password_expiration_date(), undef, "Password expiration date is not set if patron does not have a password"); + + $category->password_expiry_days(undef)->store(); + $patron = $builder->build_object({ class => 'Koha::Patrons', value => { + categorycode => $category->categorycode + } + }); + $patron->delete()->store()->discard_changes(); + is( $patron->password_expiration_date(), undef, "Password expiration date is not set if category does not have expiry days set"); + + $schema->storage->txn_rollback; + + subtest 'password_expired' => sub { + + plan tests => 3; + + $schema->storage->txn_begin; + my $date = dt_from_string(); + $patron = $builder->build_object({ class => 'Koha::Patrons', value => { + password_expiration_date => undef + } + }); + is( $patron->password_expired, 0, "Patron with no password expiration date, password not expired"); + $patron->password_expiration_date( $date )->store; + $patron->discard_changes(); + is( $patron->password_expired, 1, "Patron with password expiration date of today, password expired"); + $date->subtract( days => 1 ); + $patron->password_expiration_date( $date )->store; + $patron->discard_changes(); + is( $patron->password_expired, 1, "Patron with password expiration date in past, password expired"); + + $schema->storage->txn_rollback; + }; + + subtest 'set_password' => sub { + + plan tests => 4; + + $schema->storage->txn_begin; + + my $date = dt_from_string(); + my $category = $builder->build_object({ class => 'Koha::Patron::Categories', value => { + password_expiry_days => 10 + } + }); + my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { + categorycode => $category->categorycode, + password_expiration_date => $date->subtract( days => 1 ) + } + }); + is( $patron->password_expired, 1, "Patron password is expired"); + + $date = dt_from_string(); + $patron->set_password({ password => "kitten", skip_validation => 1 })->discard_changes(); + is( $patron->password_expired, 0, "Patron password no longer expired when new password set"); + is( $patron->password_expiration_date(), $date->add( days => 10 )->ymd(), "Password expiration date set correctly on patron creation"); + + + $category->password_expiry_days( undef )->store(); + $patron->set_password({ password => "puppies", skip_validation => 1 })->discard_changes(); + is( $patron->password_expiration_date(), undef, "Password expiration date is unset if category does not have expiry days"); + + $schema->storage->txn_rollback; + }; + +}; + subtest 'safe_to_delete() tests' => sub { plan tests => 14; diff --git a/t/db_dependent/Koha/Patron/Category.t b/t/db_dependent/Koha/Patron/Category.t index 5accf5679ea..be7f32b8de5 100755 --- a/t/db_dependent/Koha/Patron/Category.t +++ b/t/db_dependent/Koha/Patron/Category.t @@ -19,12 +19,13 @@ use Modern::Perl; -use Test::More tests => 5; +use Test::More tests => 6; use t::lib::TestBuilder; use t::lib::Mocks; use Koha::Database; +use Koha::DateUtils qw( dt_from_string ); my $schema = Koha::Database->new->schema; my $builder = t::lib::TestBuilder->new; @@ -202,3 +203,19 @@ subtest 'effective_require_strong_password' => sub { $schema->storage->txn_rollback; }; + +subtest 'get_password_expiry_date() tests' => sub { + + plan tests => 2; + + $schema->storage->txn_begin; + + my $category = $builder->build_object({ class => 'Koha::Patron::Categories' }); + $category->password_expiry_days( undef )->store; + + is( $category->get_password_expiry_date(), undef, "No date returned if expiry days undef" ); + + $category->password_expiry_days( 32 )->store; + is( $category->get_password_expiry_date(), dt_from_string()->add( days => 32 )->ymd, "Date correctly calculated from password_expiry_days when set"); + +}; -- 2.34.1