|
23 | 23 | class TwoFAAuthenticationCest extends BaseCest |
24 | 24 | { |
25 | 25 | private string $otpInput = '#auth_code'; |
26 | | - private string $otpSubmitBtn = '#auth_submit'; |
27 | | - private string $otpResendBtn = 'RESEND_CODE'; |
| 26 | + private string $twofaCheckbox = '#twofa_enabled'; |
28 | 27 |
|
29 | 28 | public function _before(AcceptanceTester $I): void |
30 | 29 | { |
31 | 30 | $this->setCaptchaState(false); |
32 | 31 | $this->setPasswordState(false); |
33 | 32 | $this->setTwoFactorAuthState(true); |
| 33 | + $this->setUserTwoFAState($I, false); |
34 | 34 | } |
35 | 35 |
|
36 | | - public function testRedirectToOTPAfterLogin(AcceptanceTester $I): void |
| 36 | + public function testEnablingTwoFAViaSettingsTriggersOtpOnNextLogin(AcceptanceTester $I): void |
37 | 37 | { |
38 | 38 | $userData = $this->getExistingUserData(); |
| 39 | + $userLoginPage = new UserLogin($I); |
| 40 | + |
| 41 | + $I->amOnPage($userLoginPage->URL); |
| 42 | + $userAccountPage = $userLoginPage->login($userData['userLoginName'], $userData['userPassword']); |
| 43 | + $I->waitForPageLoad(); |
| 44 | + |
| 45 | + $I->amOnPage('?cl=account_security'); |
| 46 | + $I->waitForPageLoad(); |
| 47 | + $I->checkOption($this->twofaCheckbox); |
| 48 | + $I->click(Translator::translate('SAVE')); |
| 49 | + $I->waitForPageLoad(); |
| 50 | + |
| 51 | + $userAccountPage->logoutUserInAccountPage(); |
| 52 | + $I->waitForPageLoad(); |
| 53 | + |
| 54 | + $I->amOnPage($userLoginPage->URL); |
| 55 | + $userLoginPage->login($userData['userLoginName'], $userData['userPassword']); |
| 56 | + $I->waitForPageLoad(); |
39 | 57 |
|
| 58 | + $I->seeElement($this->otpInput); |
| 59 | + } |
| 60 | + |
| 61 | + public function testLoginOtpBehaviourChangesWithUserTwoFASetting(AcceptanceTester $I): void |
| 62 | + { |
| 63 | + $userData = $this->getExistingUserData(); |
40 | 64 | $userLoginPage = new UserLogin($I); |
| 65 | + |
| 66 | + $this->setUserTwoFAState($I, true); |
| 67 | + |
| 68 | + $I->amOnPage($userLoginPage->URL); |
| 69 | + $userLoginPage->login($userData['userLoginName'], $userData['userPassword']); |
| 70 | + $I->waitForPageLoad(); |
| 71 | + $I->seeElement($this->otpInput); |
| 72 | + |
| 73 | + $this->setUserTwoFAState($I, false); |
| 74 | + |
41 | 75 | $I->amOnPage($userLoginPage->URL); |
| 76 | + $userLoginPage->login($userData['userLoginName'], $userData['userPassword']); |
| 77 | + $I->waitForPageLoad(); |
| 78 | + $I->dontSeeElement($this->otpInput); |
| 79 | + } |
| 80 | + |
| 81 | + public function testSettingsPageReflectsTwoFAState(AcceptanceTester $I): void |
| 82 | + { |
| 83 | + $userData = $this->getExistingUserData(); |
| 84 | + $userLoginPage = new UserLogin($I); |
| 85 | + |
| 86 | + $I->amOnPage($userLoginPage->URL); |
| 87 | + $userLoginPage->login($userData['userLoginName'], $userData['userPassword']); |
| 88 | + $I->waitForPageLoad(); |
| 89 | + |
| 90 | + $I->amOnPage('?cl=account_security'); |
| 91 | + $I->waitForPageLoad(); |
| 92 | + $I->dontSeeCheckboxIsChecked($this->twofaCheckbox); |
| 93 | + |
| 94 | + $I->checkOption($this->twofaCheckbox); |
| 95 | + $I->click(Translator::translate('SAVE')); |
| 96 | + $I->waitForPageLoad(); |
| 97 | + |
| 98 | + $I->amOnPage('?cl=account_security'); |
| 99 | + $I->waitForPageLoad(); |
| 100 | + $I->seeCheckboxIsChecked($this->twofaCheckbox); |
| 101 | + |
| 102 | + $I->uncheckOption($this->twofaCheckbox); |
| 103 | + $I->click(Translator::translate('SAVE')); |
| 104 | + $I->waitForPageLoad(); |
| 105 | + |
| 106 | + $I->amOnPage('?cl=account_security'); |
| 107 | + $I->waitForPageLoad(); |
| 108 | + $I->dontSeeCheckboxIsChecked($this->twofaCheckbox); |
| 109 | + } |
| 110 | + |
| 111 | + public function testUnauthenticatedAccessToAccountSecurityRedirectsToLogin(AcceptanceTester $I): void |
| 112 | + { |
| 113 | + $I->amOnPage('?cl=account_security'); |
| 114 | + $I->waitForPageLoad(); |
| 115 | + |
42 | 116 | $I->see(Translator::translate('LOGIN')); |
| 117 | + $I->dontSeeElement($this->twofaCheckbox); |
| 118 | + } |
| 119 | + |
| 120 | + public function testShopLevelTwoFADisabledOverridesUserSetting(AcceptanceTester $I): void |
| 121 | + { |
| 122 | + $this->setUserTwoFAState($I, true); |
| 123 | + $this->setTwoFactorAuthState(false); |
| 124 | + |
| 125 | + $userData = $this->getExistingUserData(); |
| 126 | + $userLoginPage = new UserLogin($I); |
| 127 | + $I->amOnPage($userLoginPage->URL); |
| 128 | + $userLoginPage->login($userData['userLoginName'], $userData['userPassword']); |
| 129 | + $I->waitForPageLoad(); |
| 130 | + |
| 131 | + $I->dontSeeElement($this->otpInput); |
| 132 | + } |
| 133 | + |
| 134 | + public function testInvalidCodeShowsError(AcceptanceTester $I): void |
| 135 | + { |
| 136 | + $this->setUserTwoFAState($I, true); |
43 | 137 |
|
| 138 | + $userData = $this->getExistingUserData(); |
| 139 | + $userLoginPage = new UserLogin($I); |
| 140 | + $I->amOnPage($userLoginPage->URL); |
44 | 141 | $userLoginPage->login($userData['userLoginName'], $userData['userPassword']); |
| 142 | + $I->waitForPageLoad(); |
45 | 143 |
|
| 144 | + $I->fillField($this->otpInput, '000000'); |
| 145 | + $I->click('#auth_submit'); |
46 | 146 | $I->waitForPageLoad(); |
47 | | - $I->seeElement($this->otpInput); |
48 | | - $I->seeElement($this->otpSubmitBtn); |
49 | | - $I->see(Translator::translate($this->otpResendBtn)); |
| 147 | + |
| 148 | + $I->see(Translator::translate('ERROR_INVALID_CODE')); |
50 | 149 | } |
51 | 150 |
|
52 | | - public function testRedirectToOTPOnLoginBox(AcceptanceTester $I): void |
| 151 | + public function testWrongCodeDecreasesRemainingAttemptsAndShowsError(AcceptanceTester $I): void |
53 | 152 | { |
| 153 | + $this->setUserTwoFAState($I, true); |
54 | 154 |
|
55 | 155 | $userData = $this->getExistingUserData(); |
| 156 | + $userLoginPage = new UserLogin($I); |
| 157 | + $I->amOnPage($userLoginPage->URL); |
| 158 | + $userLoginPage->login($userData['userLoginName'], $userData['userPassword']); |
| 159 | + $I->waitForPageLoad(); |
56 | 160 |
|
57 | | - $homePage = $I->openShop(); |
58 | | - $accountMenu = $homePage->openAccountMenu(); |
59 | | - $I->waitForText(Translator::translate('FORGOT_PASSWORD')); |
60 | | - $I->retryFillField($accountMenu->userLoginName, $userData['userLoginName']); |
61 | | - $I->retryFillField($accountMenu->userLoginPassword, $userData['userPassword']); |
62 | | - $I->retryClick($accountMenu->userLoginButton); |
| 161 | + $attemptsBefore = (int) $I->grabTextFrom('#remaining-attempts'); |
63 | 162 |
|
| 163 | + $I->fillField($this->otpInput, '000000'); |
| 164 | + $I->click('#auth_submit'); |
64 | 165 | $I->waitForPageLoad(); |
65 | | - $I->seeElement($this->otpInput); |
66 | | - $I->seeElement($this->otpSubmitBtn); |
67 | | - $I->see(Translator::translate($this->otpResendBtn)); |
| 166 | + |
| 167 | + $I->see(Translator::translate('ERROR_INVALID_CODE')); |
| 168 | + $I->see((string) ($attemptsBefore - 1), '#remaining-attempts'); |
| 169 | + } |
| 170 | + |
| 171 | + public function testResendButtonIsDisabledOnOtpPageLoad(AcceptanceTester $I): void |
| 172 | + { |
| 173 | + $this->setUserTwoFAState($I, true); |
| 174 | + |
| 175 | + $userData = $this->getExistingUserData(); |
| 176 | + $userLoginPage = new UserLogin($I); |
| 177 | + $I->amOnPage($userLoginPage->URL); |
| 178 | + $userLoginPage->login($userData['userLoginName'], $userData['userPassword']); |
| 179 | + $I->waitForPageLoad(); |
| 180 | + |
| 181 | + // A code was just sent at login, so the server-driven cooldown should disable the button |
| 182 | + $I->waitForJS("return document.getElementById('resend-btn').disabled === true", 5); |
| 183 | + $I->seeElement('#resend-btn[disabled]'); |
| 184 | + } |
| 185 | + |
| 186 | + public function testResendButtonShowsCountdownAfterClick(AcceptanceTester $I): void |
| 187 | + { |
| 188 | + $this->setUserTwoFAState($I, true); |
| 189 | + |
| 190 | + $userData = $this->getExistingUserData(); |
| 191 | + $userLoginPage = new UserLogin($I); |
| 192 | + $I->amOnPage($userLoginPage->URL); |
| 193 | + $userLoginPage->login($userData['userLoginName'], $userData['userPassword']); |
| 194 | + $I->waitForPageLoad(); |
| 195 | + |
| 196 | + // Backdate LAST_SENT_AT so the server reports zero remaining cooldown |
| 197 | + $I->updateInDatabase( |
| 198 | + 'oesm_2fa_otp', |
| 199 | + ['LAST_SENT_AT' => date('Y-m-d H:i:s', strtotime('-2 minutes'))], |
| 200 | + ['OXUSERID' => $userData['userId']] |
| 201 | + ); |
| 202 | + |
| 203 | + // Clear stored cooldown from localStorage so JS reads from the (now zero) server value |
| 204 | + $I->executeJS("localStorage.clear()"); |
| 205 | + $I->reloadPage(); |
| 206 | + $I->waitForPageLoad(); |
| 207 | + |
| 208 | + $I->seeElement('#resend-btn:not([disabled])'); |
| 209 | + |
| 210 | + $I->click('#resend-btn'); |
| 211 | + |
| 212 | + $I->waitForJS( |
| 213 | + "return document.getElementById('resend-btn').textContent.includes('Resend in')", |
| 214 | + 30 |
| 215 | + ); |
68 | 216 | } |
69 | 217 | } |
0 commit comments