2015-04-20 17:23:09 +03:00
< ? php
/**
2016-07-21 17:49:16 +03:00
* @ copyright Copyright ( c ) 2016 , ownCloud , Inc .
*
2020-03-31 11:49:10 +03:00
* @ author Christoph Wurst < christoph @ winzerhof - wurst . at >
2015-06-25 12:43:55 +03:00
* @ author Clark Tomlinson < fallen013 @ gmail . com >
2016-07-21 17:49:16 +03:00
* @ author Joas Schilling < coding @ schilljs . com >
2017-11-06 17:56:42 +03:00
* @ author Morris Jobke < hey @ morrisjobke . de >
2019-12-03 21:57:53 +03:00
* @ author Roeland Jago Douma < roeland @ famdouma . nl >
2015-04-20 17:23:09 +03:00
*
* @ license AGPL - 3.0
2015-06-25 12:43:55 +03:00
*
* This code is free software : you can redistribute it and / or modify
* it under the terms of the GNU Affero General Public License , version 3 ,
* as published by the Free Software Foundation .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU Affero General Public License for more details .
*
* You should have received a copy of the GNU Affero General Public License , version 3 ,
2019-12-03 21:57:53 +03:00
* along with this program . If not , see < http :// www . gnu . org / licenses />
2015-06-25 12:43:55 +03:00
*
2015-04-20 17:23:09 +03:00
*/
2015-04-20 20:49:21 +03:00
namespace OCA\Encryption\Tests\Controller ;
2015-04-20 17:23:09 +03:00
use OCA\Encryption\Controller\RecoveryController ;
2017-10-26 14:46:16 +03:00
use OCA\Encryption\Recovery ;
2015-04-20 20:49:21 +03:00
use OCP\AppFramework\Http ;
2017-10-24 16:26:53 +03:00
use OCP\IConfig ;
use OCP\IL10N ;
use OCP\IRequest ;
2015-04-20 17:23:09 +03:00
use Test\TestCase ;
class RecoveryControllerTest extends TestCase {
2016-05-12 10:42:19 +03:00
/** @var RecoveryController */
2015-04-20 17:23:09 +03:00
private $controller ;
2016-05-12 10:42:19 +03:00
/** @var \OCP\IRequest|\PHPUnit_Framework_MockObject_MockObject */
2015-04-20 17:23:09 +03:00
private $requestMock ;
2016-05-12 10:42:19 +03:00
/** @var \OCP\IConfig|\PHPUnit_Framework_MockObject_MockObject */
2015-04-20 17:23:09 +03:00
private $configMock ;
2016-05-12 10:42:19 +03:00
/** @var \OCP\IL10N|\PHPUnit_Framework_MockObject_MockObject */
2015-04-20 17:23:09 +03:00
private $l10nMock ;
2016-05-12 10:42:19 +03:00
/** @var \OCA\Encryption\Recovery|\PHPUnit_Framework_MockObject_MockObject */
2015-04-20 17:23:09 +03:00
private $recoveryMock ;
2015-04-20 20:49:21 +03:00
public function adminRecoveryProvider () {
return [
2016-05-12 10:42:19 +03: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 ],
[ 'test' , 'soimething that doesn\'t match' , '1' , 'Repeated recovery key password does not match the provided recovery key password' , Http :: STATUS_BAD_REQUEST ],
[ 'test' , 'test' , '0' , 'Recovery key successfully disabled' , Http :: STATUS_OK ],
2015-04-20 20:49:21 +03:00
];
}
/**
* @ dataProvider adminRecoveryProvider
* @ param $recoveryPassword
2016-05-12 10:42:19 +03:00
* @ param $passConfirm
2015-04-20 20:49:21 +03:00
* @ param $enableRecovery
* @ param $expectedMessage
* @ param $expectedStatus
*/
2016-05-12 10:42:19 +03:00
public function testAdminRecovery ( $recoveryPassword , $passConfirm , $enableRecovery , $expectedMessage , $expectedStatus ) {
2015-04-20 17:23:09 +03:00
$this -> recoveryMock -> expects ( $this -> any ())
-> method ( 'enableAdminRecovery' )
-> willReturn ( true );
2015-04-20 20:49:21 +03:00
$this -> recoveryMock -> expects ( $this -> any ())
2015-04-20 17:23:09 +03:00
-> method ( 'disableAdminRecovery' )
-> willReturn ( true );
$response = $this -> controller -> adminRecovery ( $recoveryPassword ,
2016-05-12 10:42:19 +03:00
$passConfirm ,
2015-04-20 20:49:21 +03:00
$enableRecovery );
2015-04-20 17:23:09 +03:00
2015-04-20 20:49:21 +03:00
$this -> assertEquals ( $expectedMessage , $response -> getData ()[ 'data' ][ 'message' ]);
$this -> assertEquals ( $expectedStatus , $response -> getStatus ());
}
2015-04-20 17:23:09 +03:00
2015-04-20 20:49:21 +03:00
public function changeRecoveryPasswordProvider () {
return [
2016-05-12 10:42:19 +03: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 20:49:21 +03:00
];
}
/**
* @ dataProvider changeRecoveryPasswordProvider
* @ param $password
* @ param $confirmPassword
* @ param $oldPassword
* @ param $expectedMessage
* @ param $expectedStatus
*/
public function testChangeRecoveryPassword ( $password , $confirmPassword , $oldPassword , $expectedMessage , $expectedStatus ) {
$this -> recoveryMock -> expects ( $this -> any ())
2015-04-20 17:23:09 +03:00
-> method ( 'changeRecoveryKeyPassword' )
-> with ( $password , $oldPassword )
2020-03-26 00:21:27 +03:00
-> willReturnMap ([
2015-04-20 20:49:21 +03:00
[ 'test' , 'oldTestFail' , false ],
[ 'test' , 'oldtest' , true ]
2020-03-26 00:21:27 +03:00
]);
2015-04-20 17:23:09 +03:00
2015-04-20 20:49:21 +03:00
$response = $this -> controller -> changeRecoveryPassword ( $password ,
2015-04-20 17:23:09 +03:00
$oldPassword ,
2015-04-20 20:49:21 +03:00
$confirmPassword );
2015-04-20 17:23:09 +03:00
2015-04-20 20:49:21 +03:00
$this -> assertEquals ( $expectedMessage , $response -> getData ()[ 'data' ][ 'message' ]);
$this -> assertEquals ( $expectedStatus , $response -> getStatus ());
}
2015-04-20 17:23:09 +03:00
2015-04-20 20:49:21 +03:00
public function userSetRecoveryProvider () {
return [
[ '1' , 'Recovery Key enabled' , Http :: STATUS_OK ],
2015-04-24 16:42:02 +03:00
[ '0' , 'Could not enable the recovery key, please try again or contact your administrator' , Http :: STATUS_BAD_REQUEST ]
2015-04-20 20:49:21 +03:00
];
2015-04-20 17:23:09 +03:00
}
2015-04-20 20:49:21 +03:00
/**
* @ dataProvider userSetRecoveryProvider
* @ param $enableRecovery
* @ param $expectedMessage
* @ param $expectedStatus
*/
public function testUserSetRecovery ( $enableRecovery , $expectedMessage , $expectedStatus ) {
$this -> recoveryMock -> expects ( $this -> any ())
2015-04-20 17:23:09 +03:00
-> method ( 'setRecoveryForUser' )
2015-04-20 20:49:21 +03:00
-> with ( $enableRecovery )
2020-03-26 00:21:27 +03:00
-> willReturnMap ([
2015-04-20 20:49:21 +03:00
[ '1' , true ],
[ '0' , false ]
2020-03-26 00:21:27 +03:00
]);
2015-04-20 17:23:09 +03:00
2015-04-20 20:49:21 +03:00
$response = $this -> controller -> userSetRecovery ( $enableRecovery );
2015-04-20 17:23:09 +03:00
2015-04-20 20:49:21 +03:00
$this -> assertEquals ( $expectedMessage , $response -> getData ()[ 'data' ][ 'message' ]);
$this -> assertEquals ( $expectedStatus , $response -> getStatus ());
2015-04-20 17:23:09 +03:00
}
2019-11-21 18:40:38 +03:00
protected function setUp () : void {
2015-04-20 17:23:09 +03:00
parent :: setUp ();
2017-10-24 16:26:53 +03:00
$this -> requestMock = $this -> getMockBuilder ( IRequest :: class )
2015-04-20 17:23:09 +03:00
-> disableOriginalConstructor ()
-> getMock ();
2017-10-24 16:26:53 +03:00
$this -> configMock = $this -> getMockBuilder ( IConfig :: class )
2015-04-20 17:23:09 +03:00
-> disableOriginalConstructor ()
-> getMock ();
2017-10-24 16:26:53 +03:00
$this -> l10nMock = $this -> getMockBuilder ( IL10N :: class )
2015-04-20 17:23:09 +03:00
-> disableOriginalConstructor ()
-> getMock ();
// Make l10n work in our tests
$this -> l10nMock -> expects ( $this -> any ())
-> method ( 't' )
-> willReturnArgument ( 0 );
2017-10-26 14:46:16 +03:00
$this -> recoveryMock = $this -> getMockBuilder ( Recovery :: class )
2015-04-20 17:23:09 +03:00
-> disableOriginalConstructor ()
-> getMock ();
2016-05-12 10:42:19 +03:00
$this -> controller = new RecoveryController ( 'encryption' ,
2015-04-20 17:23:09 +03:00
$this -> requestMock ,
$this -> configMock ,
$this -> l10nMock ,
$this -> recoveryMock );
}
}