ElixirVault/tests/UnitTests/App/PubUsers/passwordForceChangeActionTe...

249 lines
8.3 KiB
PHP

<?php
namespace ElixirAudit\Tests\UnitTests\App\PubUsers;
use BRSystems\Core\Library\cGoogle2FA\cGoogle2FA;
use Exception;
use BRSystems\Core\App\pubUsers\pubUsers;
use ElixirAudit\Tests\BasePhpUnitCase;
use ElixirAudit\Tests\DataSetup\Constants\SiteMap;
use ElixirAudit\Tests\DataSetup\Constants\siteUsers;
class passwordForceChangeActionTest extends BasePhpUnitCase
{
private pubUsers $users;
private pubUsers $pubUserLogin;
protected function setUp(): void
{
// Init base structure
$this->unitTestsAreRunning = true;
parent::setUp();
$this->clearDbState();
// Set force pass change param
$this->cDb->_queryUpdate('mod_users', ['user_fchange' => 1], ['id' => 12]);
// Create new class
$this->users = $this->initBaseClassObjectBySiteMapId(SiteMap::PUB_USER_PASS_CHANGE, null);
$this->loginUserBeforePassChangeRedirect();
}
protected function tearDown(): void
{
$this->clearDbState();
parent::tearDown();
}
protected function clearDbState(): void
{
$this->logOutUnitTestUser();
$this->resetGetAndPost();
$this->reloadDbTables(['mod_users', 'mod_account_manager']);
}
public function passwordForceChangeActionLoginRequirement(): void
{
$this->testModuleLoginRequirement(SiteMap::PUB_USER_PASS_CHANGE);
}
public function testForcePasswordChangeActionDefaultAction(): void
{
$this->setPost([
'action' => 'test'
]);
// Default form generation
ob_start();
$this->users->passwordForceChangeAction();
$pageHTML = ob_get_contents();
ob_end_clean();
$this->assertExpected(
[
'<title>Zutenis | Paroles maiņa</title>',
'Mainīt lietotāja paroli',
'<input name="user_name" id="user_name" data-validator="user_name" ',
'<input name="old_password" id="old_password" data-validator="old_password" type="password"',
'<input name="new_password" id="new_password" data-validator="new_password" type="password"',
'<input name="rnew_password" id="rnew_password" data-validator="rnew_password" type="password"',
'<button type="submit"',
],
$pageHTML
);
}
public function testForcePasswordChangeActionDefaultActionWithPostTestCase1(): void
{
$userData = $this->getLoginCredentialsByType(siteUsers::loginAppSelfEmployed);
$this->setPost([
'action' => 'pwd',
'user_name' => '',
'old_password' => $userData['password'],
'new_password' => 'asdasdasd',
]);
try {
$this->users->passwordForceChangeAction();
$this->fail('Exception Expected!');
} catch (Exception $error) {
$this->assertExpected(['{"error":true,"data":{"new_password":"Lauki nesakr\u012bt!"}}'], $error->getMessage());
}
}
public function testForcePasswordChangeActionDefaultActionWithPostTestCase2(): void
{
$this->setPost([
'action' => 'pwd',
'user_name' => '',
'old_password' => 'asd',
'new_password' => 'asdasdasd',
'rnew_password' => 'asdasdasd',
]);
try {
$this->users->passwordForceChangeAction();
$this->fail('Exception Expected!');
} catch (Exception $error) {
$this->assertExpected(
['{"error":true,"data":{"msg":"Old user password doesn\'t match!"'],
$error->getMessage()
);
}
}
public function testForcePasswordChangeActionDefaultActionWithPostTestCase3(): void
{
// Update pass time
$this->cDb->_queryUpdate(
'mod_users',
[
'user_newpwd' => '$2y$11$N0OyJI/y1gXRsw1eKhU.yu6xZnjlcrASTVIdObLfYI3VsnUnHkBuO',
'user_pwdtime' => time() + 2
],
['id' => 12]
);
// Create new class
$this->users = $this->initBaseClassObjectBySiteMapId(SiteMap::PUB_USER_PASS_CHANGE, null);
$this->loginUserBeforePassChangeRedirect('baTQCVRQpp');
sleep(5);
$this->setPost([
'action' => 'pwd',
'user_name' => '',
'old_password' => 'baTQCVRQpp',
'new_password' => 'asdasdasd',
'rnew_password' => 'asdasdasd',
]);
try {
$this->users->passwordForceChangeAction();
$this->fail('Exception Expected!');
} catch (Exception $error) {
$this->assertExpected(['{"error":true,"url":"\/ienakt-gramatvedibas-sistema"}'], $error->getMessage());
}
}
public function testForcePasswordChangeActionDefaultActionWithPostTestCase4(): void
{
$userData = $this->getLoginCredentialsByType(siteUsers::loginAppSelfEmployed);
$this->setPost([
'action' => 'pwd',
'user_name' => '',
'old_password' => $userData['password'],
'new_password' => $userData['password'],
'rnew_password' => $userData['password'],
]);
try {
$this->users->passwordForceChangeAction();
$this->fail('Exception Expected!');
} catch (Exception $error) {
$this->assertExpected(
['{"error":true,"data":{"msg":"Jaun\u0101 lietot\u0101ja parole nevar sakrist ar veco!"'],
$error->getMessage()
);
}
}
public function testForcePasswordChangeActionDefaultActionWithPostTestCase5(): void
{
$userData = $this->getLoginCredentialsByType(siteUsers::loginAppSelfEmployed);
$this->setPost([
'action' => 'pwd',
'user_name' => '',
'old_password' => $userData['password'],
'new_password' => 'asdasdasd123',
'rnew_password' => 'asdasdasd123',
]);
try {
$this->users->passwordForceChangeAction();
$this->fail('Exception Expected!');
} catch (Exception $error) {
$this->assertExpected(
['{"jsonok":true,"url":"\/gramatvedibas-sistema\/konta-izvele"}'],
$error->getMessage()
);
}
}
public function testForcePasswordChangeActionDefaultActionWithPostTestCase6(): void
{
// Force user logout after module setup & login
$this->logOutUnitTestUser();
// Reset force pass change param
$this->cDb->_queryUpdate('mod_users', ['user_fchange' => '0'], ['id' => 12]);
// Create new class
$this->users = $this->initBaseClassObjectBySiteMapId(SiteMap::PUB_USER_PASS_CHANGE);
try {
$this->users->passwordForceChangeAction();
$this->fail('Exception Expected!');
} catch (Exception $error) {
$this->assertExpected(['Redirected to Location: /ienakt-gramatvedibas-sistema'], $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());
}
// Submit 2fa security & catch json send data exception
try {
$oGoogle2FA = new cGoogle2FA();
$next2FACode = $oGoogle2FA->getGoogleAuthCode($userData['2fa']);
$_POST['code'] = $next2FACode;
$this->pubUserLogin->controller->set2FAForm();
$this->fail('Exception Expected!');
} catch (Exception $error) {
$this->assertExpected(
['Redirected to Location: /gramatvedibas-sistema/paroles-maina'],
$error->getMessage()
);
}
}
}