diff options
author | Robin Appelman <icewind@owncloud.com> | 2015-11-02 13:13:06 +0100 |
---|---|---|
committer | Vincent Petry <pvince81@owncloud.com> | 2015-12-04 13:48:21 +0100 |
commit | a1898dc2bf9a89def29c1437903e560609f0cf40 (patch) | |
tree | 527d941bd240dd1c02760819724d21dfdd5d041c /apps/files_external/tests | |
parent | 98bb8372f7f0ab1f669cdd92d439814e1b6aaa1a (diff) | |
download | nextcloud-server-a1898dc2bf9a89def29c1437903e560609f0cf40.tar.gz nextcloud-server-a1898dc2bf9a89def29c1437903e560609f0cf40.zip |
db config backend for files_external
Diffstat (limited to 'apps/files_external/tests')
5 files changed, 350 insertions, 457 deletions
diff --git a/apps/files_external/tests/service/dbconfigservicetest.php b/apps/files_external/tests/service/dbconfigservicetest.php new file mode 100644 index 00000000000..d5b4ff1585d --- /dev/null +++ b/apps/files_external/tests/service/dbconfigservicetest.php @@ -0,0 +1,233 @@ +<?php +/** + * @author Robin Appelman <icewind@owncloud.com> + * + * @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\Files_External\Tests\Service; + + +use OCA\Files_External\Service\DBConfigService; +use OCP\IDBConnection; +use Test\TestCase; + +/** + * @group DB + */ +class DBConfigServiceTest extends TestCase { + /** + * @var DBConfigService + */ + private $dbConfig; + + /** + * @var IDBConnection + */ + private $connection; + + private $mounts = []; + + public function setUp() { + parent::setUp(); + $this->connection = \OC::$server->getDatabaseConnection(); + $this->dbConfig = new DBConfigService($this->connection); + } + + public function tearDown() { + foreach ($this->mounts as $mount) { + $this->dbConfig->removeMount($mount); + } + $this->mounts = []; + } + + private function addMount($mountPoint, $storageBackend, $authBackend, $priority, $type) { + $id = $this->dbConfig->addMount($mountPoint, $storageBackend, $authBackend, $priority, $type); + $this->mounts[] = $id; + return $id; + } + + public function testAddSimpleMount() { + $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); + + $mount = $this->dbConfig->getMountById($id); + $this->assertEquals('/test', $mount['mount_point']); + $this->assertEquals('foo', $mount['storage_backend']); + $this->assertEquals('bar', $mount['auth_backend']); + $this->assertEquals(100, $mount['priority']); + $this->assertEquals(DBConfigService::MOUNT_TYPE_ADMIN, $mount['type']); + $this->assertEquals([], $mount['applicable']); + $this->assertEquals([], $mount['config']); + $this->assertEquals([], $mount['options']); + } + + public function testAddApplicable() { + $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); + $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test'); + + $mount = $this->dbConfig->getMountById($id); + $this->assertEquals([ + ['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id] + ], $mount['applicable']); + + $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GROUP, 'bar'); + $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GLOBAL, null); + + $mount = $this->dbConfig->getMountById($id); + $this->assertEquals([ + ['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id], + ['type' => DBConfigService::APPLICABLE_TYPE_GROUP, 'value' => 'bar', 'mount_id' => $id], + ['type' => DBConfigService::APPLICABLE_TYPE_GLOBAL, 'value' => null, 'mount_id' => $id] + ], $mount['applicable']); + } + + public function testAddApplicableDouble() { + $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); + $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test'); + $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test'); + + $mount = $this->dbConfig->getMountById($id); + $this->assertEquals([ + ['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id] + ], $mount['applicable']); + } + + public function testDeleteMount() { + $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); + + $this->dbConfig->removeMount($id); + + $mount = $this->dbConfig->getMountById($id); + $this->assertEquals(null, $mount); + } + + public function testRemoveApplicable() { + $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); + $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test'); + $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test'); + + $mount = $this->dbConfig->getMountById($id); + $this->assertEquals([], $mount['applicable']); + } + + public function testSetConfig() { + $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); + $this->dbConfig->setConfig($id, 'foo', 'bar'); + + $mount = $this->dbConfig->getMountById($id); + $this->assertEquals(['foo' => 'bar'], $mount['config']); + + $this->dbConfig->setConfig($id, 'foo2', 'bar2'); + + $mount = $this->dbConfig->getMountById($id); + $this->assertEquals(['foo' => 'bar', 'foo2' => 'bar2'], $mount['config']); + } + + public function testSetConfigOverwrite() { + $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); + $this->dbConfig->setConfig($id, 'foo', 'bar'); + $this->dbConfig->setConfig($id, 'asd', '1'); + $this->dbConfig->setConfig($id, 'foo', 'qwerty'); + + $mount = $this->dbConfig->getMountById($id); + $this->assertEquals(['foo' => 'qwerty', 'asd' => '1'], $mount['config']); + } + + public function testSetOption() { + $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); + $this->dbConfig->setOption($id, 'foo', 'bar'); + + $mount = $this->dbConfig->getMountById($id); + $this->assertEquals(['foo' => 'bar'], $mount['options']); + + $this->dbConfig->setOption($id, 'foo2', 'bar2'); + + $mount = $this->dbConfig->getMountById($id); + $this->assertEquals(['foo' => 'bar', 'foo2' => 'bar2'], $mount['options']); + } + + public function testSetOptionOverwrite() { + $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); + $this->dbConfig->setOption($id, 'foo', 'bar'); + $this->dbConfig->setOption($id, 'asd', '1'); + $this->dbConfig->setOption($id, 'foo', 'qwerty'); + + $mount = $this->dbConfig->getMountById($id); + $this->assertEquals(['foo' => 'qwerty', 'asd' => '1'], $mount['options']); + } + + public function testGetMountsFor() { + $mounts = $this->dbConfig->getMountsFor(DBConfigService::APPLICABLE_TYPE_USER, 'test'); + $this->assertEquals([], $mounts); + + $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); + $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test'); + + $mounts = $this->dbConfig->getMountsFor(DBConfigService::APPLICABLE_TYPE_USER, 'test'); + $this->assertCount(1, $mounts); + $this->assertEquals($id, $mounts[0]['mount_id']); + $this->assertEquals([['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id]], $mounts[0]['applicable']); + } + + public function testGetAdminMounts() { + $id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); + $this->addMount('/test2', 'foo2', 'bar2', 100, DBConfigService::MOUNT_TYPE_PERSONAl); + + $mounts = $this->dbConfig->getAdminMounts(); + $this->assertCount(1, $mounts); + $this->assertEquals($id1, $mounts[0]['mount_id']); + } + + public function testGetAdminMountsFor() { + $id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); + $this->addMount('/test2', 'foo2', 'bar2', 100, DBConfigService::MOUNT_TYPE_ADMIN); + $id3 = $this->addMount('/test3', 'foo3', 'bar3', 100, DBConfigService::MOUNT_TYPE_PERSONAl); + + $this->dbConfig->addApplicable($id1, DBConfigService::APPLICABLE_TYPE_USER, 'test'); + $this->dbConfig->addApplicable($id3, DBConfigService::APPLICABLE_TYPE_USER, 'test'); + + $mounts = $this->dbConfig->getAdminMountsFor(DBConfigService::APPLICABLE_TYPE_USER, 'test'); + $this->assertCount(1, $mounts); + $this->assertEquals($id1, $mounts[0]['mount_id']); + $this->assertEquals([['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id1]], $mounts[0]['applicable']); + } + + public function testGetUserMountsFor() { + $id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); + $this->addMount('/test2', 'foo2', 'bar2', 100, DBConfigService::MOUNT_TYPE_PERSONAl); + $id3 = $this->addMount('/test3', 'foo3', 'bar3', 100, DBConfigService::MOUNT_TYPE_PERSONAl); + + $this->dbConfig->addApplicable($id1, DBConfigService::APPLICABLE_TYPE_USER, 'test'); + $this->dbConfig->addApplicable($id3, DBConfigService::APPLICABLE_TYPE_USER, 'test'); + + $mounts = $this->dbConfig->getUserMountsFor(DBConfigService::APPLICABLE_TYPE_USER, 'test'); + $this->assertCount(1, $mounts); + $this->assertEquals($id3, $mounts[0]['mount_id']); + $this->assertEquals([['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id3]], $mounts[0]['applicable']); + } + + public function testGetAdminMountsForGlobal() { + $id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); + + $this->dbConfig->addApplicable($id1, DBConfigService::APPLICABLE_TYPE_GLOBAL, null); + + $mounts = $this->dbConfig->getAdminMountsFor(DBConfigService::APPLICABLE_TYPE_GLOBAL, null); + $this->assertCount(1, $mounts); + $this->assertEquals($id1, $mounts[0]['mount_id']); + $this->assertEquals([['type' => DBConfigService::APPLICABLE_TYPE_GLOBAL, 'value' => null, 'mount_id' => $id1]], $mounts[0]['applicable']); + } +} diff --git a/apps/files_external/tests/service/globalstoragesservicetest.php b/apps/files_external/tests/service/globalstoragesservicetest.php index c129365913f..7c77616563c 100644 --- a/apps/files_external/tests/service/globalstoragesservicetest.php +++ b/apps/files_external/tests/service/globalstoragesservicetest.php @@ -23,14 +23,18 @@ namespace OCA\Files_external\Tests\Service; use \OC\Files\Filesystem; +use OCA\Files_External\Service\DBConfigService; use \OCA\Files_external\Service\GlobalStoragesService; use \OCA\Files_external\NotFoundException; use \OCA\Files_external\Lib\StorageConfig; +/** + * @group DB + */ class GlobalStoragesServiceTest extends StoragesServiceTest { public function setUp() { parent::setUp(); - $this->service = new GlobalStoragesService($this->backendService); + $this->service = new GlobalStoragesService($this->backendService, $this->dbConfig); } public function tearDown() { @@ -39,7 +43,7 @@ class GlobalStoragesServiceTest extends StoragesServiceTest { } protected function makeTestStorageData() { - return $this->makeStorageConfig([ + return $this->makeStorageConfig([ 'mountPoint' => 'mountpoint', 'backendIdentifier' => 'identifier:\OCA\Files_External\Lib\Backend\SMB', 'authMechanismIdentifier' => 'identifier:\Auth\Mechanism', @@ -133,10 +137,9 @@ class GlobalStoragesServiceTest extends StoragesServiceTest { $storage = $this->makeStorageConfig($storageParams); $newStorage = $this->service->addStorage($storage); - $this->assertEquals(1, $newStorage->getId()); - + $baseId = $newStorage->getId(); - $newStorage = $this->service->getStorage(1); + $newStorage = $this->service->getStorage($baseId); $this->assertEquals($storage->getMountPoint(), $newStorage->getMountPoint()); $this->assertEquals($storage->getBackend(), $newStorage->getBackend()); @@ -145,12 +148,10 @@ class GlobalStoragesServiceTest extends StoragesServiceTest { $this->assertEquals($storage->getApplicableUsers(), $newStorage->getApplicableUsers()); $this->assertEquals($storage->getApplicableGroups(), $newStorage->getApplicableGroups()); $this->assertEquals($storage->getPriority(), $newStorage->getPriority()); - $this->assertEquals(1, $newStorage->getId()); $this->assertEquals(0, $newStorage->getStatus()); - // next one gets id 2 $nextStorage = $this->service->addStorage($storage); - $this->assertEquals(2, $nextStorage->getId()); + $this->assertEquals($baseId + 1, $nextStorage->getId()); } /** @@ -173,19 +174,18 @@ class GlobalStoragesServiceTest extends StoragesServiceTest { ]); $newStorage = $this->service->addStorage($storage); - $this->assertEquals(1, $newStorage->getId()); + $id = $newStorage->getId(); - $updatedStorage->setId(1); + $updatedStorage->setId($id); $this->service->updateStorage($updatedStorage); - $newStorage = $this->service->getStorage(1); + $newStorage = $this->service->getStorage($id); $this->assertEquals($updatedStorage->getMountPoint(), $newStorage->getMountPoint()); $this->assertEquals($updatedStorage->getBackendOptions()['password'], $newStorage->getBackendOptions()['password']); $this->assertEquals($updatedStorage->getApplicableUsers(), $newStorage->getApplicableUsers()); $this->assertEquals($updatedStorage->getApplicableGroups(), $newStorage->getApplicableGroups()); $this->assertEquals($updatedStorage->getPriority(), $newStorage->getPriority()); - $this->assertEquals(1, $newStorage->getId()); $this->assertEquals(0, $newStorage->getStatus()); } @@ -442,7 +442,7 @@ class GlobalStoragesServiceTest extends StoragesServiceTest { $sourceApplicableGroups, $updatedApplicableUsers, $updatedApplicableGroups, - $expectedCalls) { + $expectedCalls) { $storage = $this->makeTestStorageData(); $storage->setApplicableUsers($sourceApplicableUsers); @@ -601,7 +601,7 @@ class GlobalStoragesServiceTest extends StoragesServiceTest { public function testHooksDeleteStorage( $sourceApplicableUsers, $sourceApplicableGroups, - $expectedCalls) { + $expectedCalls) { $storage = $this->makeTestStorageData(); $storage->setApplicableUsers($sourceApplicableUsers); @@ -626,321 +626,4 @@ class GlobalStoragesServiceTest extends StoragesServiceTest { } } - /** - * Make sure it uses the correct format when reading/writing - * the legacy config - */ - public function testLegacyConfigConversionApplicableAll() { - $configFile = $this->dataDir . '/mount.json'; - - $storage = $this->makeTestStorageData(); - $storage = $this->service->addStorage($storage); - - $json = json_decode(file_get_contents($configFile), true); - - $this->assertCount(1, $json); - - $this->assertEquals([\OC_Mount_Config::MOUNT_TYPE_USER], array_keys($json)); - $this->assertEquals(['all'], array_keys($json[\OC_Mount_config::MOUNT_TYPE_USER])); - - $mountPointData = $json[\OC_Mount_config::MOUNT_TYPE_USER]['all']; - $this->assertEquals(['/$user/files/mountpoint'], array_keys($mountPointData)); - - $mountPointOptions = current($mountPointData); - $this->assertEquals(1, $mountPointOptions['id']); - $this->assertEquals('identifier:\OCA\Files_External\Lib\Backend\SMB', $mountPointOptions['backend']); - $this->assertEquals('identifier:\Auth\Mechanism', $mountPointOptions['authMechanism']); - $this->assertEquals(15, $mountPointOptions['priority']); - $this->assertEquals(false, $mountPointOptions['mountOptions']['preview']); - - $backendOptions = $mountPointOptions['options']; - $this->assertEquals('value1', $backendOptions['option1']); - $this->assertEquals('value2', $backendOptions['option2']); - $this->assertEquals('', $backendOptions['password']); - $this->assertNotEmpty($backendOptions['password_encrypted']); - } - - /** - * Make sure it uses the correct format when reading/writing - * the legacy config - */ - public function testLegacyConfigConversionApplicableUserAndGroup() { - $configFile = $this->dataDir . '/mount.json'; - - $storage = $this->makeTestStorageData(); - $storage->setApplicableUsers(['user1', 'user2']); - $storage->setApplicableGroups(['group1', 'group2']); - - $storage = $this->service->addStorage($storage); - - $json = json_decode(file_get_contents($configFile), true); - - $this->assertCount(2, $json); - - $this->assertTrue(isset($json[\OC_Mount_Config::MOUNT_TYPE_USER])); - $this->assertTrue(isset($json[\OC_Mount_Config::MOUNT_TYPE_GROUP])); - $this->assertEquals(['user1', 'user2'], array_keys($json[\OC_Mount_config::MOUNT_TYPE_USER])); - $this->assertEquals(['group1', 'group2'], array_keys($json[\OC_Mount_config::MOUNT_TYPE_GROUP])); - - // check that all options are the same for both users and both groups - foreach ($json[\OC_Mount_Config::MOUNT_TYPE_USER] as $mountPointData) { - $this->assertEquals(['/$user/files/mountpoint'], array_keys($mountPointData)); - - $mountPointOptions = current($mountPointData); - - $this->assertEquals(1, $mountPointOptions['id']); - $this->assertEquals('identifier:\OCA\Files_External\Lib\Backend\SMB', $mountPointOptions['backend']); - $this->assertEquals('identifier:\Auth\Mechanism', $mountPointOptions['authMechanism']); - $this->assertEquals(15, $mountPointOptions['priority']); - $this->assertEquals(false, $mountPointOptions['mountOptions']['preview']); - - $backendOptions = $mountPointOptions['options']; - $this->assertEquals('value1', $backendOptions['option1']); - $this->assertEquals('value2', $backendOptions['option2']); - $this->assertEquals('', $backendOptions['password']); - $this->assertNotEmpty($backendOptions['password_encrypted']); - } - - foreach ($json[\OC_Mount_Config::MOUNT_TYPE_GROUP] as $mountPointData) { - $this->assertEquals(['/$user/files/mountpoint'], array_keys($mountPointData)); - - $mountPointOptions = current($mountPointData); - - $this->assertEquals(1, $mountPointOptions['id']); - $this->assertEquals('identifier:\OCA\Files_External\Lib\Backend\SMB', $mountPointOptions['backend']); - $this->assertEquals('identifier:\Auth\Mechanism', $mountPointOptions['authMechanism']); - $this->assertEquals(15, $mountPointOptions['priority']); - $this->assertEquals(false, $mountPointOptions['mountOptions']['preview']); - - $backendOptions = $mountPointOptions['options']; - $this->assertEquals('value1', $backendOptions['option1']); - $this->assertEquals('value2', $backendOptions['option2']); - $this->assertEquals('', $backendOptions['password']); - $this->assertNotEmpty($backendOptions['password_encrypted']); - } - } - - /** - * Test reading in a legacy config and generating config ids. - */ - public function testReadLegacyConfigAndGenerateConfigId() { - $configFile = $this->dataDir . '/mount.json'; - - $legacyBackendOptions = [ - 'user' => 'someuser', - 'password' => 'somepassword', - ]; - $legacyBackendOptions = \OC_Mount_Config::encryptPasswords($legacyBackendOptions); - - $legacyConfig = [ - 'backend' => 'identifier:\OCA\Files_External\Lib\Backend\SMB', - 'authMechanism' => 'identifier:\Auth\Mechanism', - 'options' => $legacyBackendOptions, - 'mountOptions' => ['preview' => false], - ]; - // different mount options - $legacyConfig2 = [ - 'backend' => 'identifier:\OCA\Files_External\Lib\Backend\SMB', - 'authMechanism' => 'identifier:\Auth\Mechanism', - 'options' => $legacyBackendOptions, - 'mountOptions' => ['preview' => true], - ]; - - $legacyBackendOptions2 = $legacyBackendOptions; - $legacyBackendOptions2 = ['user' => 'someuser2', 'password' => 'somepassword2']; - $legacyBackendOptions2 = \OC_Mount_Config::encryptPasswords($legacyBackendOptions2); - - // different config - $legacyConfig3 = [ - 'backend' => 'identifier:\OCA\Files_External\Lib\Backend\SMB', - 'authMechanism' => 'identifier:\Auth\Mechanism', - 'options' => $legacyBackendOptions2, - 'mountOptions' => ['preview' => true], - ]; - - $json = [ - 'user' => [ - 'user1' => [ - '/$user/files/somemount' => $legacyConfig, - ], - // same config - 'user2' => [ - '/$user/files/somemount' => $legacyConfig, - ], - // different mountOptions - 'user3' => [ - '/$user/files/somemount' => $legacyConfig2, - ], - // different mount point - 'user4' => [ - '/$user/files/anothermount' => $legacyConfig, - ], - // different storage config - 'user5' => [ - '/$user/files/somemount' => $legacyConfig3, - ], - ], - 'group' => [ - 'group1' => [ - // will get grouped with user configs - '/$user/files/somemount' => $legacyConfig, - ], - ], - ]; - - file_put_contents($configFile, json_encode($json)); - - $this->backendService->getBackend('identifier:\OCA\Files_External\Lib\Backend\SMB') - ->expects($this->exactly(4)) - ->method('validateStorageDefinition'); - $this->backendService->getAuthMechanism('identifier:\Auth\Mechanism') - ->expects($this->exactly(4)) - ->method('validateStorageDefinition'); - - $allStorages = $this->service->getAllStorages(); - - $this->assertCount(4, $allStorages); - - $storage1 = $allStorages[1]; - $storage2 = $allStorages[2]; - $storage3 = $allStorages[3]; - $storage4 = $allStorages[4]; - - $this->assertEquals('/somemount', $storage1->getMountPoint()); - $this->assertEquals('someuser', $storage1->getBackendOptions()['user']); - $this->assertEquals('somepassword', $storage1->getBackendOptions()['password']); - $this->assertEquals(['user1', 'user2'], $storage1->getApplicableUsers()); - $this->assertEquals(['group1'], $storage1->getApplicableGroups()); - $this->assertEquals(['preview' => false], $storage1->getMountOptions()); - - $this->assertEquals('/somemount', $storage2->getMountPoint()); - $this->assertEquals('someuser', $storage2->getBackendOptions()['user']); - $this->assertEquals('somepassword', $storage2->getBackendOptions()['password']); - $this->assertEquals(['user3'], $storage2->getApplicableUsers()); - $this->assertEquals([], $storage2->getApplicableGroups()); - $this->assertEquals(['preview' => true], $storage2->getMountOptions()); - - $this->assertEquals('/anothermount', $storage3->getMountPoint()); - $this->assertEquals('someuser', $storage3->getBackendOptions()['user']); - $this->assertEquals('somepassword', $storage3->getBackendOptions()['password']); - $this->assertEquals(['user4'], $storage3->getApplicableUsers()); - $this->assertEquals([], $storage3->getApplicableGroups()); - $this->assertEquals(['preview' => false], $storage3->getMountOptions()); - - $this->assertEquals('/somemount', $storage4->getMountPoint()); - $this->assertEquals('someuser2', $storage4->getBackendOptions()['user']); - $this->assertEquals('somepassword2', $storage4->getBackendOptions()['password']); - $this->assertEquals(['user5'], $storage4->getApplicableUsers()); - $this->assertEquals([], $storage4->getApplicableGroups()); - $this->assertEquals(['preview' => true], $storage4->getMountOptions()); - } - - public function testReadLegacyConfigNoAuthMechanism() { - $configFile = $this->dataDir . '/mount.json'; - - $json = [ - 'user' => [ - 'user1' => [ - '/$user/files/somemount' => [ - 'backend' => 'identifier:\OCA\Files_External\Lib\Backend\SFTP', - 'authMechanism' => 'identifier:\Auth\Mechanism', - 'options' => [], - 'mountOptions' => [], - ], - '/$user/files/othermount' => [ - 'backend' => 'identifier:\OCA\Files_External\Lib\Backend\SFTP', - // no authMechanism - 'options' => [], - 'mountOptions' => [], - ], - ] - ] - ]; - - file_put_contents($configFile, json_encode($json)); - - $allStorages = $this->service->getAllStorages(); - - $this->assertCount(2, $allStorages); - - $storage1 = $allStorages[1]; - $storage2 = $allStorages[2]; - - $this->assertEquals('/somemount', $storage1->getMountPoint()); - $this->assertEquals('identifier:\OCA\Files_External\Lib\Backend\SFTP', $storage1->getBackend()->getIdentifier()); - $this->assertEquals('identifier:\Auth\Mechanism', $storage1->getAuthMechanism()->getIdentifier()); - - $this->assertEquals('/othermount', $storage2->getMountPoint()); - $this->assertEquals('identifier:\OCA\Files_External\Lib\Backend\SFTP', $storage2->getBackend()->getIdentifier()); - $this->assertEquals('identifier:\Other\Auth\Mechanism', $storage2->getAuthMechanism()->getIdentifier()); - } - - public function testReadLegacyConfigClass() { - $configFile = $this->dataDir . '/mount.json'; - - $json = [ - 'user' => [ - 'user1' => [ - '/$user/files/somemount' => [ - 'class' => 'identifier:\OCA\Files_External\Lib\Backend\SFTP', - 'authMechanism' => 'identifier:\Auth\Mechanism', - 'options' => [], - 'mountOptions' => [], - ], - '/$user/files/othermount' => [ - 'class' => 'identifier:sftp_alias', - 'authMechanism' => 'identifier:\Auth\Mechanism', - 'options' => [], - 'mountOptions' => [], - ], - ] - ] - ]; - - file_put_contents($configFile, json_encode($json)); - - $allStorages = $this->service->getAllStorages(); - - $this->assertCount(2, $allStorages); - - $storage1 = $allStorages[1]; - $storage2 = $allStorages[2]; - - $this->assertEquals('/somemount', $storage1->getMountPoint()); - $this->assertEquals('identifier:\OCA\Files_External\Lib\Backend\SFTP', $storage1->getBackend()->getIdentifier()); - $this->assertEquals('identifier:\Auth\Mechanism', $storage1->getAuthMechanism()->getIdentifier()); - - $this->assertEquals('/othermount', $storage2->getMountPoint()); - $this->assertEquals('identifier:\OCA\Files_External\Lib\Backend\SFTP', $storage2->getBackend()->getIdentifier()); - $this->assertEquals('identifier:\Auth\Mechanism', $storage2->getAuthMechanism()->getIdentifier()); - } - - public function testReadEmptyMountPoint() { - $configFile = $this->dataDir . '/mount.json'; - - $json = [ - 'user' => [ - 'user1' => [ - '/$user/files/' => [ - 'backend' => 'identifier:\OCA\Files_External\Lib\Backend\SFTP', - 'authMechanism' => 'identifier:\Auth\Mechanism', - 'options' => [], - 'mountOptions' => [], - ], - ] - ] - ]; - - file_put_contents($configFile, json_encode($json)); - - $allStorages = $this->service->getAllStorages(); - - $this->assertCount(1, $allStorages); - - $storage1 = $allStorages[1]; - - $this->assertEquals('/', $storage1->getMountPoint()); - } - - } diff --git a/apps/files_external/tests/service/storagesservicetest.php b/apps/files_external/tests/service/storagesservicetest.php index 7487ba459af..7847bd45d4a 100644 --- a/apps/files_external/tests/service/storagesservicetest.php +++ b/apps/files_external/tests/service/storagesservicetest.php @@ -25,8 +25,29 @@ use \OC\Files\Filesystem; use \OCA\Files_external\NotFoundException; use \OCA\Files_external\Lib\StorageConfig; -use \OCA\Files_External\Lib\BackendService; +use OCA\Files_External\Service\BackendService; +use OCA\Files_External\Service\DBConfigService; +use OCA\Files_external\Service\StoragesService; +class CleaningDBConfig extends DBConfigService { + private $mountIds = []; + + public function addMount($mountPoint, $storageBackend, $authBackend, $priority, $type) { + $id = parent::addMount($mountPoint, $storageBackend, $authBackend, $priority, $type); // TODO: Change the autogenerated stub + $this->mountIds[] = $id; + return $id; + } + + public function clean() { + foreach ($this->mountIds as $id) { + $this->removeMount($id); + } + } +} + +/** + * @group DB + */ abstract class StoragesServiceTest extends \Test\TestCase { /** @@ -44,6 +65,9 @@ abstract class StoragesServiceTest extends \Test\TestCase { */ protected $dataDir; + /** @var CleaningDBConfig */ + protected $dbConfig; + /** * Hook calls * @@ -52,6 +76,8 @@ abstract class StoragesServiceTest extends \Test\TestCase { protected static $hookCalls; public function setUp() { + parent::setUp(); + $this->dbConfig = new CleaningDBConfig(\OC::$server->getDatabaseConnection()); self::$hookCalls = array(); $config = \OC::$server->getConfig(); $this->dataDir = $config->getSystemValue( @@ -63,8 +89,8 @@ abstract class StoragesServiceTest extends \Test\TestCase { // prepare BackendService mock $this->backendService = $this->getMockBuilder('\OCA\Files_External\Service\BackendService') - ->disableOriginalConstructor() - ->getMock(); + ->disableOriginalConstructor() + ->getMock(); $authMechanisms = [ 'identifier:\Auth\Mechanism' => $this->getAuthMechMock('null', '\Auth\Mechanism'), @@ -72,14 +98,14 @@ abstract class StoragesServiceTest extends \Test\TestCase { 'identifier:\OCA\Files_External\Lib\Auth\NullMechanism' => $this->getAuthMechMock(), ]; $this->backendService->method('getAuthMechanism') - ->will($this->returnCallback(function($class) use ($authMechanisms) { + ->will($this->returnCallback(function ($class) use ($authMechanisms) { if (isset($authMechanisms[$class])) { return $authMechanisms[$class]; } return null; })); $this->backendService->method('getAuthMechanismsByScheme') - ->will($this->returnCallback(function($schemes) use ($authMechanisms) { + ->will($this->returnCallback(function ($schemes) use ($authMechanisms) { return array_filter($authMechanisms, function ($authMech) use ($schemes) { return in_array($authMech->getScheme(), $schemes, true); }); @@ -96,7 +122,7 @@ abstract class StoragesServiceTest extends \Test\TestCase { $backends['identifier:\OCA\Files_External\Lib\Backend\SFTP']->method('getLegacyAuthMechanism') ->willReturn($authMechanisms['identifier:\Other\Auth\Mechanism']); $this->backendService->method('getBackend') - ->will($this->returnCallback(function($backendClass) use ($backends) { + ->will($this->returnCallback(function ($backendClass) use ($backends) { if (isset($backends[$backendClass])) { return $backends[$backendClass]; } @@ -116,7 +142,7 @@ abstract class StoragesServiceTest extends \Test\TestCase { $containerMock = $this->getMock('\OCP\AppFramework\IAppContainer'); $containerMock->method('query') - ->will($this->returnCallback(function($name) { + ->will($this->returnCallback(function ($name) { if ($name === 'OCA\Files_External\Service\BackendService') { return $this->backendService; } @@ -132,6 +158,9 @@ abstract class StoragesServiceTest extends \Test\TestCase { public function tearDown() { \OC_Mount_Config::$skipTest = false; self::$hookCalls = array(); + if ($this->dbConfig) { + $this->dbConfig->clean(); + } } protected function getBackendMock($class = '\OCA\Files_External\Lib\Backend\SMB', $storageClass = '\OC\Files\Storage\SMB') { @@ -141,7 +170,7 @@ abstract class StoragesServiceTest extends \Test\TestCase { $backend->method('getStorageClass') ->willReturn($storageClass); $backend->method('getIdentifier') - ->willReturn('identifier:'.$class); + ->willReturn('identifier:' . $class); return $backend; } @@ -152,7 +181,7 @@ abstract class StoragesServiceTest extends \Test\TestCase { $authMech->method('getScheme') ->willReturn($scheme); $authMech->method('getIdentifier') - ->willReturn('identifier:'.$class); + ->willReturn('identifier:' . $class); return $authMech; } @@ -258,7 +287,7 @@ abstract class StoragesServiceTest extends \Test\TestCase { $storage->setBackendOptions($backendOptions); $newStorage = $this->service->addStorage($storage); - $this->assertEquals(1, $newStorage->getId()); + $id = $newStorage->getId(); // manually trigger storage entry because normally it happens on first // access, which isn't possible within this test @@ -267,7 +296,7 @@ abstract class StoragesServiceTest extends \Test\TestCase { // get numeric id for later check $numericId = $storageCache->getNumericId(); - $newStorage = $this->service->removeStorage(1); + $this->service->removeStorage($id); $caught = false; try { @@ -317,7 +346,7 @@ abstract class StoragesServiceTest extends \Test\TestCase { $priority ); - $this->assertEquals('/'.$mountPoint, $storage->getMountPoint()); + $this->assertEquals('/' . $mountPoint, $storage->getMountPoint()); $this->assertEquals($backend, $storage->getBackend()); $this->assertEquals($authMechanism, $storage->getAuthMechanism()); $this->assertEquals($backendOptions, $storage->getBackendOptions()); diff --git a/apps/files_external/tests/service/userglobalstoragesservicetest.php b/apps/files_external/tests/service/userglobalstoragesservicetest.php index e88764d0f78..b8379288d43 100644 --- a/apps/files_external/tests/service/userglobalstoragesservicetest.php +++ b/apps/files_external/tests/service/userglobalstoragesservicetest.php @@ -21,17 +21,33 @@ */ namespace OCA\Files_External\Tests\Service; +use OCA\Files_external\Service\StoragesService; use \OCA\Files_External\Service\UserGlobalStoragesService; use \OCP\IGroupManager; use \OCA\Files_External\Lib\StorageConfig; +use OCP\IUser; +use Test\Traits\UserTrait; +/** + * @group DB + */ class UserGlobalStoragesServiceTest extends GlobalStoragesServiceTest { + use UserTrait; + /** @var \OCP\IGroupManager|\PHPUnit_Framework_MockObject_MockObject groupManager */ protected $groupManager; + /** + * @var StoragesService + */ protected $globalStoragesService; + /** + * @var UserGlobalStoragesService + */ + protected $service; + protected $user; const USER_ID = 'test_user'; @@ -44,6 +60,7 @@ class UserGlobalStoragesServiceTest extends GlobalStoragesServiceTest { $this->globalStoragesService = $this->service; $this->user = new \OC\User\User(self::USER_ID, null); + /** @var \OCP\IUserSession|\PHPUnit_Framework_MockObject_MockObject $userSession */ $userSession = $this->getMock('\OCP\IUserSession'); $userSession ->expects($this->any()) @@ -52,19 +69,28 @@ class UserGlobalStoragesServiceTest extends GlobalStoragesServiceTest { $this->groupManager = $this->getMock('\OCP\IGroupManager'); $this->groupManager->method('isInGroup') - ->will($this->returnCallback(function($userId, $groupId) { + ->will($this->returnCallback(function ($userId, $groupId) { if ($userId === self::USER_ID) { switch ($groupId) { - case self::GROUP_ID: - case self::GROUP_ID2: - return true; + case self::GROUP_ID: + case self::GROUP_ID2: + return true; } } return false; })); + $this->groupManager->method('getUserGroupIds') + ->will($this->returnCallback(function (IUser $user) { + if ($user->getUID() === self::USER_ID) { + return [self::GROUP_ID, self::GROUP_ID2]; + } else { + return []; + } + })); $this->service = new UserGlobalStoragesService( $this->backendService, + $this->dbConfig, $userSession, $this->groupManager ); @@ -156,6 +182,13 @@ class UserGlobalStoragesServiceTest extends GlobalStoragesServiceTest { /** * @expectedException \DomainException + */ + public function testNonExistingStorage() { + parent::testNonExistingStorage(); + } + + /** + * @expectedException \DomainException * @dataProvider deleteStorageDataProvider */ public function testDeleteStorage($backendOptions, $rustyStorageId, $expectedCountAfterDeletion) { @@ -169,9 +202,16 @@ class UserGlobalStoragesServiceTest extends GlobalStoragesServiceTest { $storage->setBackendOptions($backendOptions); $newStorage = $this->globalStoragesService->addStorage($storage); - $this->assertEquals(1, $newStorage->getId()); + $id = $newStorage->getId(); - $this->service->removeStorage(1); + $this->service->removeStorage($id); + } + + /** + * @expectedException \DomainException + */ + public function testDeleteUnexistingStorage() { + parent::testDeleteUnexistingStorage(); } public function getUniqueStoragesProvider() { diff --git a/apps/files_external/tests/service/userstoragesservicetest.php b/apps/files_external/tests/service/userstoragesservicetest.php index 78f9231c3d1..5e984c52bfd 100644 --- a/apps/files_external/tests/service/userstoragesservicetest.php +++ b/apps/files_external/tests/service/userstoragesservicetest.php @@ -26,36 +26,33 @@ use \OC\Files\Filesystem; use \OCA\Files_external\Service\UserStoragesService; use \OCA\Files_external\NotFoundException; use \OCA\Files_external\Lib\StorageConfig; +use Test\Traits\UserTrait; +/** + * @group DB + */ class UserStoragesServiceTest extends StoragesServiceTest { + use UserTrait; + + private $user; + + private $userId; public function setUp() { parent::setUp(); - $userManager = \OC::$server->getUserManager(); - $this->userId = $this->getUniqueID('user_'); - $this->user = $userManager->createUser( - $this->userId, - $this->userId - ); + $this->createUser($this->userId, $this->userId); + $this->user = \OC::$server->getUserManager()->get($this->userId); + /** @var \OCP\IUserSession|\PHPUnit_Framework_MockObject_MockObject $userSession */ $userSession = $this->getMock('\OCP\IUserSession'); $userSession ->expects($this->any()) ->method('getUser') ->will($this->returnValue($this->user)); - $this->service = new UserStoragesService($this->backendService, $userSession); - - // create home folder - mkdir($this->dataDir . '/' . $this->userId . '/'); - } - - public function tearDown() { - @unlink($this->dataDir . '/' . $this->userId . '/mount.json'); - $this->user->delete(); - parent::tearDown(); + $this->service = new UserStoragesService($this->backendService, $this->dbConfig, $userSession); } private function makeTestStorageData() { @@ -79,15 +76,14 @@ class UserStoragesServiceTest extends StoragesServiceTest { $newStorage = $this->service->addStorage($storage); - $this->assertEquals(1, $newStorage->getId()); + $id = $newStorage->getId(); - $newStorage = $this->service->getStorage(1); + $newStorage = $this->service->getStorage($id); $this->assertEquals($storage->getMountPoint(), $newStorage->getMountPoint()); $this->assertEquals($storage->getBackend(), $newStorage->getBackend()); $this->assertEquals($storage->getAuthMechanism(), $newStorage->getAuthMechanism()); $this->assertEquals($storage->getBackendOptions(), $newStorage->getBackendOptions()); - $this->assertEquals(1, $newStorage->getId()); $this->assertEquals(0, $newStorage->getStatus()); // hook called once for user @@ -99,9 +95,8 @@ class UserStoragesServiceTest extends StoragesServiceTest { $this->userId ); - // next one gets id 2 $nextStorage = $this->service->addStorage($storage); - $this->assertEquals(2, $nextStorage->getId()); + $this->assertEquals($id + 1, $nextStorage->getId()); } public function testUpdateStorage() { @@ -117,7 +112,6 @@ class UserStoragesServiceTest extends StoragesServiceTest { ]); $newStorage = $this->service->addStorage($storage); - $this->assertEquals(1, $newStorage->getId()); $backendOptions = $newStorage->getBackendOptions(); $backendOptions['password'] = 'anotherPassword'; @@ -131,7 +125,6 @@ class UserStoragesServiceTest extends StoragesServiceTest { // these attributes are unused for user storages $this->assertEmpty($newStorage->getApplicableUsers()); $this->assertEmpty($newStorage->getApplicableGroups()); - $this->assertEquals(1, $newStorage->getId()); $this->assertEquals(0, $newStorage->getStatus()); // no hook calls @@ -181,89 +174,4 @@ class UserStoragesServiceTest extends StoragesServiceTest { $this->userId ); } - - /** - * Make sure it uses the correct format when reading/writing - * the legacy config - */ - public function testLegacyConfigConversion() { - $configFile = $this->dataDir . '/' . $this->userId . '/mount.json'; - - $storage = $this->makeTestStorageData(); - $storage = $this->service->addStorage($storage); - - $json = json_decode(file_get_contents($configFile), true); - - $this->assertCount(1, $json); - - $this->assertEquals([\OC_Mount_Config::MOUNT_TYPE_USER], array_keys($json)); - $this->assertEquals([$this->userId], array_keys($json[\OC_Mount_config::MOUNT_TYPE_USER])); - - $mountPointData = $json[\OC_Mount_config::MOUNT_TYPE_USER][$this->userId]; - $this->assertEquals(['/' . $this->userId . '/files/mountpoint'], array_keys($mountPointData)); - - $mountPointOptions = current($mountPointData); - $this->assertEquals(1, $mountPointOptions['id']); - $this->assertEquals('identifier:\OCA\Files_External\Lib\Backend\SMB', $mountPointOptions['backend']); - $this->assertEquals('identifier:\Auth\Mechanism', $mountPointOptions['authMechanism']); - $this->assertEquals(false, $mountPointOptions['mountOptions']['preview']); - - $backendOptions = $mountPointOptions['options']; - $this->assertEquals('value1', $backendOptions['option1']); - $this->assertEquals('value2', $backendOptions['option2']); - $this->assertEquals('', $backendOptions['password']); - $this->assertNotEmpty($backendOptions['password_encrypted']); - } - - /** - * Test reading in a legacy config and generating config ids. - */ - public function testReadLegacyConfigAndGenerateConfigId() { - $configFile = $this->dataDir . '/' . $this->userId . '/mount.json'; - - $legacyBackendOptions = [ - 'user' => 'someuser', - 'password' => 'somepassword', - ]; - $legacyBackendOptions = \OC_Mount_Config::encryptPasswords($legacyBackendOptions); - - $legacyConfig = [ - 'backend' => 'identifier:\OCA\Files_External\Lib\Backend\SMB', - 'authMechanism' => 'identifier:\Auth\Mechanism', - 'options' => $legacyBackendOptions, - 'mountOptions' => ['preview' => false], - ]; - // different mount options - $legacyConfig2 = [ - 'backend' => 'identifier:\OCA\Files_External\Lib\Backend\SMB', - 'authMechanism' => 'identifier:\Auth\Mechanism', - 'options' => $legacyBackendOptions, - 'mountOptions' => ['preview' => true], - ]; - - $json = ['user' => []]; - $json['user'][$this->userId] = [ - '/$user/files/somemount' => $legacyConfig, - '/$user/files/anothermount' => $legacyConfig2, - ]; - - file_put_contents($configFile, json_encode($json)); - - $allStorages = $this->service->getAllStorages(); - - $this->assertCount(2, $allStorages); - - $storage1 = $allStorages[1]; - $storage2 = $allStorages[2]; - - $this->assertEquals('/somemount', $storage1->getMountPoint()); - $this->assertEquals('someuser', $storage1->getBackendOptions()['user']); - $this->assertEquals('somepassword', $storage1->getBackendOptions()['password']); - $this->assertEquals(['preview' => false], $storage1->getMountOptions()); - - $this->assertEquals('/anothermount', $storage2->getMountPoint()); - $this->assertEquals('someuser', $storage2->getBackendOptions()['user']); - $this->assertEquals('somepassword', $storage2->getBackendOptions()['password']); - $this->assertEquals(['preview' => true], $storage2->getMountOptions()); - } } |