UserStoragesServiceTest.php 5.7 KB

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