maison_alexandra/tests/UnitTests/App/PubUsers/passwordRecoveryActionTest.php

281 lines
8.7 KiB
PHP

<?php
namespace ElixirAudit\Tests\UnitTests\App\PubUsers;
use DOMDocument;
use DOMXPath;
use Exception;
use BRSystems\Core\App\pubUsers\pubUsers;
use ElixirAudit\Tests\BasePhpUnitCase;
use ElixirAudit\Tests\DataSetup\Constants\siteLanguages;
use ElixirAudit\Tests\DataSetup\Constants\SiteMap;
use ElixirAudit\Tests\SeleniumTests\App\PubUsers\pubUserPwdResetTest;
class passwordRecoveryActionTest extends BasePhpUnitCase
{
private pubUsers $users;
private pubUsers $pubUserLogin;
protected function setUp(): void
{
// Init base structure
$this->unitTestsAreRunning = true;
$this->resetGetAndPost();
parent::setUp();
$this->clearDbState();
// Create new class
$this->users = $this->initBaseClassObjectBySiteMapId(SiteMap::PUB_USER_PASS_RECOVERY, null);
}
protected function tearDown(): void
{
$this->clearDbState();
parent::tearDown();
}
protected function clearDbState(): void
{
$this->clearMailHogMessages();
$this->reloadDbTables(['mod_users']);
}
public function testPasswordRecoveryActionDefaultAction(): void
{
// Default form generation
ob_start();
$this->users->passwordRecoveryAction();
$pageHTML = ob_get_contents();
ob_end_clean();
$this->assertExpected(
[
'<title>Zutenis | Paroles atgūšana</title>',
'>Paroles atgūšana</p>',
'<p>Lūdzu ievadiet lietotāja e-pastu vai lietotājvārdu, kuram vēlieties atgūt paroli.</p>',
'<form id="passRecovery_cnt" class="ajax_loader">',
'<input name="user_data" id="user_data" data-validator="user_data" type="text" class="form-control',
'<button type="recovery_btn" class="btn btn-block',
],
$pageHTML
);
}
public function testPasswordRecoveryActionWithPostTestCase1(): void
{
$this->setPost([
'action' => 'recovery',
]);
try {
$this->users->passwordRecoveryAction();
$this->fail('Exception Expected!');
} catch (Exception $error) {
$this->assertExpected(
['{"error":true,"msg":"no email entered"}'],
$error->getMessage()
);
}
}
public function testPasswordRecoveryActionWithPostTestCase2(): void
{
$this->setPost([
'action' => 'recovery',
'user_data' => 'asdasd',
]);
try {
$this->users->passwordRecoveryAction();
$this->fail('Exception Expected!');
} catch (Exception $error) {
$this->assertExpected(
['{"error":true,"msg":"Lietot\u0101js nav atrasts vai ir blo\u0137\u0113ts!"}'],
$error->getMessage()
);
}
}
public function testPasswordRecoveryActionWithPostTestCase3(): void
{
$this->setPost([
'action' => 'recovery',
'user_data' => 'testmareks'
]);
try {
$this->users->passwordRecoveryAction();
$this->fail('Exception Expected!');
} catch (Exception $error) {
$this->assertExpected(
['{"jsonok":true,"msg":"E-pasts tika nos\u016bt\u012bts"}'],
$error->getMessage()
);
}
// Wait for mail to be sent - otherwise it messes with the secondary mail sent
// in call function testPasswordRecoveryActionWithLinkFromEmail - which gets wrong mail for process
$this->awaitEmailFromMailHog(
'keiss.mareks@gmail.com',
'Konts - Paroles atjaunošana',
60 * 5,
true
);
}
public function testPasswordRecoveryActionWithPostTestCase4(): void
{
// Set double email link
$this->cDb->_queryUpdate('mod_users', ['user_email' => 'keiss.mareks@gmail.com'], ['id' => 14]);
$this->setPost([
'action' => 'recovery',
'user_data' => 'keiss.mareks@gmail.com'
]);
try {
$this->users->passwordRecoveryAction();
$this->fail('Exception Expected!');
} catch (Exception $error) {
$this->assertExpected(
['{"error":true,"msg":"There are more then one user registered to this email,'],
$error->getMessage()
);
}
}
public function testPasswordRecoveryActionWithLinkFromEmail(): void
{
// Force user logout if active login pending
$this->logOutUnitTestUser();
$this->setPost([
'action' => 'recovery',
'user_data' => 'testmareks'
]);
try {
$this->users->passwordRecoveryAction();
$this->fail('Exception Expected!');
} catch (Exception $error) {
$this->assertExpected(
['{"jsonok":true,"msg":"E-pasts tika nos\u016bt\u012bts"}'],
$error->getMessage()
);
}
// Destroy session
$this->destroyUserSession();
$this->resetGetAndPost();
// Fetch email from MailHog
$messages = $this->awaitEmailFromMailHog(
'keiss.mareks@gmail.com',
'Konts - Paroles atjaunošana',
60 * 5,
true
);
// Validate mail content
$testData = pubUserPwdResetTest::url[siteLanguages::LANG_LV];
$this->assertEquals($testData['email-subject'], $this->emailHtmlDecode($messages->subject));
$this->assertExpected($testData['email-expected-body'], $this->emailHtmlDecode($messages->body));
// Get password reset url from mail
$doc = new DOMDocument();
$doc->loadHTML($messages->body);
$xpath = new DOMXpath($doc);
$activationUrl = $xpath->query("//a[contains(., '{$testData['email-a']}')]")
->item(0)->attributes->item(0)->nodeValue;
$urlData = explode('/pwd:', $activationUrl);
$this->setGet([
'pwd' => $urlData[1]
]);
try {
// Catch output - flaky test, fails from time to time
ob_start();
$this->users->loginAction();
$pageHTML = ob_get_contents();
ob_end_clean();
$this->fail('Output generated! Exception Expected!');
} catch (Exception $error) {
ob_end_clean();
$this->assertExpected(
['Redirected to Location: /ienakt-gramatvedibas-sistema/divi-faktori'],
$error->getMessage()
);
}
}
public function testPasswordRecoveryActionWithLinkFromEmailErrorCase(): void
{
// Force user logout if active login pending
$this->logOutUnitTestUser();
$this->setPost([
'action' => 'recovery',
'user_data' => 'testmareks'
]);
try {
$this->users->passwordRecoveryAction();
$this->fail('Exception Expected!');
} catch (Exception $error) {
$this->assertExpected(
['{"jsonok":true,"msg":"E-pasts tika nos\u016bt\u012bts"}'],
$error->getMessage()
);
}
// Set double email link
$this->cDb->_queryUpdate('mod_users', ['user_pwdtime' => time() - 100], ['id' => 12]);
// Fetch email from MailHog
$messages = $this->awaitEmailFromMailHog(
'keiss.mareks@gmail.com',
'Konts - Paroles atjaunošana',
60 * 5,
true
);
// Validate mail content
$testData = pubUserPwdResetTest::url[siteLanguages::LANG_LV];
$this->assertEquals($testData['email-subject'], $this->emailHtmlDecode($messages->subject));
$this->assertExpected($testData['email-expected-body'], $this->emailHtmlDecode($messages->body));
// Get password reset url from mail
$doc = new DOMDocument();
$doc->loadHTML($messages->body);
$xpath = new DOMXpath($doc);
$activationUrl = $xpath->query("//a[contains(., '{$testData['email-a']}')]")
->item(0)->attributes->item(0)->nodeValue;
$this->resetGetAndPost();
$urlData = explode('/pwd:', $activationUrl);
$this->setGet([
'pwd' => $urlData[1]
]);
ob_start();
$this->users->loginAction();
$pageHTML = ob_get_contents();
ob_end_clean();
$this->assertExpected(
[
'<div class="status_error">Lietotāja paroles atgūšana neizdevās!',
'<br/>Lūdzu, mēģiniet vēlreiz atiestatīt paroli,',
'<br/>ja ziņojums atkārtojas, lūdzu, sazinieties ar atbalsta dienestu!</div>',
],
$pageHTML
);
}
}