Skip to content

Commit 44da73b

Browse files
akaashakaash
authored andcommitted
ACQE-4975 | Limit number of reset requests per hour by Email
1 parent 98fb0dc commit 44da73b

File tree

1 file changed

+104
-1
lines changed

1 file changed

+104
-1
lines changed

dev/tests/integration/testsuite/Magento/User/Controller/Adminhtml/UserResetPasswordEmailTest.php

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,26 @@
77

88
namespace Magento\User\Controller\Adminhtml;
99

10+
use Magento\Framework\App\Config\Storage\WriterInterface;
1011
use Magento\Framework\Exception\LocalizedException;
1112
use Magento\Framework\Mail\EmailMessage;
1213
use Magento\Store\Model\Store;
1314
use Magento\TestFramework\Fixture\Config as Config;
1415
use Magento\TestFramework\Fixture\DataFixture;
1516
use Magento\TestFramework\Fixture\DataFixtureStorage;
1617
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
18+
use Magento\TestFramework\Fixture\DbIsolation;
19+
use Magento\TestFramework\Helper\Bootstrap;
1720
use Magento\TestFramework\Mail\Template\TransportBuilderMock;
1821
use Magento\TestFramework\TestCase\AbstractBackendController;
1922
use Magento\User\Model\User as UserModel;
23+
use Magento\User\Model\UserFactory;
2024
use Magento\User\Test\Fixture\User as UserDataFixture;
25+
use Magento\Framework\App\ResourceConnection;
2126

2227
/**
2328
* Test class for user reset password email
24-
*
29+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2530
* @magentoAppArea adminhtml
2631
*/
2732
class UserResetPasswordEmailTest extends AbstractBackendController
@@ -36,6 +41,21 @@ class UserResetPasswordEmailTest extends AbstractBackendController
3641
*/
3742
protected $userModel;
3843

44+
/**
45+
* @var UserFactory
46+
*/
47+
private $userFactory;
48+
49+
/**
50+
* @var WriterInterface
51+
*/
52+
private $configWriter;
53+
54+
/**
55+
* @var ResourceConnection
56+
*/
57+
private $resourceConnection;
58+
3959
/**
4060
* @throws LocalizedException
4161
*/
@@ -44,6 +64,9 @@ protected function setUp(): void
4464
parent::setUp();
4565
$this->fixtures = DataFixtureStorageManager::getStorage();
4666
$this->userModel = $this->_objectManager->create(UserModel::class);
67+
$this->userFactory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(UserFactory::class);
68+
$this->configWriter = $this->_objectManager->get(WriterInterface::class);
69+
$this->resourceConnection = $this->_objectManager->get(ResourceConnection::class);
4770
}
4871

4972
#[
@@ -74,4 +97,84 @@ private function getResetPasswordUri(EmailMessage $message): string
7497
$urlString = trim($match[0][0], $store->getBaseUrl('web'));
7598
return substr($urlString, 0, strpos($urlString, "/key"));
7699
}
100+
101+
/**
102+
* @return void
103+
* @throws LocalizedException
104+
*/
105+
#[
106+
DbIsolation(false),
107+
DataFixture(UserDataFixture::class, ['role_id' => 1], 'user')
108+
]
109+
public function testLimitNumberOfResetRequestPerHourByEmail(): void
110+
{
111+
// Load admin user
112+
$user = $this->fixtures->get('user');
113+
$username = $user->getDataByKey('username');
114+
$adminEmail = $user->getDataByKey('email');
115+
116+
// login admin
117+
$adminUser = $this->userFactory->create();
118+
$adminUser->login($username, \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD);
119+
120+
// Setting Password Reset Protection Type to By Email
121+
$this->configWriter->save('admin/security/password_reset_protection_type', 3);
122+
123+
// Setting Max Number of Password Reset Requests 0
124+
$this->configWriter->save('admin/security/max_number_password_reset_requests', 0);
125+
126+
// Setting Min Time Between Password Reset Requests 0
127+
$this->configWriter->save('admin/security/min_time_between_password_reset_requests', 0);
128+
129+
// Resetting Password
130+
$this->getRequest()->setPostValue('email', $adminEmail);
131+
$this->dispatch('backend/admin/auth/forgotpassword');
132+
133+
/** @var TransportBuilderMock $transportMock */
134+
$transportMock = Bootstrap::getObjectManager()->get(
135+
TransportBuilderMock::class
136+
);
137+
$sendMessage = $transportMock->getSentMessage()->getBody()->getParts()[0]->getRawContent();
138+
139+
$this->assertStringContainsString(
140+
'There was recently a request to change the password for your account',
141+
$sendMessage
142+
);
143+
144+
$this->assertSessionMessages(
145+
$this->equalTo([]),
146+
MessageInterface::TYPE_ERROR
147+
);
148+
149+
// Setting Max Number of Password Reset Requests greater than 0
150+
$this->configWriter->save('admin/security/max_number_password_reset_requests', 3);
151+
152+
// Resetting password multiple times
153+
for ($i = 0; $i < 1; $i++) {
154+
$this->getRequest()->setPostValue('email', $adminEmail);
155+
$this->dispatch('backend/admin/auth/forgotpassword');
156+
157+
$this->assertSessionMessages(
158+
$this->equalTo(
159+
['We received too many requests for password resets.'
160+
. ' Please wait and try again later or contact [email protected].']
161+
),
162+
MessageInterface::TYPE_ERROR
163+
);
164+
}
165+
166+
$connection = $this->resourceConnection->getConnection();
167+
$tableName = $this->resourceConnection->getTableName('password_reset_request_event');
168+
169+
$connection->truncateTable($tableName);
170+
171+
$this->assertEquals(0, $connection->fetchOne("SELECT COUNT(*) FROM $tableName"));
172+
173+
$sendMessage = $transportMock->getSentMessage()->getBody()->getParts()[0]->getRawContent();
174+
175+
$this->assertStringContainsString(
176+
'There was recently a request to change the password for your account',
177+
$sendMessage
178+
);
179+
}
77180
}

0 commit comments

Comments
 (0)