From 1a531a0759c3dba8171f2b31a4db2d459cc05dc4 Mon Sep 17 00:00:00 2001
From: Arthur Suzuki <arthur.suzuki@biblibre.com>
Date: Tue, 10 Sep 2019 02:03:52 +0200
Subject: [PATCH] Bug23011 : Add new tests (AuthenticatePatron error codes)

This bug add new tests to the AuthenticatePatron ILS-DI service.
It tests new error codes patron might get when successfully connecting
but with a password which doesn't match Koha security rules.
---
 t/db_dependent/ILSDI_Services.t | 50 +++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)

diff --git a/t/db_dependent/ILSDI_Services.t b/t/db_dependent/ILSDI_Services.t
index b1288ec469..d6631a3f03 100644
--- a/t/db_dependent/ILSDI_Services.t
+++ b/t/db_dependent/ILSDI_Services.t
@@ -104,6 +104,56 @@ subtest 'AuthenticatePatron test' => sub {
 };
 
 
+subtest 'AuthenticatePatron Secure Password test' => sub {
+
+    plan tests => 10;
+
+    $schema->storage->txn_begin;
+
+    my $plain_password = 'tomasito';
+
+    $builder->build({
+        source => 'Borrower',
+        value => {
+            cardnumber => undef,
+        }
+    });
+
+    my $borrower = $builder->build({
+        source => 'Borrower',
+        value  => {
+            cardnumber => undef,
+            password => Koha::AuthUtils::hash_password( $plain_password )
+        }
+    });
+
+    my $query = new CGI;
+    $query->param( 'username', $borrower->{userid});
+    $query->param( 'password', $plain_password);
+
+    my $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
+    is( $reply->{id}, $borrower->{borrowernumber}, "userid and password - Patron authenticated" );
+    is( $reply->{code}, undef, "Error code undef");
+
+    t::lib::Mocks::mock_preference( 'minPasswordLength', 10 );
+    $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
+    is( $reply->{id}, $borrower->{borrowernumber}, "existing user");
+    is( $reply->{code}, 'PatronPasswordNotSecure', "PasswordNotSecure" );
+    is( $reply->{securePasswordLabel}, 'too_short', "PasswordTooShort" );
+    is( $reply->{securePasswordPattern}, '.{10}', "Password must be at least 10 char" );
+
+    t::lib::Mocks::mock_preference( 'RequireStrongPassword', 1 );
+    t::lib::Mocks::mock_preference( 'minPasswordLength', 5 );
+    $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
+    is( $reply->{id}, $borrower->{borrowernumber}, "existing user");
+    is( $reply->{code}, 'PatronPasswordNotSecure', "PasswordNotSecure" );
+    is( $reply->{securePasswordLabel}, 'too_weak', "PasswordTooWeak" );
+    is( $reply->{securePasswordPattern}, '(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{5}', "Password must be strong (pattern match)" );
+
+    $schema->storage->txn_rollback;
+};
+
+
 subtest 'GetPatronInfo/GetBorrowerAttributes test for extended patron attributes' => sub {
 
     plan tests => 5;
-- 
2.20.1