diff options
Diffstat (limited to 'apps/files_external/tests/service')
4 files changed, 466 insertions, 0 deletions
diff --git a/apps/files_external/tests/service/backendservicetest.php b/apps/files_external/tests/service/backendservicetest.php new file mode 100644 index 00000000000..08f6b9bf988 --- /dev/null +++ b/apps/files_external/tests/service/backendservicetest.php @@ -0,0 +1,152 @@ +<?php +/** + * @author Robin McCorkell <rmccorkell@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\BackendService; + +class BackendServiceTest extends \Test\TestCase { + + /** @var \OCP\IConfig */ + protected $config; + + /** @var \OCP\IL10N */ + protected $l10n; + + protected function setUp() { + $this->config = $this->getMock('\OCP\IConfig'); + $this->l10n = $this->getMock('\OCP\IL10N'); + } + + protected function getBackendMock($class) { + $backend = $this->getMockBuilder('\OCA\Files_External\Lib\Backend\Backend') + ->disableOriginalConstructor() + ->getMock(); + $backend->method('getIdentifier')->will($this->returnValue('identifier:'.$class)); + $backend->method('getIdentifierAliases')->will($this->returnValue(['identifier:'.$class])); + return $backend; + } + + public function testRegisterBackend() { + $service = new BackendService($this->config, $this->l10n); + + $backend = $this->getBackendMock('\Foo\Bar'); + + $backendAlias = $this->getMockBuilder('\OCA\Files_External\Lib\Backend\Backend') + ->disableOriginalConstructor() + ->getMock(); + $backendAlias->method('getIdentifierAliases') + ->willReturn(['identifier_real', 'identifier_alias']); + $backendAlias->method('getIdentifier') + ->willReturn('identifier_real'); + + $service->registerBackend($backend); + $service->registerBackend($backendAlias); + + $this->assertEquals($backend, $service->getBackend('identifier:\Foo\Bar')); + $this->assertEquals($backendAlias, $service->getBackend('identifier_real')); + $this->assertEquals($backendAlias, $service->getBackend('identifier_alias')); + + $backends = $service->getBackends(); + $this->assertCount(2, $backends); + $this->assertArrayHasKey('identifier:\Foo\Bar', $backends); + $this->assertArrayHasKey('identifier_real', $backends); + $this->assertArrayNotHasKey('identifier_alias', $backends); + } + + public function testUserMountingBackends() { + $this->config->expects($this->exactly(2)) + ->method('getAppValue') + ->will($this->returnValueMap([ + ['files_external', 'allow_user_mounting', 'yes', 'yes'], + ['files_external', 'user_mounting_backends', '', 'identifier:\User\Mount\Allowed,identifier_alias'] + ])); + + $service = new BackendService($this->config, $this->l10n); + + $backendAllowed = $this->getBackendMock('\User\Mount\Allowed'); + $backendAllowed->expects($this->never()) + ->method('removeVisibility'); + $backendNotAllowed = $this->getBackendMock('\User\Mount\NotAllowed'); + $backendNotAllowed->expects($this->once()) + ->method('removeVisibility') + ->with(BackendService::VISIBILITY_PERSONAL); + + $backendAlias = $this->getMockBuilder('\OCA\Files_External\Lib\Backend\Backend') + ->disableOriginalConstructor() + ->getMock(); + $backendAlias->method('getIdentifierAliases') + ->willReturn(['identifier_real', 'identifier_alias']); + $backendAlias->expects($this->never()) + ->method('removeVisibility'); + + $service->registerBackend($backendAllowed); + $service->registerBackend($backendNotAllowed); + $service->registerBackend($backendAlias); + } + + public function testGetAvailableBackends() { + $service = new BackendService($this->config, $this->l10n); + + $backendAvailable = $this->getBackendMock('\Backend\Available'); + $backendAvailable->expects($this->once()) + ->method('checkDependencies') + ->will($this->returnValue([])); + $backendNotAvailable = $this->getBackendMock('\Backend\NotAvailable'); + $backendNotAvailable->expects($this->once()) + ->method('checkDependencies') + ->will($this->returnValue([ + $this->getMockBuilder('\OCA\Files_External\Lib\MissingDependency') + ->disableOriginalConstructor() + ->getMock() + ])); + + $service->registerBackend($backendAvailable); + $service->registerBackend($backendNotAvailable); + + $availableBackends = $service->getAvailableBackends(); + $this->assertArrayHasKey('identifier:\Backend\Available', $availableBackends); + $this->assertArrayNotHasKey('identifier:\Backend\NotAvailable', $availableBackends); + } + + public function testGetUserBackends() { + $service = new BackendService($this->config, $this->l10n); + + $backendAllowed = $this->getBackendMock('\User\Mount\Allowed'); + $backendAllowed->expects($this->once()) + ->method('isVisibleFor') + ->with(BackendService::VISIBILITY_PERSONAL) + ->will($this->returnValue(true)); + $backendNotAllowed = $this->getBackendMock('\User\Mount\NotAllowed'); + $backendNotAllowed->expects($this->once()) + ->method('isVisibleFor') + ->with(BackendService::VISIBILITY_PERSONAL) + ->will($this->returnValue(false)); + + $service->registerBackend($backendAllowed); + $service->registerBackend($backendNotAllowed); + + $userBackends = $service->getBackendsVisibleFor(BackendService::VISIBILITY_PERSONAL); + $this->assertArrayHasKey('identifier:\User\Mount\Allowed', $userBackends); + $this->assertArrayNotHasKey('identifier:\User\Mount\NotAllowed', $userBackends); + } + +} + diff --git a/apps/files_external/tests/service/globalstoragesservicetest.php b/apps/files_external/tests/service/globalstoragesservicetest.php index f6ed9f1422a..05585d2c065 100644 --- a/apps/files_external/tests/service/globalstoragesservicetest.php +++ b/apps/files_external/tests/service/globalstoragesservicetest.php @@ -827,6 +827,46 @@ class GlobalStoragesServiceTest extends StoragesServiceTest { $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'; diff --git a/apps/files_external/tests/service/storagesservicetest.php b/apps/files_external/tests/service/storagesservicetest.php index 397f2a2e5c5..07286106c7b 100644 --- a/apps/files_external/tests/service/storagesservicetest.php +++ b/apps/files_external/tests/service/storagesservicetest.php @@ -1,6 +1,7 @@ <?php /** * @author Vincent Petry <pvince81@owncloud.com> + * @author Robin McCorkell <rmccorkell@owncloud.com> * * @copyright Copyright (c) 2015, ownCloud, Inc. * @license AGPL-3.0 @@ -241,6 +242,64 @@ abstract class StoragesServiceTest extends \Test\TestCase { $this->service->removeStorage(255); } + public function testCreateStorage() { + $mountPoint = 'mount'; + $backendIdentifier = 'identifier:\OCA\Files_External\Lib\Backend\SMB'; + $authMechanismIdentifier = 'identifier:\Auth\Mechanism'; + $backendOptions = ['param' => 'foo', 'param2' => 'bar']; + $mountOptions = ['option' => 'foobar']; + $applicableUsers = ['user1', 'user2']; + $applicableGroups = ['group']; + $priority = 123; + + $backend = $this->backendService->getBackend($backendIdentifier); + $authMechanism = $this->backendService->getAuthMechanism($authMechanismIdentifier); + + $storage = $this->service->createStorage( + $mountPoint, + $backendIdentifier, + $authMechanismIdentifier, + $backendOptions, + $mountOptions, + $applicableUsers, + $applicableGroups, + $priority + ); + + $this->assertEquals('/'.$mountPoint, $storage->getMountPoint()); + $this->assertEquals($backend, $storage->getBackend()); + $this->assertEquals($authMechanism, $storage->getAuthMechanism()); + $this->assertEquals($backendOptions, $storage->getBackendOptions()); + $this->assertEquals($mountOptions, $storage->getMountOptions()); + $this->assertEquals($applicableUsers, $storage->getApplicableUsers()); + $this->assertEquals($applicableGroups, $storage->getApplicableGroups()); + $this->assertEquals($priority, $storage->getPriority()); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testCreateStorageInvalidClass() { + $this->service->createStorage( + 'mount', + 'identifier:\OC\Not\A\Backend', + 'identifier:\Auth\Mechanism', + [] + ); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testCreateStorageInvalidAuthMechanismClass() { + $this->service->createStorage( + 'mount', + 'identifier:\OCA\Files_External\Lib\Backend\SMB', + 'identifier:\Not\An\Auth\Mechanism', + [] + ); + } + public static function createHookCallback($params) { self::$hookCalls[] = array( 'signal' => Filesystem::signal_create_mount, diff --git a/apps/files_external/tests/service/userglobalstoragesservicetest.php b/apps/files_external/tests/service/userglobalstoragesservicetest.php new file mode 100644 index 00000000000..49a02453840 --- /dev/null +++ b/apps/files_external/tests/service/userglobalstoragesservicetest.php @@ -0,0 +1,215 @@ +<?php +/** + * @author Robin McCorkell <rmccorkell@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\UserGlobalStoragesService; +use \OCP\IGroupManager; + +use \OCA\Files_External\Lib\StorageConfig; + +class UserGlobalStoragesServiceTest extends GlobalStoragesServiceTest { + + protected $groupManager; + + protected $globalStoragesService; + + protected $user; + + const USER_ID = 'test_user'; + const GROUP_ID = 'test_group'; + + public function setUp() { + parent::setUp(); + + $this->globalStoragesService = $this->service; + + $this->user = new \OC\User\User(self::USER_ID, null); + $userSession = $this->getMock('\OCP\IUserSession'); + $userSession + ->expects($this->any()) + ->method('getUser') + ->will($this->returnValue($this->user)); + + $this->groupManager = $this->getMock('\OCP\IGroupManager'); + $this->groupManager->method('isInGroup') + ->will($this->returnCallback(function($userId, $groupId) { + if ($userId === self::USER_ID && $groupId === self::GROUP_ID) { + return true; + } + return false; + })); + + $this->service = new UserGlobalStoragesService( + $this->backendService, + $userSession, + $this->groupManager + ); + } + + public function applicableStorageProvider() { + return [ + [[], [], true], + + // not applicable cases + [['user1'], [], false], + [[], ['group1'], false], + [['user1'], ['group1'], false], + + // applicable cases + [[self::USER_ID], [], true], + [[], [self::GROUP_ID], true], + [[self::USER_ID], ['group1'], true], + [['user1'], [self::GROUP_ID], true], + + // sanity checks + [['user1', 'user2', self::USER_ID, 'user3'], [], true], + ]; + } + + /** + * @dataProvider applicableStorageProvider + */ + public function testGetStorageWithApplicable($applicableUsers, $applicableGroups, $isVisible) { + $backend = $this->backendService->getBackend('identifier:\OCA\Files_External\Lib\Backend\SMB'); + $authMechanism = $this->backendService->getAuthMechanism('identifier:\Auth\Mechanism'); + + $storage = new StorageConfig(); + $storage->setMountPoint('mountpoint'); + $storage->setBackend($backend); + $storage->setAuthMechanism($authMechanism); + $storage->setBackendOptions(['password' => 'testPassword']); + $storage->setApplicableUsers($applicableUsers); + $storage->setApplicableGroups($applicableGroups); + + $newStorage = $this->globalStoragesService->addStorage($storage); + + $storages = $this->service->getAllStorages(); + if ($isVisible) { + $this->assertEquals(1, count($storages)); + $retrievedStorage = $this->service->getStorage($newStorage->getId()); + $this->assertEquals('/mountpoint', $retrievedStorage->getMountPoint()); + } else { + $this->assertEquals(0, count($storages)); + } + + } + + /** + * @expectedException \DomainException + */ + public function testAddStorage($storageParams = null) { + $backend = $this->backendService->getBackend('identifier:\OCA\Files_External\Lib\Backend\SMB'); + $authMechanism = $this->backendService->getAuthMechanism('identifier:\Auth\Mechanism'); + + $storage = new StorageConfig(255); + $storage->setMountPoint('mountpoint'); + $storage->setBackend($backend); + $storage->setAuthMechanism($authMechanism); + $storage->setBackendOptions(['password' => 'testPassword']); + + $this->service->addStorage($storage); + } + + /** + * @expectedException \DomainException + */ + public function testUpdateStorage($storageParams = null) { + $backend = $this->backendService->getBackend('identifier:\OCA\Files_External\Lib\Backend\SMB'); + $authMechanism = $this->backendService->getAuthMechanism('identifier:\Auth\Mechanism'); + + $storage = new StorageConfig(255); + $storage->setMountPoint('mountpoint'); + $storage->setBackend($backend); + $storage->setAuthMechanism($authMechanism); + $storage->setBackendOptions(['password' => 'testPassword']); + + $newStorage = $this->globalStoragesService->addStorage($storage); + + $retrievedStorage = $this->service->getStorage($newStorage->getId()); + $retrievedStorage->setMountPoint('abc'); + $this->service->updateStorage($retrievedStorage); + } + + /** + * @expectedException \DomainException + */ + public function testDeleteStorage() { + $backend = $this->backendService->getBackend('identifier:\OCA\Files_External\Lib\Backend\SMB'); + $authMechanism = $this->backendService->getAuthMechanism('identifier:\Auth\Mechanism'); + + $storage = new StorageConfig(255); + $storage->setMountPoint('mountpoint'); + $storage->setBackend($backend); + $storage->setAuthMechanism($authMechanism); + $storage->setBackendOptions(['password' => 'testPassword']); + + $newStorage = $this->globalStoragesService->addStorage($storage); + $this->assertEquals(1, $newStorage->getId()); + + $this->service->removeStorage(1); + } + + public function testHooksAddStorage($a = null, $b = null, $c = null) { + // we don't test this here + $this->assertTrue(true); + } + + public function testHooksUpdateStorage($a = null, $b = null, $c = null, $d = null, $e = null) { + // we don't test this here + $this->assertTrue(true); + } + + public function testHooksRenameMountPoint() { + // we don't test this here + $this->assertTrue(true); + } + + public function testHooksDeleteStorage($a = null, $b = null, $c = null) { + // we don't test this here + $this->assertTrue(true); + } + + public function testLegacyConfigConversionApplicableAll() { + // we don't test this here + $this->assertTrue(true); + } + + public function testLegacyConfigConversionApplicableUserAndGroup() { + // we don't test this here + $this->assertTrue(true); + } + + public function testReadLegacyConfigAndGenerateConfigId() { + // we don't test this here + $this->assertTrue(true); + } + + public function testReadLegacyConfigNoAuthMechanism() { + // we don't test this here + $this->assertTrue(true); + } + + public function testReadLegacyConfigClass() { + // we don't test this here + $this->assertTrue(true); + } + +} |