View | Details | Raw Unified | Return to bug 29924
Collapse All | Expand All

(-)a/Koha/Patron.pm (-3 / +3 lines)
Lines 266-272 sub store { Link Here
266
                $self->plain_text_password( $self->password );
266
                $self->plain_text_password( $self->password );
267
267
268
                $self->password_expiration_date( $self->password
268
                $self->password_expiration_date( $self->password
269
                    ? $self->category->get_password_expiry_date
269
                    ? $self->category->get_password_expiry_date || undef
270
                    : undef );
270
                    : undef );
271
                # Create a disabled account if no password provided
271
                # Create a disabled account if no password provided
272
                $self->password( $self->password
272
                $self->password( $self->password
Lines 792-798 Returns 1 if the patron's password is expired or 0; Link Here
792
sub password_expired {
792
sub password_expired {
793
    my ($self) = @_;
793
    my ($self) = @_;
794
    return 0 unless $self->password_expiration_date;
794
    return 0 unless $self->password_expiration_date;
795
    return 1 if dt_from_string( $self->password_expiration_date ) < dt_from_string->truncate( to => 'day' );
795
    return 1 if dt_from_string( $self->password_expiration_date ) <= dt_from_string->truncate( to => 'day' );
796
    return 0;
796
    return 0;
797
}
797
}
798
798
Lines 895-901 sub set_password { Link Here
895
895
896
    my $digest = Koha::AuthUtils::hash_password($password);
896
    my $digest = Koha::AuthUtils::hash_password($password);
897
897
898
    $self->password_expiration_date( $self->category->get_password_expiry_date );
898
    $self->password_expiration_date( $self->category->get_password_expiry_date || undef );
899
899
900
    # We do not want to call $self->store and retrieve password from DB
900
    # We do not want to call $self->store and retrieve password from DB
901
    $self->password($digest);
901
    $self->password($digest);
(-)a/t/db_dependent/Koha/Patron.t (+95 lines)
Lines 852-857 subtest 'article_requests() tests' => sub { Link Here
852
    $schema->storage->txn_rollback;
852
    $schema->storage->txn_rollback;
853
};
853
};
854
854
855
subtest 'password expiration tests' => sub {
856
857
    plan tests => 5;
858
859
    $schema->storage->txn_begin;
860
    my $date = dt_from_string();
861
    my $category = $builder->build_object({ class => 'Koha::Patron::Categories', value => {
862
            password_expiry_days => 10,
863
            require_strong_password => 0,
864
        }
865
    });
866
    my $patron = $builder->build_object({ class=> 'Koha::Patrons', value => {
867
            categorycode => $category->categorycode,
868
            password => 'hats'
869
        }
870
    });
871
872
    $patron->delete()->store()->discard_changes(); # Make sure we are storing a 'new' patron
873
874
    is( $patron->password_expiration_date(), $date->add( days => 10 )->ymd() , "Password expiration date set correctly on patron creation");
875
876
    $patron = $builder->build_object({ class => 'Koha::Patrons', value => {
877
            categorycode => $category->categorycode,
878
            password => undef
879
        }
880
    });
881
    $patron->delete()->store()->discard_changes();
882
883
    is( $patron->password_expiration_date(), undef, "Password expiration date is not set if patron does not have a password");
884
885
    $category->password_expiry_days(undef)->store();
886
    $patron = $builder->build_object({ class => 'Koha::Patrons', value => {
887
            categorycode => $category->categorycode
888
        }
889
    });
890
    $patron->delete()->store()->discard_changes();
891
    is( $patron->password_expiration_date(), undef, "Password expiration date is not set if category does not have expiry days set");
892
893
    $schema->storage->txn_rollback;
894
895
    subtest 'password_expired' => sub {
896
897
        plan tests => 3;
898
899
        $schema->storage->txn_begin;
900
        my $date = dt_from_string();
901
        $patron = $builder->build_object({ class => 'Koha::Patrons', value => {
902
                password_expiration_date => undef
903
            }
904
        });
905
        is( $patron->password_expired, 0, "Patron with no password expiration date, password not expired");
906
        $patron->password_expiration_date( $date )->store;
907
        $patron->discard_changes();
908
        is( $patron->password_expired, 1, "Patron with password expiration date of today, password expired");
909
        $date->subtract( days => 1 );
910
        $patron->password_expiration_date( $date )->store;
911
        $patron->discard_changes();
912
        is( $patron->password_expired, 1, "Patron with password expiration date in past, password expired");
913
914
        $schema->storage->txn_rollback;
915
    };
916
917
    subtest 'set_password' => sub {
918
919
        plan tests => 4;
920
921
        $schema->storage->txn_begin;
922
923
        my $date = dt_from_string();
924
        my $category = $builder->build_object({ class => 'Koha::Patron::Categories', value => {
925
                password_expiry_days => 10
926
            }
927
        });
928
        my $patron = $builder->build_object({ class => 'Koha::Patrons', value => {
929
                categorycode => $category->categorycode,
930
                password_expiration_date =>  $date->subtract( days => 1 )
931
            }
932
        });
933
        is( $patron->password_expired, 1, "Patron password is expired");
934
935
        $date = dt_from_string();
936
        $patron->set_password({ password => "kitten", skip_validation => 1 })->discard_changes();
937
        is( $patron->password_expired, 0, "Patron password no longer expired when new password set");
938
        is( $patron->password_expiration_date(), $date->add( days => 10 )->ymd(), "Password expiration date set correctly on patron creation");
939
940
941
        $category->password_expiry_days( undef )->store();
942
        $patron->set_password({ password => "puppies", skip_validation => 1 })->discard_changes();
943
        is( $patron->password_expiration_date(), undef, "Password expiration date is unset if category does not have expiry days");
944
945
        $schema->storage->txn_rollback;
946
    };
947
948
};
949
855
subtest 'safe_to_delete() tests' => sub {
950
subtest 'safe_to_delete() tests' => sub {
856
951
857
    plan tests => 14;
952
    plan tests => 14;
(-)a/t/db_dependent/Koha/Patron/Category.t (-2 / +18 lines)
Lines 19-30 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 5;
22
use Test::More tests => 6;
23
23
24
use t::lib::TestBuilder;
24
use t::lib::TestBuilder;
25
use t::lib::Mocks;
25
use t::lib::Mocks;
26
26
27
use Koha::Database;
27
use Koha::Database;
28
use Koha::DateUtils qw( dt_from_string );
28
29
29
my $schema  = Koha::Database->new->schema;
30
my $schema  = Koha::Database->new->schema;
30
my $builder = t::lib::TestBuilder->new;
31
my $builder = t::lib::TestBuilder->new;
Lines 202-204 subtest 'effective_require_strong_password' => sub { Link Here
202
203
203
  $schema->storage->txn_rollback;
204
  $schema->storage->txn_rollback;
204
};
205
};
205
- 
206
207
subtest 'get_password_expiry_date() tests' => sub {
208
209
    plan tests => 2;
210
211
    $schema->storage->txn_begin;
212
213
    my $category = $builder->build_object({ class => 'Koha::Patron::Categories' });
214
    $category->password_expiry_days( undef )->store;
215
216
    is( $category->get_password_expiry_date(), undef, "No date returned if expiry days undef" );
217
218
    $category->password_expiry_days( 32 )->store;
219
    is( $category->get_password_expiry_date(), dt_from_string()->add( days => 32 )->ymd, "Date correctly calculated from password_expiry_days when set");
220
221
};

Return to bug 29924