UserGlobalStoragesServiceTest.php 10 KB

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