2015-04-20 10:23:09 -04:00
< ? php
2024-05-28 10:42:42 -04:00
2025-05-28 04:59:55 -04:00
declare ( strict_types = 1 );
2015-04-20 10:23:09 -04:00
/**
2024-05-28 10:42:42 -04:00
* SPDX - FileCopyrightText : 2017 - 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX - FileCopyrightText : 2016 ownCloud , Inc .
* SPDX - License - Identifier : AGPL - 3.0 - only
2015-04-20 10:23:09 -04:00
*/
2015-04-20 13:49:21 -04:00
namespace OCA\Encryption\Tests\Controller ;
2015-04-20 10:23:09 -04:00
use OCA\Encryption\Controller\RecoveryController ;
2017-10-26 07:46:16 -04:00
use OCA\Encryption\Recovery ;
2015-04-20 13:49:21 -04:00
use OCP\AppFramework\Http ;
2017-10-24 09:26:53 -04:00
use OCP\IConfig ;
use OCP\IL10N ;
use OCP\IRequest ;
2025-05-28 04:59:55 -04:00
use PHPUnit\Framework\MockObject\MockObject ;
2015-04-20 10:23:09 -04:00
use Test\TestCase ;
class RecoveryControllerTest extends TestCase {
2025-05-28 04:59:55 -04:00
protected RecoveryController $controller ;
protected IRequest & MockObject $requestMock ;
protected IConfig & MockObject $configMock ;
protected IL10N & MockObject $l10nMock ;
protected Recovery & MockObject $recoveryMock ;
public static function adminRecoveryProvider () : array {
2015-04-20 13:49:21 -04:00
return [
2016-05-12 03:42:19 -04:00
[ 'test' , 'test' , '1' , 'Recovery key successfully enabled' , Http :: STATUS_OK ],
[ '' , 'test' , '1' , 'Missing recovery key password' , Http :: STATUS_BAD_REQUEST ],
[ 'test' , '' , '1' , 'Please repeat the recovery key password' , Http :: STATUS_BAD_REQUEST ],
2025-05-28 04:59:55 -04:00
[ 'test' , 'something that doesn\'t match' , '1' , 'Repeated recovery key password does not match the provided recovery key password' , Http :: STATUS_BAD_REQUEST ],
2016-05-12 03:42:19 -04:00
[ 'test' , 'test' , '0' , 'Recovery key successfully disabled' , Http :: STATUS_OK ],
2015-04-20 13:49:21 -04:00
];
}
/**
* @ param $recoveryPassword
2016-05-12 03:42:19 -04:00
* @ param $passConfirm
2015-04-20 13:49:21 -04:00
* @ param $enableRecovery
* @ param $expectedMessage
* @ param $expectedStatus
*/
2025-06-30 10:56:59 -04:00
#[\PHPUnit\Framework\Attributes\DataProvider('adminRecoveryProvider')]
2024-09-15 16:32:31 -04:00
public function testAdminRecovery ( $recoveryPassword , $passConfirm , $enableRecovery , $expectedMessage , $expectedStatus ) : void {
2015-04-20 10:23:09 -04:00
$this -> recoveryMock -> expects ( $this -> any ())
-> method ( 'enableAdminRecovery' )
-> willReturn ( true );
2015-04-20 13:49:21 -04:00
$this -> recoveryMock -> expects ( $this -> any ())
2015-04-20 10:23:09 -04:00
-> method ( 'disableAdminRecovery' )
-> willReturn ( true );
$response = $this -> controller -> adminRecovery ( $recoveryPassword ,
2016-05-12 03:42:19 -04:00
$passConfirm ,
2015-04-20 13:49:21 -04:00
$enableRecovery );
2015-04-20 10:23:09 -04:00
2015-04-20 13:49:21 -04:00
$this -> assertEquals ( $expectedMessage , $response -> getData ()[ 'data' ][ 'message' ]);
$this -> assertEquals ( $expectedStatus , $response -> getStatus ());
}
2015-04-20 10:23:09 -04:00
2025-05-28 04:59:55 -04:00
public static function changeRecoveryPasswordProvider () : array {
2015-04-20 13:49:21 -04:00
return [
2016-05-12 03:42:19 -04:00
[ 'test' , 'test' , 'oldtestFail' , 'Could not change the password. Maybe the old password was not correct.' , Http :: STATUS_BAD_REQUEST ],
[ 'test' , 'test' , 'oldtest' , 'Password successfully changed.' , Http :: STATUS_OK ],
[ 'test' , 'notmatch' , 'oldtest' , 'Repeated recovery key password does not match the provided recovery key password' , Http :: STATUS_BAD_REQUEST ],
[ '' , 'test' , 'oldtest' , 'Please provide a new recovery password' , Http :: STATUS_BAD_REQUEST ],
[ 'test' , 'test' , '' , 'Please provide the old recovery password' , Http :: STATUS_BAD_REQUEST ]
2015-04-20 13:49:21 -04:00
];
}
/**
* @ param $password
* @ param $confirmPassword
* @ param $oldPassword
* @ param $expectedMessage
* @ param $expectedStatus
*/
2025-06-30 10:56:59 -04:00
#[\PHPUnit\Framework\Attributes\DataProvider('changeRecoveryPasswordProvider')]
2024-09-15 16:32:31 -04:00
public function testChangeRecoveryPassword ( $password , $confirmPassword , $oldPassword , $expectedMessage , $expectedStatus ) : void {
2015-04-20 13:49:21 -04:00
$this -> recoveryMock -> expects ( $this -> any ())
2015-04-20 10:23:09 -04:00
-> method ( 'changeRecoveryKeyPassword' )
-> with ( $password , $oldPassword )
2020-03-25 17:21:27 -04:00
-> willReturnMap ([
2025-09-04 04:53:10 -04:00
[ 'test' , 'oldtestFail' , false ],
2015-04-20 13:49:21 -04:00
[ 'test' , 'oldtest' , true ]
2020-03-25 17:21:27 -04:00
]);
2015-04-20 10:23:09 -04:00
2015-04-20 13:49:21 -04:00
$response = $this -> controller -> changeRecoveryPassword ( $password ,
2015-04-20 10:23:09 -04:00
$oldPassword ,
2015-04-20 13:49:21 -04:00
$confirmPassword );
2015-04-20 10:23:09 -04:00
2015-04-20 13:49:21 -04:00
$this -> assertEquals ( $expectedMessage , $response -> getData ()[ 'data' ][ 'message' ]);
$this -> assertEquals ( $expectedStatus , $response -> getStatus ());
}
2015-04-20 10:23:09 -04:00
2025-05-28 04:59:55 -04:00
public static function userSetRecoveryProvider () : array {
2015-04-20 13:49:21 -04:00
return [
[ '1' , 'Recovery Key enabled' , Http :: STATUS_OK ],
2015-04-24 09:42:02 -04:00
[ '0' , 'Could not enable the recovery key, please try again or contact your administrator' , Http :: STATUS_BAD_REQUEST ]
2015-04-20 13:49:21 -04:00
];
2015-04-20 10:23:09 -04:00
}
2015-04-20 13:49:21 -04:00
/**
* @ param $enableRecovery
* @ param $expectedMessage
* @ param $expectedStatus
*/
2025-06-30 10:56:59 -04:00
#[\PHPUnit\Framework\Attributes\DataProvider('userSetRecoveryProvider')]
2024-09-15 16:32:31 -04:00
public function testUserSetRecovery ( $enableRecovery , $expectedMessage , $expectedStatus ) : void {
2015-04-20 13:49:21 -04:00
$this -> recoveryMock -> expects ( $this -> any ())
2015-04-20 10:23:09 -04:00
-> method ( 'setRecoveryForUser' )
2015-04-20 13:49:21 -04:00
-> with ( $enableRecovery )
2020-03-25 17:21:27 -04:00
-> willReturnMap ([
2015-04-20 13:49:21 -04:00
[ '1' , true ],
[ '0' , false ]
2020-03-25 17:21:27 -04:00
]);
2015-04-20 10:23:09 -04:00
2015-04-20 13:49:21 -04:00
$response = $this -> controller -> userSetRecovery ( $enableRecovery );
2015-04-20 10:23:09 -04:00
2015-04-20 13:49:21 -04:00
$this -> assertEquals ( $expectedMessage , $response -> getData ()[ 'data' ][ 'message' ]);
$this -> assertEquals ( $expectedStatus , $response -> getStatus ());
2015-04-20 10:23:09 -04:00
}
2019-11-21 10:40:38 -05:00
protected function setUp () : void {
2015-04-20 10:23:09 -04:00
parent :: setUp ();
2017-10-24 09:26:53 -04:00
$this -> requestMock = $this -> getMockBuilder ( IRequest :: class )
2015-04-20 10:23:09 -04:00
-> disableOriginalConstructor ()
-> getMock ();
2017-10-24 09:26:53 -04:00
$this -> configMock = $this -> getMockBuilder ( IConfig :: class )
2015-04-20 10:23:09 -04:00
-> disableOriginalConstructor ()
-> getMock ();
2017-10-24 09:26:53 -04:00
$this -> l10nMock = $this -> getMockBuilder ( IL10N :: class )
2015-04-20 10:23:09 -04:00
-> disableOriginalConstructor ()
-> getMock ();
// Make l10n work in our tests
$this -> l10nMock -> expects ( $this -> any ())
-> method ( 't' )
-> willReturnArgument ( 0 );
2017-10-26 07:46:16 -04:00
$this -> recoveryMock = $this -> getMockBuilder ( Recovery :: class )
2015-04-20 10:23:09 -04:00
-> disableOriginalConstructor ()
-> getMock ();
2016-05-12 03:42:19 -04:00
$this -> controller = new RecoveryController ( 'encryption' ,
2015-04-20 10:23:09 -04:00
$this -> requestMock ,
$this -> configMock ,
$this -> l10nMock ,
$this -> recoveryMock );
}
}