From 681d4470643c7e2017aa3823ad7cc113dfa20135 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Wed, 26 Dec 2018 10:59:55 -0300 Subject: [PATCH] Bug 22048: Use set_password in tests To test: - Apply this patch - Run: $ kshell k$ prove t/db_dependent/Auth.t \ t/db_dependent/Koha/Patrons.t \ t/db_dependent/Search/History.t \ t/db_dependent/selenium/authentication.t \ t/db_dependent/selenium/regressions.t => SUCCESS: Tests pass! - Sign off :-D --- t/db_dependent/Auth.t | 40 +++++++++++++----------- t/db_dependent/Koha/Patrons.t | 3 +- t/db_dependent/Search/History.t | 3 +- t/db_dependent/selenium/authentication.t | 7 +++-- t/db_dependent/selenium/regressions.t | 4 ++- 5 files changed, 33 insertions(+), 24 deletions(-) diff --git a/t/db_dependent/Auth.t b/t/db_dependent/Auth.t index 82193f32bf..e0b6036f41 100644 --- a/t/db_dependent/Auth.t +++ b/t/db_dependent/Auth.t @@ -10,7 +10,7 @@ use CGI qw ( -utf8 ); use Test::MockObject; use Test::MockModule; use List::MoreUtils qw/all any none/; -use Test::More tests => 26; +use Test::More tests => 20; use Test::Warn; use t::lib::Mocks; use t::lib::TestBuilder; @@ -115,28 +115,27 @@ subtest 'track_login_daily tests' => sub { }; -my $hash1 = hash_password('password'); -my $hash2 = hash_password('password'); +subtest 'no_set_userenv parameter tests' => sub { -{ # tests no_set_userenv parameter - my $patron = $builder->build( { source => 'Borrower' } ); - Koha::Patrons->find( $patron->{borrowernumber} )->update_password( $patron->{userid}, 'password' ); - my $library = $builder->build( - { - source => 'Branch', - } - ); + plan tests => 7; + + my $library = $builder->build_object( { class => 'Koha::Libraries' } ); + my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); + my $password = 'password'; - ok( checkpw( $dbh, $patron->{userid}, 'password', undef, undef, 1 ), 'checkpw returns true' ); + t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 ); + $patron->set_password({ password => $password }); + + ok( checkpw( $dbh, $patron->userid, $password, undef, undef, 1 ), 'checkpw returns true' ); is( C4::Context->userenv, undef, 'Userenv should be undef as required' ); C4::Context->_new_userenv('DUMMY SESSION'); - C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, 'Library 1', 0, '', ''); - is( C4::Context->userenv->{branch}, $library->{branchcode}, 'Userenv gives correct branch' ); - ok( checkpw( $dbh, $patron->{userid}, 'password', undef, undef, 1 ), 'checkpw returns true' ); - is( C4::Context->userenv->{branch}, $library->{branchcode}, 'Userenv branch is preserved if no_set_userenv is true' ); - ok( checkpw( $dbh, $patron->{userid}, 'password', undef, undef, 0 ), 'checkpw still returns true' ); - isnt( C4::Context->userenv->{branch}, $library->{branchcode}, 'Userenv branch is overwritten if no_set_userenv is false' ); -} + C4::Context->set_userenv(0,0,0,'firstname','surname', $library->branchcode, 'Library 1', 0, '', ''); + is( C4::Context->userenv->{branch}, $library->branchcode, 'Userenv gives correct branch' ); + ok( checkpw( $dbh, $patron->userid, $password, undef, undef, 1 ), 'checkpw returns true' ); + is( C4::Context->userenv->{branch}, $library->branchcode, 'Userenv branch is preserved if no_set_userenv is true' ); + ok( checkpw( $dbh, $patron->userid, $password, undef, undef, 0 ), 'checkpw still returns true' ); + isnt( C4::Context->userenv->{branch}, $library->branchcode, 'Userenv branch is overwritten if no_set_userenv is false' ); +}; # get_template_and_user tests @@ -311,5 +310,8 @@ my ( $template2 ); ok( ( any { 'OPACBaseURL' eq $_ } keys %{$template2->{VARS}} ), 'OPACBaseURL is in Staff template' ); +my $hash1 = hash_password('password'); +my $hash2 = hash_password('password'); + ok(C4::Auth::checkpw_hash('password', $hash1), 'password validates with first hash'); ok(C4::Auth::checkpw_hash('password', $hash2), 'password validates with second hash'); diff --git a/t/db_dependent/Koha/Patrons.t b/t/db_dependent/Koha/Patrons.t index d980db5842..185644bbde 100644 --- a/t/db_dependent/Koha/Patrons.t +++ b/t/db_dependent/Koha/Patrons.t @@ -1516,8 +1516,9 @@ subtest '->store' => sub { 'Koha::Patron->store raises an exception on duplicate ID'; # Test password + t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 ); my $password = 'password'; - $patron_1->update_password($patron_1->userid, $password); + $patron_1->set_password({ password => $password }); like( $patron_1->password, qr|^\$2|, 'Password should be hashed using bcrypt (start with $2)' ); my $digest = $patron_1->password; $patron_1->surname('xxx')->store; diff --git a/t/db_dependent/Search/History.t b/t/db_dependent/Search/History.t index b8c912545e..2e3aa1fd1e 100644 --- a/t/db_dependent/Search/History.t +++ b/t/db_dependent/Search/History.t @@ -369,7 +369,8 @@ my $builder = t::lib::TestBuilder->new; # Borrower Creation our $patron = $builder->build( { source => 'Borrower' } ); -Koha::Patrons->find( $patron->{borrowernumber} )->update_password( $patron->{userid}, 'password' ); +t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 ); +Koha::Patrons->find( $patron->{borrowernumber} )->set_password({ password => 'password' }); my $session = C4::Auth::get_session(""); $session->flush; diff --git a/t/db_dependent/selenium/authentication.t b/t/db_dependent/selenium/authentication.t index 0ce4052dc2..b3738a561d 100644 --- a/t/db_dependent/selenium/authentication.t +++ b/t/db_dependent/selenium/authentication.t @@ -27,6 +27,7 @@ use Test::More tests => 2; use C4::Context; use Koha::AuthUtils; +use t::lib::Mocks; use t::lib::Selenium; use t::lib::TestBuilder; @@ -48,7 +49,8 @@ SKIP: { my $password = Koha::AuthUtils::generate_password(); my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { flags => 0 }}); - $patron->update_password( $patron->userid, $password ); + t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 ); + $patron->set_password({ password => $password }); # Patron does not have permission to access staff interface $s->auth( $patron->userid, $password ); @@ -79,7 +81,8 @@ SKIP: { my $password = Koha::AuthUtils::generate_password(); my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { flags => 0 }}); - $patron->update_password( $patron->userid, $password ); + t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 ); + $patron->set_password({ password => $password }); # Using the modal $driver->find_element('//a[@class="login-link loginModal-trigger"]')->click; diff --git a/t/db_dependent/selenium/regressions.t b/t/db_dependent/selenium/regressions.t index 4e8c9d7423..485ea7a01a 100644 --- a/t/db_dependent/selenium/regressions.t +++ b/t/db_dependent/selenium/regressions.t @@ -26,6 +26,7 @@ use C4::Context; use C4::Biblio qw( AddBiblio ); use C4::Circulation; use Koha::AuthUtils; +use t::lib::Mocks; use t::lib::Selenium; use t::lib::TestBuilder; @@ -53,7 +54,8 @@ subtest 'OPAC - borrowernumber and branchcode as html attributes' => sub { my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { flags => 1 } } ); my $password = Koha::AuthUtils::generate_password(); - $patron->update_password( $patron->userid, $password ); + t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 ); + $patron->set_password({ password => $password }); $s->opac_auth( $patron->userid, $password ); my $elt = $driver->find_element('//span[@class="loggedinusername"]'); is( $elt->get_attribute('data-branchcode'), $patron->library->branchcode, -- 2.20.1