Skip to content

Commit a036cc7

Browse files
committed
MC-30537: Test automation with the new 2FA enabled by default
- MFTF fixes
1 parent 3f82697 commit a036cc7

17 files changed

+268
-6
lines changed

TwoFactorAuth/Controller/Adminhtml/Duo/Configure.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Configure extends AbstractAction implements HttpGetActionInterface
1818
/**
1919
* @see _isAllowed()
2020
*/
21-
const ADMIN_RESOURCE = 'Magento_TwoFactorAuth::config';
21+
const ADMIN_RESOURCE = 'Magento_TwoFactorAuth::tfa';
2222

2323
/**
2424
* @inheritdoc

TwoFactorAuth/Observer/ControllerActionPredispatch.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public function execute(Observer $observer)
122122
/** @var $controllerAction AbstractAction */
123123
$controllerAction = $observer->getEvent()->getData('controller_action');
124124
$this->action = $controllerAction;
125-
$fullActionName = $controllerAction->getRequest()->getFullActionName();
125+
$fullActionName = $observer->getEvent()->getData('request')->getFullActionName();
126126
$userId = $this->userContext->getUserId();
127127

128128
$this->tokenManager->readConfigToken();
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\TwoFactorAuth\Plugin;
10+
11+
use Magento\Backend\Model\Url;
12+
13+
/**
14+
* Redirect to the correct first available item
15+
*/
16+
class FirstAvailableMenu
17+
{
18+
/**
19+
* Fix the default admin item for a tfa corner case where the default would be incorrect due to 2fa
20+
*
21+
* @param Url $subject
22+
* @param string|null $result
23+
* @return string|null
24+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
25+
*/
26+
public function afterFindFirstAvailableMenu(Url $subject, ?string $result): ?string
27+
{
28+
if ($result === '*/denied') {
29+
return 'admin/denied';
30+
}
31+
32+
return $result;
33+
}
34+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<overrides xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="$LOCAL_SCHEMA$"
9+
xsi:noNamespaceSchemaLocation="../dev/tests/integration/framework/Magento/TestFramework/Workaround/etc/overrides.xsd">
10+
11+
</overrides>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
10+
<actionGroup name="AdminCreateRoleActionGroup">
11+
<waitForElementVisible stepKey="waitForTfaRole" selector="{{AdminEditRoleInfoSection.checkboxByRole('Magento_TwoFactorAuth::tfa')}}" time="30" after="clickContentBlockCheckbox"/>
12+
<click stepKey="clickCheckbox" selector="{{AdminEditRoleInfoSection.checkboxByRole('Magento_TwoFactorAuth::tfa')}}" after="waitForTfaRole"/>
13+
</actionGroup>
14+
</actionGroups>
15+
16+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
10+
<actionGroup name="AdminFillUserRoleRequiredDataActionGroup">
11+
<remove keyForRemoval="waitForTfaRole"/>
12+
<remove keyForRemoval="clickCheckbox"/>
13+
</actionGroup>
14+
</actionGroups>
15+
16+

TwoFactorAuth/Test/Mftf/ActionGroup/AdminLoginActionGroup.xml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@
88
<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
99
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
1010
<actionGroup name="AdminLoginActionGroup">
11-
<getOTP stepKey="getOtp" before="clickDontAllowButtonIfVisible"/>
12-
<fillField selector="{{AdminGoogleTfaSection.tfaAuthCode}}" userInput="{$getOtp}" stepKey="fillOtp" after="getOtp"/>
13-
<click selector="{{AdminGoogleTfaSection.confirm}}" stepKey="confirmOtp" after="fillOtp"/>
11+
<helper class="\Magento\TwoFactorAuth\Test\Mftf\Helper\SetSharedSecret" method="execute" stepKey="setSharedSecret" before="clickLogin">
12+
<argument name="username">{{username}}</argument>
13+
</helper>
14+
<helper class="\Magento\TwoFactorAuth\Test\Mftf\Helper\FillOtp" method="execute" stepKey="fillOtp" before="clickDontAllowButtonIfVisible">
15+
<argument name="tfaAuthCodeSelector">{{AdminGoogleTfaSection.tfaAuthCode}}</argument>
16+
<argument name="confirmSelector">{{AdminGoogleTfaSection.confirm}}</argument>
17+
<argument name="errorMessageSelector">{{AdminLoginMessagesSection.messageByType('error')}}</argument>
18+
<argument name="username">{{username}}</argument>
19+
</helper>
1420
</actionGroup>
1521
</actionGroups>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
10+
<actionGroup name="CreateInvoice">
11+
<helper class="\Magento\TwoFactorAuth\Test\Mftf\Helper\FillOtp" method="execute" stepKey="fillOtp" before="closeAdminNotification">
12+
<argument name="tfaAuthCodeSelector">{{AdminGoogleTfaSection.tfaAuthCode}}</argument>
13+
<argument name="confirmSelector">{{AdminGoogleTfaSection.confirm}}</argument>
14+
<argument name="errorMessageSelector">{{AdminLoginMessagesSection.messageByType('error')}}</argument>
15+
<argument name="username">{{username}}</argument>
16+
</helper>
17+
</actionGroup>
18+
</actionGroups>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\TwoFactorAuth\Test\Mftf\Helper;
10+
11+
use Magento\FunctionalTestingFramework\Helper\Helper;
12+
use Magento\FunctionalTestingFramework\Module\MagentoWebDriver;
13+
14+
/**
15+
* Fills in the OTP information
16+
*/
17+
class FillOtp extends Helper
18+
{
19+
/**
20+
* Fill the OTP form if appropriate
21+
*
22+
* @param string $tfaAuthCodeSelector
23+
* @param string $confirmSelector
24+
* @param string $errorMessageSelector
25+
*/
26+
public function execute(string $tfaAuthCodeSelector, string $confirmSelector, string $errorMessageSelector): void {
27+
/** @var MagentoWebDriver $webDriver */
28+
$webDriver = $this->getModule('\\' . MagentoWebDriver::class);
29+
try {
30+
$webDriver->seeElementInDOM($errorMessageSelector);
31+
// Login failed so don't handle 2fa
32+
} catch (\Exception $e) {
33+
$otp = $webDriver->getOTP();
34+
$webDriver->fillField($tfaAuthCodeSelector, $otp);
35+
$webDriver->click($confirmSelector);
36+
$webDriver->waitForPageLoad();
37+
}
38+
}
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\TwoFactorAuth\Test\Mftf\Helper;
10+
11+
use Magento\FunctionalTestingFramework\DataGenerator\Handlers\CredentialStore;
12+
use Magento\FunctionalTestingFramework\Helper\Helper;
13+
use Magento\FunctionalTestingFramework\Module\MagentoWebDriver;
14+
15+
/**
16+
* Set the shared secret for OTP generation when needed
17+
*/
18+
class SetSharedSecret extends Helper
19+
{
20+
/**
21+
* Set the shared secret if appropriate
22+
*
23+
* @param string $username
24+
*/
25+
public function execute(string $username): void
26+
{
27+
/** @var MagentoWebDriver $webDriver */
28+
$webDriver = $this->getModule('\\' . MagentoWebDriver::class);
29+
$credentialStore = CredentialStore::getInstance();
30+
if ($username !== getenv('MAGENTO_ADMIN_USERNAME')) {
31+
$sharedSecret = $credentialStore->decryptSecretValue(
32+
$credentialStore->getSecret('magento/tfa/OTP_SHARED_SECRET')
33+
);
34+
$webDriver->magentoCLI(
35+
'security:tfa:google:set-secret ' . $username .' ' . $sharedSecret
36+
);
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)