GlobalStoragesServiceTest.php 14 KB

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