From 662c3c907dadbd46370604448a2f9829ded204a1 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 --- Koha/Patron.pm | 6 +- t/db_dependent/Koha/Patron.t | 95 ++++++++++++++++++++++++++- t/db_dependent/Koha/Patron/Category.t | 19 +++++- 3 files changed, 115 insertions(+), 5 deletions(-) diff --git a/Koha/Patron.pm b/Koha/Patron.pm index 4400c9946e..87853b5d8d 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -265,7 +265,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 @@ -791,7 +791,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; } @@ -894,7 +894,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 8d492168d7..f69c97d027 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 => 11; +use Test::More tests => 12; use Test::Exception; use Test::Warn; @@ -847,6 +847,99 @@ subtest 'article_requests() 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; + is( $patron->password_expired, 1, "Patron with password expiration date of today, password expired"); + $date->subtract( days => 1 ); + $patron->password_expiration_date( $date )->store; + 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 5accf5679e..be7f32b8de 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.30.2