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 ;
13
+ use Magento \Framework \Message \MessageInterface ;
12
14
use Magento \Store \Model \Store ;
13
15
use Magento \TestFramework \Fixture \Config as Config ;
14
16
use Magento \TestFramework \Fixture \DataFixture ;
15
17
use Magento \TestFramework \Fixture \DataFixtureStorage ;
16
18
use Magento \TestFramework \Fixture \DataFixtureStorageManager ;
19
+ use Magento \TestFramework \Fixture \DbIsolation ;
20
+ use Magento \TestFramework \Helper \Bootstrap ;
17
21
use Magento \TestFramework \Mail \Template \TransportBuilderMock ;
18
22
use Magento \TestFramework \TestCase \AbstractBackendController ;
19
23
use Magento \User \Model \User as UserModel ;
24
+ use Magento \User \Model \UserFactory ;
20
25
use Magento \User \Test \Fixture \User as UserDataFixture ;
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,16 @@ 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
+
39
54
/**
40
55
* @throws LocalizedException
41
56
*/
@@ -44,6 +59,8 @@ protected function setUp(): void
44
59
parent ::setUp ();
45
60
$ this ->fixtures = DataFixtureStorageManager::getStorage ();
46
61
$ this ->userModel = $ this ->_objectManager ->create (UserModel::class);
62
+ $ this ->userFactory = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->create (UserFactory::class);
63
+ $ this ->configWriter = $ this ->_objectManager ->get (WriterInterface::class);
47
64
}
48
65
49
66
#[
@@ -74,4 +91,75 @@ private function getResetPasswordUri(EmailMessage $message): string
74
91
$ urlString = trim ($ match [0 ][0 ], $ store ->getBaseUrl ('web ' ));
75
92
return substr ($ urlString , 0 , strpos ($ urlString , "/key " ));
76
93
}
94
+
95
+ /**
96
+ * @return void
97
+ * @throws LocalizedException
98
+ */
99
+ #[
100
+ DbIsolation(false ),
101
+ Config(
102
+ 'admin/security/min_time_between_password_reset_requests ' ,
103
+ '0 ' ,
104
+ 'store '
105
+ ),
106
+ DataFixture(UserDataFixture::class, ['role_id ' => 1 ], 'user ' )
107
+ ]
108
+ public function testEnablePasswordChangeFrequencyLimit (): void
109
+ {
110
+ // Load admin user
111
+ $ user = $ this ->fixtures ->get ('user ' );
112
+ $ username = $ user ->getDataByKey ('username ' );
113
+ $ adminEmail = $ user ->getDataByKey ('email ' );
114
+
115
+ // login admin
116
+ $ adminUser = $ this ->userFactory ->create ();
117
+ $ adminUser ->login ($ username , \Magento \TestFramework \Bootstrap::ADMIN_PASSWORD );
118
+
119
+ // Resetting password multiple times
120
+ for ($ i = 0 ; $ i < 5 ; $ i ++) {
121
+ $ this ->getRequest ()->setPostValue ('email ' , $ adminEmail );
122
+ $ this ->dispatch ('backend/admin/auth/forgotpassword ' );
123
+ }
124
+
125
+ /** @var TransportBuilderMock $transportMock */
126
+ $ transportMock = Bootstrap::getObjectManager ()->get (
127
+ TransportBuilderMock::class
128
+ );
129
+ $ sendMessage = $ transportMock ->getSentMessage ()->getBody ()->getParts ()[0 ]->getRawContent ();
130
+
131
+ $ this ->assertStringContainsString (
132
+ 'There was recently a request to change the password for your account ' ,
133
+ $ sendMessage
134
+ );
135
+
136
+ // Setting the limit to greater than 0
137
+ $ this ->configWriter ->save ('admin/security/min_time_between_password_reset_requests ' , 2 );
138
+
139
+ // Resetting password multiple times
140
+ for ($ i = 0 ; $ i < 5 ; $ i ++) {
141
+ $ this ->getRequest ()->setPostValue ('email ' , $ adminEmail );
142
+ $ this ->dispatch ('backend/admin/auth/forgotpassword ' );
143
+ }
144
+
145
+ $ this ->assertSessionMessages (
146
+ $ this ->equalTo (
147
+ ['We received too many requests for password resets. '
148
+ . ' Please wait and try again later or contact hello@example.com. ' ]
149
+ ),
150
+ MessageInterface::TYPE_ERROR
151
+ );
152
+
153
+ // Wait for 2 minutes before resetting password
154
+ sleep (120 );
155
+
156
+ $ this ->getRequest ()->setPostValue ('email ' , $ adminEmail );
157
+ $ this ->dispatch ('backend/admin/auth/forgotpassword ' );
158
+
159
+ $ sendMessage = $ transportMock ->getSentMessage ()->getBody ()->getParts ()[0 ]->getRawContent ();
160
+ $ this ->assertStringContainsString (
161
+ 'There was recently a request to change the password for your account ' ,
162
+ $ sendMessage
163
+ );
164
+ }
77
165
}
0 commit comments