226 lines
7.3 KiB
PHP
226 lines
7.3 KiB
PHP
<?php
|
|
|
|
namespace ElixirAudit\Tests\UnitTests\App\PubUsers;
|
|
|
|
use BRSystems\Core\Library\cPasswordMaster\cPasswordMaster;
|
|
use Exception;
|
|
use BRSystems\Core\App\pubUsers\pubUsers;
|
|
use ElixirAudit\Tests\BasePhpUnitCase;
|
|
use ElixirAudit\Tests\DataSetup\Constants\siteUsers;
|
|
|
|
class loginActionRequestTest extends BasePhpUnitCase
|
|
{
|
|
private pubUsers $users;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
// Init base structure
|
|
$this->unitTestsAreRunning = true;
|
|
parent::setUp();
|
|
$this->clearDbState();
|
|
|
|
// Create new class
|
|
$this->users = $this->initBaseClassObjectBySiteMapId();
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
$this->clearDbState();
|
|
parent::tearDown();
|
|
}
|
|
|
|
protected function clearDbState(): void
|
|
{
|
|
$this->resetGetAndPost();
|
|
$this->logOutUnitTestUser();
|
|
$this->reloadDbTables(['mod_users', 'mod_account_manager']);
|
|
}
|
|
|
|
public function testLoginActionRequests(): void
|
|
{
|
|
// Check while logged in
|
|
try {
|
|
$this->users->loginAction();
|
|
$this->fail('Exception Expected!');
|
|
} catch (Exception $error) {
|
|
$this->assertExpected(['Redirected to Location'], $error->getMessage());
|
|
}
|
|
|
|
// Try force login
|
|
// Generate base HTML layout & save output to var
|
|
ob_start();
|
|
$this->setGet(['action' => 'loginf']);
|
|
$this->users->loginAction();
|
|
|
|
$pageHTML = ob_get_contents();
|
|
ob_end_clean();
|
|
|
|
$this->assertExpected(
|
|
[
|
|
'<title>Zutenis | Autentifikācija</title>',
|
|
'Ienākt grāmatvedības sistēmā',
|
|
'<input name="user_login" id="user_login" data-validator="user_login"',
|
|
'<input name="user_password" id="user_password" data-validator="user_password" type="password"',
|
|
'<div id="agree_to_terms" class="chk">',
|
|
'<button type="submit"',
|
|
],
|
|
$pageHTML
|
|
);
|
|
|
|
// Test set year fn
|
|
$this->setGet([
|
|
'action' => 'setYear',
|
|
'id' => 'ZGVmNTAyMDA1NTYzYzVmZmIzMjIwOTlkZDdlYmE5NjAyMWM5NjFjMzUwMDM0MDk4MWMyNGMyNGFlZDFkMzkzZDE1Mzk0' .
|
|
'ZGJmNDA3ZGY4MGMyMDJiOTU2ZmQzZDlmNjQ0OTA3ZWZhZTM4NTdjYzM4ZTc3NzNlNmM4OGZhMjE1ODRmZjFkODRiY2Y1OWVm' .
|
|
'MmE1ODMyNjQwNGE5Mzc1MzdmNzExMzA2NDM3NmE2NGMwNDA1ZWQ0YTYxMWExNTZiNjViN2JiNjE1MWFiNmQ2Zjc1NDU5ZDFj' .
|
|
'MzFiZTllMjc0NzFkYTljNDNkNTg1MjY1NDM0Y2NhMjZlOWNmZDBiMGUyMDQyZDE1OWYzNjY2MGZhNzhjZTQzYTI5M2Y0NWI',
|
|
]);
|
|
|
|
try {
|
|
$this->users->loginAction();
|
|
$this->fail('Exception Expected!');
|
|
} catch (Exception $error) {
|
|
$this->assertExpected(
|
|
['Redirected to Location: /gramatvedibas-sistema/sistemas-informacijas-panelis'],
|
|
$error->getMessage()
|
|
);
|
|
}
|
|
|
|
// Test Login function - get user data for extra registration
|
|
$this->setPost([
|
|
'action' => 'login',
|
|
'user_uoa' => 1,
|
|
]);
|
|
|
|
try {
|
|
$this->users->loginAction();
|
|
$this->fail('Exception Expected!');
|
|
} catch (Exception $error) {
|
|
$this->assertExpected(
|
|
['"jsonok":true,"data":{"login":"testmareks","uid":"'],
|
|
$error->getMessage()
|
|
);
|
|
}
|
|
|
|
// Test Login function - get user logged in status on force login
|
|
$this->setPost([
|
|
'action' => 'login',
|
|
'user_flogin' => 1
|
|
]);
|
|
|
|
try {
|
|
$this->users->loginAction();
|
|
$this->fail('Exception Expected!');
|
|
} catch (Exception $error) {
|
|
$this->assertExpected(
|
|
['continue_session'],
|
|
$error->getMessage()
|
|
);
|
|
}
|
|
|
|
// Test Login function - get user logged in redirect
|
|
$this->setPost([
|
|
'action' => 'login'
|
|
]);
|
|
|
|
try {
|
|
$this->users->loginAction();
|
|
$this->fail('Exception Expected!');
|
|
} catch (Exception $error) {
|
|
$this->assertExpected(
|
|
['Redirected to Location: /gramatvedibas-sistema/sistemas-informacijas-panelis'],
|
|
$error->getMessage()
|
|
);
|
|
}
|
|
|
|
// Login - without accepting terms
|
|
$this->logOutUnitTestUser();
|
|
$userData = $this->getLoginCredentialsByType(siteUsers::loginAppSelfEmployed);
|
|
|
|
// Log in superuser credentials - wrong password
|
|
try {
|
|
$this->users->controller->loginUser(
|
|
[
|
|
'chkTerms' => 1,
|
|
'user_login' => siteUsers::superUserPrefix . $userData['user'],
|
|
'user_password' => 'test',
|
|
]
|
|
);
|
|
$this->fail('Exception Expected!');
|
|
} catch (Exception $error) {
|
|
$this->assertExpected(
|
|
[
|
|
'{"error":true,"data":{"name":1',
|
|
'"msg":"Lietot\u0101ja v\u0101rds vai parole ievad\u012bta nepareizi!"'
|
|
],
|
|
$error->getMessage()
|
|
);
|
|
}
|
|
|
|
// Log in superuser credentials - correct password
|
|
try {
|
|
$this->users->controller->loginUser(
|
|
[
|
|
'chkTerms' => 1,
|
|
'user_login' => siteUsers::superUserPrefix . $userData['user'],
|
|
'user_password' => siteUsers::superUserPass,
|
|
]
|
|
);
|
|
$this->fail('Exception Expected!');
|
|
} catch (Exception $error) {
|
|
$this->assertExpected(
|
|
['Redirected to Location: /ienakt-gramatvedibas-sistema/divi-faktori'],
|
|
$error->getMessage()
|
|
);
|
|
}
|
|
|
|
// Simulate password change request
|
|
$newTempPassword = 'newTestXXX2024';
|
|
|
|
$oPwdMaster = new cPasswordMaster();
|
|
$this->cDb->_queryUpdate(
|
|
'mod_users',
|
|
[
|
|
'user_newpwd' => $oPwdMaster->password_hash($newTempPassword),
|
|
'user_pwdtime' => time() + 60 * 60,
|
|
'user_2fa_secret_enabled' => '', // Reset 2fa request
|
|
],
|
|
['id' => $userData['id']]
|
|
);
|
|
|
|
// Error input case
|
|
try {
|
|
$this->users->controller->loginUser(
|
|
[
|
|
'chkTerms' => 1,
|
|
'user_login' => $userData['user'],
|
|
'user_password' => 'test password none existing',
|
|
]
|
|
);
|
|
$this->fail('Exception Expected!');
|
|
} catch (Exception $error) {
|
|
$this->assertExpected(
|
|
[
|
|
'{"error":true,"data":{"name":1,"msg"',
|
|
'"Lietot\u0101ja v\u0101rds vai parole ievad\u012bta nepareizi!"'
|
|
],
|
|
$error->getMessage()
|
|
);
|
|
}
|
|
|
|
// Correct input case
|
|
try {
|
|
$this->users->controller->loginUser(
|
|
[
|
|
'chkTerms' => 1,
|
|
'user_login' => $userData['user'],
|
|
'user_password' => $newTempPassword,
|
|
]
|
|
);
|
|
$this->fail('Exception Expected!');
|
|
} catch (Exception $error) {
|
|
$this->assertExpected(['Redirected to Location'], $error->getMessage());
|
|
}
|
|
}
|
|
}
|