2015-08-12 12:00:37 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2020-08-24 15:54:25 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 19:13:36 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-01-12 17:02:16 +03:00
|
|
|
* @author Robin McCorkell <robin@mccorkell.me.uk>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2020-12-16 16:54:15 +03:00
|
|
|
* @author Vincent Petry <vincent@nextcloud.com>
|
2015-08-12 12:00:37 +03:00
|
|
|
*
|
|
|
|
* @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,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-08-12 12:00:37 +03:00
|
|
|
*
|
|
|
|
*/
|
2019-11-22 22:52:10 +03:00
|
|
|
|
2015-08-12 12:00:37 +03:00
|
|
|
namespace OCA\Files_External\Tests\Service;
|
|
|
|
|
2016-05-25 15:57:41 +03:00
|
|
|
use OCA\Files_External\Lib\StorageConfig;
|
2016-05-13 12:56:47 +03:00
|
|
|
use OCA\Files_External\NotFoundException;
|
2016-05-13 12:22:28 +03:00
|
|
|
use OCA\Files_External\Service\StoragesService;
|
|
|
|
use OCA\Files_External\Service\UserGlobalStoragesService;
|
2016-09-02 11:37:20 +03:00
|
|
|
use OCP\IGroupManager;
|
2015-11-02 15:13:06 +03:00
|
|
|
use OCP\IUser;
|
2016-09-02 11:37:20 +03:00
|
|
|
use OCP\IUserSession;
|
2015-11-02 15:13:06 +03:00
|
|
|
use Test\Traits\UserTrait;
|
2015-08-12 12:00:37 +03:00
|
|
|
|
2015-11-02 15:13:06 +03:00
|
|
|
/**
|
|
|
|
* @group DB
|
|
|
|
*/
|
2015-08-12 12:00:37 +03:00
|
|
|
class UserGlobalStoragesServiceTest extends GlobalStoragesServiceTest {
|
2015-11-02 15:13:06 +03:00
|
|
|
use UserTrait;
|
2015-08-12 12:00:37 +03:00
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \OCP\IGroupManager|\PHPUnit\Framework\MockObject\MockObject groupManager */
|
2015-08-12 12:00:37 +03:00
|
|
|
protected $groupManager;
|
|
|
|
|
2015-11-02 15:13:06 +03:00
|
|
|
/**
|
|
|
|
* @var StoragesService
|
|
|
|
*/
|
2015-08-12 12:00:37 +03:00
|
|
|
protected $globalStoragesService;
|
|
|
|
|
2015-11-02 15:13:06 +03:00
|
|
|
/**
|
|
|
|
* @var UserGlobalStoragesService
|
|
|
|
*/
|
|
|
|
protected $service;
|
|
|
|
|
2015-08-12 12:00:37 +03:00
|
|
|
protected $user;
|
|
|
|
|
2020-04-10 17:54:27 +03:00
|
|
|
public const USER_ID = 'test_user';
|
|
|
|
public const GROUP_ID = 'test_group';
|
|
|
|
public const GROUP_ID2 = 'test_group2';
|
2015-08-12 12:00:37 +03:00
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2015-08-12 12:00:37 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->globalStoragesService = $this->service;
|
|
|
|
|
2019-02-25 16:35:25 +03:00
|
|
|
$this->user = new \OC\User\User(self::USER_ID, null, \OC::$server->getEventDispatcher());
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \OCP\IUserSession|\PHPUnit\Framework\MockObject\MockObject $userSession */
|
2016-09-02 11:37:20 +03:00
|
|
|
$userSession = $this->createMock(IUserSession::class);
|
2015-08-12 12:00:37 +03:00
|
|
|
$userSession
|
|
|
|
->expects($this->any())
|
|
|
|
->method('getUser')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($this->user);
|
2015-08-12 12:00:37 +03:00
|
|
|
|
2016-09-02 11:37:20 +03:00
|
|
|
$this->groupManager = $this->createMock(IGroupManager::class);
|
2015-08-12 12:00:37 +03:00
|
|
|
$this->groupManager->method('isInGroup')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturnCallback(function ($userId, $groupId) {
|
2015-09-14 22:55:22 +03:00
|
|
|
if ($userId === self::USER_ID) {
|
|
|
|
switch ($groupId) {
|
2015-11-02 15:13:06 +03:00
|
|
|
case self::GROUP_ID:
|
|
|
|
case self::GROUP_ID2:
|
|
|
|
return true;
|
2015-09-14 22:55:22 +03:00
|
|
|
}
|
2015-08-12 12:00:37 +03:00
|
|
|
}
|
|
|
|
return false;
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
2015-11-02 15:13:06 +03:00
|
|
|
$this->groupManager->method('getUserGroupIds')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturnCallback(function (IUser $user) {
|
2015-11-02 15:13:06 +03:00
|
|
|
if ($user->getUID() === self::USER_ID) {
|
|
|
|
return [self::GROUP_ID, self::GROUP_ID2];
|
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
2015-08-12 12:00:37 +03:00
|
|
|
|
|
|
|
$this->service = new UserGlobalStoragesService(
|
|
|
|
$this->backendService,
|
2015-11-02 15:13:06 +03:00
|
|
|
$this->dbConfig,
|
2015-08-12 12:00:37 +03:00
|
|
|
$userSession,
|
2016-01-13 17:29:53 +03:00
|
|
|
$this->groupManager,
|
|
|
|
$this->mountCache
|
2015-08-12 12:00:37 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
2016-01-05 18:40:34 +03:00
|
|
|
|
|
|
|
try {
|
|
|
|
$this->service->getStorage($newStorage->getId());
|
|
|
|
$this->fail('Failed asserting that storage can\'t be accessed by id');
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
}
|
2015-08-12 12:00:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-27 15:47:39 +03:00
|
|
|
|
2015-08-12 12:00:37 +03:00
|
|
|
public function testAddStorage($storageParams = null) {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\DomainException::class);
|
|
|
|
|
2015-08-12 12:00:37 +03:00
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
2019-11-27 15:47:39 +03:00
|
|
|
|
2015-08-12 12:00:37 +03:00
|
|
|
public function testUpdateStorage($storageParams = null) {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\DomainException::class);
|
|
|
|
|
2015-08-12 12:00:37 +03:00
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
2019-11-27 15:47:39 +03:00
|
|
|
|
2015-11-02 15:13:06 +03:00
|
|
|
public function testNonExistingStorage() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\DomainException::class);
|
|
|
|
|
2019-11-27 15:47:39 +03:00
|
|
|
$this->ActualNonExistingStorageTest();
|
2015-11-02 15:13:06 +03:00
|
|
|
}
|
|
|
|
|
2015-08-12 12:00:37 +03:00
|
|
|
/**
|
2015-10-13 12:52:56 +03:00
|
|
|
* @dataProvider deleteStorageDataProvider
|
2015-08-12 12:00:37 +03:00
|
|
|
*/
|
2015-10-13 12:52:56 +03:00
|
|
|
public function testDeleteStorage($backendOptions, $rustyStorageId, $expectedCountAfterDeletion) {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\DomainException::class);
|
|
|
|
|
2015-08-12 12:00:37 +03:00
|
|
|
$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);
|
2015-10-13 12:52:56 +03:00
|
|
|
$storage->setBackendOptions($backendOptions);
|
2015-08-12 12:00:37 +03:00
|
|
|
|
|
|
|
$newStorage = $this->globalStoragesService->addStorage($storage);
|
2015-11-02 15:13:06 +03:00
|
|
|
$id = $newStorage->getId();
|
2015-08-12 12:00:37 +03:00
|
|
|
|
2015-11-02 15:13:06 +03:00
|
|
|
$this->service->removeStorage($id);
|
|
|
|
}
|
|
|
|
|
2019-11-27 15:47:39 +03:00
|
|
|
|
2015-11-02 15:13:06 +03:00
|
|
|
public function testDeleteUnexistingStorage() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\DomainException::class);
|
|
|
|
|
2020-04-09 10:22:29 +03:00
|
|
|
$this->actualDeletedUnexistingStorageTest();
|
2015-08-12 12:00:37 +03:00
|
|
|
}
|
|
|
|
|
2015-09-14 22:55:22 +03:00
|
|
|
public function getUniqueStoragesProvider() {
|
|
|
|
return [
|
|
|
|
// 'all' vs group
|
|
|
|
[100, [], [], 100, [], [self::GROUP_ID], 2],
|
|
|
|
[100, [], [self::GROUP_ID], 100, [], [], 1],
|
|
|
|
|
|
|
|
// 'all' vs user
|
|
|
|
[100, [], [], 100, [self::USER_ID], [], 2],
|
|
|
|
[100, [self::USER_ID], [], 100, [], [], 1],
|
|
|
|
|
|
|
|
// group vs user
|
|
|
|
[100, [], [self::GROUP_ID], 100, [self::USER_ID], [], 2],
|
|
|
|
[100, [self::USER_ID], [], 100, [], [self::GROUP_ID], 1],
|
|
|
|
|
|
|
|
// group+user vs group
|
|
|
|
[100, [], [self::GROUP_ID2], 100, [self::USER_ID], [self::GROUP_ID], 2],
|
|
|
|
[100, [self::USER_ID], [self::GROUP_ID], 100, [], [self::GROUP_ID2], 1],
|
|
|
|
|
|
|
|
// user vs 'all' (higher priority)
|
|
|
|
[200, [], [], 100, [self::USER_ID], [], 2],
|
|
|
|
[100, [self::USER_ID], [], 200, [], [], 1],
|
|
|
|
|
|
|
|
// group vs group (higher priority)
|
|
|
|
[100, [], [self::GROUP_ID2], 200, [], [self::GROUP_ID], 2],
|
|
|
|
[200, [], [self::GROUP_ID], 100, [], [self::GROUP_ID2], 1],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getUniqueStoragesProvider
|
|
|
|
*/
|
|
|
|
public function testGetUniqueStorages(
|
|
|
|
$priority1, $applicableUsers1, $applicableGroups1,
|
|
|
|
$priority2, $applicableUsers2, $applicableGroups2,
|
|
|
|
$expectedPrecedence
|
|
|
|
) {
|
|
|
|
$backend = $this->backendService->getBackend('identifier:\OCA\Files_External\Lib\Backend\SMB');
|
2015-09-23 17:35:17 +03:00
|
|
|
$backend->method('isVisibleFor')
|
|
|
|
->willReturn(true);
|
2015-09-14 22:55:22 +03:00
|
|
|
$authMechanism = $this->backendService->getAuthMechanism('identifier:\Auth\Mechanism');
|
2015-09-23 17:35:17 +03:00
|
|
|
$authMechanism->method('isVisibleFor')
|
|
|
|
->willReturn(true);
|
2015-09-14 22:55:22 +03:00
|
|
|
|
|
|
|
$storage1 = new StorageConfig();
|
|
|
|
$storage1->setMountPoint('mountpoint');
|
|
|
|
$storage1->setBackend($backend);
|
|
|
|
$storage1->setAuthMechanism($authMechanism);
|
|
|
|
$storage1->setBackendOptions(['password' => 'testPassword']);
|
|
|
|
$storage1->setPriority($priority1);
|
|
|
|
$storage1->setApplicableUsers($applicableUsers1);
|
|
|
|
$storage1->setApplicableGroups($applicableGroups1);
|
|
|
|
|
|
|
|
$storage1 = $this->globalStoragesService->addStorage($storage1);
|
|
|
|
|
|
|
|
$storage2 = new StorageConfig();
|
|
|
|
$storage2->setMountPoint('mountpoint');
|
|
|
|
$storage2->setBackend($backend);
|
|
|
|
$storage2->setAuthMechanism($authMechanism);
|
|
|
|
$storage2->setBackendOptions(['password' => 'testPassword']);
|
|
|
|
$storage2->setPriority($priority2);
|
|
|
|
$storage2->setApplicableUsers($applicableUsers2);
|
|
|
|
$storage2->setApplicableGroups($applicableGroups2);
|
|
|
|
|
|
|
|
$storage2 = $this->globalStoragesService->addStorage($storage2);
|
|
|
|
|
|
|
|
$storages = $this->service->getUniqueStorages();
|
|
|
|
$this->assertCount(1, $storages);
|
|
|
|
|
|
|
|
if ($expectedPrecedence === 1) {
|
|
|
|
$this->assertArrayHasKey($storage1->getID(), $storages);
|
|
|
|
} elseif ($expectedPrecedence === 2) {
|
|
|
|
$this->assertArrayHasKey($storage2->getID(), $storages);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 18:00:21 +03:00
|
|
|
public function testGetStoragesBackendNotVisible() {
|
|
|
|
// we don't test this here
|
2018-01-25 13:23:12 +03:00
|
|
|
$this->addToAssertionCount(1);
|
2015-09-23 18:00:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetStoragesAuthMechanismNotVisible() {
|
|
|
|
// we don't test this here
|
2018-01-25 13:23:12 +03:00
|
|
|
$this->addToAssertionCount(1);
|
2015-09-23 18:00:21 +03:00
|
|
|
}
|
|
|
|
|
2015-08-12 12:00:37 +03:00
|
|
|
public function testHooksAddStorage($a = null, $b = null, $c = null) {
|
|
|
|
// we don't test this here
|
2018-01-25 13:23:12 +03:00
|
|
|
$this->addToAssertionCount(1);
|
2015-08-12 12:00:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testHooksUpdateStorage($a = null, $b = null, $c = null, $d = null, $e = null) {
|
|
|
|
// we don't test this here
|
2018-01-25 13:23:12 +03:00
|
|
|
$this->addToAssertionCount(1);
|
2015-08-12 12:00:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testHooksRenameMountPoint() {
|
|
|
|
// we don't test this here
|
2018-01-25 13:23:12 +03:00
|
|
|
$this->addToAssertionCount(1);
|
2015-08-12 12:00:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testHooksDeleteStorage($a = null, $b = null, $c = null) {
|
|
|
|
// we don't test this here
|
2018-01-25 13:23:12 +03:00
|
|
|
$this->addToAssertionCount(1);
|
2015-08-12 12:00:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testLegacyConfigConversionApplicableAll() {
|
|
|
|
// we don't test this here
|
2018-01-25 13:23:12 +03:00
|
|
|
$this->addToAssertionCount(1);
|
2015-08-12 12:00:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testLegacyConfigConversionApplicableUserAndGroup() {
|
|
|
|
// we don't test this here
|
2018-01-25 13:23:12 +03:00
|
|
|
$this->addToAssertionCount(1);
|
2015-08-12 12:00:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testReadLegacyConfigAndGenerateConfigId() {
|
|
|
|
// we don't test this here
|
2018-01-25 13:23:12 +03:00
|
|
|
$this->addToAssertionCount(1);
|
2015-08-12 12:00:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testReadLegacyConfigNoAuthMechanism() {
|
|
|
|
// we don't test this here
|
2018-01-25 13:23:12 +03:00
|
|
|
$this->addToAssertionCount(1);
|
2015-08-12 12:00:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testReadLegacyConfigClass() {
|
|
|
|
// we don't test this here
|
2018-01-25 13:23:12 +03:00
|
|
|
$this->addToAssertionCount(1);
|
2015-08-12 12:00:37 +03:00
|
|
|
}
|
|
|
|
|
2015-08-31 16:01:05 +03:00
|
|
|
public function testReadEmptyMountPoint() {
|
|
|
|
// we don't test this here
|
2018-01-25 13:23:12 +03:00
|
|
|
$this->addToAssertionCount(1);
|
2015-08-31 16:01:05 +03:00
|
|
|
}
|
|
|
|
|
2015-12-22 16:22:10 +03:00
|
|
|
public function testUpdateStorageMountPoint() {
|
|
|
|
// we don't test this here
|
2018-01-25 13:23:12 +03:00
|
|
|
$this->addToAssertionCount(1);
|
2015-12-22 16:22:10 +03:00
|
|
|
}
|
2015-08-12 12:00:37 +03:00
|
|
|
}
|