From f8fc1953842fa9d0b119f7d3cee305c60389636c Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Thu, 16 Aug 2018 07:13:36 -0300 Subject: [PATCH] Bug 21178: Unit tests --- t/db_dependent/Koha/Patrons.t | 74 ++++++++++++++++++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/Koha/Patrons.t b/t/db_dependent/Koha/Patrons.t index 6521db1a51..f8c8a8046e 100644 --- a/t/db_dependent/Koha/Patrons.t +++ b/t/db_dependent/Koha/Patrons.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 32; +use Test::More tests => 33; use Test::Warn; use Test::Exception; use Time::Fake; @@ -1501,6 +1501,78 @@ subtest '->store' => sub { }; +subtest '->set_password' => sub { + + plan tests => 11; + + $schema->storage->txn_begin; + + my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { login_attempts => 3 } } ); + + # Disable logging password changes for this tests + t::lib::Mocks::mock_preference( 'BorrowersLog', 0 ); + + # Password-length tests + t::lib::Mocks::mock_preference( 'minPasswordLength', undef ); + throws_ok { $patron->set_password('ab'); } + 'Koha::Exceptions::Password::TooShort', + 'minPasswordLength is undef, fall back to 3, fail test'; + + t::lib::Mocks::mock_preference( 'minPasswordLength', 2 ); + throws_ok { $patron->set_password('ab'); } + 'Koha::Exceptions::Password::TooShort', + 'minPasswordLength is 2, fall back to 3, fail test'; + + t::lib::Mocks::mock_preference( 'minPasswordLength', 5 ); + throws_ok { $patron->set_password('abcb'); } + 'Koha::Exceptions::Password::TooShort', + 'minPasswordLength is 5, fail test'; + + # Trailing spaces tests + throws_ok { $patron->set_password('abcD12d '); } + 'Koha::Exceptions::Password::TrailingWhitespaces', + 'Password contains trailing spaces, exception is thrown'; + + # Require strong password tests + t::lib::Mocks::mock_preference( 'RequireStrongPassword', 1 ); + throws_ok { $patron->set_password('abcd a'); } + 'Koha::Exceptions::Password::TooWeak', + 'Password is too weak, exception is thrown'; + + # Refresh patron from DB, just to make sure + $patron->discard_changes; + is( $patron->login_attempts, 3, 'Previous tests kept login attemps count' ); + + $patron->set_password('abcD12 34'); + $patron->discard_changes; + + is( $patron->login_attempts, 0, 'Changing the password resets the login attempts count' ); + + # Completeness + t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 ); + $patron->login_attempts(3)->store; + my $old_digest = $patron->password; + $patron->set_password('abcd a'); + $patron->discard_changes; + + isnt( $patron->password, $old_digest, 'Password has been updated' ); + is( $patron->login_attempts, 0, 'Login attemps have been reset' ); + + my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'CHANGE PASS', object => $patron->borrowernumber } )->count; + is( $number_of_logs, 0, 'Without BorrowerLogs, Koha::Patron->set_password doesn\'t log password changes' ); + + # Enable logging password changes + t::lib::Mocks::mock_preference( 'BorrowersLog', 1 ); + $patron->set_password('abcd b'); + + $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'CHANGE PASS', object => $patron->borrowernumber } )->count; + is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->set_password does log password changes' ); + + $schema->storage->txn_rollback; +}; + + + # TODO Move to t::lib::Mocks and reuse it! sub set_logged_in_user { my ($patron) = @_; -- 2.18.0