Skip to content

Commit ff423c6

Browse files
authored
Merge pull request #5 from BorisovskiP/disable-tfa-mftf
Disable 2FA for MFTF
2 parents 120490d + 1810d9a commit ff423c6

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
4+
<actionGroup name="AdminLoginActionGroup">
5+
<helper class="MarkShust\DisableTwoFactorAuth\Test\Mftf\Helper\SetSharedSecretOverride" method="execute" stepKey="setSharedSecret" before="clickLogin">
6+
<argument name="username">{{username}}</argument>
7+
</helper>
8+
<helper class="MarkShust\DisableTwoFactorAuth\Test\Mftf\Helper\FillOtpOverride" method="execute" stepKey="fillOtp" before="clickDontAllowButtonIfVisible">
9+
<argument name="tfaAuthCodeSelector">{{AdminGoogleTfaSection.tfaAuthCode}}</argument>
10+
<argument name="confirmSelector">{{AdminGoogleTfaSection.confirm}}</argument>
11+
<argument name="errorMessageSelector">{{AdminLoginMessagesSection.messageByType('error')}}</argument>
12+
</helper>
13+
</actionGroup>
14+
</actionGroups>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MarkShust\DisableTwoFactorAuth\Test\Mftf\Helper;
6+
7+
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
8+
use Magento\FunctionalTestingFramework\Helper\Helper;
9+
use Magento\FunctionalTestingFramework\Module\MagentoWebDriver;
10+
11+
class FillOtpOverride extends Helper
12+
{
13+
/**
14+
* Fill the OTP form if appropriate
15+
*
16+
* @param string $tfaAuthCodeSelector
17+
* @param string $confirmSelector
18+
* @param string $errorMessageSelector
19+
*/
20+
public function execute(string $tfaAuthCodeSelector, string $confirmSelector, string $errorMessageSelector): void
21+
{
22+
/** @var MagentoWebDriver $webDriver */
23+
$webDriver = $this->getModule('\\' . MagentoWebDriver::class);
24+
if (!$this->checkIfTwoFactorIsEnabled($webDriver)) {
25+
return;
26+
}
27+
try {
28+
$webDriver->seeElementInDOM($errorMessageSelector);
29+
// Login failed so don't handle 2fa
30+
} catch (\Exception $e) {
31+
$otp = $webDriver->getOTP();
32+
$webDriver->waitForPageLoad();
33+
$webDriver->waitForElementVisible($tfaAuthCodeSelector);
34+
$webDriver->fillField($tfaAuthCodeSelector, $otp);
35+
$webDriver->click($confirmSelector);
36+
$webDriver->waitForPageLoad();
37+
}
38+
}
39+
40+
/**
41+
* @param MagentoWebDriver $webDriver
42+
*/
43+
private function checkIfTwoFactorIsEnabled(MagentoWebDriver $webDriver): bool
44+
{
45+
try {
46+
return (bool)$webDriver->magentoCLI('config:show twofactorauth/general/enable');
47+
} catch (TestFrameworkException $exception) {
48+
49+
return false;
50+
}
51+
52+
}
53+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MarkShust\DisableTwoFactorAuth\Test\Mftf\Helper;
6+
7+
use Magento\FunctionalTestingFramework\DataGenerator\Handlers\CredentialStore;
8+
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
9+
use Magento\FunctionalTestingFramework\Helper\Helper;
10+
use Magento\FunctionalTestingFramework\Module\MagentoWebDriver;
11+
12+
class SetSharedSecretOverride extends Helper
13+
{
14+
/**
15+
* Set the shared secret if appropriate
16+
*
17+
* @param string $username
18+
*/
19+
/**
20+
* Set the shared secret if appropriate
21+
*
22+
* @param string $username
23+
*/
24+
public function execute(string $username): void
25+
{
26+
/** @var MagentoWebDriver $webDriver */
27+
$webDriver = $this->getModule('\\' . MagentoWebDriver::class);
28+
$credentialStore = CredentialStore::getInstance();
29+
if ($username !== getenv('MAGENTO_ADMIN_USERNAME')) {
30+
$sharedSecret = $credentialStore->decryptSecretValue(
31+
$credentialStore->getSecret('magento/tfa/OTP_SHARED_SECRET')
32+
);
33+
if (!$this->checkIfTwoFactorIsEnabled($webDriver)) {
34+
return;
35+
}
36+
try {
37+
$webDriver->magentoCLI(
38+
'security:tfa:google:set-secret ' . $username . ' ' . $sharedSecret
39+
);
40+
} catch (\Throwable $exception) {
41+
// Some tests intentionally use bad credentials.
42+
}
43+
}
44+
}
45+
46+
/**
47+
* @param MagentoWebDriver $webDriver
48+
*/
49+
private function checkIfTwoFactorIsEnabled(MagentoWebDriver $webDriver): bool
50+
{
51+
try {
52+
return (bool)$webDriver->magentoCLI('config:show twofactorauth/general/enable');
53+
} catch (TestFrameworkException $exception) {
54+
55+
return false;
56+
}
57+
58+
}
59+
}

0 commit comments

Comments
 (0)