From c81bc152d7bd649b24b00482e0d57b6ce24643c4 Mon Sep 17 00:00:00 2001 From: Clark Tomlinson Date: Mon, 20 Apr 2015 10:23:09 -0400 Subject: [PATCH 1/5] fixing return values and adding tests --- .../controller/recoverycontroller.php | 70 ++++--- .../controller/RecoveryControllerTest.php | 182 ++++++++++++++++++ 2 files changed, 220 insertions(+), 32 deletions(-) create mode 100644 apps/encryption/tests/controller/RecoveryControllerTest.php diff --git a/apps/encryption/controller/recoverycontroller.php b/apps/encryption/controller/recoverycontroller.php index 9c07bda62e4..550190e952a 100644 --- a/apps/encryption/controller/recoverycontroller.php +++ b/apps/encryption/controller/recoverycontroller.php @@ -72,31 +72,36 @@ class RecoveryController extends Controller { public function adminRecovery($recoveryPassword, $confirmPassword, $adminEnableRecovery) { // Check if both passwords are the same if (empty($recoveryPassword)) { - $errorMessage = (string) $this->l->t('Missing recovery key password'); - return new DataResponse(['data' => ['message' => $errorMessage]], 500); + $errorMessage = (string)$this->l->t('Missing recovery key password'); + return new DataResponse(['data' => ['message' => $errorMessage]], + 500); } if (empty($confirmPassword)) { - $errorMessage = (string) $this->l->t('Please repeat the recovery key password'); - return new DataResponse(['data' => ['message' => $errorMessage]], 500); + $errorMessage = (string)$this->l->t('Please repeat the recovery key password'); + return new DataResponse(['data' => ['message' => $errorMessage]], + 500); } if ($recoveryPassword !== $confirmPassword) { - $errorMessage = (string) $this->l->t('Repeated recovery key password does not match the provided recovery key password'); - return new DataResponse(['data' => ['message' => $errorMessage]], 500); + $errorMessage = (string)$this->l->t('Repeated recovery key password does not match the provided recovery key password'); + return new DataResponse(['data' => ['message' => $errorMessage]], + 500); } if (isset($adminEnableRecovery) && $adminEnableRecovery === '1') { if ($this->recovery->enableAdminRecovery($recoveryPassword)) { - return new DataResponse(['status' =>'success', 'data' => array('message' => (string) $this->l->t('Recovery key successfully enabled'))]); + return new DataResponse(['status' => 'success', 'data' => array('message' => (string)$this->l->t('Recovery key successfully enabled'))]); } - return new DataResponse(['data' => array('message' => (string) $this->l->t('Could not enable recovery key. Please check your recovery key password!'))]); + return new DataResponse(['data' => array('message' => (string)$this->l->t('Could not enable recovery key. Please check your recovery key password!'))]); } elseif (isset($adminEnableRecovery) && $adminEnableRecovery === '0') { if ($this->recovery->disableAdminRecovery($recoveryPassword)) { - return new DataResponse(['data' => array('message' => (string) $this->l->t('Recovery key successfully disabled'))]); + return new DataResponse(['data' => array('message' => (string)$this->l->t('Recovery key successfully disabled'))]); } - return new DataResponse(['data' => array('message' => (string) $this->l->t('Could not disable recovery key. Please check your recovery key password!'))]); + return new DataResponse(['data' => array('message' => (string)$this->l->t('Could not disable recovery key. Please check your recovery key password!'))]); } + // this response should never be sent but just in case. + return new DataResponse(['data' => ['message' => (string)$this->l->t('Missing parameters')]]); } /** @@ -108,42 +113,43 @@ class RecoveryController extends Controller { public function changeRecoveryPassword($newPassword, $oldPassword, $confirmPassword) { //check if both passwords are the same if (empty($oldPassword)) { - $errorMessage = (string) $this->l->t('Please provide the old recovery password'); + $errorMessage = (string)$this->l->t('Please provide the old recovery password'); return new DataResponse(array('data' => array('message' => $errorMessage))); } if (empty($newPassword)) { - $errorMessage = (string) $this->l->t('Please provide a new recovery password'); + $errorMessage = (string)$this->l->t('Please provide a new recovery password'); return new DataResponse (array('data' => array('message' => $errorMessage))); } if (empty($confirmPassword)) { - $errorMessage = (string) $this->l->t('Please repeat the new recovery password'); + $errorMessage = (string)$this->l->t('Please repeat the new recovery password'); return new DataResponse(array('data' => array('message' => $errorMessage))); } if ($newPassword !== $confirmPassword) { - $errorMessage = (string) $this->l->t('Repeated recovery key password does not match the provided recovery key password'); + $errorMessage = (string)$this->l->t('Repeated recovery key password does not match the provided recovery key password'); return new DataResponse(array('data' => array('message' => $errorMessage))); } - $result = $this->recovery->changeRecoveryKeyPassword($newPassword, $oldPassword); + $result = $this->recovery->changeRecoveryKeyPassword($newPassword, + $oldPassword); if ($result) { return new DataResponse( array( - 'status' => 'success' , + 'status' => 'success', 'data' => array( - 'message' => (string) $this->l->t('Password successfully changed.')) - ) - ); + 'message' => (string)$this->l->t('Password successfully changed.')) + ) + ); } else { return new DataResponse( array( 'data' => array - ('message' => (string) $this->l->t('Could not change the password. Maybe the old password was not correct.')) - ) - ); + ('message' => (string)$this->l->t('Could not change the password. Maybe the old password was not correct.')) + ) + ); } } @@ -161,19 +167,19 @@ class RecoveryController extends Controller { if ($result) { return new DataResponse( array( - 'status' => 'success', - 'data' => array( - 'message' => (string) $this->l->t('Recovery Key enabled')) - ) - ); - } else { - return new DataResponse( - array( - 'data' => array - ('message' => (string) $this->l->t('Could not enable the recovery key, please try again or contact your administrator')) + 'status' => 'success', + 'data' => array( + 'message' => (string)$this->l->t('Recovery Key enabled')) ) ); } + + return new DataResponse( + array( + 'data' => array + ('message' => (string)$this->l->t('Could not enable the recovery key, please try again or contact your administrator')) + ) + ); } } diff --git a/apps/encryption/tests/controller/RecoveryControllerTest.php b/apps/encryption/tests/controller/RecoveryControllerTest.php new file mode 100644 index 00000000000..289fe60e88c --- /dev/null +++ b/apps/encryption/tests/controller/RecoveryControllerTest.php @@ -0,0 +1,182 @@ + + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + */ + + +namespace OC\apps\encryption\tests\lib\controller; + + +use OCA\Encryption\Controller\RecoveryController; +use Test\TestCase; + +class RecoveryControllerTest extends TestCase { + /** + * @var RecoveryController + */ + private $controller; + private $appName; + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + private $requestMock; + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + private $configMock; + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + private $l10nMock; + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + private $recoveryMock; + + public function testAdminRecovery() { + + $recoveryPassword = 'test'; + $enableRecovery = '1'; + + $this->recoveryMock->expects($this->any()) + ->method('enableAdminRecovery') + ->willReturn(true); + + $response = $this->controller->adminRecovery($recoveryPassword, + $recoveryPassword, + $enableRecovery)->getData(); + + + $this->assertEquals('Recovery key successfully enabled', + $response['data']['message']); + + $response = $this->controller->adminRecovery('', + $recoveryPassword, + $enableRecovery)->getData(); + + $this->assertEquals('Missing recovery key password', + $response['data']['message']); + + $response = $this->controller->adminRecovery($recoveryPassword, + '', + $enableRecovery)->getData(); + + $this->assertEquals('Please repeat the recovery key password', + $response['data']['message']); + + $response = $this->controller->adminRecovery($recoveryPassword, + 'something that doesn\'t match', + $enableRecovery)->getData(); + + $this->assertEquals('Repeated recovery key password does not match the provided recovery key password', + $response['data']['message']); + + $this->recoveryMock->expects($this->once()) + ->method('disableAdminRecovery') + ->willReturn(true); + + $response = $this->controller->adminRecovery($recoveryPassword, + $recoveryPassword, + '0')->getData(); + + $this->assertEquals('Recovery key successfully disabled', + $response['data']['message']); + } + + public function testChangeRecoveryPassword() { + $password = 'test'; + $oldPassword = 'oldtest'; + + $data = $this->controller->changeRecoveryPassword($password, + $oldPassword, + $password)->getData(); + + $this->assertEquals('Could not change the password. Maybe the old password was not correct.', + $data['data']['message']); + + $this->recoveryMock->expects($this->once()) + ->method('changeRecoveryKeyPassword') + ->with($password, $oldPassword) + ->willReturn(true); + + $data = $this->controller->changeRecoveryPassword($password, + $oldPassword, + $password)->getData(); + + $this->assertEquals('Password successfully changed.', + $data['data']['message']); + + $data = $this->controller->changeRecoveryPassword($password, + $oldPassword, + 'not match')->getData(); + + $this->assertEquals('Repeated recovery key password does not match the provided recovery key password', + $data['data']['message']); + + $data = $this->controller->changeRecoveryPassword('', + $oldPassword, + $password)->getData(); + + $this->assertEquals('Please provide a new recovery password', + $data['data']['message']); + + $data = $this->controller->changeRecoveryPassword($password, + '', + $password)->getData(); + + $this->assertEquals('Please provide the old recovery password', + $data['data']['message']); + } + + public function testUserSetRecovery() { + $this->recoveryMock->expects($this->exactly(2)) + ->method('setRecoveryForUser') + ->willReturnOnConsecutiveCalls(true, false); + + $data = $this->controller->userSetRecovery('1')->getData(); + + $this->assertEquals('Recovery Key enabled', $data['data']['message']); + + $data = $this->controller->userSetRecovery('1')->getData(); + + $this->assertEquals('Could not enable the recovery key, please try again or contact your administrator', + $data['data']['message']); + + } + + protected function setUp() { + parent::setUp(); + + $this->appName = 'encryption'; + $this->requestMock = $this->getMockBuilder('\OCP\IRequest') + ->disableOriginalConstructor() + ->getMock(); + + $this->configMock = $this->getMockBuilder('OCP\IConfig') + ->disableOriginalConstructor() + ->getMock(); + + $this->l10nMock = $this->getMockBuilder('OCP\IL10N') + ->disableOriginalConstructor() + ->getMock(); + + // Make l10n work in our tests + $this->l10nMock->expects($this->any()) + ->method('t') + ->willReturnArgument(0); + + $this->recoveryMock = $this->getMockBuilder('OCA\Encryption\Recovery') + ->disableOriginalConstructor() + ->getMock(); + + $this->controller = new RecoveryController($this->appName, + $this->requestMock, + $this->configMock, + $this->l10nMock, + $this->recoveryMock); + } + +} From 1747117edfd337d50075e3612ca56cac18b96f5a Mon Sep 17 00:00:00 2001 From: Clark Tomlinson Date: Mon, 20 Apr 2015 13:49:21 -0400 Subject: [PATCH 2/5] destupify tests --- .../controller/recoverycontroller.php | 47 +++-- .../controller/RecoveryControllerTest.php | 161 +++++++++--------- 2 files changed, 102 insertions(+), 106 deletions(-) diff --git a/apps/encryption/controller/recoverycontroller.php b/apps/encryption/controller/recoverycontroller.php index 550190e952a..bf548f24fc6 100644 --- a/apps/encryption/controller/recoverycontroller.php +++ b/apps/encryption/controller/recoverycontroller.php @@ -93,15 +93,15 @@ class RecoveryController extends Controller { if ($this->recovery->enableAdminRecovery($recoveryPassword)) { return new DataResponse(['status' => 'success', 'data' => array('message' => (string)$this->l->t('Recovery key successfully enabled'))]); } - return new DataResponse(['data' => array('message' => (string)$this->l->t('Could not enable recovery key. Please check your recovery key password!'))]); + return new DataResponse(['data' => array('message' => (string)$this->l->t('Could not enable recovery key. Please check your recovery key password!'))], 500); } elseif (isset($adminEnableRecovery) && $adminEnableRecovery === '0') { if ($this->recovery->disableAdminRecovery($recoveryPassword)) { return new DataResponse(['data' => array('message' => (string)$this->l->t('Recovery key successfully disabled'))]); } - return new DataResponse(['data' => array('message' => (string)$this->l->t('Could not disable recovery key. Please check your recovery key password!'))]); + return new DataResponse(['data' => array('message' => (string)$this->l->t('Could not disable recovery key. Please check your recovery key password!'))], 500); } // this response should never be sent but just in case. - return new DataResponse(['data' => ['message' => (string)$this->l->t('Missing parameters')]]); + return new DataResponse(['data' => ['message' => (string)$this->l->t('Missing parameters')]], 500); } /** @@ -114,22 +114,22 @@ class RecoveryController extends Controller { //check if both passwords are the same if (empty($oldPassword)) { $errorMessage = (string)$this->l->t('Please provide the old recovery password'); - return new DataResponse(array('data' => array('message' => $errorMessage))); + return new DataResponse(array('data' => array('message' => $errorMessage)), 500); } if (empty($newPassword)) { $errorMessage = (string)$this->l->t('Please provide a new recovery password'); - return new DataResponse (array('data' => array('message' => $errorMessage))); + return new DataResponse (array('data' => array('message' => $errorMessage)), 500); } if (empty($confirmPassword)) { $errorMessage = (string)$this->l->t('Please repeat the new recovery password'); - return new DataResponse(array('data' => array('message' => $errorMessage))); + return new DataResponse(array('data' => array('message' => $errorMessage)), 500); } if ($newPassword !== $confirmPassword) { $errorMessage = (string)$this->l->t('Repeated recovery key password does not match the provided recovery key password'); - return new DataResponse(array('data' => array('message' => $errorMessage))); + return new DataResponse(array('data' => array('message' => $errorMessage)), 500); } $result = $this->recovery->changeRecoveryKeyPassword($newPassword, @@ -139,18 +139,17 @@ class RecoveryController extends Controller { return new DataResponse( array( 'status' => 'success', - 'data' => array( - 'message' => (string)$this->l->t('Password successfully changed.')) - ) - ); - } else { - return new DataResponse( - array( - 'data' => array - ('message' => (string)$this->l->t('Could not change the password. Maybe the old password was not correct.')) + 'data' => [ + 'message' => (string)$this->l->t('Password successfully changed.')] ) ); } + return new DataResponse( + array( + 'data' => [ + 'message' => (string)$this->l->t('Could not change the password. Maybe the old password was not correct.') + ] + ), 500); } /** @@ -168,19 +167,19 @@ class RecoveryController extends Controller { return new DataResponse( array( 'status' => 'success', - 'data' => array( - 'message' => (string)$this->l->t('Recovery Key enabled')) + 'data' => [ + 'message' => (string)$this->l->t('Recovery Key enabled')] ) ); } - return new DataResponse( - array( - 'data' => array - ('message' => (string)$this->l->t('Could not enable the recovery key, please try again or contact your administrator')) - ) - ); } + return new DataResponse( + array( + 'data' => [ + 'message' => (string)$this->l->t('Could not enable the recovery key, please try again or contact your administrator') + ] + ), 500); } } diff --git a/apps/encryption/tests/controller/RecoveryControllerTest.php b/apps/encryption/tests/controller/RecoveryControllerTest.php index 289fe60e88c..0ac76774c5f 100644 --- a/apps/encryption/tests/controller/RecoveryControllerTest.php +++ b/apps/encryption/tests/controller/RecoveryControllerTest.php @@ -7,10 +7,11 @@ */ -namespace OC\apps\encryption\tests\lib\controller; +namespace OCA\Encryption\Tests\Controller; use OCA\Encryption\Controller\RecoveryController; +use OCP\AppFramework\Http; use Test\TestCase; class RecoveryControllerTest extends TestCase { @@ -36,114 +37,110 @@ class RecoveryControllerTest extends TestCase { */ private $recoveryMock; - public function testAdminRecovery() { + public function adminRecoveryProvider() { + return [ + ['test', 'test', '1', 'Recovery key successfully enabled', HTTP::STATUS_OK], + ['', 'test', '1', 'Missing recovery key password', HTTP::STATUS_INTERNAL_SERVER_ERROR], + ['test', '', '1', 'Please repeat the recovery key password', HTTP::STATUS_INTERNAL_SERVER_ERROR], + ['test', 'soimething that doesn\'t match', '1', 'Repeated recovery key password does not match the provided recovery key password', HTTP::STATUS_INTERNAL_SERVER_ERROR], + ['test', 'test', '0', 'Recovery key successfully disabled', HTTP::STATUS_OK], + ]; + } + + /** + * @dataProvider adminRecoveryProvider + * @param $recoveryPassword + * @param $passconfirm + * @param $enableRecovery + * @param $expectedMessage + * @param $expectedStatus + */ + public function testAdminRecovery($recoveryPassword, $passconfirm, $enableRecovery, $expectedMessage, $expectedStatus) { - $recoveryPassword = 'test'; - $enableRecovery = '1'; $this->recoveryMock->expects($this->any()) ->method('enableAdminRecovery') ->willReturn(true); - $response = $this->controller->adminRecovery($recoveryPassword, - $recoveryPassword, - $enableRecovery)->getData(); - - - $this->assertEquals('Recovery key successfully enabled', - $response['data']['message']); - - $response = $this->controller->adminRecovery('', - $recoveryPassword, - $enableRecovery)->getData(); - - $this->assertEquals('Missing recovery key password', - $response['data']['message']); - - $response = $this->controller->adminRecovery($recoveryPassword, - '', - $enableRecovery)->getData(); - - $this->assertEquals('Please repeat the recovery key password', - $response['data']['message']); - - $response = $this->controller->adminRecovery($recoveryPassword, - 'something that doesn\'t match', - $enableRecovery)->getData(); - - $this->assertEquals('Repeated recovery key password does not match the provided recovery key password', - $response['data']['message']); - - $this->recoveryMock->expects($this->once()) + $this->recoveryMock->expects($this->any()) ->method('disableAdminRecovery') ->willReturn(true); $response = $this->controller->adminRecovery($recoveryPassword, - $recoveryPassword, - '0')->getData(); + $passconfirm, + $enableRecovery); + + + $this->assertEquals($expectedMessage, $response->getData()['data']['message']); + $this->assertEquals($expectedStatus, $response->getStatus()); + - $this->assertEquals('Recovery key successfully disabled', - $response['data']['message']); } - public function testChangeRecoveryPassword() { - $password = 'test'; - $oldPassword = 'oldtest'; + public function changeRecoveryPasswordProvider() { + return [ + ['test', 'test', 'oldtestFail', 'Could not change the password. Maybe the old password was not correct.', HTTP::STATUS_INTERNAL_SERVER_ERROR], + ['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_INTERNAL_SERVER_ERROR], + ['', 'test', 'oldtest', 'Please provide a new recovery password', HTTP::STATUS_INTERNAL_SERVER_ERROR], + ['test', 'test', '', 'Please provide the old recovery password', HTTP::STATUS_INTERNAL_SERVER_ERROR] + ]; + } - $data = $this->controller->changeRecoveryPassword($password, - $oldPassword, - $password)->getData(); - - $this->assertEquals('Could not change the password. Maybe the old password was not correct.', - $data['data']['message']); - - $this->recoveryMock->expects($this->once()) + /** + * @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()) ->method('changeRecoveryKeyPassword') ->with($password, $oldPassword) - ->willReturn(true); + ->will($this->returnValueMap([ + ['test', 'oldTestFail', false], + ['test', 'oldtest', true] + ])); - $data = $this->controller->changeRecoveryPassword($password, + $response = $this->controller->changeRecoveryPassword($password, $oldPassword, - $password)->getData(); + $confirmPassword); - $this->assertEquals('Password successfully changed.', - $data['data']['message']); + $this->assertEquals($expectedMessage, $response->getData()['data']['message']); + $this->assertEquals($expectedStatus, $response->getStatus()); - $data = $this->controller->changeRecoveryPassword($password, - $oldPassword, - 'not match')->getData(); - $this->assertEquals('Repeated recovery key password does not match the provided recovery key password', - $data['data']['message']); - - $data = $this->controller->changeRecoveryPassword('', - $oldPassword, - $password)->getData(); - - $this->assertEquals('Please provide a new recovery password', - $data['data']['message']); - - $data = $this->controller->changeRecoveryPassword($password, - '', - $password)->getData(); - - $this->assertEquals('Please provide the old recovery password', - $data['data']['message']); } - public function testUserSetRecovery() { - $this->recoveryMock->expects($this->exactly(2)) + public function userSetRecoveryProvider() { + return [ + ['1', 'Recovery Key enabled', Http::STATUS_OK], + ['0', 'Could not enable the recovery key, please try again or contact your administrator', Http::STATUS_INTERNAL_SERVER_ERROR] + ]; + } + + /** + * @dataProvider userSetRecoveryProvider + * @param $enableRecovery + * @param $expectedMessage + * @param $expectedStatus + */ + public function testUserSetRecovery($enableRecovery, $expectedMessage, $expectedStatus) { + $this->recoveryMock->expects($this->any()) ->method('setRecoveryForUser') - ->willReturnOnConsecutiveCalls(true, false); + ->with($enableRecovery) + ->will($this->returnValueMap([ + ['1', true], + ['0', false] + ])); - $data = $this->controller->userSetRecovery('1')->getData(); - $this->assertEquals('Recovery Key enabled', $data['data']['message']); + $response = $this->controller->userSetRecovery($enableRecovery); - $data = $this->controller->userSetRecovery('1')->getData(); - - $this->assertEquals('Could not enable the recovery key, please try again or contact your administrator', - $data['data']['message']); + $this->assertEquals($expectedMessage, $response->getData()['data']['message']); + $this->assertEquals($expectedStatus, $response->getStatus()); } From e3ec1a8bb8b913bd176b76bd59f8c0c209aff5cb Mon Sep 17 00:00:00 2001 From: Clark Tomlinson Date: Wed, 22 Apr 2015 10:41:47 -0400 Subject: [PATCH 3/5] remove status's and adjust js --- .../controller/recoverycontroller.php | 42 +++++++----- apps/encryption/js/settings-admin.js | 67 ++++++++++--------- apps/encryption/js/settings-personal.js | 48 +++++++------ 3 files changed, 89 insertions(+), 68 deletions(-) diff --git a/apps/encryption/controller/recoverycontroller.php b/apps/encryption/controller/recoverycontroller.php index bf548f24fc6..8ae37d97ecb 100644 --- a/apps/encryption/controller/recoverycontroller.php +++ b/apps/encryption/controller/recoverycontroller.php @@ -91,14 +91,14 @@ class RecoveryController extends Controller { if (isset($adminEnableRecovery) && $adminEnableRecovery === '1') { if ($this->recovery->enableAdminRecovery($recoveryPassword)) { - return new DataResponse(['status' => 'success', 'data' => array('message' => (string)$this->l->t('Recovery key successfully enabled'))]); + return new DataResponse(['data' => ['message' => (string)$this->l->t('Recovery key successfully enabled')]]); } - return new DataResponse(['data' => array('message' => (string)$this->l->t('Could not enable recovery key. Please check your recovery key password!'))], 500); + return new DataResponse(['data' => ['message' => (string)$this->l->t('Could not enable recovery key. Please check your recovery key password!')]], 500); } elseif (isset($adminEnableRecovery) && $adminEnableRecovery === '0') { if ($this->recovery->disableAdminRecovery($recoveryPassword)) { - return new DataResponse(['data' => array('message' => (string)$this->l->t('Recovery key successfully disabled'))]); + return new DataResponse(['data' => ['message' => (string)$this->l->t('Recovery key successfully disabled')]]); } - return new DataResponse(['data' => array('message' => (string)$this->l->t('Could not disable recovery key. Please check your recovery key password!'))], 500); + return new DataResponse(['data' => ['message' => (string)$this->l->t('Could not disable recovery key. Please check your recovery key password!')]], 500); } // this response should never be sent but just in case. return new DataResponse(['data' => ['message' => (string)$this->l->t('Missing parameters')]], 500); @@ -114,22 +114,22 @@ class RecoveryController extends Controller { //check if both passwords are the same if (empty($oldPassword)) { $errorMessage = (string)$this->l->t('Please provide the old recovery password'); - return new DataResponse(array('data' => array('message' => $errorMessage)), 500); + return new DataResponse(['data' => ['message' => $errorMessage]], 500); } if (empty($newPassword)) { $errorMessage = (string)$this->l->t('Please provide a new recovery password'); - return new DataResponse (array('data' => array('message' => $errorMessage)), 500); + return new DataResponse (['data' => ['message' => $errorMessage]], 500); } if (empty($confirmPassword)) { $errorMessage = (string)$this->l->t('Please repeat the new recovery password'); - return new DataResponse(array('data' => array('message' => $errorMessage)), 500); + return new DataResponse(['data' => ['message' => $errorMessage]], 500); } if ($newPassword !== $confirmPassword) { $errorMessage = (string)$this->l->t('Repeated recovery key password does not match the provided recovery key password'); - return new DataResponse(array('data' => array('message' => $errorMessage)), 500); + return new DataResponse(['data' => ['message' => $errorMessage]], 500); } $result = $this->recovery->changeRecoveryKeyPassword($newPassword, @@ -137,19 +137,18 @@ class RecoveryController extends Controller { if ($result) { return new DataResponse( - array( - 'status' => 'success', + [ 'data' => [ 'message' => (string)$this->l->t('Password successfully changed.')] - ) + ] ); } return new DataResponse( - array( + [ 'data' => [ 'message' => (string)$this->l->t('Could not change the password. Maybe the old password was not correct.') ] - ), 500); + ], 500); } /** @@ -164,22 +163,29 @@ class RecoveryController extends Controller { $result = $this->recovery->setRecoveryForUser($userEnableRecovery); if ($result) { + if ($userEnableRecovery === '0') { + return new DataResponse( + [ + 'data' => [ + 'message' => (string)$this->l->t('Recovery Key disabled')] + ] + ); + } return new DataResponse( - array( - 'status' => 'success', + [ 'data' => [ 'message' => (string)$this->l->t('Recovery Key enabled')] - ) + ] ); } } return new DataResponse( - array( + [ 'data' => [ 'message' => (string)$this->l->t('Could not enable the recovery key, please try again or contact your administrator') ] - ), 500); + ], 500); } } diff --git a/apps/encryption/js/settings-admin.js b/apps/encryption/js/settings-admin.js index bb539f6a4e2..fdc53c52152 100644 --- a/apps/encryption/js/settings-admin.js +++ b/apps/encryption/js/settings-admin.js @@ -7,52 +7,59 @@ * See the COPYING-README file. */ -$(document).ready(function(){ +$(document).ready(function () { - $( 'input:radio[name="adminEnableRecovery"]' ).change( - function() { - var recoveryStatus = $( this ).val(); - var oldStatus = (1+parseInt(recoveryStatus, 10)) % 2; - var recoveryPassword = $( '#encryptionRecoveryPassword' ).val(); - var confirmPassword = $( '#repeatEncryptionRecoveryPassword' ).val(); + $('input:radio[name="adminEnableRecovery"]').change( + function () { + var recoveryStatus = $(this).val(); + var oldStatus = (1 + parseInt(recoveryStatus)) % 2; + var recoveryPassword = $('#encryptionRecoveryPassword').val(); + var confirmPassword = $('#repeatEncryptionRecoveryPassword').val(); OC.msg.startSaving('#encryptionSetRecoveryKey .msg'); $.post( OC.generateUrl('/apps/encryption/ajax/adminRecovery'), - { adminEnableRecovery: recoveryStatus, + { + adminEnableRecovery: recoveryStatus, recoveryPassword: recoveryPassword, - confirmPassword: confirmPassword }, - function( result ) { - OC.msg.finishedSaving('#encryptionSetRecoveryKey .msg', result); - if (result.status === "error") { - $('input:radio[name="adminEnableRecovery"][value="'+oldStatus.toString()+'"]') - .attr("checked", "true"); - } else { - if (recoveryStatus === "0") { - $('p[name="changeRecoveryPasswordBlock"]').addClass("hidden"); - } else { - $('input:password[name="changeRecoveryPassword"]').val(""); - $('p[name="changeRecoveryPasswordBlock"]').removeClass("hidden"); - } - } + confirmPassword: confirmPassword } - ); + ).done(function (data) { + OC.msg.finishedSuccess('#encryptionSetRecoveryKey .msg', data.data.message); + + if (recoveryStatus === "0") { + $('p[name="changeRecoveryPasswordBlock"]').addClass("hidden"); + } else { + $('input:password[name="changeRecoveryPassword"]').val(""); + $('p[name="changeRecoveryPasswordBlock"]').removeClass("hidden"); + } + }) + .fail(function (jqXHR) { + $('input:radio[name="adminEnableRecovery"][value="' + oldStatus.toString() + '"]').attr("checked", "true"); + OC.msg.finishedError('#encryptionSetRecoveryKey .msg', JSON.parse(jqXHR.responseText).data.message); + }); } ); // change recovery password - $('button:button[name="submitChangeRecoveryKey"]').click(function() { + $('button:button[name="submitChangeRecoveryKey"]').click(function () { var oldRecoveryPassword = $('#oldEncryptionRecoveryPassword').val(); var newRecoveryPassword = $('#newEncryptionRecoveryPassword').val(); var confirmNewPassword = $('#repeatedNewEncryptionRecoveryPassword').val(); OC.msg.startSaving('#encryptionChangeRecoveryKey .msg'); $.post( - OC.generateUrl('/apps/encryption/ajax/changeRecoveryPassword'), - { oldPassword: oldRecoveryPassword, newPassword: newRecoveryPassword, confirmPassword: confirmNewPassword }, - function( data ) { - OC.msg.finishedSaving('#encryptionChangeRecoveryKey .msg', data); - } - ); + OC.generateUrl('/apps/encryption/ajax/changeRecoveryPassword'), + { + oldPassword: oldRecoveryPassword, + newPassword: newRecoveryPassword, + confirmPassword: confirmNewPassword + } + ).done(function (data) { + OC.msg.finishedSuccess('#encryptionChangeRecoveryKey .msg', data.data.message); + }) + .fail(function (jqXHR) { + OC.msg.finishedError('#encryptionChangeRecoveryKey .msg', JSON.parse(jqXHR.responseText).data.message); + }); }); }); diff --git a/apps/encryption/js/settings-personal.js b/apps/encryption/js/settings-personal.js index e36f10a244e..4728da87082 100644 --- a/apps/encryption/js/settings-personal.js +++ b/apps/encryption/js/settings-personal.js @@ -9,35 +9,43 @@ if (!OC.Encryption) { } OC.Encryption = { - updatePrivateKeyPassword: function() { + updatePrivateKeyPassword: function () { var oldPrivateKeyPassword = $('input:password[id="oldPrivateKeyPassword"]').val(); var newPrivateKeyPassword = $('input:password[id="newPrivateKeyPassword"]').val(); OC.msg.startSaving('#encryption .msg'); $.post( OC.generateUrl('/apps/encryption/ajax/updatePrivateKeyPassword'), - {oldPassword: oldPrivateKeyPassword, newPassword: newPrivateKeyPassword} - ).success(function (response) { - OC.msg.finishedSuccess('#encryption .msg', response.message); - }).fail(function (response) { - OC.msg.finishedError('#encryption .msg', response.responseJSON.message); - }); + { + oldPassword: oldPrivateKeyPassword, + newPassword: newPrivateKeyPassword + } + ).done(function (data) { + OC.msg.finishedSuccess('#encryption .msg', data.data.message); + }) + .fail(function (jqXHR) { + OC.msg.finishedError('#encryption .msg', JSON.parse(jqXHR.responseText).data.message); + }); } }; -$(document).ready(function(){ +$(document).ready(function () { // Trigger ajax on recoveryAdmin status change - $( 'input:radio[name="userEnableRecovery"]' ).change( - function() { - var recoveryStatus = $( this ).val(); + $('input:radio[name="userEnableRecovery"]').change( + function () { + var recoveryStatus = $(this).val(); OC.msg.startAction('#userEnableRecovery .msg', 'Updating recovery keys. This can take some time...'); $.post( - OC.generateUrl('/apps/encryption/ajax/userSetRecovery'), - { userEnableRecovery: recoveryStatus }, - function( data ) { - OC.msg.finishedAction('#userEnableRecovery .msg', data); + OC.generateUrl('/apps/encryption/ajax/userSetRecovery'), + { + userEnableRecovery: recoveryStatus } - ); + ).done(function (data) { + OC.msg.finishedSuccess('#userEnableRecovery .msg', data.data.message); + }) + .fail(function (jqXHR) { + OC.msg.finishedError('#userEnableRecovery .msg', JSON.parse(jqXHR.responseText).data.message); + }); // Ensure page is not reloaded on form submit return false; } @@ -45,12 +53,12 @@ $(document).ready(function(){ // update private key password - $('input:password[name="changePrivateKeyPassword"]').keyup(function(event) { + $('input:password[name="changePrivateKeyPassword"]').keyup(function (event) { var oldPrivateKeyPassword = $('input:password[id="oldPrivateKeyPassword"]').val(); var newPrivateKeyPassword = $('input:password[id="newPrivateKeyPassword"]').val(); - if (newPrivateKeyPassword !== '' && oldPrivateKeyPassword !== '' ) { + if (newPrivateKeyPassword !== '' && oldPrivateKeyPassword !== '') { $('button:button[name="submitChangePrivateKeyPassword"]').removeAttr("disabled"); - if(event.which === 13) { + if (event.which === 13) { OC.Encryption.updatePrivateKeyPassword(); } } else { @@ -58,7 +66,7 @@ $(document).ready(function(){ } }); - $('button:button[name="submitChangePrivateKeyPassword"]').click(function() { + $('button:button[name="submitChangePrivateKeyPassword"]').click(function () { OC.Encryption.updatePrivateKeyPassword(); }); From 29168665cb8acb3296ba734500a869a70313abdc Mon Sep 17 00:00:00 2001 From: Clark Tomlinson Date: Wed, 22 Apr 2015 13:26:06 -0400 Subject: [PATCH 4/5] fix messages from settings crontroller --- .../controller/recoverycontroller.php | 25 ++++++++++--------- apps/encryption/js/settings-personal.js | 4 +-- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/apps/encryption/controller/recoverycontroller.php b/apps/encryption/controller/recoverycontroller.php index 8ae37d97ecb..f163b8fe64b 100644 --- a/apps/encryption/controller/recoverycontroller.php +++ b/apps/encryption/controller/recoverycontroller.php @@ -26,6 +26,7 @@ namespace OCA\Encryption\Controller; use OCA\Encryption\Recovery; use OCP\AppFramework\Controller; +use OCP\AppFramework\Http; use OCP\IConfig; use OCP\IL10N; use OCP\IRequest; @@ -74,34 +75,34 @@ class RecoveryController extends Controller { if (empty($recoveryPassword)) { $errorMessage = (string)$this->l->t('Missing recovery key password'); return new DataResponse(['data' => ['message' => $errorMessage]], - 500); + Http::STATUS_INTERNAL_SERVER_ERROR); } if (empty($confirmPassword)) { $errorMessage = (string)$this->l->t('Please repeat the recovery key password'); return new DataResponse(['data' => ['message' => $errorMessage]], - 500); + Http::STATUS_INTERNAL_SERVER_ERROR); } if ($recoveryPassword !== $confirmPassword) { $errorMessage = (string)$this->l->t('Repeated recovery key password does not match the provided recovery key password'); return new DataResponse(['data' => ['message' => $errorMessage]], - 500); + Http::STATUS_INTERNAL_SERVER_ERROR); } if (isset($adminEnableRecovery) && $adminEnableRecovery === '1') { if ($this->recovery->enableAdminRecovery($recoveryPassword)) { return new DataResponse(['data' => ['message' => (string)$this->l->t('Recovery key successfully enabled')]]); } - return new DataResponse(['data' => ['message' => (string)$this->l->t('Could not enable recovery key. Please check your recovery key password!')]], 500); + return new DataResponse(['data' => ['message' => (string)$this->l->t('Could not enable recovery key. Please check your recovery key password!')]], Http::STATUS_INTERNAL_SERVER_ERROR); } elseif (isset($adminEnableRecovery) && $adminEnableRecovery === '0') { if ($this->recovery->disableAdminRecovery($recoveryPassword)) { return new DataResponse(['data' => ['message' => (string)$this->l->t('Recovery key successfully disabled')]]); } - return new DataResponse(['data' => ['message' => (string)$this->l->t('Could not disable recovery key. Please check your recovery key password!')]], 500); + return new DataResponse(['data' => ['message' => (string)$this->l->t('Could not disable recovery key. Please check your recovery key password!')]], Http::STATUS_INTERNAL_SERVER_ERROR); } // this response should never be sent but just in case. - return new DataResponse(['data' => ['message' => (string)$this->l->t('Missing parameters')]], 500); + return new DataResponse(['data' => ['message' => (string)$this->l->t('Missing parameters')]], Http::STATUS_INTERNAL_SERVER_ERROR); } /** @@ -114,22 +115,22 @@ class RecoveryController extends Controller { //check if both passwords are the same if (empty($oldPassword)) { $errorMessage = (string)$this->l->t('Please provide the old recovery password'); - return new DataResponse(['data' => ['message' => $errorMessage]], 500); + return new DataResponse(['data' => ['message' => $errorMessage]], Http::STATUS_INTERNAL_SERVER_ERROR); } if (empty($newPassword)) { $errorMessage = (string)$this->l->t('Please provide a new recovery password'); - return new DataResponse (['data' => ['message' => $errorMessage]], 500); + return new DataResponse (['data' => ['message' => $errorMessage]], Http::STATUS_INTERNAL_SERVER_ERROR); } if (empty($confirmPassword)) { $errorMessage = (string)$this->l->t('Please repeat the new recovery password'); - return new DataResponse(['data' => ['message' => $errorMessage]], 500); + return new DataResponse(['data' => ['message' => $errorMessage]], Http::STATUS_INTERNAL_SERVER_ERROR); } if ($newPassword !== $confirmPassword) { $errorMessage = (string)$this->l->t('Repeated recovery key password does not match the provided recovery key password'); - return new DataResponse(['data' => ['message' => $errorMessage]], 500); + return new DataResponse(['data' => ['message' => $errorMessage]], Http::STATUS_INTERNAL_SERVER_ERROR); } $result = $this->recovery->changeRecoveryKeyPassword($newPassword, @@ -148,7 +149,7 @@ class RecoveryController extends Controller { 'data' => [ 'message' => (string)$this->l->t('Could not change the password. Maybe the old password was not correct.') ] - ], 500); + ], Http::STATUS_INTERNAL_SERVER_ERROR); } /** @@ -185,7 +186,7 @@ class RecoveryController extends Controller { 'data' => [ 'message' => (string)$this->l->t('Could not enable the recovery key, please try again or contact your administrator') ] - ], 500); + ], Http::STATUS_INTERNAL_SERVER_ERROR); } } diff --git a/apps/encryption/js/settings-personal.js b/apps/encryption/js/settings-personal.js index 4728da87082..658ba2a86ec 100644 --- a/apps/encryption/js/settings-personal.js +++ b/apps/encryption/js/settings-personal.js @@ -20,10 +20,10 @@ OC.Encryption = { newPassword: newPrivateKeyPassword } ).done(function (data) { - OC.msg.finishedSuccess('#encryption .msg', data.data.message); + OC.msg.finishedSuccess('#encryption .msg', data.message); }) .fail(function (jqXHR) { - OC.msg.finishedError('#encryption .msg', JSON.parse(jqXHR.responseText).data.message); + OC.msg.finishedError('#encryption .msg', JSON.parse(jqXHR.responseText).message); }); } }; From 8c0856779bccb41014f677c5ebdec79aec0a5602 Mon Sep 17 00:00:00 2001 From: Clark Tomlinson Date: Fri, 24 Apr 2015 09:42:02 -0400 Subject: [PATCH 5/5] change error codes to 400 --- .../controller/recoverycontroller.php | 24 +++++++++---------- .../controller/RecoveryControllerTest.php | 16 ++++++------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/apps/encryption/controller/recoverycontroller.php b/apps/encryption/controller/recoverycontroller.php index f163b8fe64b..f1a2651443e 100644 --- a/apps/encryption/controller/recoverycontroller.php +++ b/apps/encryption/controller/recoverycontroller.php @@ -75,34 +75,34 @@ class RecoveryController extends Controller { if (empty($recoveryPassword)) { $errorMessage = (string)$this->l->t('Missing recovery key password'); return new DataResponse(['data' => ['message' => $errorMessage]], - Http::STATUS_INTERNAL_SERVER_ERROR); + Http::STATUS_BAD_REQUEST); } if (empty($confirmPassword)) { $errorMessage = (string)$this->l->t('Please repeat the recovery key password'); return new DataResponse(['data' => ['message' => $errorMessage]], - Http::STATUS_INTERNAL_SERVER_ERROR); + Http::STATUS_BAD_REQUEST); } if ($recoveryPassword !== $confirmPassword) { $errorMessage = (string)$this->l->t('Repeated recovery key password does not match the provided recovery key password'); return new DataResponse(['data' => ['message' => $errorMessage]], - Http::STATUS_INTERNAL_SERVER_ERROR); + Http::STATUS_BAD_REQUEST); } if (isset($adminEnableRecovery) && $adminEnableRecovery === '1') { if ($this->recovery->enableAdminRecovery($recoveryPassword)) { return new DataResponse(['data' => ['message' => (string)$this->l->t('Recovery key successfully enabled')]]); } - return new DataResponse(['data' => ['message' => (string)$this->l->t('Could not enable recovery key. Please check your recovery key password!')]], Http::STATUS_INTERNAL_SERVER_ERROR); + return new DataResponse(['data' => ['message' => (string)$this->l->t('Could not enable recovery key. Please check your recovery key password!')]], Http::STATUS_BAD_REQUEST); } elseif (isset($adminEnableRecovery) && $adminEnableRecovery === '0') { if ($this->recovery->disableAdminRecovery($recoveryPassword)) { return new DataResponse(['data' => ['message' => (string)$this->l->t('Recovery key successfully disabled')]]); } - return new DataResponse(['data' => ['message' => (string)$this->l->t('Could not disable recovery key. Please check your recovery key password!')]], Http::STATUS_INTERNAL_SERVER_ERROR); + return new DataResponse(['data' => ['message' => (string)$this->l->t('Could not disable recovery key. Please check your recovery key password!')]], Http::STATUS_BAD_REQUEST); } // this response should never be sent but just in case. - return new DataResponse(['data' => ['message' => (string)$this->l->t('Missing parameters')]], Http::STATUS_INTERNAL_SERVER_ERROR); + return new DataResponse(['data' => ['message' => (string)$this->l->t('Missing parameters')]], Http::STATUS_BAD_REQUEST); } /** @@ -115,22 +115,22 @@ class RecoveryController extends Controller { //check if both passwords are the same if (empty($oldPassword)) { $errorMessage = (string)$this->l->t('Please provide the old recovery password'); - return new DataResponse(['data' => ['message' => $errorMessage]], Http::STATUS_INTERNAL_SERVER_ERROR); + return new DataResponse(['data' => ['message' => $errorMessage]], Http::STATUS_BAD_REQUEST); } if (empty($newPassword)) { $errorMessage = (string)$this->l->t('Please provide a new recovery password'); - return new DataResponse (['data' => ['message' => $errorMessage]], Http::STATUS_INTERNAL_SERVER_ERROR); + return new DataResponse (['data' => ['message' => $errorMessage]], Http::STATUS_BAD_REQUEST); } if (empty($confirmPassword)) { $errorMessage = (string)$this->l->t('Please repeat the new recovery password'); - return new DataResponse(['data' => ['message' => $errorMessage]], Http::STATUS_INTERNAL_SERVER_ERROR); + return new DataResponse(['data' => ['message' => $errorMessage]], Http::STATUS_BAD_REQUEST); } if ($newPassword !== $confirmPassword) { $errorMessage = (string)$this->l->t('Repeated recovery key password does not match the provided recovery key password'); - return new DataResponse(['data' => ['message' => $errorMessage]], Http::STATUS_INTERNAL_SERVER_ERROR); + return new DataResponse(['data' => ['message' => $errorMessage]], Http::STATUS_BAD_REQUEST); } $result = $this->recovery->changeRecoveryKeyPassword($newPassword, @@ -149,7 +149,7 @@ class RecoveryController extends Controller { 'data' => [ 'message' => (string)$this->l->t('Could not change the password. Maybe the old password was not correct.') ] - ], Http::STATUS_INTERNAL_SERVER_ERROR); + ], Http::STATUS_BAD_REQUEST); } /** @@ -186,7 +186,7 @@ class RecoveryController extends Controller { 'data' => [ 'message' => (string)$this->l->t('Could not enable the recovery key, please try again or contact your administrator') ] - ], Http::STATUS_INTERNAL_SERVER_ERROR); + ], Http::STATUS_BAD_REQUEST); } } diff --git a/apps/encryption/tests/controller/RecoveryControllerTest.php b/apps/encryption/tests/controller/RecoveryControllerTest.php index 0ac76774c5f..89b541e7bd6 100644 --- a/apps/encryption/tests/controller/RecoveryControllerTest.php +++ b/apps/encryption/tests/controller/RecoveryControllerTest.php @@ -40,9 +40,9 @@ class RecoveryControllerTest extends TestCase { public function adminRecoveryProvider() { return [ ['test', 'test', '1', 'Recovery key successfully enabled', HTTP::STATUS_OK], - ['', 'test', '1', 'Missing recovery key password', HTTP::STATUS_INTERNAL_SERVER_ERROR], - ['test', '', '1', 'Please repeat the recovery key password', HTTP::STATUS_INTERNAL_SERVER_ERROR], - ['test', 'soimething that doesn\'t match', '1', 'Repeated recovery key password does not match the provided recovery key password', HTTP::STATUS_INTERNAL_SERVER_ERROR], + ['', '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], ]; } @@ -79,11 +79,11 @@ class RecoveryControllerTest extends TestCase { public function changeRecoveryPasswordProvider() { return [ - ['test', 'test', 'oldtestFail', 'Could not change the password. Maybe the old password was not correct.', HTTP::STATUS_INTERNAL_SERVER_ERROR], + ['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_INTERNAL_SERVER_ERROR], - ['', 'test', 'oldtest', 'Please provide a new recovery password', HTTP::STATUS_INTERNAL_SERVER_ERROR], - ['test', 'test', '', 'Please provide the old recovery password', HTTP::STATUS_INTERNAL_SERVER_ERROR] + ['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] ]; } @@ -117,7 +117,7 @@ class RecoveryControllerTest extends TestCase { public function userSetRecoveryProvider() { return [ ['1', 'Recovery Key enabled', Http::STATUS_OK], - ['0', 'Could not enable the recovery key, please try again or contact your administrator', Http::STATUS_INTERNAL_SERVER_ERROR] + ['0', 'Could not enable the recovery key, please try again or contact your administrator', Http::STATUS_BAD_REQUEST] ]; }