ElixirVault/tests/UnitTests/App/PubUserManager/securityEnforcement2faTrapA...

245 lines
8.7 KiB
PHP

<?php
namespace ElixirAudit\Tests\UnitTests\App\PubUserManager;
use BRSystems\Core\Library\cCore\cCore;
use BRSystems\Core\Library\cGoogle2FA\cGoogle2FA;
use Exception;
use BRSystems\Core\App\pubUserManager\pubUserManager;
use ElixirAudit\Tests\BasePhpUnitCase;
use ElixirAudit\Tests\DataSetup\Constants\siteMap;
use ElixirAudit\Tests\DataSetup\Constants\siteUsers;
class securityEnforcement2faTrapActionTest extends BasePhpUnitCase
{
private pubUserManager $pubUserManager;
protected function setUp(): void
{
// Init base structure
$this->unitTestsAreRunning = true;
$this->resetGetAndPost();
parent::setUp();
$this->reloadDbTables(['mod_users']);
// Create new class
$this->pubUserManager = $this->initBaseClassObjectBySiteMapId(siteMap::PUB_USER_SECURITY_ENFORCEMENT);
}
protected function tearDown(): void
{
$this->reloadDbTables(['mod_users', 'mod_users_permissions']);
$this->logOutUnitTestUser();
parent::tearDown();
}
public function testSecurityEnforcement2faTrapActionBaseCase(): void
{
$this->logOutUnitTestUser();
// Clear 2fa
$loginData = $this->getLoginCredentialsByType(siteUsers::loginAppSelfEmployed);
$this->cDb->_queryUpdate('mod_users', ['user_2fa_secret_enabled' => ''], ['id' => $loginData['id']]);
// Log in & catch redirect exception
$this->loginUnitTestUserForCoverage(siteUsers::loginAppSelfEmployed, false, false);
ob_start();
$this->pubUserManager->securityEnforcement2faTrapAction();
$pageHTML = ob_get_contents();
ob_end_clean();
$this->assertExpected(
[
'<title>Zutenis | Lapas piekļuves kļūda ...</title>',
'<span>Lai piekļūtu grāmatvedības sistēmai, ir jāiespējo <br/>2FA - Divu faktoru autentifikācijas drošība! </span>',
'Divu faktoru autentifikācijas uzstādīšana</p>',
'Uzinstalējiet "Google Authenticator" lietojumprogrammu savā ierīcē',
'Noskenējiet svītrkodu ar "Google Authenticator"',
'<p>Ievadi Google Authenticator lietotnes piešķirto <br/>Divu faktoru autentifikācijas kodu</p>',
'<button class="submit',
'data-href="/ienakt-gramatvedibas-sistema/divu-faktoru-iestatisana">Ieslēgt</button>',
],
$pageHTML
);
}
public function testSecurityEnforcement2faTrapActionBaseCaseWhenUserHasLoggedIn(): void
{
try {
$this->pubUserManager->securityEnforcement2faTrapAction();
$this->fail("Exception expected!");
} catch (Exception $error) {
$this->assertExpected(
['Redirected to Location: /gramatvedibas-sistema/sistemas-informacijas-panelis'],
$error->getMessage()
);
}
}
public function testSecurityEnforcement2faTrapActionSet2FAQRCode(): void
{
$this->logOutUnitTestUser();
// Clear 2fa
$loginData = $this->getLoginCredentialsByType(siteUsers::loginAppSelfEmployed);
$this->cDb->_queryUpdate('mod_users', ['user_2fa_secret_enabled' => ''], ['id' => $loginData['id']]);
// Log in & catch redirect exception
$this->loginUnitTestUserForCoverage(siteUsers::loginAppSelfEmployed, false, false);
$oGoogle2FA = new cGoogle2FA();
$secret = $oGoogle2FA->getNewGoogleAuthSecret();
$this->setPost([
'action' => cCore::C_ACTION_ACTIVATE_2FA,
'secret' => $secret,
'code' => $oGoogle2FA->getGoogleAuthCode($secret)
]);
try {
$this->pubUserManager->securityEnforcement2faTrapAction();
$this->fail("Exception expected!");
} catch (Exception $error) {
$this->assertExpected(
['{"jsonok":true,"url":"\/gramatvedibas-sistema\/sistemas-informacijas-panelis"}'],
$error->getMessage()
);
}
}
public function testSecurityEnforcement2faTrapActionSet2FAQRCodeErrorCase(): void
{
$this->logOutUnitTestUser();
// Clear 2fa
$loginData = $this->getLoginCredentialsByType(siteUsers::loginAppSelfEmployed);
$this->cDb->_queryUpdate('mod_users', ['user_2fa_secret_enabled' => ''], ['id' => $loginData['id']]);
// Log in & catch redirect exception
$this->loginUnitTestUserForCoverage(siteUsers::loginAppSelfEmployed, false, false);
$this->setPost([
'action' => cCore::C_ACTION_ACTIVATE_2FA,
'secret' => '',
'code' => ''
]);
try {
$this->pubUserManager->securityEnforcement2faTrapAction();
$this->fail("Exception expected!");
} catch (Exception $error) {
$this->assertExpected(
[
'{"error":true,"data":{"code":"Ievad\u012bts k\u013c\u016bdains ' .
'2FA iestat\u012b\u0161anas dro\u0161\u012bbas kods!"}}'
],
$error->getMessage()
);
}
}
public function testSecurityEnforcement2faTrapActionDeactivate2FA(): void
{
$oGoogle2FA = new cGoogle2FA();
$loginData = $this->getLoginCredentialsByType(siteUsers::loginAppSelfEmployed);
$this->setPost([
'action' => 'deactivate-2FA',
'code' => $oGoogle2FA->getGoogleAuthCode($loginData['2fa'])
]);
try {
$this->pubUserManager->securityEnforcement2faTrapAction();
$this->fail("Exception expected!");
} catch (Exception $error) {
$this->assertExpected(
['{"jsonok":true,"data":true}'],
$error->getMessage()
);
}
}
public function testSecurityEnforcement2faTrapActionDeactivate2FAErrorCase1(): void
{
$this->setPost([
'action' => 'deactivate-2FA',
'code' => '123456'
]);
try {
$this->pubUserManager->securityEnforcement2faTrapAction();
$this->fail("Exception expected!");
} catch (Exception $error) {
$this->assertExpected(
['{"error":true,"data":{"code":"Login 2FA Security removal code input error!"}}'],
$error->getMessage()
);
}
}
public function testSecurityEnforcement2faTrapActionDeactivate2FAErrorCase2(): void
{
$this->setPost([
'action' => 'deactivate-2FA',
'code' => ''
]);
try {
$this->pubUserManager->securityEnforcement2faTrapAction();
$this->fail("Exception expected!");
} catch (Exception $error) {
$this->assertExpected(
['{"error":true,"data":{"code":"Login 2FA Security removal code input error!"}}'],
$error->getMessage()
);
}
}
public function testSecurityEnforcement2faTrapActionGet2FAQRCodeCase1(): void
{
$this->setPost(['action' => 'get2FA-QRCode']);
try {
$this->pubUserManager->securityEnforcement2faTrapAction();
$this->fail("Exception expected!");
} catch (Exception $error) {
// get JSON response
$response = json_decode($error->getMessage(), true);
$this->assertTrue(isset($response['jsonok']));
$this->assertTrue(isset($response['html']));
$this->assertTrue(isset($response['msgEnableAuth']));
$this->assertTrue(isset($response['buttonYes']));
$this->assertTrue(isset($response['buttonCancel']));
$this->assertTrue(isset($response['buttonActivate']));
$this->assertExpected(
[
'<p>Pierakstiet savu slepeno atslēgu un glabājiet to droši.<br/>',
'Tā būs nepieciešama, lai atgūtu 2FA, nozaudēšanas gadījumā.</p>',
'<input id="code" value="" data-validator="code" autocomplete="off"/>',
],
$response['html']
);
}
}
public function testSecurityEnforcement2faTrapActionGet2FAQRCodeCase2(): void
{
$this->setPost([
'action' => 'get2FA-QRCode',
'reload' => true
]);
try {
$this->pubUserManager->securityEnforcement2faTrapAction();
$this->fail("Exception expected!");
} catch (Exception $error) {
// get JSON response
$response = json_decode($error->getMessage(), true);
$this->assertTrue(isset($response['jsonok']));
$this->assertTrue(isset($response['sUrl']));
$this->assertTrue(isset($response['sSecret']));
$this->assertTrue(strlen($response['sSecret']) > 0);
}
}
}