123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 |
- <?php
- /**
- * 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\Service;
- use OC\User\User;
- use OCA\Files_External\Lib\StorageConfig;
- use OCA\Files_External\NotFoundException;
- use OCA\Files_External\Service\StoragesService;
- use OCA\Files_External\Service\UserGlobalStoragesService;
- use OCP\EventDispatcher\IEventDispatcher;
- use OCP\IGroupManager;
- use OCP\IUser;
- use OCP\IUserSession;
- use Test\Traits\UserTrait;
- /**
- * @group DB
- */
- class UserGlobalStoragesServiceTest extends GlobalStoragesServiceTest {
- use UserTrait;
- /** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject groupManager */
- protected $groupManager;
- /**
- * @var StoragesService
- */
- protected $globalStoragesService;
- /**
- * @var UserGlobalStoragesService
- */
- protected $service;
- protected $user;
- public const USER_ID = 'test_user';
- public const GROUP_ID = 'test_group';
- public const GROUP_ID2 = 'test_group2';
- protected function setUp(): void {
- parent::setUp();
- $this->globalStoragesService = $this->service;
- $this->user = new User(self::USER_ID, null, \OC::$server->get(IEventDispatcher::class));
- /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject $userSession */
- $userSession = $this->createMock(IUserSession::class);
- $userSession
- ->expects($this->any())
- ->method('getUser')
- ->willReturn($this->user);
- $this->groupManager = $this->createMock(IGroupManager::class);
- $this->groupManager->method('isInGroup')
- ->willReturnCallback(function ($userId, $groupId) {
- if ($userId === self::USER_ID) {
- switch ($groupId) {
- case self::GROUP_ID:
- case self::GROUP_ID2:
- return true;
- }
- }
- return false;
- });
- $this->groupManager->method('getUserGroupIds')
- ->willReturnCallback(function (IUser $user) {
- if ($user->getUID() === self::USER_ID) {
- return [self::GROUP_ID, self::GROUP_ID2];
- } else {
- return [];
- }
- });
- $this->service = new UserGlobalStoragesService(
- $this->backendService,
- $this->dbConfig,
- $userSession,
- $this->groupManager,
- $this->mountCache,
- $this->eventDispatcher,
- );
- }
- 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): void {
- $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));
- try {
- $this->service->getStorage($newStorage->getId());
- $this->fail('Failed asserting that storage can\'t be accessed by id');
- } catch (NotFoundException $e) {
- }
- }
- }
- public function testAddStorage($storageParams = null): void {
- $this->expectException(\DomainException::class);
- $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);
- }
- public function testUpdateStorage($storageParams = null): void {
- $this->expectException(\DomainException::class);
- $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);
- }
- public function testNonExistingStorage(): void {
- $this->expectException(\DomainException::class);
- $this->ActualNonExistingStorageTest();
- }
- /**
- * @dataProvider deleteStorageDataProvider
- */
- public function testDeleteStorage($backendOptions, $rustyStorageId): void {
- $this->expectException(\DomainException::class);
- $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($backendOptions);
- $newStorage = $this->globalStoragesService->addStorage($storage);
- $id = $newStorage->getId();
- $this->service->removeStorage($id);
- }
- public function testDeleteUnexistingStorage(): void {
- $this->expectException(\DomainException::class);
- $this->actualDeletedUnexistingStorageTest();
- }
- 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,
- ): void {
- $backend = $this->backendService->getBackend('identifier:\OCA\Files_External\Lib\Backend\SMB');
- $backend->method('isVisibleFor')
- ->willReturn(true);
- $authMechanism = $this->backendService->getAuthMechanism('identifier:\Auth\Mechanism');
- $authMechanism->method('isVisibleFor')
- ->willReturn(true);
- $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);
- }
- }
- public function testGetStoragesBackendNotVisible(): void {
- // we don't test this here
- $this->addToAssertionCount(1);
- }
- public function testGetStoragesAuthMechanismNotVisible(): void {
- // we don't test this here
- $this->addToAssertionCount(1);
- }
- public function testHooksAddStorage($a = null, $b = null, $c = null): void {
- // we don't test this here
- $this->addToAssertionCount(1);
- }
- public function testHooksUpdateStorage($a = null, $b = null, $c = null, $d = null, $e = null): void {
- // we don't test this here
- $this->addToAssertionCount(1);
- }
- public function testHooksRenameMountPoint(): void {
- // we don't test this here
- $this->addToAssertionCount(1);
- }
- public function testHooksDeleteStorage($a = null, $b = null, $c = null): void {
- // we don't test this here
- $this->addToAssertionCount(1);
- }
- public function testLegacyConfigConversionApplicableAll(): void {
- // we don't test this here
- $this->addToAssertionCount(1);
- }
- public function testLegacyConfigConversionApplicableUserAndGroup(): void {
- // we don't test this here
- $this->addToAssertionCount(1);
- }
- public function testReadLegacyConfigAndGenerateConfigId(): void {
- // we don't test this here
- $this->addToAssertionCount(1);
- }
- public function testReadLegacyConfigNoAuthMechanism(): void {
- // we don't test this here
- $this->addToAssertionCount(1);
- }
- public function testReadLegacyConfigClass(): void {
- // we don't test this here
- $this->addToAssertionCount(1);
- }
- public function testReadEmptyMountPoint(): void {
- // we don't test this here
- $this->addToAssertionCount(1);
- }
- public function testUpdateStorageMountPoint(): void {
- // we don't test this here
- $this->addToAssertionCount(1);
- }
- }
|