adding util test and reducing keymanager instances to one in test

This commit is contained in:
Clark Tomlinson 2015-04-01 13:07:54 -04:00 committed by Thomas Müller
parent 2511c32e61
commit e507dc11a0
2 changed files with 161 additions and 108 deletions

View File

@ -68,6 +68,7 @@ class KeyManagerTest extends TestCase {
\OC_App::disable('files_trashbin');
$userManager = \OC::$server->getUserManager();
$userManager->get(self::$testUser)->delete();
$userManager->createUser(self::$testUser,
self::$testUser);
@ -95,14 +96,8 @@ class KeyManagerTest extends TestCase {
$this->utilMock = $this->getMockBuilder('OCA\Encryption\Util')
->disableOriginalConstructor()
->getMock();
}
public function testDeleteShareKey() {
$this->keyStorageMock->expects($this->any())
->method('deleteFileKey')
->with($this->equalTo('/path'), $this->equalTo('keyId.shareKey'))
->willReturn(true);
$keymanager = new KeyManager(
$this->instance = new KeyManager(
$this->keyStorageMock,
$this->cryptMock,
$this->configMock,
@ -110,9 +105,16 @@ class KeyManagerTest extends TestCase {
$this->sessionMock,
$this->logMock,
$this->utilMock);
}
public function testDeleteShareKey() {
$this->keyStorageMock->expects($this->any())
->method('deleteFileKey')
->with($this->equalTo('/path'), $this->equalTo('keyId.shareKey'))
->willReturn(true);
$this->assertTrue(
$keymanager->deleteShareKey('/path', 'keyId')
$this->instance->deleteShareKey('/path', 'keyId')
);
}
@ -122,17 +124,10 @@ class KeyManagerTest extends TestCase {
->method('getUserKey')
->with($this->equalTo($this->userId), $this->equalTo('privateKey'))
->willReturn('privateKey');
$keymanager = new KeyManager(
$this->keyStorageMock,
$this->cryptMock,
$this->configMock,
$this->userMock,
$this->sessionMock,
$this->logMock,
$this->utilMock);
$this->assertSame('privateKey',
$keymanager->getPrivateKey($this->userId)
$this->instance->getPrivateKey($this->userId)
);
}
@ -141,17 +136,10 @@ class KeyManagerTest extends TestCase {
->method('getUserKey')
->with($this->equalTo($this->userId), $this->equalTo('publicKey'))
->willReturn('publicKey');
$keymanager = new KeyManager(
$this->keyStorageMock,
$this->cryptMock,
$this->configMock,
$this->userMock,
$this->sessionMock,
$this->logMock,
$this->utilMock);
$this->assertSame('publicKey',
$keymanager->getPublicKey($this->userId)
$this->instance->getPublicKey($this->userId)
);
}
@ -160,16 +148,9 @@ class KeyManagerTest extends TestCase {
->method('getSystemUserKey')
->with($this->equalTo($this->systemKeyId . '.publicKey'))
->willReturn('recoveryKey');
$keymanager = new KeyManager(
$this->keyStorageMock,
$this->cryptMock,
$this->configMock,
$this->userMock,
$this->sessionMock,
$this->logMock,
$this->utilMock);
$this->assertTrue($keymanager->recoveryKeyExists());
$this->assertTrue($this->instance->recoveryKeyExists());
}
/**
@ -184,16 +165,9 @@ class KeyManagerTest extends TestCase {
->method('decryptPrivateKey')
->with($this->equalTo('recoveryKey'), $this->equalTo('pass'))
->willReturn('decryptedRecoveryKey');
$keymanager = new KeyManager(
$this->keyStorageMock,
$this->cryptMock,
$this->configMock,
$this->userMock,
$this->sessionMock,
$this->logMock,
$this->utilMock);
$this->assertTrue($keymanager->checkRecoveryPassword('pass'));
$this->assertTrue($this->instance->checkRecoveryPassword('pass'));
}
@ -205,17 +179,10 @@ class KeyManagerTest extends TestCase {
$this->equalTo('publicKey'),
$this->equalTo('key'))
->willReturn(true);
$keymanager = new KeyManager(
$this->keyStorageMock,
$this->cryptMock,
$this->configMock,
$this->userMock,
$this->sessionMock,
$this->logMock,
$this->utilMock);
$this->assertTrue(
$keymanager->setPublicKey($this->userId, 'key')
$this->instance->setPublicKey($this->userId, 'key')
);
}
@ -228,17 +195,10 @@ class KeyManagerTest extends TestCase {
$this->equalTo('privateKey'),
$this->equalTo('key'))
->willReturn(true);
$keymanager = new KeyManager(
$this->keyStorageMock,
$this->cryptMock,
$this->configMock,
$this->userMock,
$this->sessionMock,
$this->logMock,
$this->utilMock);
$this->assertTrue(
$keymanager->setPrivateKey($this->userId, 'key')
$this->instance->setPrivateKey($this->userId, 'key')
);
}
@ -247,17 +207,10 @@ class KeyManagerTest extends TestCase {
->method('getUserKey')
->with($this->equalTo($this->userId), $this->anything())
->willReturn('key');
$keymanager = new KeyManager(
$this->keyStorageMock,
$this->cryptMock,
$this->configMock,
$this->userMock,
$this->sessionMock,
$this->logMock,
$this->utilMock);
$this->assertTrue(
$keymanager->userHasKeys($this->userId)
$this->instance->userHasKeys($this->userId)
);
}
@ -273,17 +226,10 @@ class KeyManagerTest extends TestCase {
->method('decryptPrivateKey')
->with($this->equalTo('privateKey'), $this->equalTo('pass'))
->willReturn('decryptedPrivateKey');
$keymanager = new KeyManager(
$this->keyStorageMock,
$this->cryptMock,
$this->configMock,
$this->userMock,
$this->sessionMock,
$this->logMock,
$this->utilMock);
$this->assertTrue(
$keymanager->init($this->userId, 'pass')
$this->instance->init($this->userId, 'pass')
);
}
@ -296,17 +242,10 @@ class KeyManagerTest extends TestCase {
->method('symmetricEncryptFileContent')
->with($this->equalTo('privateKey'), $this->equalTo('pass'))
->willReturn('decryptedPrivateKey');
$keymanager = new KeyManager(
$this->keyStorageMock,
$this->cryptMock,
$this->configMock,
$this->userMock,
$this->sessionMock,
$this->logMock,
$this->utilMock);
$this->assertTrue(
$keymanager->setRecoveryKey('pass', array('publicKey' => 'publicKey', 'privateKey' => 'privateKey'))
$this->instance->setRecoveryKey('pass', array('publicKey' => 'publicKey', 'privateKey' => 'privateKey'))
);
}
@ -315,17 +254,10 @@ class KeyManagerTest extends TestCase {
->method('setSystemUserKey')
->with($this->equalTo('keyId.privateKey'), $this->equalTo('key'))
->willReturn(true);
$keymanager = new KeyManager(
$this->keyStorageMock,
$this->cryptMock,
$this->configMock,
$this->userMock,
$this->sessionMock,
$this->logMock,
$this->utilMock);
$this->assertTrue(
$keymanager->setSystemPrivateKey('keyId', 'key')
$this->instance->setSystemPrivateKey('keyId', 'key')
);
}
@ -334,17 +266,10 @@ class KeyManagerTest extends TestCase {
->method('setSystemUserKey')
->with($this->equalTo('keyId.privateKey'))
->willReturn('systemPrivateKey');
$keymanager = new KeyManager(
$this->keyStorageMock,
$this->cryptMock,
$this->configMock,
$this->userMock,
$this->sessionMock,
$this->logMock,
$this->utilMock);
$this->assertSame('systemPrivateKey',
$keymanager->getSystemPrivateKey('keyId')
$this->instance->getSystemPrivateKey('keyId')
);
}

View File

@ -0,0 +1,128 @@
<?php
/**
* @author Clark Tomlinson <clark@owncloud.com>
* @since 3/31/15, 3:49 PM
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @license AGPL-3.0
*
* 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,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\Encryption\Tests;
use OCA\Encryption\Util;
use Test\TestCase;
class UtilTest extends TestCase {
private static $tempStorage = [];
private $configMock;
private $filesMock;
/**
* @var Util
*/
private $instance;
public function testSetRecoveryForUser() {
$this->instance->setRecoveryForUser('1');
$this->assertArrayHasKey('recoveryEnabled', self::$tempStorage);
}
/**
*
*/
public function testIsRecoveryEnabledForUser() {
$this->assertTrue($this->instance->isRecoveryEnabledForUser());
// Assert recovery will return default value if not set
unset(self::$tempStorage['recoveryEnabled']);
$this->assertEquals(0, $this->instance->isRecoveryEnabledForUser());
}
public function testUserHasFiles() {
$this->filesMock->expects($this->once())
->method('file_exists')
->will($this->returnValue(true));
$this->assertTrue($this->instance->userHasFiles('admin'));
}
protected function setUp() {
parent::setUp();
$this->filesMock = $this->getMock('OC\Files\View');
$cryptMock = $this->getMockBuilder('OCA\Encryption\Crypto\Crypt')
->disableOriginalConstructor()
->getMock();
$loggerMock = $this->getMock('OCP\ILogger');
$userSessionMock = $this->getMockBuilder('OCP\IUserSession')
->disableOriginalConstructor()
->setMethods([
'isLoggedIn',
'getUID',
'login',
'logout',
'setUser',
'getUser'
])
->getMock();
$userSessionMock->method('isLoggedIn')->will($this->returnValue(true));
$userSessionMock->method('getUID')->will($this->returnValue('admin'));
$userSessionMock->expects($this->any())
->method($this->anything())
->will($this->returnSelf());
$this->configMock = $configMock = $this->getMock('OCP\IConfig');
$this->configMock->expects($this->any())
->method('getUserValue')
->will($this->returnCallback([$this, 'getValueTester']));
$this->configMock->expects($this->any())
->method('setUserValue')
->will($this->returnCallback([$this, 'setValueTester']));
$this->instance = new Util($this->filesMock, $cryptMock, $loggerMock, $userSessionMock, $configMock);
}
/**
* @param $userId
* @param $app
* @param $key
* @param $value
*/
public function setValueTester($userId, $app, $key, $value) {
self::$tempStorage[$key] = $value;
}
/**
* @param $userId
* @param $app
* @param $key
* @param $default
* @return mixed
*/
public function getValueTester($userId, $app, $key, $default) {
if (!empty(self::$tempStorage[$key])) {
return self::$tempStorage[$key];
}
return $default ?: null;
}
}