From a889c8d99cf8fc6c57c602fb7bab3b43696f3b50 Mon Sep 17 00:00:00 2001
From: Arthur Suzuki <arthur.suzuki@biblibre.com>
Date: Fri, 20 Dec 2019 18:01:42 +0100
Subject: [PATCH] Bug 23011: 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.

Signed-off-by: Emmi Takkinen <emmi.takkinen@outlook.com>
---
 t/db_dependent/ILSDI_Services.t | 52 ++++++++++++++++++++++++++++++++-
 1 file changed, 51 insertions(+), 1 deletion(-)

diff --git a/t/db_dependent/ILSDI_Services.t b/t/db_dependent/ILSDI_Services.t
index d7fe08f65f..6789d3e390 100644
--- a/t/db_dependent/ILSDI_Services.t
+++ b/t/db_dependent/ILSDI_Services.t
@@ -19,7 +19,7 @@ use Modern::Perl;
 
 use CGI qw ( -utf8 );
 
-use Test::More tests => 9;
+use Test::More tests => 10;
 use Test::MockModule;
 use t::lib::Mocks;
 use t::lib::TestBuilder;
@@ -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.17.1