GlobalStoragesServiceTest.php 15 KB

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