summaryrefslogtreecommitdiffstats
path: root/apps/files_external/tests/Command
diff options
context:
space:
mode:
Diffstat (limited to 'apps/files_external/tests/Command')
-rw-r--r--apps/files_external/tests/Command/ApplicableTest.php168
-rw-r--r--apps/files_external/tests/Command/CommandTest.php104
-rw-r--r--apps/files_external/tests/Command/ListCommandTest.php70
3 files changed, 342 insertions, 0 deletions
diff --git a/apps/files_external/tests/Command/ApplicableTest.php b/apps/files_external/tests/Command/ApplicableTest.php
new file mode 100644
index 00000000000..64d41f6f245
--- /dev/null
+++ b/apps/files_external/tests/Command/ApplicableTest.php
@@ -0,0 +1,168 @@
+<?php
+/**
+ * @author Robin Appelman <icewind@owncloud.com>
+ *
+ * @copyright Copyright (c) 2016, 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\Command;
+
+use OCA\Files_External\Command\Applicable;
+
+class ApplicableTest extends CommandTest {
+ private function getInstance($storageService) {
+ /** @var \OCP\IUserManager|\PHPUnit_Framework_MockObject_MockObject $userManager */
+ $userManager = $this->getMock('\OCP\IUserManager');
+ /** @var \OCP\IGroupManager|\PHPUnit_Framework_MockObject_MockObject $groupManager */
+ $groupManager = $this->getMock('\OCP\IGroupManager');
+
+ $userManager->expects($this->any())
+ ->method('userExists')
+ ->will($this->returnValue(true));
+
+ $groupManager->expects($this->any())
+ ->method('groupExists')
+ ->will($this->returnValue(true));
+
+ return new Applicable($storageService, $userManager, $groupManager);
+ }
+
+ public function testListEmpty() {
+ $mount = $this->getMount(1, '', '');
+
+ $storageService = $this->getGlobalStorageService([$mount]);
+ $command = $this->getInstance($storageService);
+
+ $input = $this->getInput($command, [
+ 'mount_id' => 1
+ ], [
+ 'output' => 'json'
+ ]);
+
+ $result = json_decode($this->executeCommand($command, $input), true);
+
+ $this->assertEquals(['users' => [], 'groups' => []], $result);
+ }
+
+ public function testList() {
+ $mount = $this->getMount(1, '', '', '', [], [], ['test', 'asd']);
+
+ $storageService = $this->getGlobalStorageService([$mount]);
+ $command = $this->getInstance($storageService);
+
+ $input = $this->getInput($command, [
+ 'mount_id' => 1
+ ], [
+ 'output' => 'json'
+ ]);
+
+ $result = json_decode($this->executeCommand($command, $input), true);
+
+ $this->assertEquals(['users' => ['test', 'asd'], 'groups' => []], $result);
+ }
+
+ public function testAddSingle() {
+ $mount = $this->getMount(1, '', '', '', [], [], []);
+
+ $storageService = $this->getGlobalStorageService([$mount]);
+ $command = $this->getInstance($storageService);
+
+ $input = $this->getInput($command, [
+ 'mount_id' => 1
+ ], [
+ 'output' => 'json',
+ 'add-user' => ['foo']
+ ]);
+
+ $this->executeCommand($command, $input);
+
+ $this->assertEquals(['foo'], $mount->getApplicableUsers());
+ }
+
+ public function testAddDuplicate() {
+ $mount = $this->getMount(1, '', '', '', [], [], ['foo']);
+
+ $storageService = $this->getGlobalStorageService([$mount]);
+ $command = $this->getInstance($storageService);
+
+ $input = $this->getInput($command, [
+ 'mount_id' => 1
+ ], [
+ 'output' => 'json',
+ 'add-user' => ['foo', 'bar']
+ ]);
+
+ $this->executeCommand($command, $input);
+
+ $this->assertEquals(['foo', 'bar'], $mount->getApplicableUsers());
+ }
+
+ public function testRemoveSingle() {
+ $mount = $this->getMount(1, '', '', '', [], [], ['foo', 'bar']);
+
+ $storageService = $this->getGlobalStorageService([$mount]);
+ $command = $this->getInstance($storageService);
+
+ $input = $this->getInput($command, [
+ 'mount_id' => 1
+ ], [
+ 'output' => 'json',
+ 'remove-user' => ['bar']
+ ]);
+
+ $this->executeCommand($command, $input);
+
+ $this->assertEquals(['foo'], $mount->getApplicableUsers());
+ }
+
+ public function testRemoveNonExisting() {
+ $mount = $this->getMount(1, '', '', '', [], [], ['foo', 'bar']);
+
+ $storageService = $this->getGlobalStorageService([$mount]);
+ $command = $this->getInstance($storageService);
+
+ $input = $this->getInput($command, [
+ 'mount_id' => 1
+ ], [
+ 'output' => 'json',
+ 'remove-user' => ['bar', 'asd']
+ ]);
+
+ $this->executeCommand($command, $input);
+
+ $this->assertEquals(['foo'], $mount->getApplicableUsers());
+ }
+
+ public function testRemoveAddRemove() {
+ $mount = $this->getMount(1, '', '', '', [], [], ['foo', 'bar']);
+
+ $storageService = $this->getGlobalStorageService([$mount]);
+ $command = $this->getInstance($storageService);
+
+ $input = $this->getInput($command, [
+ 'mount_id' => 1
+ ], [
+ 'output' => 'json',
+ 'remove-user' => ['bar', 'asd'],
+ 'add-user' => ['test']
+ ]);
+
+ $this->executeCommand($command, $input);
+
+ $this->assertEquals(['foo', 'test'], $mount->getApplicableUsers());
+ }
+}
diff --git a/apps/files_external/tests/Command/CommandTest.php b/apps/files_external/tests/Command/CommandTest.php
new file mode 100644
index 00000000000..73a1ea8315c
--- /dev/null
+++ b/apps/files_external/tests/Command/CommandTest.php
@@ -0,0 +1,104 @@
+<?php
+/**
+ * @author Robin Appelman <icewind@owncloud.com>
+ *
+ * @copyright Copyright (c) 2016, 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\Command;
+
+use OCA\Files_external\Lib\StorageConfig;
+use OCA\Files_external\NotFoundException;
+use Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Input\ArrayInput;
+use Symfony\Component\Console\Input\Input;
+use Symfony\Component\Console\Output\BufferedOutput;
+use Test\TestCase;
+
+abstract class CommandTest extends TestCase {
+ /**
+ * @param StorageConfig[] $mounts
+ * @return \OCA\Files_External\Service\GlobalStoragesService|\PHPUnit_Framework_MockObject_MockObject
+ */
+ protected function getGlobalStorageService(array $mounts = []) {
+ $mock = $this->getMockBuilder('OCA\Files_External\Service\GlobalStoragesService')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $this->bindMounts($mock, $mounts);
+
+ return $mock;
+ }
+
+ /**
+ * @param \PHPUnit_Framework_MockObject_MockObject $mock
+ * @param StorageConfig[] $mounts
+ */
+ protected function bindMounts(\PHPUnit_Framework_MockObject_MockObject $mock, array $mounts) {
+ $mock->expects($this->any())
+ ->method('getStorage')
+ ->will($this->returnCallback(function ($id) use ($mounts) {
+ foreach ($mounts as $mount) {
+ if ($mount->getId() === $id) {
+ return $mount;
+ }
+ }
+ throw new NotFoundException();
+ }));
+ }
+
+ /**
+ * @param $id
+ * @param $mountPoint
+ * @param $backendClass
+ * @param string $applicableIdentifier
+ * @param array $config
+ * @param array $options
+ * @param array $users
+ * @param array $groups
+ * @return StorageConfig
+ */
+ protected function getMount($id, $mountPoint, $backendClass, $applicableIdentifier = 'password::password', $config = [], $options = [], $users = [], $groups = []) {
+ $mount = new StorageConfig($id);
+
+ $mount->setMountPoint($mountPoint);
+ $mount->setBackendOptions($config);
+ $mount->setMountOptions($options);
+ $mount->setApplicableUsers($users);
+ $mount->setApplicableGroups($groups);
+
+ return $mount;
+ }
+
+ protected function getInput(Command $command, array $arguments = [], array $options = []) {
+ $input = new ArrayInput([]);
+ $input->bind($command->getDefinition());
+ foreach ($arguments as $key => $value) {
+ $input->setArgument($key, $value);
+ }
+ foreach ($options as $key => $value) {
+ $input->setOption($key, $value);
+ }
+ return $input;
+ }
+
+ protected function executeCommand(Command $command, Input $input) {
+ $output = new BufferedOutput();
+ $this->invokePrivate($command, 'execute', [$input, $output]);
+ return $output->fetch();
+ }
+}
diff --git a/apps/files_external/tests/Command/ListCommandTest.php b/apps/files_external/tests/Command/ListCommandTest.php
new file mode 100644
index 00000000000..1475249ae43
--- /dev/null
+++ b/apps/files_external/tests/Command/ListCommandTest.php
@@ -0,0 +1,70 @@
+<?php
+/**
+ * @author Robin Appelman <icewind@owncloud.com>
+ *
+ * @copyright Copyright (c) 2016, 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\Command;
+
+use OCA\Files_External\Command\ListCommand;
+use OCA\Files_External\Lib\Auth\NullMechanism;
+use OCA\Files_External\Lib\Auth\Password\Password;
+use OCA\Files_External\Lib\Auth\Password\SessionCredentials;
+use OCA\Files_External\Lib\Backend\Local;
+use OCA\Files_external\Lib\StorageConfig;
+use Symfony\Component\Console\Output\BufferedOutput;
+
+class ListCommandTest extends CommandTest {
+ /**
+ * @return \OCA\Files_External\Command\ListCommand|\PHPUnit_Framework_MockObject_MockObject
+ */
+ private function getInstance() {
+ /** @var \OCA\Files_External\Service\GlobalStoragesService|\PHPUnit_Framework_MockObject_MockObject $globalService */
+ $globalService = $this->getMock('\OCA\Files_External\Service\GlobalStoragesService', null, [], '', false);
+ /** @var \OCA\Files_External\Service\UserStoragesService|\PHPUnit_Framework_MockObject_MockObject $userService */
+ $userService = $this->getMock('\OCA\Files_External\Service\UserStoragesService', null, [], '', false);
+ /** @var \OCP\IUserManager|\PHPUnit_Framework_MockObject_MockObject $userManager */
+ $userManager = $this->getMock('\OCP\IUserManager');
+ /** @var \OCP\IUserSession|\PHPUnit_Framework_MockObject_MockObject $userSession */
+ $userSession = $this->getMock('\OCP\IUserSession');
+
+ return new ListCommand($globalService, $userService, $userSession, $userManager);
+ }
+
+ public function testListAuthIdentifier() {
+ $l10n = $this->getMock('\OC_L10N', null, [], '', false);
+ $session = $this->getMock('\OCP\ISession');
+ $crypto = $this->getMock('\OCP\Security\ICrypto');
+ $instance = $this->getInstance();
+ $mount1 = new StorageConfig();
+ $mount1->setAuthMechanism(new Password($l10n));
+ $mount1->setBackend(new Local($l10n, new NullMechanism($l10n)));
+ $mount2 = new StorageConfig();
+ $mount2->setAuthMechanism(new SessionCredentials($l10n, $session, $crypto));
+ $mount2->setBackend(new Local($l10n, new NullMechanism($l10n)));
+ $input = $this->getInput($instance, [], [
+ 'output' => 'json'
+ ]);
+ $output = new BufferedOutput();
+
+ $instance->listMounts('', [$mount1, $mount2], $input, $output);
+ $output = json_decode($output->fetch(), true);
+
+ $this->assertNotEquals($output[0]['authentication_type'], $output[1]['authentication_type']);
+ }
+}