aboutsummaryrefslogtreecommitdiffstats
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.php158
-rw-r--r--apps/files_external/tests/Command/CommandTestCase.php90
-rw-r--r--apps/files_external/tests/Command/ListCommandTest.php60
3 files changed, 308 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..59db18a42de
--- /dev/null
+++ b/apps/files_external/tests/Command/ApplicableTest.php
@@ -0,0 +1,158 @@
+<?php
+
+declare(strict_types=1);
+/**
+ * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-only
+ */
+namespace OCA\Files_External\Tests\Command;
+
+use OCA\Files_External\Command\Applicable;
+use OCP\IGroupManager;
+use OCP\IUserManager;
+use PHPUnit\Framework\MockObject\MockObject;
+
+class ApplicableTest extends CommandTestCase {
+ private function getInstance($storageService): Applicable {
+ /** @var IUserManager&MockObject $userManager */
+ $userManager = $this->createMock(IUserManager::class);
+ /** @var IGroupManager&MockObject $groupManager */
+ $groupManager = $this->createMock(IGroupManager::class);
+
+ $userManager->expects($this->any())
+ ->method('userExists')
+ ->willReturn(true);
+
+ $groupManager->expects($this->any())
+ ->method('groupExists')
+ ->willReturn(true);
+
+ return new Applicable($storageService, $userManager, $groupManager);
+ }
+
+ public function testListEmpty(): void {
+ $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(): void {
+ $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(): void {
+ $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(): void {
+ $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(): void {
+ $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(): void {
+ $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(): void {
+ $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/CommandTestCase.php b/apps/files_external/tests/Command/CommandTestCase.php
new file mode 100644
index 00000000000..e42ad9cd68a
--- /dev/null
+++ b/apps/files_external/tests/Command/CommandTestCase.php
@@ -0,0 +1,90 @@
+<?php
+
+/**
+ * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-only
+ */
+namespace OCA\Files_External\Tests\Command;
+
+use OCA\Files_External\Lib\StorageConfig;
+use OCA\Files_External\NotFoundException;
+use OCA\Files_External\Service\GlobalStoragesService;
+use PHPUnit\Framework\MockObject\MockObject;
+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 CommandTestCase extends TestCase {
+ /**
+ * @param StorageConfig[] $mounts
+ * @return GlobalStoragesService&MockObject
+ */
+ protected function getGlobalStorageService(array $mounts = []) {
+ $mock = $this->createMock(GlobalStoragesService::class);
+
+ $this->bindMounts($mock, $mounts);
+
+ return $mock;
+ }
+
+ /**
+ * @param MockObject $mock
+ * @param StorageConfig[] $mounts
+ */
+ protected function bindMounts(MockObject $mock, array $mounts) {
+ $mock->expects($this->any())
+ ->method('getStorage')
+ ->willReturnCallback(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 = []): ArrayInput {
+ $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): string {
+ $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..5b84e500e3f
--- /dev/null
+++ b/apps/files_external/tests/Command/ListCommandTest.php
@@ -0,0 +1,60 @@
+<?php
+
+declare(strict_types=1);
+/**
+ * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-only
+ */
+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 OCA\Files_External\Service\GlobalStoragesService;
+use OCA\Files_External\Service\UserStoragesService;
+use OCP\Authentication\LoginCredentials\IStore;
+use OCP\IL10N;
+use OCP\IUserManager;
+use OCP\IUserSession;
+use PHPUnit\Framework\MockObject\MockObject;
+use Symfony\Component\Console\Output\BufferedOutput;
+
+class ListCommandTest extends CommandTestCase {
+ private function getInstance(): ListCommand {
+ /** @var GlobalStoragesService&MockObject $globalService */
+ $globalService = $this->createMock(GlobalStoragesService::class);
+ /** @var UserStoragesService&MockObject $userService */
+ $userService = $this->createMock(UserStoragesService::class);
+ /** @var IUserManager&MockObject $userManager */
+ $userManager = $this->createMock(IUserManager::class);
+ /** @var IUserSession&MockObject $userSession */
+ $userSession = $this->createMock(IUserSession::class);
+
+ return new ListCommand($globalService, $userService, $userSession, $userManager);
+ }
+
+ public function testListAuthIdentifier(): void {
+ $l10n = $this->createMock(IL10N::class);
+ $instance = $this->getInstance();
+ $mount1 = new StorageConfig();
+ $mount1->setAuthMechanism(new Password($l10n));
+ $mount1->setBackend(new Local($l10n, new NullMechanism($l10n)));
+ $mount2 = new StorageConfig();
+ $credentialStore = $this->createMock(IStore::class);
+ $mount2->setAuthMechanism(new SessionCredentials($l10n, $credentialStore));
+ $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']);
+ }
+}