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

@ -73,17 +73,20 @@ class RecoveryController extends Controller {
// 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);
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);
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);
return new DataResponse(['data' => ['message' => $errorMessage]],
500);
}
if (isset($adminEnableRecovery) && $adminEnableRecovery === '1') {
@ -97,6 +100,8 @@ class RecoveryController extends Controller {
}
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')]]);
}
/**
@ -127,7 +132,8 @@ class RecoveryController extends Controller {
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(
@ -166,7 +172,8 @@ class RecoveryController extends Controller {
'message' => (string)$this->l->t('Recovery Key enabled'))
)
);
} else {
}
return new DataResponse(
array(
'data' => array
@ -175,6 +182,5 @@ class RecoveryController extends Controller {
);
}
}
}
}

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