ElixirVault/tests/UnitTests/App/PubUsers/twoFactorAuthActionTest.php

196 lines
6.1 KiB
PHP

<?php
namespace ElixirAudit\Tests\UnitTests\App\PubUsers;
use BRSystems\Core\Library\cSession\cSession;
use BRSystems\Core\App\pubUsers\pubUsers;
use BRSystems\Core\App\pubUsers\inc\pubUsersController;
use Exception;
use ElixirAudit\Tests\BasePhpUnitCase;
use ElixirAudit\Tests\DataSetup\Constants\SiteMap;
use ElixirAudit\Tests\DataSetup\Constants\siteUsers;
class twoFactorAuthActionTest extends BasePhpUnitCase
{
private pubUsers $users;
private pubUsers $pubUserLogin;
protected function setUp(): void
{
// Init base structure
$this->unitTestsAreRunning = true;
$this->resetGetAndPost();
parent::setUp();
// Create new class
$this->users = $this->initBaseClassObjectBySiteMapId(SiteMap::PUB_USER_2FA, null);
$this->loginUserBeforePassChangeRedirect();
}
protected function tearDown(): void
{
$this->logOutUnitTestUser();
$this->reloadDbTables(['mod_users']);
parent::tearDown();
}
public function twoFactorAuthActionLoginRequirement(): void
{
$this->testModuleLoginRequirement(SiteMap::PUB_USER_PASS_CHANGE);
}
public function testTwoFactorAuthActionDefaultAction(): void
{
// Default form generation
ob_start();
$this->users->twoFactorAuthAction();
$pageHTML = ob_get_contents();
ob_end_clean();
$twoFactorInput = '<div class="validate"><input type="text" inputmode="numeric" pattern="[0-9]*" ' .
'data-validator="codeOne" class="code2fa" placeholder="" autocomplete="off" maxlength="1" ' .
'tabindex="%INDEX%" /></div>';
$this->assertExpected(
[
'<title>Zutenis | Divu faktoru autentifikācija</title>',
'>Divu faktoru autentifikācija</p>',
str_replace('%INDEX%', '1', $twoFactorInput),
str_replace('%INDEX%', '2', $twoFactorInput),
str_replace('%INDEX%', '3', $twoFactorInput),
str_replace('%INDEX%', '4', $twoFactorInput),
str_replace('%INDEX%', '5', $twoFactorInput),
str_replace('%INDEX%', '6', $twoFactorInput),
'<button id="submit" type="submit" class="submit btn btn-block',
],
$pageHTML
);
}
public function testTwoFactorAuthActionWithPostTestCase1(): void
{
$this->loginUnitTestUserForCoverage();
$this->setPost([
'action' => '2fa',
]);
try {
$this->users->twoFactorAuthAction();
$this->fail('Exception Expected!');
} catch (Exception $error) {
$this->assertExpected(
['Redirected to Location: /gramatvedibas-sistema/sistemas-informacijas-panelis'],
$error->getMessage()
);
}
}
public function testTwoFactorAuthActionWithPostTestCase2(): void
{
$this->loginUnitTestUserForCoverage();
$o2FAUser = new cSession(pubUsersController::USER_2FA_SESSION_KEY);
$vUserData = $o2FAUser->vUserData;
$vUserData['user_uoa'] = 1;
$o2FAUser->vUserData = $vUserData;
$this->setPost([
'action' => '2fa',
]);
try {
$this->users->twoFactorAuthAction();
$this->fail('Exception Expected!');
} catch (Exception $error) {
$this->assertExpected(
['{"jsonok":true,"loginok":1,"data":{"login":"testmareks","uid":"Z'],
$error->getMessage()
);
}
}
public function testTwoFactorAuthActionWithPostTestCase3(): void
{
$this->loginUnitTestUserForCoverage();
$o2FAUser = new cSession(pubUsersController::USER_2FA_SESSION_KEY);
$vUserData = $o2FAUser->vUserData;
$vUserData['user_flogin'] = 1;
$o2FAUser->vUserData = $vUserData;
$this->setPost([
'action' => '2fa',
]);
try {
$this->users->twoFactorAuthAction();
$this->fail('Exception Expected!');
} catch (Exception $error) {
$this->assertExpected(
['{"jsonok":true,"continue_session":1}'],
$error->getMessage()
);
}
}
public function testTwoFactorAuthActionWithPostTestCase4(): void
{
$o2FAUser = new cSession(pubUsersController::USER_2FA_SESSION_KEY);
$vUserData = [];
$o2FAUser->vUserData = $vUserData;
$this->setPost([
'action' => '2fa',
]);
try {
$this->users->twoFactorAuthAction();
$this->fail('Exception Expected!');
} catch (Exception $error) {
$this->assertExpected(
['Redirected to Location: /ienakt-gramatvedibas-sistema'],
$error->getMessage()
);
}
}
public function testTwoFactorAuthActionWithPostTestCase5(): void
{
$this->setPost([
'action' => '2fa',
'code' => '666666',
]);
try {
$this->users->twoFactorAuthAction();
$this->fail('Exception Expected!');
} catch (Exception $error) {
$this->assertExpected(
['{"error":true,"data":{"code":"2 faktoru autentifik\u0101cijas koda k\u013c\u016bda!"}}'],
$error->getMessage()
);
}
}
private function loginUserBeforePassChangeRedirect(string $tempPassword = null): void
{
$this->pubUserLogin = new pubUsers(getFirstMatchByMapId(SiteMap::PUB_LOGIN));
$userData = $this->getLoginCredentialsByType(siteUsers::loginAppSelfEmployed);
// Log in & catch redirect exception
try {
$this->pubUserLogin->controller->loginUser(
[
'chkTerms' => 1,
'user_login' => $userData['user'],
'user_password' => $tempPassword ?: $userData['password'],
]
);
$this->fail('Exception Expected!');
} catch (Exception $error) {
$this->assertExpected(['Redirected to Location'], $error->getMessage());
}
}
}