1919use OxidEsales \EshopCommunity \Core \Di \ContainerFacade ;
2020// phpcs:ignore Generic.Files.LineLength
2121use OxidEsales \SecurityModule \Authentication \TwoFactorAuth \Service \TwoFAUserServiceInterface ;
22- use OxidEsales \SecurityModule \Authentication \TwoFactorAuth \Settings \TwoFASettingsInterface ;
2322use OxidEsales \SecurityModule \Captcha \Service \ModuleSettingsServiceInterface as CaptchaSettingsServiceInterface ;
2423use OxidEsales \SecurityModule \Shared \Model \User as SecurityModuleUser ;
2524use OxidEsales \SecurityModule \Tests \Integration \IntegrationTestCase ;
@@ -63,7 +62,9 @@ public function testCheckValuesWithInvalidCaptcha()
6362 $ captchaSettings = $ this ->createStub (CaptchaSettingsServiceInterface::class);
6463 $ captchaSettings ->method ('isCaptchaEnabled ' )->willReturn (true );
6564
66- $ sut = $ this ->getSut ([CaptchaSettingsServiceInterface::class => $ captchaSettings ]);
65+ $ sut = $ this ->getSut ([
66+ CaptchaSettingsServiceInterface::class => $ captchaSettings ,
67+ ]);
6768 $ sut ->checkValues ('' , '' , '' , [], []);
6869 }
6970
@@ -85,7 +86,9 @@ public function testCheckValuesWithEmptyCaptcha()
8586 $ captchaSettings = $ this ->createStub (CaptchaSettingsServiceInterface::class);
8687 $ captchaSettings ->method ('isCaptchaEnabled ' )->willReturn (true );
8788
88- $ sut = $ this ->getSut ([CaptchaSettingsServiceInterface::class => $ captchaSettings ]);
89+ $ sut = $ this ->getSut ([
90+ CaptchaSettingsServiceInterface::class => $ captchaSettings ,
91+ ]);
8992 $ sut ->checkValues ('' , '' , '' , [], []);
9093 }
9194
@@ -106,7 +109,9 @@ public function testLoginWithInvalidCaptcha()
106109 $ captchaSettings = $ this ->createStub (CaptchaSettingsServiceInterface::class);
107110 $ captchaSettings ->method ('isCaptchaEnabled ' )->willReturn (true );
108111
109- $ sut = $ this ->getSut ([CaptchaSettingsServiceInterface::class => $ captchaSettings ]);
112+ $ sut = $ this ->getSut ([
113+ CaptchaSettingsServiceInterface::class => $ captchaSettings ,
114+ ]);
110115 $ sut ->login ('' , '' );
111116 }
112117
@@ -127,7 +132,9 @@ public function testLoginWithEmptyCaptcha()
127132 $ captchaSettings = $ this ->createStub (CaptchaSettingsServiceInterface::class);
128133 $ captchaSettings ->method ('isCaptchaEnabled ' )->willReturn (true );
129134
130- $ sut = $ this ->getSut ([CaptchaSettingsServiceInterface::class => $ captchaSettings ]);
135+ $ sut = $ this ->getSut ([
136+ CaptchaSettingsServiceInterface::class => $ captchaSettings ,
137+ ]);
131138 $ sut ->login ('' , '' );
132139 }
133140
@@ -145,7 +152,9 @@ public function testLoginWithValidCaptchaAndValidCredentials(): void
145152 $ captchaSettings = $ this ->createStub (CaptchaSettingsServiceInterface::class);
146153 $ captchaSettings ->method ('isCaptchaEnabled ' )->willReturn (true );
147154
148- $ sut = $ this ->getSut ([CaptchaSettingsServiceInterface::class => $ captchaSettings ]);
155+ $ sut = $ this ->getSut ([
156+ CaptchaSettingsServiceInterface::class => $ captchaSettings ,
157+ ]);
149158 $ result = $ sut ->login (self ::TWO_FA_USER_NAME , self ::TWO_FA_USER_PASSWORD );
150159
151160 $ this ->assertTrue ($ result );
@@ -155,20 +164,15 @@ public function testLoginWith2FAEnabledAndUnverifiedChallengeTriggersChallenge()
155164 {
156165 $ userId = $ this ->getTwoFAUserId ();
157166
158- $ twoFaSettings = $ this ->createStub (TwoFASettingsInterface::class);
159- $ twoFaSettings ->method ('isTwoFactorAuthEnabled ' )->willReturn (true );
160-
161167 $ userServiceSpy = $ this ->createMock (TwoFAUserServiceInterface::class);
162- $ userServiceSpy ->method ('isChallengeVerified ' )
163- ->with ($ userId )
164- ->willReturn (false );
168+ $ userServiceSpy ->method ('isTwoFARequired ' )->with ($ userId )->willReturn (true );
169+ $ userServiceSpy ->method ('isChallengeVerified ' )->with ($ userId )->willReturn (false );
165170 $ userServiceSpy ->expects ($ this ->once ())
166171 ->method ('startChallengeForUser ' )
167172 ->with ($ userId );
168173
169174 $ sut = $ this ->getSut ([
170- TwoFASettingsInterface::class => $ twoFaSettings ,
171- TwoFAUserServiceInterface::class => $ userServiceSpy
175+ TwoFAUserServiceInterface::class => $ userServiceSpy ,
172176 ]);
173177 $ sut ->login (self ::TWO_FA_USER_NAME , self ::TWO_FA_USER_PASSWORD );
174178 }
@@ -177,19 +181,13 @@ public function testLoginWith2FAEnabledAndVerifiedChallengeNOTTriggeringChalleng
177181 {
178182 $ userId = $ this ->getTwoFAUserId ();
179183
180- $ twoFaSettings = $ this ->createStub (TwoFASettingsInterface::class);
181- $ twoFaSettings ->method ('isTwoFactorAuthEnabled ' )->willReturn (true );
182-
183184 $ userServiceSpy = $ this ->createMock (TwoFAUserServiceInterface::class);
184- $ userServiceSpy ->method ('isChallengeVerified ' )
185- ->with ($ userId )
186- ->willReturn (true );
187- $ userServiceSpy ->expects ($ this ->never ())
188- ->method ('startChallengeForUser ' );
185+ $ userServiceSpy ->method ('isTwoFARequired ' )->with ($ userId )->willReturn (true );
186+ $ userServiceSpy ->method ('isChallengeVerified ' )->with ($ userId )->willReturn (true );
187+ $ userServiceSpy ->expects ($ this ->never ())->method ('startChallengeForUser ' );
189188
190189 $ sut = $ this ->getSut ([
191- TwoFASettingsInterface::class => $ twoFaSettings ,
192- TwoFAUserServiceInterface::class => $ userServiceSpy
190+ TwoFAUserServiceInterface::class => $ userServiceSpy ,
193191 ]);
194192
195193 $ result = $ sut ->login (self ::TWO_FA_USER_NAME , self ::TWO_FA_USER_PASSWORD );
@@ -200,14 +198,11 @@ public function testLoginWithoutPasswordOnLoadedUserWith2FAEnabledAndChallengeVe
200198 {
201199 $ userId = $ this ->getTwoFAUserId ();
202200
203- $ twoFaSettings = $ this ->createStub (TwoFASettingsInterface::class);
204- $ twoFaSettings ->method ('isTwoFactorAuthEnabled ' )->willReturn (true );
205-
206201 $ userServiceMock = $ this ->createMock (TwoFAUserServiceInterface::class);
202+ $ userServiceMock ->method ('isTwoFARequired ' )->with ($ userId )->willReturn (true );
207203 $ userServiceMock ->method ('isChallengeVerified ' )->with ($ userId )->willReturn (true );
208204
209205 $ sut = $ this ->getSut ([
210- TwoFASettingsInterface::class => $ twoFaSettings ,
211206 TwoFAUserServiceInterface::class => $ userServiceMock ,
212207 ]);
213208 $ sut ->load ($ userId );
@@ -216,21 +211,18 @@ public function testLoginWithoutPasswordOnLoadedUserWith2FAEnabledAndChallengeVe
216211 $ this ->assertTrue ($ result );
217212 }
218213
219- public function testLoginWithoutPasswordOnLoadedUserWith2FAEnabledAndChallengeNotVerifiedNOTLogsUserIn (): void
214+ public function testLoginWithoutPasswordOnLoadedUserWith2FAEnabledAndChallengeNotVerifiedTriggersChallenge (): void
220215 {
221216 $ userId = $ this ->getTwoFAUserId ();
222217
223- $ twoFaSettings = $ this ->createStub (TwoFASettingsInterface::class);
224- $ twoFaSettings ->method ('isTwoFactorAuthEnabled ' )->willReturn (true );
225-
226218 $ userServiceSpy = $ this ->createMock (TwoFAUserServiceInterface::class);
219+ $ userServiceSpy ->method ('isTwoFARequired ' )->with ($ userId )->willReturn (true );
227220 $ userServiceSpy ->method ('isChallengeVerified ' )->with ($ userId )->willReturn (false );
228221 $ userServiceSpy ->expects ($ this ->once ())
229222 ->method ('startChallengeForUser ' )
230223 ->with ($ userId );
231224
232225 $ sut = $ this ->getSut ([
233- TwoFASettingsInterface::class => $ twoFaSettings ,
234226 TwoFAUserServiceInterface::class => $ userServiceSpy ,
235227 ]);
236228 $ sut ->load ($ userId );
@@ -246,10 +238,7 @@ public function testLoginWith2FAEnabledAndBadCredentialsThrowsException(
246238 $ this ->expectException (UserException::class);
247239 $ this ->expectExceptionMessage ('ERROR_MESSAGE_USER_NOVALIDLOGIN ' );
248240
249- $ twoFaSettings = $ this ->createStub (TwoFASettingsInterface::class);
250- $ twoFaSettings ->method ('isTwoFactorAuthEnabled ' )->willReturn (true );
251-
252- $ sut = $ this ->getSut ([TwoFASettingsInterface::class => $ twoFaSettings ]);
241+ $ sut = $ this ->getSut ();
253242 $ sut ->login ($ username , $ password );
254243 }
255244
@@ -259,20 +248,17 @@ public static function invalidLoginDataProvider(): Generator
259248 yield 'nonexistent user ' => ['nonexistent@test.com ' , 'anypassword ' ];
260249 }
261250
262- public function testLoginWith2FADisabledDoesntTouch2FA (): void
251+ public function testLogin2FANotRequiredDoesntTouchChallengeAndJustLogins (): void
263252 {
264- $ twoFaSettings = $ this ->createStub (TwoFASettingsInterface::class);
265- $ twoFaSettings ->method ('isTwoFactorAuthEnabled ' )->willReturn (false );
253+ $ userId = $ this ->getTwoFAUserId ();
266254
267255 $ userServiceSpy = $ this ->createMock (TwoFAUserServiceInterface::class);
268- $ userServiceSpy ->expects ($ this ->never ())
269- ->method ('isChallengeVerified ' );
270- $ userServiceSpy ->expects ($ this ->never ())
271- ->method ('startChallengeForUser ' );
256+ $ userServiceSpy ->method ('isTwoFARequired ' )->with ($ userId )->willReturn (false );
257+ $ userServiceSpy ->expects ($ this ->never ())->method ('isChallengeVerified ' );
258+ $ userServiceSpy ->expects ($ this ->never ())->method ('startChallengeForUser ' );
272259
273260 $ sut = $ this ->getSut ([
274- TwoFASettingsInterface::class => $ twoFaSettings ,
275- TwoFAUserServiceInterface::class => $ userServiceSpy
261+ TwoFAUserServiceInterface::class => $ userServiceSpy ,
276262 ]);
277263
278264 $ result = $ sut ->login (self ::TWO_FA_USER_NAME , self ::TWO_FA_USER_PASSWORD );
@@ -281,16 +267,16 @@ public function testLoginWith2FADisabledDoesntTouch2FA(): void
281267
282268 private function getSut (array $ serviceOverrides = []): SecurityModuleUser
283269 {
284- $ captchaDefault = $ this ->createStub (CaptchaSettingsServiceInterface::class);
285- $ captchaDefault ->method ('isCaptchaEnabled ' )->willReturn (false );
286-
287- $ twoFaDefault = $ this ->createStub (TwoFASettingsInterface::class);
288- $ twoFaDefault ->method ('isTwoFactorAuthEnabled ' )->willReturn (false );
289-
290270 $ services = array_merge (
291271 [
292- CaptchaSettingsServiceInterface::class => $ captchaDefault ,
293- TwoFASettingsInterface::class => $ twoFaDefault ,
272+ CaptchaSettingsServiceInterface::class => $ this ->createConfiguredStub (
273+ CaptchaSettingsServiceInterface::class,
274+ ['isCaptchaEnabled ' => false ]
275+ ),
276+ TwoFAUserServiceInterface::class => $ this ->createConfiguredStub (
277+ TwoFAUserServiceInterface::class,
278+ ['isTwoFARequired ' => false ]
279+ ),
294280 ],
295281 $ serviceOverrides
296282 );
0 commit comments