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