|
18 | 18 | use Magento\Framework\Exception\NoSuchEntityException;
|
19 | 19 | use Magento\Framework\Intl\DateTimeFactory;
|
20 | 20 | use Magento\Framework\Math\Random;
|
| 21 | +use Magento\Framework\Exception\LocalizedException; |
21 | 22 | use Magento\Framework\Message\MessageInterface;
|
22 | 23 | use Magento\Framework\ObjectManagerInterface;
|
23 | 24 | use Magento\Framework\Serialize\Serializer\Json;
|
@@ -523,4 +524,72 @@ private function clearCookieMessagesList(): void
|
523 | 524 | $jsonSerializer->serialize([])
|
524 | 525 | );
|
525 | 526 | }
|
| 527 | + |
| 528 | + /** |
| 529 | + * Test to enable password change frequency limit for customer |
| 530 | + * |
| 531 | + * @magentoDbIsolation disabled |
| 532 | + * @magentoConfigFixture current_store customer/password/min_time_between_password_reset_requests 0 |
| 533 | + * @magentoConfigFixture current_store customer/captcha/enable 0 |
| 534 | + * @magentoDataFixture Magento/Customer/_files/customer.php |
| 535 | + * @return void |
| 536 | + * @throws LocalizedException |
| 537 | + */ |
| 538 | + public function testEnablePasswordChangeFrequencyLimitForCustomer(): void |
| 539 | + { |
| 540 | + $email = 'customer@example.com'; |
| 541 | + |
| 542 | + // Resetting password multiple times |
| 543 | + for ($i = 0; $i < 5; $i++) { |
| 544 | + $this->getRequest()->setPostValue(['email' => $email]); |
| 545 | + $this->getRequest()->setMethod(HttpRequest::METHOD_POST); |
| 546 | + $this->dispatch('customer/account/forgotPasswordPost'); |
| 547 | + } |
| 548 | + |
| 549 | + // Asserting mail received after forgot password |
| 550 | + $sendMessage = $this->transportBuilderMock->getSentMessage()->getBody()->getParts()[0]->getRawContent(); |
| 551 | + $this->assertStringContainsString( |
| 552 | + 'There was recently a request to change the password for your account', |
| 553 | + $sendMessage |
| 554 | + ); |
| 555 | + |
| 556 | + // Updating the limit to greater than 0 |
| 557 | + $this->resourceConfig->saveConfig( |
| 558 | + 'customer/password/min_time_between_password_reset_requests', |
| 559 | + 2, |
| 560 | + ScopeConfigInterface::SCOPE_TYPE_DEFAULT, |
| 561 | + 0 |
| 562 | + ); |
| 563 | + |
| 564 | + // Resetting password multiple times |
| 565 | + for ($i = 0; $i < 5; $i++) { |
| 566 | + $this->clearCookieMessagesList(); |
| 567 | + $this->getRequest()->setPostValue('email', $email); |
| 568 | + $this->dispatch('customer/account/forgotPasswordPost'); |
| 569 | + } |
| 570 | + |
| 571 | + // Asserting the error message |
| 572 | + $this->assertSessionMessages( |
| 573 | + $this->equalTo( |
| 574 | + ['We received too many requests for password resets.' |
| 575 | + . ' Please wait and try again later or contact hello@example.com.'] |
| 576 | + ), |
| 577 | + MessageInterface::TYPE_ERROR |
| 578 | + ); |
| 579 | + |
| 580 | + // Wait for 2 minutes before resetting password |
| 581 | + sleep(120); |
| 582 | + |
| 583 | + // Clicking on the forgot password link |
| 584 | + $this->getRequest()->setPostValue('email', $email); |
| 585 | + $this->dispatch('customer/account/forgotPasswordPost'); |
| 586 | + |
| 587 | + // Asserting mail received after forgot password |
| 588 | + $sendMessage = $this->transportBuilderMock->getSentMessage()->getBody()->getParts()[0]->getRawContent(); |
| 589 | + $this->assertStringContainsString( |
| 590 | + 'There was recently a request to change the password for your account', |
| 591 | + $sendMessage |
| 592 | + ); |
| 593 | + } |
| 594 | + |
526 | 595 | }
|
0 commit comments