1111use DateTimeZone ;
1212use OC \Files \Utils \PathHelper ;
1313use OC \KnownUser \KnownUserService ;
14+ use OC \OneTimePassword \OneTimePassword ;
1415use OC \Share20 \DefaultShareProvider ;
1516use OC \Share20 \Exception \ProviderException ;
1617use OC \Share20 \Manager ;
@@ -222,15 +223,15 @@ public function testDeleteNoShareId(): void {
222223
223224 public static function dataTestDelete (): array {
224225 return [
225- [IShare::TYPE_USER , 'sharedWithUser ' ],
226- [IShare::TYPE_GROUP , 'sharedWithGroup ' ],
227- [IShare::TYPE_LINK , '' ],
228- [IShare::TYPE_REMOTE , 'foo@bar.com ' ],
226+ [IShare::TYPE_USER , 'sharedWithUser ' , 1 ],
227+ [IShare::TYPE_GROUP , 'sharedWithGroup ' , 5 ],
228+ [IShare::TYPE_LINK , '' , 2 ],
229+ [IShare::TYPE_REMOTE , 'foo@bar.com ' , 1 ],
229230 ];
230231 }
231232
232233 #[DataProvider('dataTestDelete ' )]
233- public function testDelete ($ shareType , $ sharedWith ): void {
234+ public function testDelete ($ shareType , $ sharedWith, $ otpId ): void {
234235 $ manager = $ this ->createManagerMock ()
235236 ->onlyMethods (['getShareById ' , 'deleteChildren ' , 'promoteReshares ' ])
236237 ->getMock ();
@@ -240,17 +241,20 @@ public function testDelete($shareType, $sharedWith): void {
240241 $ path = $ this ->createMock (File::class);
241242 $ path ->method ('getId ' )->willReturn (1 );
242243
244+ $ otp = (new OneTimePassword ('mock ' , 'rec ' ))->setId ($ otpId );
243245 $ share = $ this ->manager ->newShare ();
244246 $ share ->setId (42 )
245247 ->setProviderId ('prov ' )
246248 ->setShareType ($ shareType )
247249 ->setSharedWith ($ sharedWith )
248250 ->setSharedBy ('sharedBy ' )
251+ ->setOneTimePassword ($ otp )
249252 ->setNode ($ path )
250253 ->setTarget ('myTarget ' );
251254
252255 $ manager ->expects ($ this ->once ())->method ('deleteChildren ' )->with ($ share );
253256 $ manager ->expects ($ this ->once ())->method ('promoteReshares ' )->with ($ share );
257+ $ this ->otpManager ->expects ($ this ->once ())->method ('deleteOTP ' )->with ($ otpId );
254258
255259 $ this ->defaultProvider
256260 ->expects ($ this ->once ())
@@ -3602,6 +3606,35 @@ public function testCheckPasswordUpdateShare(): void {
36023606 $ this ->assertTrue ($ this ->manager ->checkPassword ($ share , 'password ' ));
36033607 }
36043608
3609+ public static function dataTestCheckPasswordOTP () {
3610+ return [
3611+ [null , (new OneTimePassword ('mock ' , 'rec ' ))->setPassword (null ), 'password ' , false ],
3612+ [null , (new OneTimePassword ('mock ' , 'rec ' ))->setPassword ('hashed(password) ' ), 'password ' , true ],
3613+ ['hashed(password) ' , (new OneTimePassword ('mock ' , 'rec ' ))->setPassword (null ), 'password ' , false ], // OTP should take precedence
3614+ ];
3615+ }
3616+
3617+ #[DataProvider('dataTestCheckPasswordOTP ' )]
3618+ public function testCheckPasswordOTP (?string $ pwHash , ?OneTimePassword $ otp , string $ password , bool $ expected ): void {
3619+ $ share = $ this ->manager ->newShare ()
3620+ ->setShareType (IShare::TYPE_LINK )
3621+ ->setPassword ($ pwHash )
3622+ ->setOneTimePassword ($ otp );
3623+
3624+ $ this ->hasher ->method ('verify ' )->willReturnCallback (function (string $ pwHash , string $ pw , &$ newHash ): bool {
3625+ return "hashed( $ pw) " === $ pwHash ;
3626+ });
3627+ $ this ->otpManager ->method ('validateOTP ' )->willReturnCallback (function (OneTimePassword $ otp , ?string $ password ): bool {
3628+ if ($ otp ->getPassword () === null ) {
3629+ return false ;
3630+ }
3631+ return $ this ->hasher ->verify ($ otp ->getPassword (), $ password , $ newHash );
3632+ });
3633+
3634+ $ actual = $ this ->manager ->checkPassword ($ share , $ password );
3635+ $ this ->assertEquals ($ expected , $ actual , "Password check " . ($ actual ? "succeeded " : "failed " ) . " unexpectedly " );
3636+ }
3637+
36053638 public function testUpdateShareCantChangeShareType (): void {
36063639 $ this ->expectException (\Exception::class);
36073640 $ this ->expectExceptionMessage ('Cannot change share type ' );
0 commit comments