Merge pull request #18901 from owncloud/encryption-dav-tests

Add tests for doing dav requests with encryption enabled
remotes/origin/db-empty-migrate
Thomas Müller 2015-09-18 14:54:06 +07:00
commit d62cffbc9a
5 changed files with 143 additions and 9 deletions

@ -0,0 +1,25 @@
<?php
/**
* Copyright (c) 2015 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace Test\Connector\Sabre\RequestTest;
use OC\Files\View;
use Test\Traits\EncryptionTrait;
class EncryptionUploadTest extends UploadTest {
use EncryptionTrait;
protected function setupUser($name, $password) {
$this->createUser($name, $password);
$tmpFolder = \OC::$server->getTempManager()->getTemporaryFolder();
$this->registerMount($name, '\OC\Files\Storage\Local', '/' . $name, ['datadir' => $tmpFolder]);
$this->setupForUser($name, $password);
$this->loginWithEncryption($name);
return new View('/' . $name . '/files');
}
}

@ -52,7 +52,8 @@ abstract class RequestTest extends TestCase {
protected function setupUser($name, $password) {
$this->createUser($name, $password);
$this->registerMount($name, '\OC\Files\Storage\Temporary', '/' . $name);
$tmpFolder = \OC::$server->getTempManager()->getTemporaryFolder();
$this->registerMount($name, '\OC\Files\Storage\Local', '/' . $name, ['datadir' => $tmpFolder]);
$this->loginAsUser($name);
return new View('/' . $name . '/files');
}

@ -8,6 +8,8 @@
namespace Test\Connector\Sabre\RequestTest;
use OC\AppFramework\Http;
class UploadTest extends RequestTest {
public function testBasicUpload() {
$user = $this->getUniqueID();
@ -16,7 +18,7 @@ class UploadTest extends RequestTest {
$this->assertFalse($view->file_exists('foo.txt'));
$response = $this->request($view, $user, 'pass', 'PUT', '/foo.txt', 'asd');
$this->assertEquals(201, $response->getStatus());
$this->assertEquals(Http::STATUS_CREATED, $response->getStatus());
$this->assertTrue($view->file_exists('foo.txt'));
$this->assertEquals('asd', $view->file_get_contents('foo.txt'));
@ -33,7 +35,7 @@ class UploadTest extends RequestTest {
$response = $this->request($view, $user, 'pass', 'PUT', '/foo.txt', 'asd');
$this->assertEquals(204, $response->getStatus());
$this->assertEquals(Http::STATUS_NO_CONTENT, $response->getStatus());
$this->assertEquals('asd', $view->file_get_contents('foo.txt'));
$info = $view->getFileInfo('foo.txt');
@ -53,7 +55,7 @@ class UploadTest extends RequestTest {
$response = $this->request($view, $user, 'pass', 'PUT', '/foo.txt-chunking-123-2-1', 'bar', ['OC-Chunked' => '1']);
$this->assertEquals(201, $response->getStatus());
$this->assertEquals(Http::STATUS_CREATED, $response->getStatus());
$this->assertTrue($view->file_exists('foo.txt'));
$this->assertEquals('asdbar', $view->file_get_contents('foo.txt'));
@ -70,12 +72,12 @@ class UploadTest extends RequestTest {
$view->file_put_contents('foo.txt', 'bar');
$response = $this->request($view, $user, 'pass', 'PUT', '/foo.txt-chunking-123-2-0', 'asd', ['OC-Chunked' => '1']);
$this->assertEquals(201, $response->getStatus());
$this->assertEquals(Http::STATUS_CREATED, $response->getStatus());
$this->assertEquals('bar', $view->file_get_contents('foo.txt'));
$response = $this->request($view, $user, 'pass', 'PUT', '/foo.txt-chunking-123-2-1', 'bar', ['OC-Chunked' => '1']);
$this->assertEquals(201, $response->getStatus());
$this->assertEquals(Http::STATUS_CREATED, $response->getStatus());
$this->assertEquals('asdbar', $view->file_get_contents('foo.txt'));
@ -91,7 +93,7 @@ class UploadTest extends RequestTest {
$this->assertFalse($view->file_exists('foo.txt'));
$response = $this->request($view, $user, 'pass', 'PUT', '/foo.txt-chunking-123-2-1', 'bar', ['OC-Chunked' => '1']);
$this->assertEquals(201, $response->getStatus());
$this->assertEquals(Http::STATUS_CREATED, $response->getStatus());
$this->assertFalse($view->file_exists('foo.txt'));
$response = $this->request($view, $user, 'pass', 'PUT', '/foo.txt-chunking-123-2-0', 'asd', ['OC-Chunked' => '1']);

@ -0,0 +1,104 @@
<?php
/**
* Copyright (c) 2015 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace Test\Traits;
use OC\Encryption\Util;
use OC\Files\View;
use OCA\Encryption\AppInfo\Application;
use OCA\Encryption\KeyManager;
use OCA\Encryption\Users\Setup;
use OC\Files\Filesystem;
/**
* Enables encryption
*/
trait EncryptionTrait {
// from MountProviderTrait
abstract protected function registerStorageWrapper($name, $wrapper);
// from phpunit
abstract protected function markTestSkipped($reason = '');
abstract protected function assertTrue($condition, $message = '');
private $encryptionWasEnabled;
private $originalEncryptionModule;
/**
* @var \OCP\IConfig
*/
private $config;
/**
* @var \OCA\Encryption\AppInfo\Application
*/
private $encryptionApp;
protected function loginWithEncryption($user = '') {
\OC_Util::tearDownFS();
\OC_User::setUserId('');
// needed for fully logout
\OC::$server->getUserSession()->setUser(null);
Filesystem::tearDown();
\OC_User::setUserId($user);
$this->postLogin();
\OC_Util::setupFS($user);
if (\OC_User::userExists($user)) {
\OC::$server->getUserFolder($user);
}
}
protected function setupForUser($name, $password) {
\OC_Util::tearDownFS();
\OC_Util::setupFS($name);
$container = $this->encryptionApp->getContainer();
/** @var KeyManager $keyManager */
$keyManager = $container->query('KeyManager');
/** @var Setup $userSetup */
$userSetup = $container->query('UserSetup');
$userSetup->setupServerSide($name, $password);
$keyManager->init($name, $password);
}
protected function postLogin() {
$util = new Util(
new View(),
\OC::$server->getUserManager(),
\OC::$server->getGroupManager(),
\OC::$server->getConfig()
);
$this->registerStorageWrapper('oc_encryption', array($util, 'wrapStorage'));
}
protected function setUpEncryptionTrait() {
$isReady = \OC::$server->getEncryptionManager()->isReady();
if (!$isReady) {
$this->markTestSkipped('Encryption not ready');
}
\OC_App::loadApp('encryption');
$this->encryptionApp = new Application([], $isReady);
$this->config = \OC::$server->getConfig();
$this->encryptionWasEnabled = $this->config->getAppValue('core', 'encryption_enabled', 'no');
$this->originalEncryptionModule = $this->config->getAppValue('core', 'default_encryption_module');
$this->config->setAppValue('core', 'default_encryption_module', \OCA\Encryption\Crypto\Encryption::ID);
$this->config->setAppValue('core', 'encryption_enabled', 'yes');
$this->assertTrue(\OC::$server->getEncryptionManager()->isEnabled());
}
protected function tearDownEncryptionTrait() {
if ($this->config) {
$this->config->setAppValue('core', 'encryption_enabled', $this->encryptionWasEnabled);
$this->config->setAppValue('core', 'default_encryption_module', $this->originalEncryptionModule);
}
}
}

@ -32,7 +32,7 @@ trait MountProviderTrait {
if (!isset($this->mounts[$userId])) {
$this->mounts[$userId] = [];
}
$this->mounts[$userId][] = new MountPoint($storage, $mountPoint, $arguments, $this->storageFactory);
$this->mounts[$userId][] = ['storage' => $storage, 'mountPoint' => $mountPoint, 'arguments' => $arguments];
}
protected function registerStorageWrapper($name, $wrapper) {
@ -46,7 +46,9 @@ trait MountProviderTrait {
->method('getMountsForUser')
->will($this->returnCallback(function (IUser $user) {
if (isset($this->mounts[$user->getUID()])) {
return $this->mounts[$user->getUID()];
return array_map(function ($config) {
return new MountPoint($config['storage'], $config['mountPoint'], $config['arguments'], $this->storageFactory);
}, $this->mounts[$user->getUID()]);
} else {
return [];
}