diff options
author | Robin McCorkell <rmccorkell@owncloud.com> | 2015-08-12 10:00:37 +0100 |
---|---|---|
committer | Robin McCorkell <rmccorkell@owncloud.com> | 2015-08-19 10:05:12 +0100 |
commit | c572631087e2b56aa48c87ac753447e709248234 (patch) | |
tree | b76a308662a8d400d58de11559b0cb475d7745a0 /apps/files_external | |
parent | 1eeca031f863a652d07ebfa2f75339232bf60dc1 (diff) | |
download | nextcloud-server-c572631087e2b56aa48c87ac753447e709248234.tar.gz nextcloud-server-c572631087e2b56aa48c87ac753447e709248234.zip |
Unit tests for new backend API
Diffstat (limited to 'apps/files_external')
11 files changed, 977 insertions, 0 deletions
diff --git a/apps/files_external/tests/auth/authmechanismtest.php b/apps/files_external/tests/auth/authmechanismtest.php new file mode 100644 index 00000000000..b09d65a02df --- /dev/null +++ b/apps/files_external/tests/auth/authmechanismtest.php @@ -0,0 +1,80 @@ +<?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\Auth; + +class AuthMechanismTest extends \Test\TestCase { + + public function testJsonSerialization() { + $mechanism = $this->getMockBuilder('\OCA\Files_External\Lib\Auth\AuthMechanism') + ->setMethods(['jsonSerializeDefinition']) + ->getMock(); + $mechanism->expects($this->once()) + ->method('jsonSerializeDefinition') + ->willReturn(['foo' => 'bar']); + + $mechanism->setScheme('scheme'); + + $json = $mechanism->jsonSerialize(); + $this->assertEquals('bar', $json['foo']); + $this->assertEquals('scheme', $json['scheme']); + } + + public function validateStorageProvider() { + return [ + [true, 'scheme', true], + [false, 'scheme', false], + [true, 'foobar', true], + [false, 'barfoo', true], + ]; + } + + /** + * @dataProvider validateStorageProvider + */ + public function testValidateStorage($expectedSuccess, $scheme, $definitionSuccess) { + $mechanism = $this->getMockBuilder('\OCA\Files_External\Lib\Auth\AuthMechanism') + ->setMethods(['validateStorageDefinition']) + ->getMock(); + $mechanism->expects($this->atMost(1)) + ->method('validateStorageDefinition') + ->willReturn($definitionSuccess); + + $mechanism->setScheme($scheme); + + $backend = $this->getMockBuilder('\OCA\Files_External\Lib\Backend\Backend') + ->disableOriginalConstructor() + ->getMock(); + $backend->expects($this->once()) + ->method('getAuthSchemes') + ->willReturn(['scheme' => true, 'foobar' => true]); + + $storageConfig = $this->getMockBuilder('\OCA\Files_External\Lib\StorageConfig') + ->disableOriginalConstructor() + ->getMock(); + $storageConfig->expects($this->once()) + ->method('getBackend') + ->willReturn($backend); + + $this->assertEquals($expectedSuccess, $mechanism->validateStorage($storageConfig)); + } + +} diff --git a/apps/files_external/tests/backend/backendtest.php b/apps/files_external/tests/backend/backendtest.php new file mode 100644 index 00000000000..4956a923e94 --- /dev/null +++ b/apps/files_external/tests/backend/backendtest.php @@ -0,0 +1,89 @@ +<?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\Backend; + +use \OCA\Files_External\Lib\Backend\Backend; + +class BackendTest extends \Test\TestCase { + + public function testJsonSerialization() { + $backend = $this->getMockBuilder('\OCA\Files_External\Lib\Backend\Backend') + ->setMethods(['jsonSerializeDefinition']) + ->getMock(); + $backend->expects($this->once()) + ->method('jsonSerializeDefinition') + ->willReturn(['foo' => 'bar', 'name' => 'abc']); + + $backend->setPriority(57); + $backend->addAuthScheme('foopass'); + $backend->addAuthScheme('barauth'); + + $json = $backend->jsonSerialize(); + $this->assertEquals('bar', $json['foo']); + $this->assertEquals('abc', $json['name']); + $this->assertEquals($json['name'], $json['backend']); + $this->assertEquals(57, $json['priority']); + + $this->assertContains('foopass', $json['authSchemes']); + $this->assertContains('barauth', $json['authSchemes']); + } + + public function validateStorageProvider() { + return [ + [true, true], + [false, false], + ]; + } + + /** + * @dataProvider validateStorageProvider + */ + public function testValidateStorage($expectedSuccess, $definitionSuccess) { + $backend = $this->getMockBuilder('\OCA\Files_External\Lib\Backend\Backend') + ->setMethods(['validateStorageDefinition']) + ->getMock(); + $backend->expects($this->atMost(1)) + ->method('validateStorageDefinition') + ->willReturn($definitionSuccess); + + $storageConfig = $this->getMockBuilder('\OCA\Files_External\Lib\StorageConfig') + ->disableOriginalConstructor() + ->getMock(); + + $this->assertEquals($expectedSuccess, $backend->validateStorage($storageConfig)); + } + + public function testLegacyAuthMechanismCallback() { + $backend = new Backend(); + $backend->setLegacyAuthMechanismCallback(function(array $params) { + if (isset($params['ping'])) { + return 'pong'; + } + return 'foobar'; + }); + + $this->assertEquals('pong', $backend->getLegacyAuthMechanism(['ping' => true])); + $this->assertEquals('foobar', $backend->getLegacyAuthMechanism(['other' => true])); + $this->assertEquals('foobar', $backend->getLegacyAuthMechanism()); + } + +} diff --git a/apps/files_external/tests/backend/legacybackendtest.php b/apps/files_external/tests/backend/legacybackendtest.php new file mode 100644 index 00000000000..ceedede1302 --- /dev/null +++ b/apps/files_external/tests/backend/legacybackendtest.php @@ -0,0 +1,81 @@ +<?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\Backend; + +use \OCA\Files_External\Lib\Backend\LegacyBackend; +use \OCA\Files_External\Lib\DefinitionParameter; + +class LegacyBackendTest extends \Test\TestCase { + + public function testConstructor() { + $auth = $this->getMockBuilder('\OCA\Files_External\Lib\Auth\NullMechanism') + ->disableOriginalConstructor() + ->getMock(); + + $class = '\OC\Files\Storage\SMB'; + $definition = [ + 'configuration' => [ + 'textfield' => 'Text field', + 'passwordfield' => '*Password field', + 'checkbox' => '!Checkbox', + 'hiddenfield' => '#Hidden field', + 'optionaltext' => '&Optional text field', + 'optionalpassword' => '&*Optional password field', + ], + 'backend' => 'Backend text', + 'priority' => 123, + 'custom' => 'foo/bar.js', + 'has_dependencies' => true, + ]; + + $backend = new LegacyBackend($class, $definition, $auth); + + $this->assertEquals('\OC\Files\Storage\SMB', $backend->getStorageClass()); + $this->assertEquals('Backend text', $backend->getText()); + $this->assertEquals(123, $backend->getPriority()); + $this->assertEquals('foo/bar.js', $backend->getCustomJs()); + $this->assertEquals(true, $backend->hasDependencies()); + $this->assertArrayHasKey('null', $backend->getAuthSchemes()); + $this->assertEquals($auth, $backend->getLegacyAuthMechanism()); + + $parameters = $backend->getParameters(); + $this->assertEquals('Text field', $parameters['textfield']->getText()); + $this->assertEquals(DefinitionParameter::VALUE_TEXT, $parameters['textfield']->getType()); + $this->assertEquals(DefinitionParameter::FLAG_NONE, $parameters['textfield']->getFlags()); + $this->assertEquals('Password field', $parameters['passwordfield']->getText()); + $this->assertEquals(DefinitionParameter::VALUE_PASSWORD, $parameters['passwordfield']->getType()); + $this->assertEquals(DefinitionParameter::FLAG_NONE, $parameters['passwordfield']->getFlags()); + $this->assertEquals('Checkbox', $parameters['checkbox']->getText()); + $this->assertEquals(DefinitionParameter::VALUE_BOOLEAN, $parameters['checkbox']->getType()); + $this->assertEquals(DefinitionParameter::FLAG_NONE, $parameters['checkbox']->getFlags()); + $this->assertEquals('Hidden field', $parameters['hiddenfield']->getText()); + $this->assertEquals(DefinitionParameter::VALUE_HIDDEN, $parameters['hiddenfield']->getType()); + $this->assertEquals(DefinitionParameter::FLAG_NONE, $parameters['hiddenfield']->getFlags()); + $this->assertEquals('Optional text field', $parameters['optionaltext']->getText()); + $this->assertEquals(DefinitionParameter::VALUE_TEXT, $parameters['optionaltext']->getType()); + $this->assertEquals(DefinitionParameter::FLAG_OPTIONAL, $parameters['optionaltext']->getFlags()); + $this->assertEquals('Optional password field', $parameters['optionalpassword']->getText()); + $this->assertEquals(DefinitionParameter::VALUE_PASSWORD, $parameters['optionalpassword']->getType()); + $this->assertEquals(DefinitionParameter::FLAG_OPTIONAL, $parameters['optionalpassword']->getFlags()); + } + +} diff --git a/apps/files_external/tests/controller/storagescontrollertest.php b/apps/files_external/tests/controller/storagescontrollertest.php index 5a9683306c7..2b0ee7a14ea 100644 --- a/apps/files_external/tests/controller/storagescontrollertest.php +++ b/apps/files_external/tests/controller/storagescontrollertest.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 @@ -314,4 +315,66 @@ abstract class StoragesControllerTest extends \Test\TestCase { $this->assertEquals(Http::STATUS_OK, $response->getStatus()); $this->assertEquals($storageConfig, $response->getData()); } + + public function validateStorageProvider() { + return [ + [true, true, true], + [false, true, false], + [true, false, false], + [false, false, false] + ]; + } + + /** + * @dataProvider validateStorageProvider + */ + public function testValidateStorage($backendValidate, $authMechValidate, $expectSuccess) { + $backend = $this->getBackendMock(); + $backend->method('validateStorage') + ->willReturn($backendValidate); + $backend->method('isVisibleFor') + ->willReturn(true); + + $authMech = $this->getAuthMechMock(); + $authMech->method('validateStorage') + ->will($this->returnValue($authMechValidate)); + + $storageConfig = new StorageConfig(); + $storageConfig->setMountPoint('mount'); + $storageConfig->setBackend($backend); + $storageConfig->setAuthMechanism($authMech); + $storageConfig->setBackendOptions([]); + + $this->service->expects($this->once()) + ->method('createStorage') + ->will($this->returnValue($storageConfig)); + + if ($expectSuccess) { + $this->service->expects($this->once()) + ->method('addStorage') + ->with($storageConfig) + ->will($this->returnValue($storageConfig)); + } else { + $this->service->expects($this->never()) + ->method('addStorage'); + } + + $response = $this->controller->create( + 'mount', + '\OC\Files\Storage\SMB', + '\OCA\Files_External\Lib\Auth\NullMechanism', + array(), + [], + [], + [], + null + ); + + if ($expectSuccess) { + $this->assertEquals(Http::STATUS_CREATED, $response->getStatus()); + } else { + $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus()); + } + } + } diff --git a/apps/files_external/tests/definitionparameterttest.php b/apps/files_external/tests/definitionparameterttest.php new file mode 100644 index 00000000000..6be00508698 --- /dev/null +++ b/apps/files_external/tests/definitionparameterttest.php @@ -0,0 +1,70 @@ +<?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; + +use \OCA\Files_External\Lib\DefinitionParameter as Param; + +class DefinitionParameterTest extends \Test\TestCase { + + public function testJsonSerialization() { + $param = new Param('foo', 'bar'); + $this->assertEquals('bar', $param->jsonSerialize()); + + $param->setType(Param::VALUE_BOOLEAN); + $this->assertEquals('!bar', $param->jsonSerialize()); + + $param->setType(Param::VALUE_PASSWORD); + $param->setFlag(Param::FLAG_OPTIONAL); + $this->assertEquals('&*bar', $param->jsonSerialize()); + + $param->setType(Param::VALUE_HIDDEN); + $param->setFlags(Param::FLAG_NONE); + $this->assertEquals('#bar', $param->jsonSerialize()); + } + + public function validateValueProvider() { + return [ + [Param::VALUE_TEXT, Param::FLAG_NONE, 'abc', true], + [Param::VALUE_TEXT, Param::FLAG_NONE, '', false], + [Param::VALUE_TEXT, Param::FLAG_OPTIONAL, '', true], + + [Param::VALUE_BOOLEAN, Param::FLAG_NONE, false, true], + [Param::VALUE_BOOLEAN, Param::FLAG_NONE, 123, false], + + [Param::VALUE_PASSWORD, Param::FLAG_NONE, 'foobar', true], + [Param::VALUE_PASSWORD, Param::FLAG_NONE, '', false], + + [Param::VALUE_HIDDEN, Param::FLAG_NONE, '', false] + ]; + } + + /** + * @dataProvider validateValueProvider + */ + public function testValidateValue($type, $flags, $value, $success) { + $param = new Param('foo', 'bar'); + $param->setType($type); + $param->setFlags($flags); + + $this->assertEquals($success, $param->validateValue($value)); + } +} diff --git a/apps/files_external/tests/dependencytraittest.php b/apps/files_external/tests/dependencytraittest.php new file mode 100644 index 00000000000..5706d97053d --- /dev/null +++ b/apps/files_external/tests/dependencytraittest.php @@ -0,0 +1,45 @@ +<?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; + +use \OCA\Files_External\Lib\MissingDependency; + +class DependencyTraitTest extends \Test\TestCase { + + public function testCheckDependencies() { + $trait = $this->getMockForTrait('\OCA\Files_External\Lib\DependencyTrait'); + $trait->setDependencyCheck(function() { + return [ + (new MissingDependency('dependency'))->setMessage('missing dependency'), + (new MissingDependency('program'))->setMessage('cannot find program'), + ]; + }); + + $dependencies = $trait->checkDependencies(); + $this->assertCount(2, $dependencies); + $this->assertEquals('dependency', $dependencies[0]->getDependency()); + $this->assertEquals('missing dependency', $dependencies[0]->getMessage()); + $this->assertEquals('program', $dependencies[1]->getDependency()); + $this->assertEquals('cannot find program', $dependencies[1]->getMessage()); + } + +} diff --git a/apps/files_external/tests/frontenddefinitiontraittest.php b/apps/files_external/tests/frontenddefinitiontraittest.php new file mode 100644 index 00000000000..871a87d4c52 --- /dev/null +++ b/apps/files_external/tests/frontenddefinitiontraittest.php @@ -0,0 +1,83 @@ +<?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; + +class FrontendDefinitionTraitTest extends \Test\TestCase { + + public function testJsonSerialization() { + $param = $this->getMockBuilder('\OCA\Files_External\Lib\DefinitionParameter') + ->disableOriginalConstructor() + ->getMock(); + $param->method('getName')->willReturn('foo'); + + $trait = $this->getMockForTrait('\OCA\Files_External\Lib\FrontendDefinitionTrait'); + $trait->setText('test'); + $trait->addParameters([$param]); + $trait->setCustomJs('foo/bar.js'); + + $json = $trait->jsonSerializeDefinition(); + + $this->assertEquals('test', $json['name']); + $this->assertEquals('foo/bar.js', $json['custom']); + + $configuration = $json['configuration']; + $this->assertArrayHasKey('foo', $configuration); + } + + public function validateStorageProvider() { + return [ + [true, ['foo' => true, 'bar' => true, 'baz' => true]], + [false, ['foo' => true, 'bar' => false]] + ]; + } + + /** + * @dataProvider validateStorageProvider + */ + public function testValidateStorage($expectedSuccess, $params) { + $backendParams = []; + foreach ($params as $name => $valid) { + $param = $this->getMockBuilder('\OCA\Files_External\Lib\DefinitionParameter') + ->disableOriginalConstructor() + ->getMock(); + $param->method('getName') + ->willReturn($name); + $param->expects($this->once()) + ->method('validateValue') + ->willReturn($valid); + $backendParams[] = $param; + } + + $storageConfig = $this->getMockBuilder('\OCA\Files_External\Lib\StorageConfig') + ->disableOriginalConstructor() + ->getMock(); + $storageConfig->expects($this->once()) + ->method('getBackendOptions') + ->willReturn([]); + + $trait = $this->getMockForTrait('\OCA\Files_External\Lib\FrontendDefinitionTrait'); + $trait->setText('test'); + $trait->addParameters($backendParams); + + $this->assertEquals($expectedSuccess, $trait->validateStorageDefinition($storageConfig)); + } +} 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); + } + +} |