diff options
author | Olivier Paroz <github@oparoz.com> | 2015-08-04 03:10:47 +0300 |
---|---|---|
committer | Olivier Paroz <github@oparoz.com> | 2015-08-04 03:10:47 +0300 |
commit | 8e1cf124675204d3c62b44909a5d2d72bc5832b2 (patch) | |
tree | 47bba5dbda4507b5ac87fe3673699b6301677e94 /tests/integration | |
parent | b8109e3373b940e1f0bf6b8629b77d549f9e4d70 (diff) |
Some integration tests for the environment builder
Diffstat (limited to 'tests/integration')
-rw-r--r-- | tests/integration/bootstrap.php | 177 | ||||
-rw-r--r-- | tests/integration/environment/EnvironmentTest.php | 132 |
2 files changed, 302 insertions, 7 deletions
diff --git a/tests/integration/bootstrap.php b/tests/integration/bootstrap.php index 6041134c..8f45fa78 100644 --- a/tests/integration/bootstrap.php +++ b/tests/integration/bootstrap.php @@ -16,12 +16,16 @@ require_once __DIR__ . '/../../../../lib/base.php'; use Test\TestCase; +use OCP\Share; use OCP\Files\IRootFolder; use OCP\Files\Folder; +use OCP\Files\File; +use OCP\IServerContainer; use OCP\AppFramework\IAppContainer; -use \OCA\Gallery\AppInfo\Application; +use OCA\Gallery\AppInfo\Application; +use OCA\Gallery\Environment\Environment; /** @@ -35,14 +39,22 @@ class GalleryIntegrationTest extends TestCase { protected $appName = 'gallery'; /** @var IAppContainer */ protected $container; + /** @var IServerContainer */ + protected $server; /** @var string */ protected $userId = 'test'; /** @var string */ - protected $userPassword = 'test'; + protected $userPassword = '1234'; + /** @var string */ + protected $sharerUserId = 'sharer'; + /** @var string */ + protected $sharerPassword = '5678'; /** @var IRootFolder */ protected $rootFolder; /** @var Folder|null */ protected $userFolder; + /** @var Environment */ + protected $environment; protected function setUp() { @@ -50,10 +62,28 @@ class GalleryIntegrationTest extends TestCase { $app = new Application($this->appName); $this->container = $app->getContainer(); + $this->server = $this->container->getServer(); + $this->rootFolder = $this->server->getRootFolder(); + + /** + * FIXME Anybody is welcome to fix the class to make it work with encryption + * + * This is the exception being thrown when trying to write to the filesystem + * OCA\Encryption\Exceptions\PrivateKeyMissingException: Private Key missing for user: please try to log-out and log-in again + */ + $this->server->getAppManager() + ->disableApp('encryption'); + + // This is because the filesystem is not properly cleaned up sometimes + $this->server->getAppManager() + ->disableApp('files_trashbin'); + + } - $this->rootFolder = $this->container->getServer() - ->getRootFolder(); + public function tearDown() { + $this->logout(); + parent::tearDown(); } /** @@ -80,9 +110,142 @@ class GalleryIntegrationTest extends TestCase { $this->loginAsUser($user); } - protected function setUserFolder() { - $this->userFolder = $this->rootFolder->newFolder('/' . $this->userId); - $this->userFolder->newFolder('/files'); + /** + * @param $userId + * + * @return Folder + */ + protected function setUserFolder($userId) { + $userFolder = $this->rootFolder->newFolder('/' . $userId); + $userFolder->newFolder('/files'); + + return $userFolder; + } + + /** + * @return mixed + */ + protected function setUserBasedEnv() { + $this->setupUser($this->userId, $this->userPassword); + $this->createEnv($this->userId, $this->userPassword); + $environment = $this->instantiateEnvironment($this->userId); + + $environment->setStandardEnv(); + + return $environment; + } + + /** + * @param strong $token + * + * @return mixed + */ + protected function setTokenBasedEnv($token) { + $environment = $this->instantiateEnvironment(null); + $linkItem = Share::getShareByToken($token, false); + + $environment->setTokenBasedEnv($linkItem); + + return $environment; + } + + /** + * Creates a token for a file + * + * @return bool|string + */ + protected function prepareFileToken() { + $sharedFolder = $this->createEnv($this->sharerUserId, $this->sharerPassword); + + /** @type File $sharedFile */ + $sharedFile = $sharedFolder->get('file1'); + $sharedFile->putContent('foobar'); + + $fileInfo = $sharedFile->getFileInfo(); + + $token = $this->getToken('file', $fileInfo['fileid']); + + $this->logout(); + + return $token; + } + + /** + * Creates a token for a folder + * + * @return bool|string + */ + protected function prepareFolderToken() { + $sharedFolder = $this->createEnv($this->sharerUserId, $this->sharerPassword); + $fileInfo = $sharedFolder->getFileInfo(); + + $token = $this->getToken('folder', $fileInfo['fileid']); + + $this->logout(); + + return $token; + } + + /** + * @param $userId + * + * @return mixed + */ + private function instantiateEnvironment($userId) { + $this->userFolder = $this->server->getUserFolder($userId); + + $this->container->registerService( + 'UserId', function ($c) { + return $this->userId; + } + ); + + $this->container->registerService( + 'userFolder', function ($c) { + return $this->userFolder; + } + ); + + return $this->container->query( + 'OCA\Gallery\Environment\Environment' + ); + } + + /** + * Creates a small folder/file hierarchy and returns the top folder + * + * @param string $userId + * @param string $userPassword + * + * @return Folder + */ + private function createEnv($userId, $userPassword) { + $this->setupUser($userId, $userPassword); + $userFolder = $this->server->getUserFolder($userId); + + $folder1 = $userFolder->newFolder('folder1'); + $folder1->newFile('file1'); + $subFolder = $folder1->newFolder('folder1.1'); + $subFolder->newFile('file1.1'); + + return $folder1; + } + + /** + * @param $nodeType + * @param $nodeId + * + * @return bool|string + */ + private function getToken($nodeType, $nodeId) { + // We need to make sure sharing via link is enabled + $this->server->getConfig() + ->setAppValue('core', 'shareapi_allow_links', 'yes'); + + return Share::shareItem( + $nodeType, $nodeId, \OCP\Share::SHARE_TYPE_LINK, $this->userId, + \OCP\Constants::PERMISSION_ALL + ); } }
\ No newline at end of file diff --git a/tests/integration/environment/EnvironmentTest.php b/tests/integration/environment/EnvironmentTest.php new file mode 100644 index 00000000..bf503da8 --- /dev/null +++ b/tests/integration/environment/EnvironmentTest.php @@ -0,0 +1,132 @@ +<?php +/** + * ownCloud - gallery + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Olivier Paroz <owncloud@interfasys.ch> + * + * @copyright Olivier Paroz 2015 + */ + +namespace OCA\Gallery\Environment; + +use \OCA\Gallery\Tests\Integration\GalleryIntegrationTest; + +/** + * Class EnvironmentTest + * + * @package OCA\Gallery\Tests\Integration + */ +class EnvironmentTest extends GalleryIntegrationTest { + + /*public function setUp() { + // Create user first + //$this->setupUser($this->userId, $this->userPassword); + + parent::setUp(); + }*/ + + /** + * Tests is setting up the environment using a normal user works + */ + public function testSetStandardEnv() { + $this->environment = $this->setUserBasedEnv(); + } + + /** + * Tests is setting up the environment using a token works + */ + public function testSetTokenBasedEnv() { + $token = $this->prepareFolderToken(); + $this->environment = $this->setTokenBasedEnv($token); + } + + public function testGetResourceFromIdAsALoggedInUser() { + $this->environment = $this->setUserBasedEnv(); + + $testFolder = $this->userFolder->newFolder('deleteMe'); + $testFolderId = $testFolder->getId(); + + $result = $this->environment->getResourceFromId($testFolderId); + $this->assertEquals($testFolder->getId(), $result->getId()); + + $testFolder->delete(); + } + + public function testGetResourceFromIdAsATokenUser() { + $token = $this->prepareFolderToken(); + $this->environment = $this->setTokenBasedEnv($token); + + $sharedFolder = $this->rootFolder->get($this->sharerUserId . '/files/folder1'); + $sharedFolderId = $sharedFolder->getId(); + + $result = $this->environment->getResourceFromId($sharedFolderId); + $this->assertEquals($sharedFolder->getId(), $result->getId()); + + $sharedFolder->delete(); + } + + public function testGetSharedNodeAsATokenUser() { + $token = $this->prepareFolderToken(); + $this->environment = $this->setTokenBasedEnv($token); + + $sharedFolder = $this->rootFolder->get($this->sharerUserId . '/files/folder1'); + $sharedFolderId = $sharedFolder->getId(); + + $result = $this->environment->getSharedNode(); + + $this->assertEquals($sharedFolderId, $result->getId()); + } + + /** + * We can't get the folder if we're given a file token + * + * @expects EnvironmentException + */ + public function testGetSharedNodeAsATokenUserWhenGivenFileToken() { + $token = $this->prepareFileToken(); + $this->environment = $this->setTokenBasedEnv($token); + + $this->environment->getSharedNode(); + } + + public function testGetVirtualRootFolderAsALoggedInUser() { + $this->environment = $this->setUserBasedEnv(); + + $result = $this->environment->getVirtualRootFolder(); + $userFolderId = $this->server->getUserFolder($this->userId) + ->getId(); + + $this->assertEquals($userFolderId, $result->getId()); + } + + public function testGetVirtualRootFolderAsATokenUser() { + $token = $this->prepareFolderToken(); + $this->environment = $this->setTokenBasedEnv($token); + + $result = $this->environment->getVirtualRootFolder(); + $userFolderId = $this->server->getUserFolder($this->sharerUserId); + $sharedFolder = $userFolderId->get('folder1') + ->getId(); + + $this->assertEquals($sharedFolder, $result->getId()); + } + + public function testGetUserIdAsALoggedInUser() { + $this->environment = $this->setUserBasedEnv(); + + $result = $this->environment->getUserId(); + + $this->assertEquals($this->userId, $result); + } + + public function testGetUserIdAsATokenUser() { + $token = $this->prepareFolderToken(); + $this->environment = $this->setTokenBasedEnv($token); + + $result = $this->environment->getUserId(); + $this->assertEquals($this->sharerUserId, $result); + } +}
\ No newline at end of file |