UserStoragesServiceTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Robin McCorkell <robin@mccorkell.me.uk>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Vincent Petry <vincent@nextcloud.com>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\Files_External\Tests\Service;
  29. use OC\Files\Filesystem;
  30. use OCA\Files_External\Lib\StorageConfig;
  31. use OCA\Files_External\Service\GlobalStoragesService;
  32. use OCA\Files_External\Service\StoragesService;
  33. use OCA\Files_External\Service\UserStoragesService;
  34. use OCP\IUserSession;
  35. use Test\Traits\UserTrait;
  36. /**
  37. * @group DB
  38. */
  39. class UserStoragesServiceTest extends StoragesServiceTest {
  40. use UserTrait;
  41. private $user;
  42. private $userId;
  43. /**
  44. * @var StoragesService
  45. */
  46. protected $globalStoragesService;
  47. protected function setUp(): void {
  48. parent::setUp();
  49. $this->globalStoragesService = new GlobalStoragesService($this->backendService, $this->dbConfig, $this->mountCache, $this->eventDispatcher);
  50. $this->userId = $this->getUniqueID('user_');
  51. $this->createUser($this->userId, $this->userId);
  52. $this->user = \OC::$server->getUserManager()->get($this->userId);
  53. /** @var \OCP\IUserSession|\PHPUnit\Framework\MockObject\MockObject $userSession */
  54. $userSession = $this->createMock(IUserSession::class);
  55. $userSession
  56. ->expects($this->any())
  57. ->method('getUser')
  58. ->willReturn($this->user);
  59. $this->service = new UserStoragesService($this->backendService, $this->dbConfig, $userSession, $this->mountCache, $this->eventDispatcher);
  60. }
  61. private function makeTestStorageData() {
  62. return $this->makeStorageConfig([
  63. 'mountPoint' => 'mountpoint',
  64. 'backendIdentifier' => 'identifier:\OCA\Files_External\Lib\Backend\SMB',
  65. 'authMechanismIdentifier' => 'identifier:\Auth\Mechanism',
  66. 'backendOptions' => [
  67. 'option1' => 'value1',
  68. 'option2' => 'value2',
  69. 'password' => 'testPassword',
  70. ],
  71. 'mountOptions' => [
  72. 'preview' => false,
  73. ]
  74. ]);
  75. }
  76. public function testAddStorage() {
  77. $storage = $this->makeTestStorageData();
  78. $newStorage = $this->service->addStorage($storage);
  79. $id = $newStorage->getId();
  80. $newStorage = $this->service->getStorage($id);
  81. $this->assertEquals($storage->getMountPoint(), $newStorage->getMountPoint());
  82. $this->assertEquals($storage->getBackend(), $newStorage->getBackend());
  83. $this->assertEquals($storage->getAuthMechanism(), $newStorage->getAuthMechanism());
  84. $this->assertEquals($storage->getBackendOptions(), $newStorage->getBackendOptions());
  85. $this->assertEquals(0, $newStorage->getStatus());
  86. // hook called once for user
  87. $this->assertHookCall(
  88. current(self::$hookCalls),
  89. Filesystem::signal_create_mount,
  90. $storage->getMountPoint(),
  91. \OCA\Files_External\MountConfig::MOUNT_TYPE_USER,
  92. $this->userId
  93. );
  94. $nextStorage = $this->service->addStorage($storage);
  95. $this->assertEquals($id + 1, $nextStorage->getId());
  96. }
  97. public function testUpdateStorage() {
  98. $storage = $this->makeStorageConfig([
  99. 'mountPoint' => 'mountpoint',
  100. 'backendIdentifier' => 'identifier:\OCA\Files_External\Lib\Backend\SMB',
  101. 'authMechanismIdentifier' => 'identifier:\Auth\Mechanism',
  102. 'backendOptions' => [
  103. 'option1' => 'value1',
  104. 'option2' => 'value2',
  105. 'password' => 'testPassword',
  106. ],
  107. ]);
  108. $newStorage = $this->service->addStorage($storage);
  109. $backendOptions = $newStorage->getBackendOptions();
  110. $backendOptions['password'] = 'anotherPassword';
  111. $newStorage->setBackendOptions($backendOptions);
  112. self::$hookCalls = [];
  113. $newStorage = $this->service->updateStorage($newStorage);
  114. $this->assertEquals('anotherPassword', $newStorage->getBackendOptions()['password']);
  115. $this->assertEquals([$this->userId], $newStorage->getApplicableUsers());
  116. // these attributes are unused for user storages
  117. $this->assertEmpty($newStorage->getApplicableGroups());
  118. $this->assertEquals(0, $newStorage->getStatus());
  119. // no hook calls
  120. $this->assertEmpty(self::$hookCalls);
  121. }
  122. /**
  123. * @dataProvider deleteStorageDataProvider
  124. */
  125. public function testDeleteStorage($backendOptions, $rustyStorageId) {
  126. parent::testDeleteStorage($backendOptions, $rustyStorageId);
  127. // hook called once for user (first one was during test creation)
  128. $this->assertHookCall(
  129. self::$hookCalls[1],
  130. Filesystem::signal_delete_mount,
  131. '/mountpoint',
  132. \OCA\Files_External\MountConfig::MOUNT_TYPE_USER,
  133. $this->userId
  134. );
  135. }
  136. public function testHooksRenameMountPoint() {
  137. $storage = $this->makeTestStorageData();
  138. $storage = $this->service->addStorage($storage);
  139. $storage->setMountPoint('renamedMountpoint');
  140. // reset calls
  141. self::$hookCalls = [];
  142. $this->service->updateStorage($storage);
  143. // hook called twice
  144. $this->assertHookCall(
  145. self::$hookCalls[0],
  146. Filesystem::signal_delete_mount,
  147. '/mountpoint',
  148. \OCA\Files_External\MountConfig::MOUNT_TYPE_USER,
  149. $this->userId
  150. );
  151. $this->assertHookCall(
  152. self::$hookCalls[1],
  153. Filesystem::signal_create_mount,
  154. '/renamedMountpoint',
  155. \OCA\Files_External\MountConfig::MOUNT_TYPE_USER,
  156. $this->userId
  157. );
  158. }
  159. public function testGetAdminStorage() {
  160. $this->expectException(\OCA\Files_External\NotFoundException::class);
  161. $backend = $this->backendService->getBackend('identifier:\OCA\Files_External\Lib\Backend\SMB');
  162. $authMechanism = $this->backendService->getAuthMechanism('identifier:\Auth\Mechanism');
  163. $storage = new StorageConfig();
  164. $storage->setMountPoint('mountpoint');
  165. $storage->setBackend($backend);
  166. $storage->setAuthMechanism($authMechanism);
  167. $storage->setBackendOptions(['password' => 'testPassword']);
  168. $storage->setApplicableUsers([$this->userId]);
  169. $newStorage = $this->globalStoragesService->addStorage($storage);
  170. $this->assertInstanceOf('\OCA\Files_External\Lib\StorageConfig', $this->globalStoragesService->getStorage($newStorage->getId()));
  171. $this->service->getStorage($newStorage->getId());
  172. }
  173. }