fixing return values and adding tests

This commit is contained in:
Clark Tomlinson 2015-04-20 10:23:09 -04:00
parent 750f0bc489
commit c81bc152d7
2 changed files with 220 additions and 32 deletions

View File

@ -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'))
)
);
}
}

View File

@ -0,0 +1,182 @@
<?php
/**
* @author Clark Tomlinson <clark@owncloud.com>
*
* @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);
}
}