PHP Classes

File: tests/src/services/UserCredentialPasswordLoginServiceTest.php

Recommend this page to a friend!
  Classes of Cyril Ogana   PHP User Credentials   tests/src/services/UserCredentialPasswordLoginServiceTest.php   Download  
File: tests/src/services/UserCredentialPasswordLoginServiceTest.php
Role: Unit test script
Content type: text/plain
Description: Add MultiOTP wrapper and SMS Token 2 factor authentication service
Class: PHP User Credentials
Implement password authentication policies
Author: By
Last change: Update to Multiotp 5.6+, PHPass 8+, PHP 7.3+ and PHPUnit 9+
Date: 3 years ago
Size: 6,879 bytes
 

Contents

Class file image Download
<?php

namespace cymapgt\core\application\authentication\UserCredential\services;

use
cymapgt\core\application\authentication\UserCredential\abstractclass\MultiotpWrapper;
use
JJG\Ping;

/**
 * Generated by PHPUnit_SkeletonGenerator 1.2.1 on 2014-05-18 at 14:28:58.
 */
class UserCredentialPasswordLoginServiceTest extends \PHPUnit\Framework\TestCase {

   
/**
     * @var UserCredentialPasswordLoginService
     */
   
protected $object;
   
   
/**
     * @var tstring
     */
   
protected $password;
   
   
/**
     * @var MultiOtpWrapper
     */
   
protected $multiOtpWrapper;

   
/**
     * Sets up the fixture, for example, opens a network connection.
     * This method is called before a test is executed.
     */
   
protected function setUp() : void {
       
//Make sure that UserCredentialPasswordLoginService is available to your auth plugin and create an instance
       
$this->object = new UserCredentialPasswordLoginService;
       
       
/**
         * This is the password that is stored in DB hashed with \password_hash function.
         * PHP 5.4 will be supported because of ircmaxell/password-compat package
         */
       
$this->password = \password_hash('123456', \PASSWORD_DEFAULT);
    }

   
/**
     * Tears down the fixture, for example, closes a network connection.
     * This method is called after a test is executed.
     */
   
protected function tearDown() : void {
       
    }

   
/**
     * @covers cymapgt\core\application\authentication\UserCredential\services\UserCredentialPasswordLoginService::initialize
     */
   
public function testInitialize() {
       
//username of authenticating user
       
$this->object->setCurrentUserName('rhossis');
       
       
//password that is stored in the DB
       
$this->object->setCurrentPassword($this->password);
       
       
//password input by the user in the login form / API
       
$this->object->setPassword('123456');
       
       
       
$this->assertEquals(null, $this->object->initialize());
    }
   
   
/**
     * @covers cymapgt\core\application\authentication\UserCredential\services\UserCredentialPasswordLoginService::initialize
     */
   
public function testInitializeException() {
       
$this->expectException('\cymapgt\Exception\UserCredentialException');
       
$this->expectExceptionMessage('The usercredential login service is not initialized with all parameters');
       
       
//if you call initialize without setting the username, password and keyed in password, an exception should be thrown
       
$this->object->initialize();
    }

   
/**
     * @covers cymapgt\core\application\authentication\UserCredential\services\UserCredentialPasswordLoginService::authenticate
     */
   
public function testAuthenticateNative() {
       
//test authentication where user has input the correct password
       
$this->object->setCurrentUserName('rhossis');
       
$this->object->setCurrentPassword($this->password);
       
$this->object->setPassword('123456');
       
       
//test authentication where user has input wrong password. We assume the user input 12345 instead of 123456
       
$this->assertEquals(true, $this->object->authenticate());
       
$this->object->setPassword('12345');
       
$this->assertEquals(false, $this->object->authenticate());
    }
   
   
/**
     * This tests that without proper initialization of LDAP settings, a UserCredentialException will be thrown.
     *
     * @covers cymapgt\core\application\authentication\UserCredential\services\UserCredentialPasswordLoginService::authenticate
     */
   
public function testInitializeLdapException() {
       
$this->expectException('\cymapgt\Exception\UserCredentialException');
       
$this->expectExceptionMessage('The LDAP feature of the usercredential login service is not initialized with all parameters');
               
       
$this->object->setPasswordAuthenticationPlatform(\USERCREDENTIAL_PASSWORDLOGINPLATFORM_LDAP);
       
$this->object->setCurrentUserName('rhossis');
       
$this->object->setCurrentPassword($this->password);
       
$ldapSettings = array();
       
$this->object->initializeLdap($ldapSettings);
    }
   
   
/**
     * LDAP settings must be initialized, then authentication can proceed. The below example uses the MultiOTP
     * LDAP auth functionality. If the remote LDAP configured cannot be pinged, this test will be skipped
     *
     * @covers cymapgt\core\application\authentication\UserCredential\services\UserCredentialPasswordLoginService::authenticate
     */
   
public function testAuthenticateLdap() {
       
//the LDAP server
       
$domainControllers = 'ldap.forumsys.com';
       
       
//ping server
       
$pingResult = new Ping($domainControllers);
       
$pingResult->setPort(389);
       
$latency = $pingResult->ping('fsockopen');
       
        if (
$latency !== false) {
           
$skipLdapTest = false;
        } else {
           
$skipLdapTest = true;
        }
       
       
//if ping was not successfull, skip the test
       
if ($skipLdapTest) {
           
$this->markTestSkipped('Cannot connect to remote LDAP test server');
        } else {
           
$this->object->setPasswordAuthenticationPlatform(\USERCREDENTIAL_PASSWORDLOGINPLATFORM_LDAP);

           
$ldapSettings = array();
           
$ldapSettings['ldap_account_suffix'] = '';
           
$ldapSettings['ad_password'] = 'password';
           
$ldapSettings['ad_username'] = 'cn=read-only-admin,dc=example,dc=com';
           
$ldapSettings['base_dn'] = '';
           
$ldapSettings['cn_identifier'] = 'TEST';
           
$ldapSettings['domain_controllers'] = $domainControllers;
           
$ldapSettings['group_attribute'] = 'dc=example,dc=com';
           
$ldapSettings['group_cn_identifier'] = 'cn';
           
$ldapSettings['ldap_server_type'] = 1;
           
$ldapSettings['network_timeout'] = 1;
           
$ldapSettings['port'] = 389;
           
$ldapSettings['recursive_groups'] = 1;
           
$ldapSettings['time_limit'] = 30;
           
$ldapSettings['use_ssl'] = 0;
           
$ldapSettings['cache_support'] = 0;
           
$ldapSettings['cache_folder'] = '';
           
$ldapSettings['expired_password_valid'] = '';

           
$this->object->setCurrentUserName($ldapSettings['ad_username']);
           
$this->object->setCurrentPassword($ldapSettings['ad_password']);
           
$this->object->setPasswordAuthenticationPlatformSettings($ldapSettings);
           
$this->object->initializeLdap($ldapSettings);
           
$isLoggedIn = $this->object->authenticate();

           
$this->assertEquals(true, $isLoggedIn);
        }
    }
}