UserGlobalStoragesServiceTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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\User\User;
  9. use OCA\Files_External\Lib\StorageConfig;
  10. use OCA\Files_External\NotFoundException;
  11. use OCA\Files_External\Service\StoragesService;
  12. use OCA\Files_External\Service\UserGlobalStoragesService;
  13. use OCP\EventDispatcher\IEventDispatcher;
  14. use OCP\IGroupManager;
  15. use OCP\IUser;
  16. use OCP\IUserSession;
  17. use Test\Traits\UserTrait;
  18. /**
  19. * @group DB
  20. */
  21. class UserGlobalStoragesServiceTest extends GlobalStoragesServiceTest {
  22. use UserTrait;
  23. /** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject groupManager */
  24. protected $groupManager;
  25. /**
  26. * @var StoragesService
  27. */
  28. protected $globalStoragesService;
  29. /**
  30. * @var UserGlobalStoragesService
  31. */
  32. protected $service;
  33. protected $user;
  34. public const USER_ID = 'test_user';
  35. public const GROUP_ID = 'test_group';
  36. public const GROUP_ID2 = 'test_group2';
  37. protected function setUp(): void {
  38. parent::setUp();
  39. $this->globalStoragesService = $this->service;
  40. $this->user = new User(self::USER_ID, null, \OC::$server->get(IEventDispatcher::class));
  41. /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject $userSession */
  42. $userSession = $this->createMock(IUserSession::class);
  43. $userSession
  44. ->expects($this->any())
  45. ->method('getUser')
  46. ->willReturn($this->user);
  47. $this->groupManager = $this->createMock(IGroupManager::class);
  48. $this->groupManager->method('isInGroup')
  49. ->willReturnCallback(function ($userId, $groupId) {
  50. if ($userId === self::USER_ID) {
  51. switch ($groupId) {
  52. case self::GROUP_ID:
  53. case self::GROUP_ID2:
  54. return true;
  55. }
  56. }
  57. return false;
  58. });
  59. $this->groupManager->method('getUserGroupIds')
  60. ->willReturnCallback(function (IUser $user) {
  61. if ($user->getUID() === self::USER_ID) {
  62. return [self::GROUP_ID, self::GROUP_ID2];
  63. } else {
  64. return [];
  65. }
  66. });
  67. $this->service = new UserGlobalStoragesService(
  68. $this->backendService,
  69. $this->dbConfig,
  70. $userSession,
  71. $this->groupManager,
  72. $this->mountCache,
  73. $this->eventDispatcher,
  74. );
  75. }
  76. public function applicableStorageProvider() {
  77. return [
  78. [[], [], true],
  79. // not applicable cases
  80. [['user1'], [], false],
  81. [[], ['group1'], false],
  82. [['user1'], ['group1'], false],
  83. // applicable cases
  84. [[self::USER_ID], [], true],
  85. [[], [self::GROUP_ID], true],
  86. [[self::USER_ID], ['group1'], true],
  87. [['user1'], [self::GROUP_ID], true],
  88. // sanity checks
  89. [['user1', 'user2', self::USER_ID, 'user3'], [], true],
  90. ];
  91. }
  92. /**
  93. * @dataProvider applicableStorageProvider
  94. */
  95. public function testGetStorageWithApplicable($applicableUsers, $applicableGroups, $isVisible): void {
  96. $backend = $this->backendService->getBackend('identifier:\OCA\Files_External\Lib\Backend\SMB');
  97. $authMechanism = $this->backendService->getAuthMechanism('identifier:\Auth\Mechanism');
  98. $storage = new StorageConfig();
  99. $storage->setMountPoint('mountpoint');
  100. $storage->setBackend($backend);
  101. $storage->setAuthMechanism($authMechanism);
  102. $storage->setBackendOptions(['password' => 'testPassword']);
  103. $storage->setApplicableUsers($applicableUsers);
  104. $storage->setApplicableGroups($applicableGroups);
  105. $newStorage = $this->globalStoragesService->addStorage($storage);
  106. $storages = $this->service->getAllStorages();
  107. if ($isVisible) {
  108. $this->assertEquals(1, count($storages));
  109. $retrievedStorage = $this->service->getStorage($newStorage->getId());
  110. $this->assertEquals('/mountpoint', $retrievedStorage->getMountPoint());
  111. } else {
  112. $this->assertEquals(0, count($storages));
  113. try {
  114. $this->service->getStorage($newStorage->getId());
  115. $this->fail('Failed asserting that storage can\'t be accessed by id');
  116. } catch (NotFoundException $e) {
  117. }
  118. }
  119. }
  120. public function testAddStorage($storageParams = null): void {
  121. $this->expectException(\DomainException::class);
  122. $backend = $this->backendService->getBackend('identifier:\OCA\Files_External\Lib\Backend\SMB');
  123. $authMechanism = $this->backendService->getAuthMechanism('identifier:\Auth\Mechanism');
  124. $storage = new StorageConfig(255);
  125. $storage->setMountPoint('mountpoint');
  126. $storage->setBackend($backend);
  127. $storage->setAuthMechanism($authMechanism);
  128. $storage->setBackendOptions(['password' => 'testPassword']);
  129. $this->service->addStorage($storage);
  130. }
  131. public function testUpdateStorage($storageParams = null): void {
  132. $this->expectException(\DomainException::class);
  133. $backend = $this->backendService->getBackend('identifier:\OCA\Files_External\Lib\Backend\SMB');
  134. $authMechanism = $this->backendService->getAuthMechanism('identifier:\Auth\Mechanism');
  135. $storage = new StorageConfig(255);
  136. $storage->setMountPoint('mountpoint');
  137. $storage->setBackend($backend);
  138. $storage->setAuthMechanism($authMechanism);
  139. $storage->setBackendOptions(['password' => 'testPassword']);
  140. $newStorage = $this->globalStoragesService->addStorage($storage);
  141. $retrievedStorage = $this->service->getStorage($newStorage->getId());
  142. $retrievedStorage->setMountPoint('abc');
  143. $this->service->updateStorage($retrievedStorage);
  144. }
  145. public function testNonExistingStorage(): void {
  146. $this->expectException(\DomainException::class);
  147. $this->ActualNonExistingStorageTest();
  148. }
  149. /**
  150. * @dataProvider deleteStorageDataProvider
  151. */
  152. public function testDeleteStorage($backendOptions, $rustyStorageId): void {
  153. $this->expectException(\DomainException::class);
  154. $backend = $this->backendService->getBackend('identifier:\OCA\Files_External\Lib\Backend\SMB');
  155. $authMechanism = $this->backendService->getAuthMechanism('identifier:\Auth\Mechanism');
  156. $storage = new StorageConfig(255);
  157. $storage->setMountPoint('mountpoint');
  158. $storage->setBackend($backend);
  159. $storage->setAuthMechanism($authMechanism);
  160. $storage->setBackendOptions($backendOptions);
  161. $newStorage = $this->globalStoragesService->addStorage($storage);
  162. $id = $newStorage->getId();
  163. $this->service->removeStorage($id);
  164. }
  165. public function testDeleteUnexistingStorage(): void {
  166. $this->expectException(\DomainException::class);
  167. $this->actualDeletedUnexistingStorageTest();
  168. }
  169. public function getUniqueStoragesProvider() {
  170. return [
  171. // 'all' vs group
  172. [100, [], [], 100, [], [self::GROUP_ID], 2],
  173. [100, [], [self::GROUP_ID], 100, [], [], 1],
  174. // 'all' vs user
  175. [100, [], [], 100, [self::USER_ID], [], 2],
  176. [100, [self::USER_ID], [], 100, [], [], 1],
  177. // group vs user
  178. [100, [], [self::GROUP_ID], 100, [self::USER_ID], [], 2],
  179. [100, [self::USER_ID], [], 100, [], [self::GROUP_ID], 1],
  180. // group+user vs group
  181. [100, [], [self::GROUP_ID2], 100, [self::USER_ID], [self::GROUP_ID], 2],
  182. [100, [self::USER_ID], [self::GROUP_ID], 100, [], [self::GROUP_ID2], 1],
  183. // user vs 'all' (higher priority)
  184. [200, [], [], 100, [self::USER_ID], [], 2],
  185. [100, [self::USER_ID], [], 200, [], [], 1],
  186. // group vs group (higher priority)
  187. [100, [], [self::GROUP_ID2], 200, [], [self::GROUP_ID], 2],
  188. [200, [], [self::GROUP_ID], 100, [], [self::GROUP_ID2], 1],
  189. ];
  190. }
  191. /**
  192. * @dataProvider getUniqueStoragesProvider
  193. */
  194. public function testGetUniqueStorages(
  195. $priority1, $applicableUsers1, $applicableGroups1,
  196. $priority2, $applicableUsers2, $applicableGroups2,
  197. $expectedPrecedence,
  198. ): void {
  199. $backend = $this->backendService->getBackend('identifier:\OCA\Files_External\Lib\Backend\SMB');
  200. $backend->method('isVisibleFor')
  201. ->willReturn(true);
  202. $authMechanism = $this->backendService->getAuthMechanism('identifier:\Auth\Mechanism');
  203. $authMechanism->method('isVisibleFor')
  204. ->willReturn(true);
  205. $storage1 = new StorageConfig();
  206. $storage1->setMountPoint('mountpoint');
  207. $storage1->setBackend($backend);
  208. $storage1->setAuthMechanism($authMechanism);
  209. $storage1->setBackendOptions(['password' => 'testPassword']);
  210. $storage1->setPriority($priority1);
  211. $storage1->setApplicableUsers($applicableUsers1);
  212. $storage1->setApplicableGroups($applicableGroups1);
  213. $storage1 = $this->globalStoragesService->addStorage($storage1);
  214. $storage2 = new StorageConfig();
  215. $storage2->setMountPoint('mountpoint');
  216. $storage2->setBackend($backend);
  217. $storage2->setAuthMechanism($authMechanism);
  218. $storage2->setBackendOptions(['password' => 'testPassword']);
  219. $storage2->setPriority($priority2);
  220. $storage2->setApplicableUsers($applicableUsers2);
  221. $storage2->setApplicableGroups($applicableGroups2);
  222. $storage2 = $this->globalStoragesService->addStorage($storage2);
  223. $storages = $this->service->getUniqueStorages();
  224. $this->assertCount(1, $storages);
  225. if ($expectedPrecedence === 1) {
  226. $this->assertArrayHasKey($storage1->getID(), $storages);
  227. } elseif ($expectedPrecedence === 2) {
  228. $this->assertArrayHasKey($storage2->getID(), $storages);
  229. }
  230. }
  231. public function testGetStoragesBackendNotVisible(): void {
  232. // we don't test this here
  233. $this->addToAssertionCount(1);
  234. }
  235. public function testGetStoragesAuthMechanismNotVisible(): void {
  236. // we don't test this here
  237. $this->addToAssertionCount(1);
  238. }
  239. public function testHooksAddStorage($a = null, $b = null, $c = null): void {
  240. // we don't test this here
  241. $this->addToAssertionCount(1);
  242. }
  243. public function testHooksUpdateStorage($a = null, $b = null, $c = null, $d = null, $e = null): void {
  244. // we don't test this here
  245. $this->addToAssertionCount(1);
  246. }
  247. public function testHooksRenameMountPoint(): void {
  248. // we don't test this here
  249. $this->addToAssertionCount(1);
  250. }
  251. public function testHooksDeleteStorage($a = null, $b = null, $c = null): void {
  252. // we don't test this here
  253. $this->addToAssertionCount(1);
  254. }
  255. public function testLegacyConfigConversionApplicableAll(): void {
  256. // we don't test this here
  257. $this->addToAssertionCount(1);
  258. }
  259. public function testLegacyConfigConversionApplicableUserAndGroup(): void {
  260. // we don't test this here
  261. $this->addToAssertionCount(1);
  262. }
  263. public function testReadLegacyConfigAndGenerateConfigId(): void {
  264. // we don't test this here
  265. $this->addToAssertionCount(1);
  266. }
  267. public function testReadLegacyConfigNoAuthMechanism(): void {
  268. // we don't test this here
  269. $this->addToAssertionCount(1);
  270. }
  271. public function testReadLegacyConfigClass(): void {
  272. // we don't test this here
  273. $this->addToAssertionCount(1);
  274. }
  275. public function testReadEmptyMountPoint(): void {
  276. // we don't test this here
  277. $this->addToAssertionCount(1);
  278. }
  279. public function testUpdateStorageMountPoint(): void {
  280. // we don't test this here
  281. $this->addToAssertionCount(1);
  282. }
  283. }