UserStoragesServiceTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_External\Tests\Service;
  8. use OC\Files\Filesystem;
  9. use OCA\Files_External\Lib\StorageConfig;
  10. use OCA\Files_External\MountConfig;
  11. use OCA\Files_External\NotFoundException;
  12. use OCA\Files_External\Service\GlobalStoragesService;
  13. use OCA\Files_External\Service\StoragesService;
  14. use OCA\Files_External\Service\UserStoragesService;
  15. use OCP\IUserSession;
  16. use Test\Traits\UserTrait;
  17. /**
  18. * @group DB
  19. */
  20. class UserStoragesServiceTest extends StoragesServiceTest {
  21. use UserTrait;
  22. private $user;
  23. private $userId;
  24. /**
  25. * @var StoragesService
  26. */
  27. protected $globalStoragesService;
  28. protected function setUp(): void {
  29. parent::setUp();
  30. $this->globalStoragesService = new GlobalStoragesService($this->backendService, $this->dbConfig, $this->mountCache, $this->eventDispatcher);
  31. $this->userId = $this->getUniqueID('user_');
  32. $this->createUser($this->userId, $this->userId);
  33. $this->user = \OC::$server->getUserManager()->get($this->userId);
  34. /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject $userSession */
  35. $userSession = $this->createMock(IUserSession::class);
  36. $userSession
  37. ->expects($this->any())
  38. ->method('getUser')
  39. ->willReturn($this->user);
  40. $this->service = new UserStoragesService($this->backendService, $this->dbConfig, $userSession, $this->mountCache, $this->eventDispatcher);
  41. }
  42. private function makeTestStorageData() {
  43. return $this->makeStorageConfig([
  44. 'mountPoint' => 'mountpoint',
  45. 'backendIdentifier' => 'identifier:\OCA\Files_External\Lib\Backend\SMB',
  46. 'authMechanismIdentifier' => 'identifier:\Auth\Mechanism',
  47. 'backendOptions' => [
  48. 'option1' => 'value1',
  49. 'option2' => 'value2',
  50. 'password' => 'testPassword',
  51. ],
  52. 'mountOptions' => [
  53. 'preview' => false,
  54. ]
  55. ]);
  56. }
  57. public function testAddStorage(): void {
  58. $storage = $this->makeTestStorageData();
  59. $newStorage = $this->service->addStorage($storage);
  60. $id = $newStorage->getId();
  61. $newStorage = $this->service->getStorage($id);
  62. $this->assertEquals($storage->getMountPoint(), $newStorage->getMountPoint());
  63. $this->assertEquals($storage->getBackend(), $newStorage->getBackend());
  64. $this->assertEquals($storage->getAuthMechanism(), $newStorage->getAuthMechanism());
  65. $this->assertEquals($storage->getBackendOptions(), $newStorage->getBackendOptions());
  66. $this->assertEquals(0, $newStorage->getStatus());
  67. // hook called once for user
  68. $this->assertHookCall(
  69. current(self::$hookCalls),
  70. Filesystem::signal_create_mount,
  71. $storage->getMountPoint(),
  72. MountConfig::MOUNT_TYPE_USER,
  73. $this->userId
  74. );
  75. $nextStorage = $this->service->addStorage($storage);
  76. $this->assertEquals($id + 1, $nextStorage->getId());
  77. }
  78. public function testUpdateStorage(): void {
  79. $storage = $this->makeStorageConfig([
  80. 'mountPoint' => 'mountpoint',
  81. 'backendIdentifier' => 'identifier:\OCA\Files_External\Lib\Backend\SMB',
  82. 'authMechanismIdentifier' => 'identifier:\Auth\Mechanism',
  83. 'backendOptions' => [
  84. 'option1' => 'value1',
  85. 'option2' => 'value2',
  86. 'password' => 'testPassword',
  87. ],
  88. ]);
  89. $newStorage = $this->service->addStorage($storage);
  90. $backendOptions = $newStorage->getBackendOptions();
  91. $backendOptions['password'] = 'anotherPassword';
  92. $newStorage->setBackendOptions($backendOptions);
  93. self::$hookCalls = [];
  94. $newStorage = $this->service->updateStorage($newStorage);
  95. $this->assertEquals('anotherPassword', $newStorage->getBackendOptions()['password']);
  96. $this->assertEquals([$this->userId], $newStorage->getApplicableUsers());
  97. // these attributes are unused for user storages
  98. $this->assertEmpty($newStorage->getApplicableGroups());
  99. $this->assertEquals(0, $newStorage->getStatus());
  100. // no hook calls
  101. $this->assertEmpty(self::$hookCalls);
  102. }
  103. /**
  104. * @dataProvider deleteStorageDataProvider
  105. */
  106. public function testDeleteStorage($backendOptions, $rustyStorageId): void {
  107. parent::testDeleteStorage($backendOptions, $rustyStorageId);
  108. // hook called once for user (first one was during test creation)
  109. $this->assertHookCall(
  110. self::$hookCalls[1],
  111. Filesystem::signal_delete_mount,
  112. '/mountpoint',
  113. MountConfig::MOUNT_TYPE_USER,
  114. $this->userId
  115. );
  116. }
  117. public function testHooksRenameMountPoint(): void {
  118. $storage = $this->makeTestStorageData();
  119. $storage = $this->service->addStorage($storage);
  120. $storage->setMountPoint('renamedMountpoint');
  121. // reset calls
  122. self::$hookCalls = [];
  123. $this->service->updateStorage($storage);
  124. // hook called twice
  125. $this->assertHookCall(
  126. self::$hookCalls[0],
  127. Filesystem::signal_delete_mount,
  128. '/mountpoint',
  129. MountConfig::MOUNT_TYPE_USER,
  130. $this->userId
  131. );
  132. $this->assertHookCall(
  133. self::$hookCalls[1],
  134. Filesystem::signal_create_mount,
  135. '/renamedMountpoint',
  136. MountConfig::MOUNT_TYPE_USER,
  137. $this->userId
  138. );
  139. }
  140. public function testGetAdminStorage(): void {
  141. $this->expectException(NotFoundException::class);
  142. $backend = $this->backendService->getBackend('identifier:\OCA\Files_External\Lib\Backend\SMB');
  143. $authMechanism = $this->backendService->getAuthMechanism('identifier:\Auth\Mechanism');
  144. $storage = new StorageConfig();
  145. $storage->setMountPoint('mountpoint');
  146. $storage->setBackend($backend);
  147. $storage->setAuthMechanism($authMechanism);
  148. $storage->setBackendOptions(['password' => 'testPassword']);
  149. $storage->setApplicableUsers([$this->userId]);
  150. $newStorage = $this->globalStoragesService->addStorage($storage);
  151. $this->assertInstanceOf('\OCA\Files_External\Lib\StorageConfig', $this->globalStoragesService->getStorage($newStorage->getId()));
  152. $this->service->getStorage($newStorage->getId());
  153. }
  154. }