ElixirVault/tests/SeleniumTests/Sys/SysUsersLogin/sysUserLoginTest.php

173 lines
7.3 KiB
PHP

<?php
namespace ElixirAudit\Tests\SeleniumTests\Sys\SysUsersLogin;
// use BRSystems\Core\Library\CGoogle2FA\cGoogle2FABase;
use BRSystems\Core\Library\cGoogle2FA\cGoogle2FA;
use Facebook\WebDriver\WebDriverBy;
use ElixirAudit\Tests\BasePhpUnitSeleniumCase;
use ElixirAudit\Tests\DataSetup\Constants\siteUsers;
class sysUserLoginTest extends BasePhpUnitSeleniumCase
{
const url = 'lslsadmin/login';
protected function tearDown(): void
{
$this->logoutAdmin();
parent::tearDown();
}
public function testSysLogin(): void
{
$loginData = $this->getLoginCredentialsByType();
$this->url(self::url);
// Login user till Two-factor authentication
self::logInUserByCredentials($this, $loginData);
// Check for logout - log out
$this->assertEquals('Back to login', $this->byCssSelector('#frm_2fa_input a')->getText());
$this->byCssSelector("#frm_2fa_input a")->click();
$this->elementWait('user_login');
$this->assertEquals('ACCOUNTING', $this->byCssSelector('#frm_input > h1')->getText());
}
public static function logInUserByCredentials(
BasePhpUnitSeleniumCase $activeTestCase,
array $loginData = null
): void {
$activeTestCase->elementWait('user_login');
$activeTestCase->assertEquals('ACCOUNTING', $activeTestCase->byCssSelector('#frm_input > h1')->getText());
// Input login data
$activeTestCase->byId("user_login")->sendKeys($loginData['user']);
$activeTestCase->byId("user_password")->sendKeys($loginData['password']);
$activeTestCase->byCssSelector("#frm_input button.btn")->click();
$activeTestCase->elementWait('frm_2fa_input');
$activeTestCase->assertEquals('Two-factor authentication', $activeTestCase->byCssSelector('#frm_2fa_input > h1')->getText());
}
public function testSysLoginErrorMessages(): void
{
$this->url(self::url);
// Login user till Two-factor authentication
$this->elementWait('user_login');
$this->assertEquals('ACCOUNTING', $this->byCssSelector('#frm_input > h1')->getText());
// Input login data
$this->byId("user_login")->sendKeys("");
$this->byId("user_password")->sendKeys("");
$this->byCssSelector("#frm_input button.btn")->click();
$this->webDriver->action()->moveToElement($this->byCssSelector('#user_login'))->perform();
$this->elementWait('div.validate.v-error #user_login + div.errMsg', self::CSS_SELECTOR);
$this->assertEquals('Please fill in the field!', $this->byCssSelector('#user_login + div.errMsg')->getText());
$this->webDriver->action()->moveToElement($this->byCssSelector('#user_password'))->perform();
$this->elementWait('div.validate.v-error #user_password + div.errMsg', self::CSS_SELECTOR);
$this->assertEquals('Please fill in the field!', $this->byCssSelector('#user_password + div.errMsg')->getText());
// Input data into fields
$this->byId("user_login")->sendKeys("test");
$this->webDriver->executeScript('$("#user_login").focus().blur();');
$this->elementWait('div.validate.v-ok #user_login', self::CSS_SELECTOR);
$xPath = '//input[@id="user_login"]/ancestor::div[contains(@class,"v-ok")]';
$this->assertTrue($this->elementExists($xPath, self::XPATH));
$this->byId("user_password")->sendKeys("test");
$this->webDriver->executeScript('$("#user_password").focus().blur();');
$xPath = '//input[@id="user_password"]/ancestor::div[contains(@class,"v-ok")]';
$this->assertTrue($this->elementExists($xPath, self::XPATH));
// Check error login
$this->byCssSelector("#frm_input button.btn")->click();
$this->waitUntilDomReadyState();
$this->webDriver->action()->moveToElement($this->byCssSelector('#user_login'))->perform();
$this->elementWait('div.validate.v-error #user_login + div.errMsg', self::CSS_SELECTOR);
$this->assertEquals('The username or password is incorrect!', $this->byCssSelector('#user_login + div.errMsg')->getText());
}
public function testSysLogin2FactorAuthentication(): void
{
$loginData = $this->getLoginCredentialsByType();
$this->url(self::url);
// Login user till Two-factor authentication
self::logInUserByCredentials($this, $loginData);
$fieldsToCheck = [
'input.code2fa[tabindex=\'1\']', 'input.code2fa[tabindex=\'2\']', 'input.code2fa[tabindex=\'3\']',
'input.code2fa[tabindex=\'4\']', 'input.code2fa[tabindex=\'5\']', 'input.code2fa[tabindex=\'6\']',
'button[type=submit]', '#frm_2fa_input a'
];
// Test if 2FA page loaded correctly
foreach ($fieldsToCheck as $selectorToFind) {
$this->assertTrue(
$this->elementExists($selectorToFind, self::CSS_SELECTOR),
"Could not find element by selector -> {$selectorToFind}"
);
}
// Check validation
$i = 1;
$inputs = $this->webDriver->findElements(WebDriverBy::cssSelector('#frm_2fa_input input.code2fa'));
foreach ($inputs as $element) {
$this->webDriver->action()->moveToElement($this->byCssSelector("input.code2fa[tabindex='" . $i . "']"))->perform();
$this->webDriver->executeScript('$("input.code2fa:focus").blur(); $("input.code2fa[tabindex=\'' . $i . '\']").focus().blur().focus();');
$this->elementWait("input.code2fa[tabindex='" . $i . "'] + div.errMsg", self::CSS_SELECTOR);
$this->assertEquals(
'Please fill in the field!',
$this->byCssSelector("input.code2fa[tabindex='" . $i . "'] + div.errMsg")->getText(),
"2FA code for 2FA not found by input index {$i}"
);
$i++;
}
// Send random code
foreach ($inputs as $element) {
$element->sendKeys('1');
}
$this->elementWait('p.msg-error', self::CSS_SELECTOR);
$this->assertEquals(
'2FA Login input code error!',
$this->byCssSelector('p.msg-error')->getText()
);
// Validate actual 2fa code from google
$oGoogle2FA = new cGoogle2FA();
$next2FACode = $oGoogle2FA->getGoogleAuthCode($loginData['2fa']);
foreach (str_split($next2FACode) as $i => $char) {
$this->byCssSelector("input.code2fa[tabindex='" . ++$i . "']")->clear()->sendKeys($char);
}
// Validate dashboard welcome messages
$this->elementWait("#dashboard", self::CSS_SELECTOR);
$this->assertStringContainsString(
mb_strtoupper('Dashboard'),
mb_strtoupper($this->byCssSelector('h2.title')->getText())
);
// Check for logout - log out
$this->webDriver->action()->moveToElement($this->byCssSelector('.menu-cnt .settbox li.sett.last a'))->perform();
$this->elementWait('div.settbox_cnt.sub-menu ul', self::CSS_SELECTOR);
$elementXpath = '//div[contains(@class,"settbox_cnt")]/ul/li/a/div[@class="ico-logout"]/..';
$this->webDriver->action()->moveToElement($this->byXPath($elementXpath))->perform();
$this->byXPath($elementXpath)->click();
$this->elementWait('user_login');
$this->assertEquals('ACCOUNTING', $this->byCssSelector('#frm_input > h1')->getText());
}
}