GlobalStoragesServiceTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2019-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\MountConfig;
  10. use OCA\Files_External\Service\GlobalStoragesService;
  11. /**
  12. * @group DB
  13. */
  14. class GlobalStoragesServiceTest extends StoragesServiceTest {
  15. protected function setUp(): void {
  16. parent::setUp();
  17. $this->service = new GlobalStoragesService($this->backendService, $this->dbConfig, $this->mountCache, $this->eventDispatcher);
  18. }
  19. protected function tearDown(): void {
  20. @unlink($this->dataDir . '/mount.json');
  21. parent::tearDown();
  22. }
  23. protected function makeTestStorageData() {
  24. return $this->makeStorageConfig([
  25. 'mountPoint' => 'mountpoint',
  26. 'backendIdentifier' => 'identifier:\OCA\Files_External\Lib\Backend\SMB',
  27. 'authMechanismIdentifier' => 'identifier:\Auth\Mechanism',
  28. 'backendOptions' => [
  29. 'option1' => 'value1',
  30. 'option2' => 'value2',
  31. 'password' => 'testPassword',
  32. ],
  33. 'applicableUsers' => [],
  34. 'applicableGroups' => [],
  35. 'priority' => 15,
  36. 'mountOptions' => [
  37. 'preview' => false,
  38. ]
  39. ]);
  40. }
  41. public function storageDataProvider() {
  42. return [
  43. // all users
  44. [
  45. [
  46. 'mountPoint' => 'mountpoint',
  47. 'backendIdentifier' => 'identifier:\OCA\Files_External\Lib\Backend\SMB',
  48. 'authMechanismIdentifier' => 'identifier:\Auth\Mechanism',
  49. 'backendOptions' => [
  50. 'option1' => 'value1',
  51. 'option2' => 'value2',
  52. 'password' => 'testPassword',
  53. ],
  54. 'applicableUsers' => [],
  55. 'applicableGroups' => [],
  56. 'priority' => 15,
  57. ],
  58. ],
  59. // some users
  60. [
  61. [
  62. 'mountPoint' => 'mountpoint',
  63. 'backendIdentifier' => 'identifier:\OCA\Files_External\Lib\Backend\SMB',
  64. 'authMechanismIdentifier' => 'identifier:\Auth\Mechanism',
  65. 'backendOptions' => [
  66. 'option1' => 'value1',
  67. 'option2' => 'value2',
  68. 'password' => 'testPassword',
  69. ],
  70. 'applicableUsers' => ['user1', 'user2'],
  71. 'applicableGroups' => [],
  72. 'priority' => 15,
  73. ],
  74. ],
  75. // some groups
  76. [
  77. [
  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. 'applicableUsers' => [],
  87. 'applicableGroups' => ['group1', 'group2'],
  88. 'priority' => 15,
  89. ],
  90. ],
  91. // both users and groups
  92. [
  93. [
  94. 'mountPoint' => 'mountpoint',
  95. 'backendIdentifier' => 'identifier:\OCA\Files_External\Lib\Backend\SMB',
  96. 'authMechanismIdentifier' => 'identifier:\Auth\Mechanism',
  97. 'backendOptions' => [
  98. 'option1' => 'value1',
  99. 'option2' => 'value2',
  100. 'password' => 'testPassword',
  101. ],
  102. 'applicableUsers' => ['user1', 'user2'],
  103. 'applicableGroups' => ['group1', 'group2'],
  104. 'priority' => 15,
  105. ],
  106. ],
  107. ];
  108. }
  109. /**
  110. * @dataProvider storageDataProvider
  111. */
  112. public function testAddStorage($storageParams): void {
  113. $storage = $this->makeStorageConfig($storageParams);
  114. $newStorage = $this->service->addStorage($storage);
  115. $baseId = $newStorage->getId();
  116. $newStorage = $this->service->getStorage($baseId);
  117. $this->assertEquals($storage->getMountPoint(), $newStorage->getMountPoint());
  118. $this->assertEquals($storage->getBackend(), $newStorage->getBackend());
  119. $this->assertEquals($storage->getAuthMechanism(), $newStorage->getAuthMechanism());
  120. $this->assertEquals($storage->getBackendOptions(), $newStorage->getBackendOptions());
  121. $this->assertEquals($storage->getApplicableUsers(), $newStorage->getApplicableUsers());
  122. $this->assertEquals($storage->getApplicableGroups(), $newStorage->getApplicableGroups());
  123. $this->assertEquals($storage->getPriority(), $newStorage->getPriority());
  124. $this->assertEquals(0, $newStorage->getStatus());
  125. $nextStorage = $this->service->addStorage($storage);
  126. $this->assertEquals($baseId + 1, $nextStorage->getId());
  127. }
  128. /**
  129. * @dataProvider storageDataProvider
  130. */
  131. public function testUpdateStorage($updatedStorageParams): void {
  132. $updatedStorage = $this->makeStorageConfig($updatedStorageParams);
  133. $storage = $this->makeStorageConfig([
  134. 'mountPoint' => 'mountpoint',
  135. 'backendIdentifier' => 'identifier:\OCA\Files_External\Lib\Backend\SMB',
  136. 'authMechanismIdentifier' => 'identifier:\Auth\Mechanism',
  137. 'backendOptions' => [
  138. 'option1' => 'value1',
  139. 'option2' => 'value2',
  140. 'password' => 'testPassword',
  141. ],
  142. 'applicableUsers' => [],
  143. 'applicableGroups' => [],
  144. 'priority' => 15,
  145. ]);
  146. $newStorage = $this->service->addStorage($storage);
  147. $id = $newStorage->getId();
  148. $updatedStorage->setId($id);
  149. $this->service->updateStorage($updatedStorage);
  150. $newStorage = $this->service->getStorage($id);
  151. $this->assertEquals($updatedStorage->getMountPoint(), $newStorage->getMountPoint());
  152. $this->assertEquals($updatedStorage->getBackendOptions()['password'], $newStorage->getBackendOptions()['password']);
  153. $this->assertEquals($updatedStorage->getApplicableUsers(), $newStorage->getApplicableUsers());
  154. $this->assertEquals($updatedStorage->getApplicableGroups(), $newStorage->getApplicableGroups());
  155. $this->assertEquals($updatedStorage->getPriority(), $newStorage->getPriority());
  156. $this->assertEquals(0, $newStorage->getStatus());
  157. }
  158. public function hooksAddStorageDataProvider() {
  159. return [
  160. // applicable all
  161. [
  162. [],
  163. [],
  164. // expected hook calls
  165. [
  166. [
  167. Filesystem::signal_create_mount,
  168. MountConfig::MOUNT_TYPE_USER,
  169. 'all'
  170. ],
  171. ],
  172. ],
  173. // single user
  174. [
  175. ['user1'],
  176. [],
  177. // expected hook calls
  178. [
  179. [
  180. Filesystem::signal_create_mount,
  181. MountConfig::MOUNT_TYPE_USER,
  182. 'user1',
  183. ],
  184. ],
  185. ],
  186. // single group
  187. [
  188. [],
  189. ['group1'],
  190. // expected hook calls
  191. [
  192. [
  193. Filesystem::signal_create_mount,
  194. MountConfig::MOUNT_TYPE_GROUP,
  195. 'group1',
  196. ],
  197. ],
  198. ],
  199. // multiple users
  200. [
  201. ['user1', 'user2'],
  202. [],
  203. [
  204. [
  205. Filesystem::signal_create_mount,
  206. MountConfig::MOUNT_TYPE_USER,
  207. 'user1',
  208. ],
  209. [
  210. Filesystem::signal_create_mount,
  211. MountConfig::MOUNT_TYPE_USER,
  212. 'user2',
  213. ],
  214. ],
  215. ],
  216. // multiple groups
  217. [
  218. [],
  219. ['group1', 'group2'],
  220. // expected hook calls
  221. [
  222. [
  223. Filesystem::signal_create_mount,
  224. MountConfig::MOUNT_TYPE_GROUP,
  225. 'group1'
  226. ],
  227. [
  228. Filesystem::signal_create_mount,
  229. MountConfig::MOUNT_TYPE_GROUP,
  230. 'group2'
  231. ],
  232. ],
  233. ],
  234. // mixed groups and users
  235. [
  236. ['user1', 'user2'],
  237. ['group1', 'group2'],
  238. // expected hook calls
  239. [
  240. [
  241. Filesystem::signal_create_mount,
  242. MountConfig::MOUNT_TYPE_USER,
  243. 'user1',
  244. ],
  245. [
  246. Filesystem::signal_create_mount,
  247. MountConfig::MOUNT_TYPE_USER,
  248. 'user2',
  249. ],
  250. [
  251. Filesystem::signal_create_mount,
  252. MountConfig::MOUNT_TYPE_GROUP,
  253. 'group1'
  254. ],
  255. [
  256. Filesystem::signal_create_mount,
  257. MountConfig::MOUNT_TYPE_GROUP,
  258. 'group2'
  259. ],
  260. ],
  261. ],
  262. ];
  263. }
  264. /**
  265. * @dataProvider hooksAddStorageDataProvider
  266. */
  267. public function testHooksAddStorage($applicableUsers, $applicableGroups, $expectedCalls): void {
  268. $storage = $this->makeTestStorageData();
  269. $storage->setApplicableUsers($applicableUsers);
  270. $storage->setApplicableGroups($applicableGroups);
  271. $this->service->addStorage($storage);
  272. $this->assertCount(count($expectedCalls), self::$hookCalls);
  273. foreach ($expectedCalls as $index => $call) {
  274. $this->assertHookCall(
  275. self::$hookCalls[$index],
  276. $call[0],
  277. $storage->getMountPoint(),
  278. $call[1],
  279. $call[2]
  280. );
  281. }
  282. }
  283. public function hooksUpdateStorageDataProvider() {
  284. return [
  285. [
  286. // nothing to multiple users and groups
  287. [],
  288. [],
  289. ['user1', 'user2'],
  290. ['group1', 'group2'],
  291. // expected hook calls
  292. [
  293. // delete the "all entry"
  294. [
  295. Filesystem::signal_delete_mount,
  296. MountConfig::MOUNT_TYPE_USER,
  297. 'all',
  298. ],
  299. [
  300. Filesystem::signal_create_mount,
  301. MountConfig::MOUNT_TYPE_USER,
  302. 'user1',
  303. ],
  304. [
  305. Filesystem::signal_create_mount,
  306. MountConfig::MOUNT_TYPE_USER,
  307. 'user2',
  308. ],
  309. [
  310. Filesystem::signal_create_mount,
  311. MountConfig::MOUNT_TYPE_GROUP,
  312. 'group1'
  313. ],
  314. [
  315. Filesystem::signal_create_mount,
  316. MountConfig::MOUNT_TYPE_GROUP,
  317. 'group2'
  318. ],
  319. ],
  320. ],
  321. [
  322. // adding a user and a group
  323. ['user1'],
  324. ['group1'],
  325. ['user1', 'user2'],
  326. ['group1', 'group2'],
  327. // expected hook calls
  328. [
  329. [
  330. Filesystem::signal_create_mount,
  331. MountConfig::MOUNT_TYPE_USER,
  332. 'user2',
  333. ],
  334. [
  335. Filesystem::signal_create_mount,
  336. MountConfig::MOUNT_TYPE_GROUP,
  337. 'group2'
  338. ],
  339. ],
  340. ],
  341. [
  342. // removing a user and a group
  343. ['user1', 'user2'],
  344. ['group1', 'group2'],
  345. ['user1'],
  346. ['group1'],
  347. // expected hook calls
  348. [
  349. [
  350. Filesystem::signal_delete_mount,
  351. MountConfig::MOUNT_TYPE_USER,
  352. 'user2',
  353. ],
  354. [
  355. Filesystem::signal_delete_mount,
  356. MountConfig::MOUNT_TYPE_GROUP,
  357. 'group2'
  358. ],
  359. ],
  360. ],
  361. [
  362. // removing all
  363. ['user1'],
  364. ['group1'],
  365. [],
  366. [],
  367. // expected hook calls
  368. [
  369. [
  370. Filesystem::signal_delete_mount,
  371. MountConfig::MOUNT_TYPE_USER,
  372. 'user1',
  373. ],
  374. [
  375. Filesystem::signal_delete_mount,
  376. MountConfig::MOUNT_TYPE_GROUP,
  377. 'group1'
  378. ],
  379. // create the "all" entry
  380. [
  381. Filesystem::signal_create_mount,
  382. MountConfig::MOUNT_TYPE_USER,
  383. 'all'
  384. ],
  385. ],
  386. ],
  387. [
  388. // no changes
  389. ['user1'],
  390. ['group1'],
  391. ['user1'],
  392. ['group1'],
  393. // no hook calls
  394. []
  395. ]
  396. ];
  397. }
  398. /**
  399. * @dataProvider hooksUpdateStorageDataProvider
  400. */
  401. public function testHooksUpdateStorage(
  402. $sourceApplicableUsers,
  403. $sourceApplicableGroups,
  404. $updatedApplicableUsers,
  405. $updatedApplicableGroups,
  406. $expectedCalls): void {
  407. $storage = $this->makeTestStorageData();
  408. $storage->setApplicableUsers($sourceApplicableUsers);
  409. $storage->setApplicableGroups($sourceApplicableGroups);
  410. $storage = $this->service->addStorage($storage);
  411. $storage->setApplicableUsers($updatedApplicableUsers);
  412. $storage->setApplicableGroups($updatedApplicableGroups);
  413. // reset calls
  414. self::$hookCalls = [];
  415. $this->service->updateStorage($storage);
  416. $this->assertCount(count($expectedCalls), self::$hookCalls);
  417. foreach ($expectedCalls as $index => $call) {
  418. $this->assertHookCall(
  419. self::$hookCalls[$index],
  420. $call[0],
  421. '/mountpoint',
  422. $call[1],
  423. $call[2]
  424. );
  425. }
  426. }
  427. public function testHooksRenameMountPoint(): void {
  428. $storage = $this->makeTestStorageData();
  429. $storage->setApplicableUsers(['user1', 'user2']);
  430. $storage->setApplicableGroups(['group1', 'group2']);
  431. $storage = $this->service->addStorage($storage);
  432. $storage->setMountPoint('renamedMountpoint');
  433. // reset calls
  434. self::$hookCalls = [];
  435. $this->service->updateStorage($storage);
  436. $expectedCalls = [
  437. // deletes old mount
  438. [
  439. Filesystem::signal_delete_mount,
  440. '/mountpoint',
  441. MountConfig::MOUNT_TYPE_USER,
  442. 'user1',
  443. ],
  444. [
  445. Filesystem::signal_delete_mount,
  446. '/mountpoint',
  447. MountConfig::MOUNT_TYPE_USER,
  448. 'user2',
  449. ],
  450. [
  451. Filesystem::signal_delete_mount,
  452. '/mountpoint',
  453. MountConfig::MOUNT_TYPE_GROUP,
  454. 'group1',
  455. ],
  456. [
  457. Filesystem::signal_delete_mount,
  458. '/mountpoint',
  459. MountConfig::MOUNT_TYPE_GROUP,
  460. 'group2',
  461. ],
  462. // creates new one
  463. [
  464. Filesystem::signal_create_mount,
  465. '/renamedMountpoint',
  466. MountConfig::MOUNT_TYPE_USER,
  467. 'user1',
  468. ],
  469. [
  470. Filesystem::signal_create_mount,
  471. '/renamedMountpoint',
  472. MountConfig::MOUNT_TYPE_USER,
  473. 'user2',
  474. ],
  475. [
  476. Filesystem::signal_create_mount,
  477. '/renamedMountpoint',
  478. MountConfig::MOUNT_TYPE_GROUP,
  479. 'group1',
  480. ],
  481. [
  482. Filesystem::signal_create_mount,
  483. '/renamedMountpoint',
  484. MountConfig::MOUNT_TYPE_GROUP,
  485. 'group2',
  486. ],
  487. ];
  488. $this->assertCount(count($expectedCalls), self::$hookCalls);
  489. foreach ($expectedCalls as $index => $call) {
  490. $this->assertHookCall(
  491. self::$hookCalls[$index],
  492. $call[0],
  493. $call[1],
  494. $call[2],
  495. $call[3]
  496. );
  497. }
  498. }
  499. public function hooksDeleteStorageDataProvider() {
  500. return [
  501. [
  502. ['user1', 'user2'],
  503. ['group1', 'group2'],
  504. // expected hook calls
  505. [
  506. [
  507. Filesystem::signal_delete_mount,
  508. MountConfig::MOUNT_TYPE_USER,
  509. 'user1',
  510. ],
  511. [
  512. Filesystem::signal_delete_mount,
  513. MountConfig::MOUNT_TYPE_USER,
  514. 'user2',
  515. ],
  516. [
  517. Filesystem::signal_delete_mount,
  518. MountConfig::MOUNT_TYPE_GROUP,
  519. 'group1'
  520. ],
  521. [
  522. Filesystem::signal_delete_mount,
  523. MountConfig::MOUNT_TYPE_GROUP,
  524. 'group2'
  525. ],
  526. ],
  527. ],
  528. [
  529. // deleting "all" entry
  530. [],
  531. [],
  532. [
  533. [
  534. Filesystem::signal_delete_mount,
  535. MountConfig::MOUNT_TYPE_USER,
  536. 'all',
  537. ],
  538. ],
  539. ],
  540. ];
  541. }
  542. /**
  543. * @dataProvider hooksDeleteStorageDataProvider
  544. */
  545. public function testHooksDeleteStorage(
  546. $sourceApplicableUsers,
  547. $sourceApplicableGroups,
  548. $expectedCalls): void {
  549. $storage = $this->makeTestStorageData();
  550. $storage->setApplicableUsers($sourceApplicableUsers);
  551. $storage->setApplicableGroups($sourceApplicableGroups);
  552. $storage = $this->service->addStorage($storage);
  553. // reset calls
  554. self::$hookCalls = [];
  555. $this->service->removeStorage($storage->getId());
  556. $this->assertCount(count($expectedCalls), self::$hookCalls);
  557. foreach ($expectedCalls as $index => $call) {
  558. $this->assertHookCall(
  559. self::$hookCalls[$index],
  560. $call[0],
  561. '/mountpoint',
  562. $call[1],
  563. $call[2]
  564. );
  565. }
  566. }
  567. }