7
7
8
8
namespace Magento \User \Controller \Adminhtml ;
9
9
10
+ use Magento \Framework \App \Config \Storage \WriterInterface ;
10
11
use Magento \Framework \Exception \LocalizedException ;
11
12
use Magento \Framework \Mail \EmailMessage ;
12
13
use Magento \Store \Model \Store ;
13
14
use Magento \TestFramework \Fixture \Config as Config ;
14
15
use Magento \TestFramework \Fixture \DataFixture ;
15
16
use Magento \TestFramework \Fixture \DataFixtureStorage ;
16
17
use Magento \TestFramework \Fixture \DataFixtureStorageManager ;
18
+ use Magento \TestFramework \Fixture \DbIsolation ;
19
+ use Magento \TestFramework \Helper \Bootstrap ;
17
20
use Magento \TestFramework \Mail \Template \TransportBuilderMock ;
18
21
use Magento \TestFramework \TestCase \AbstractBackendController ;
19
22
use Magento \User \Model \User as UserModel ;
23
+ use Magento \User \Model \UserFactory ;
20
24
use Magento \User \Test \Fixture \User as UserDataFixture ;
25
+ use Magento \Framework \App \ResourceConnection ;
21
26
22
27
/**
23
28
* Test class for user reset password email
24
- *
29
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
25
30
* @magentoAppArea adminhtml
26
31
*/
27
32
class UserResetPasswordEmailTest extends AbstractBackendController
@@ -36,6 +41,21 @@ class UserResetPasswordEmailTest extends AbstractBackendController
36
41
*/
37
42
protected $ userModel ;
38
43
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
+
39
59
/**
40
60
* @throws LocalizedException
41
61
*/
@@ -44,6 +64,9 @@ protected function setUp(): void
44
64
parent ::setUp ();
45
65
$ this ->fixtures = DataFixtureStorageManager::getStorage ();
46
66
$ 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);
47
70
}
48
71
49
72
#[
@@ -74,4 +97,84 @@ private function getResetPasswordUri(EmailMessage $message): string
74
97
$ urlString = trim ($ match [0 ][0 ], $ store ->getBaseUrl ('web ' ));
75
98
return substr ($ urlString , 0 , strpos ($ urlString , "/key " ));
76
99
}
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
+ }
77
180
}
0 commit comments