GlobalStoragesServiceTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Robin Appelman <robin@icewind.nl>
  7. * @author Robin McCorkell <robin@mccorkell.me.uk>
  8. * @author Vincent Petry <pvince81@owncloud.com>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\Files_External\Tests\Service;
  26. use \OC\Files\Filesystem;
  27. use \OCA\Files_External\Service\GlobalStoragesService;
  28. /**
  29. * @group DB
  30. */
  31. class GlobalStoragesServiceTest extends StoragesServiceTest {
  32. public function setUp() {
  33. parent::setUp();
  34. $this->service = new GlobalStoragesService($this->backendService, $this->dbConfig, $this->mountCache);
  35. }
  36. public function tearDown() {
  37. @unlink($this->dataDir . '/mount.json');
  38. parent::tearDown();
  39. }
  40. protected function makeTestStorageData() {
  41. return $this->makeStorageConfig([
  42. 'mountPoint' => 'mountpoint',
  43. 'backendIdentifier' => 'identifier:\OCA\Files_External\Lib\Backend\SMB',
  44. 'authMechanismIdentifier' => 'identifier:\Auth\Mechanism',
  45. 'backendOptions' => [
  46. 'option1' => 'value1',
  47. 'option2' => 'value2',
  48. 'password' => 'testPassword',
  49. ],
  50. 'applicableUsers' => [],
  51. 'applicableGroups' => [],
  52. 'priority' => 15,
  53. 'mountOptions' => [
  54. 'preview' => false,
  55. ]
  56. ]);
  57. }
  58. function storageDataProvider() {
  59. return [
  60. // all users
  61. [
  62. [
  63. 'mountPoint' => 'mountpoint',
  64. 'backendIdentifier' => 'identifier:\OCA\Files_External\Lib\Backend\SMB',
  65. 'authMechanismIdentifier' => 'identifier:\Auth\Mechanism',
  66. 'backendOptions' => [
  67. 'option1' => 'value1',
  68. 'option2' => 'value2',
  69. 'password' => 'testPassword',
  70. ],
  71. 'applicableUsers' => [],
  72. 'applicableGroups' => [],
  73. 'priority' => 15,
  74. ],
  75. ],
  76. // some users
  77. [
  78. [
  79. 'mountPoint' => 'mountpoint',
  80. 'backendIdentifier' => 'identifier:\OCA\Files_External\Lib\Backend\SMB',
  81. 'authMechanismIdentifier' => 'identifier:\Auth\Mechanism',
  82. 'backendOptions' => [
  83. 'option1' => 'value1',
  84. 'option2' => 'value2',
  85. 'password' => 'testPassword',
  86. ],
  87. 'applicableUsers' => ['user1', 'user2'],
  88. 'applicableGroups' => [],
  89. 'priority' => 15,
  90. ],
  91. ],
  92. // some groups
  93. [
  94. [
  95. 'mountPoint' => 'mountpoint',
  96. 'backendIdentifier' => 'identifier:\OCA\Files_External\Lib\Backend\SMB',
  97. 'authMechanismIdentifier' => 'identifier:\Auth\Mechanism',
  98. 'backendOptions' => [
  99. 'option1' => 'value1',
  100. 'option2' => 'value2',
  101. 'password' => 'testPassword',
  102. ],
  103. 'applicableUsers' => [],
  104. 'applicableGroups' => ['group1', 'group2'],
  105. 'priority' => 15,
  106. ],
  107. ],
  108. // both users and groups
  109. [
  110. [
  111. 'mountPoint' => 'mountpoint',
  112. 'backendIdentifier' => 'identifier:\OCA\Files_External\Lib\Backend\SMB',
  113. 'authMechanismIdentifier' => 'identifier:\Auth\Mechanism',
  114. 'backendOptions' => [
  115. 'option1' => 'value1',
  116. 'option2' => 'value2',
  117. 'password' => 'testPassword',
  118. ],
  119. 'applicableUsers' => ['user1', 'user2'],
  120. 'applicableGroups' => ['group1', 'group2'],
  121. 'priority' => 15,
  122. ],
  123. ],
  124. ];
  125. }
  126. /**
  127. * @dataProvider storageDataProvider
  128. */
  129. public function testAddStorage($storageParams) {
  130. $storage = $this->makeStorageConfig($storageParams);
  131. $newStorage = $this->service->addStorage($storage);
  132. $baseId = $newStorage->getId();
  133. $newStorage = $this->service->getStorage($baseId);
  134. $this->assertEquals($storage->getMountPoint(), $newStorage->getMountPoint());
  135. $this->assertEquals($storage->getBackend(), $newStorage->getBackend());
  136. $this->assertEquals($storage->getAuthMechanism(), $newStorage->getAuthMechanism());
  137. $this->assertEquals($storage->getBackendOptions(), $newStorage->getBackendOptions());
  138. $this->assertEquals($storage->getApplicableUsers(), $newStorage->getApplicableUsers());
  139. $this->assertEquals($storage->getApplicableGroups(), $newStorage->getApplicableGroups());
  140. $this->assertEquals($storage->getPriority(), $newStorage->getPriority());
  141. $this->assertEquals(0, $newStorage->getStatus());
  142. $nextStorage = $this->service->addStorage($storage);
  143. $this->assertEquals($baseId + 1, $nextStorage->getId());
  144. }
  145. /**
  146. * @dataProvider storageDataProvider
  147. */
  148. public function testUpdateStorage($updatedStorageParams) {
  149. $updatedStorage = $this->makeStorageConfig($updatedStorageParams);
  150. $storage = $this->makeStorageConfig([
  151. 'mountPoint' => 'mountpoint',
  152. 'backendIdentifier' => 'identifier:\OCA\Files_External\Lib\Backend\SMB',
  153. 'authMechanismIdentifier' => 'identifier:\Auth\Mechanism',
  154. 'backendOptions' => [
  155. 'option1' => 'value1',
  156. 'option2' => 'value2',
  157. 'password' => 'testPassword',
  158. ],
  159. 'applicableUsers' => [],
  160. 'applicableGroups' => [],
  161. 'priority' => 15,
  162. ]);
  163. $newStorage = $this->service->addStorage($storage);
  164. $id = $newStorage->getId();
  165. $updatedStorage->setId($id);
  166. $this->service->updateStorage($updatedStorage);
  167. $newStorage = $this->service->getStorage($id);
  168. $this->assertEquals($updatedStorage->getMountPoint(), $newStorage->getMountPoint());
  169. $this->assertEquals($updatedStorage->getBackendOptions()['password'], $newStorage->getBackendOptions()['password']);
  170. $this->assertEquals($updatedStorage->getApplicableUsers(), $newStorage->getApplicableUsers());
  171. $this->assertEquals($updatedStorage->getApplicableGroups(), $newStorage->getApplicableGroups());
  172. $this->assertEquals($updatedStorage->getPriority(), $newStorage->getPriority());
  173. $this->assertEquals(0, $newStorage->getStatus());
  174. }
  175. function hooksAddStorageDataProvider() {
  176. return [
  177. // applicable all
  178. [
  179. [],
  180. [],
  181. // expected hook calls
  182. [
  183. [
  184. Filesystem::signal_create_mount,
  185. \OC_Mount_Config::MOUNT_TYPE_USER,
  186. 'all'
  187. ],
  188. ],
  189. ],
  190. // single user
  191. [
  192. ['user1'],
  193. [],
  194. // expected hook calls
  195. [
  196. [
  197. Filesystem::signal_create_mount,
  198. \OC_Mount_Config::MOUNT_TYPE_USER,
  199. 'user1',
  200. ],
  201. ],
  202. ],
  203. // single group
  204. [
  205. [],
  206. ['group1'],
  207. // expected hook calls
  208. [
  209. [
  210. Filesystem::signal_create_mount,
  211. \OC_Mount_Config::MOUNT_TYPE_GROUP,
  212. 'group1',
  213. ],
  214. ],
  215. ],
  216. // multiple users
  217. [
  218. ['user1', 'user2'],
  219. [],
  220. [
  221. [
  222. Filesystem::signal_create_mount,
  223. \OC_Mount_Config::MOUNT_TYPE_USER,
  224. 'user1',
  225. ],
  226. [
  227. Filesystem::signal_create_mount,
  228. \OC_Mount_Config::MOUNT_TYPE_USER,
  229. 'user2',
  230. ],
  231. ],
  232. ],
  233. // multiple groups
  234. [
  235. [],
  236. ['group1', 'group2'],
  237. // expected hook calls
  238. [
  239. [
  240. Filesystem::signal_create_mount,
  241. \OC_Mount_Config::MOUNT_TYPE_GROUP,
  242. 'group1'
  243. ],
  244. [
  245. Filesystem::signal_create_mount,
  246. \OC_Mount_Config::MOUNT_TYPE_GROUP,
  247. 'group2'
  248. ],
  249. ],
  250. ],
  251. // mixed groups and users
  252. [
  253. ['user1', 'user2'],
  254. ['group1', 'group2'],
  255. // expected hook calls
  256. [
  257. [
  258. Filesystem::signal_create_mount,
  259. \OC_Mount_Config::MOUNT_TYPE_USER,
  260. 'user1',
  261. ],
  262. [
  263. Filesystem::signal_create_mount,
  264. \OC_Mount_Config::MOUNT_TYPE_USER,
  265. 'user2',
  266. ],
  267. [
  268. Filesystem::signal_create_mount,
  269. \OC_Mount_Config::MOUNT_TYPE_GROUP,
  270. 'group1'
  271. ],
  272. [
  273. Filesystem::signal_create_mount,
  274. \OC_Mount_Config::MOUNT_TYPE_GROUP,
  275. 'group2'
  276. ],
  277. ],
  278. ],
  279. ];
  280. }
  281. /**
  282. * @dataProvider hooksAddStorageDataProvider
  283. */
  284. public function testHooksAddStorage($applicableUsers, $applicableGroups, $expectedCalls) {
  285. $storage = $this->makeTestStorageData();
  286. $storage->setApplicableUsers($applicableUsers);
  287. $storage->setApplicableGroups($applicableGroups);
  288. $this->service->addStorage($storage);
  289. $this->assertCount(count($expectedCalls), self::$hookCalls);
  290. foreach ($expectedCalls as $index => $call) {
  291. $this->assertHookCall(
  292. self::$hookCalls[$index],
  293. $call[0],
  294. $storage->getMountPoint(),
  295. $call[1],
  296. $call[2]
  297. );
  298. }
  299. }
  300. function hooksUpdateStorageDataProvider() {
  301. return [
  302. [
  303. // nothing to multiple users and groups
  304. [],
  305. [],
  306. ['user1', 'user2'],
  307. ['group1', 'group2'],
  308. // expected hook calls
  309. [
  310. // delete the "all entry"
  311. [
  312. Filesystem::signal_delete_mount,
  313. \OC_Mount_Config::MOUNT_TYPE_USER,
  314. 'all',
  315. ],
  316. [
  317. Filesystem::signal_create_mount,
  318. \OC_Mount_Config::MOUNT_TYPE_USER,
  319. 'user1',
  320. ],
  321. [
  322. Filesystem::signal_create_mount,
  323. \OC_Mount_Config::MOUNT_TYPE_USER,
  324. 'user2',
  325. ],
  326. [
  327. Filesystem::signal_create_mount,
  328. \OC_Mount_Config::MOUNT_TYPE_GROUP,
  329. 'group1'
  330. ],
  331. [
  332. Filesystem::signal_create_mount,
  333. \OC_Mount_Config::MOUNT_TYPE_GROUP,
  334. 'group2'
  335. ],
  336. ],
  337. ],
  338. [
  339. // adding a user and a group
  340. ['user1'],
  341. ['group1'],
  342. ['user1', 'user2'],
  343. ['group1', 'group2'],
  344. // expected hook calls
  345. [
  346. [
  347. Filesystem::signal_create_mount,
  348. \OC_Mount_Config::MOUNT_TYPE_USER,
  349. 'user2',
  350. ],
  351. [
  352. Filesystem::signal_create_mount,
  353. \OC_Mount_Config::MOUNT_TYPE_GROUP,
  354. 'group2'
  355. ],
  356. ],
  357. ],
  358. [
  359. // removing a user and a group
  360. ['user1', 'user2'],
  361. ['group1', 'group2'],
  362. ['user1'],
  363. ['group1'],
  364. // expected hook calls
  365. [
  366. [
  367. Filesystem::signal_delete_mount,
  368. \OC_Mount_Config::MOUNT_TYPE_USER,
  369. 'user2',
  370. ],
  371. [
  372. Filesystem::signal_delete_mount,
  373. \OC_Mount_Config::MOUNT_TYPE_GROUP,
  374. 'group2'
  375. ],
  376. ],
  377. ],
  378. [
  379. // removing all
  380. ['user1'],
  381. ['group1'],
  382. [],
  383. [],
  384. // expected hook calls
  385. [
  386. [
  387. Filesystem::signal_delete_mount,
  388. \OC_Mount_Config::MOUNT_TYPE_USER,
  389. 'user1',
  390. ],
  391. [
  392. Filesystem::signal_delete_mount,
  393. \OC_Mount_Config::MOUNT_TYPE_GROUP,
  394. 'group1'
  395. ],
  396. // create the "all" entry
  397. [
  398. Filesystem::signal_create_mount,
  399. \OC_Mount_Config::MOUNT_TYPE_USER,
  400. 'all'
  401. ],
  402. ],
  403. ],
  404. [
  405. // no changes
  406. ['user1'],
  407. ['group1'],
  408. ['user1'],
  409. ['group1'],
  410. // no hook calls
  411. []
  412. ]
  413. ];
  414. }
  415. /**
  416. * @dataProvider hooksUpdateStorageDataProvider
  417. */
  418. public function testHooksUpdateStorage(
  419. $sourceApplicableUsers,
  420. $sourceApplicableGroups,
  421. $updatedApplicableUsers,
  422. $updatedApplicableGroups,
  423. $expectedCalls) {
  424. $storage = $this->makeTestStorageData();
  425. $storage->setApplicableUsers($sourceApplicableUsers);
  426. $storage->setApplicableGroups($sourceApplicableGroups);
  427. $storage = $this->service->addStorage($storage);
  428. $storage->setApplicableUsers($updatedApplicableUsers);
  429. $storage->setApplicableGroups($updatedApplicableGroups);
  430. // reset calls
  431. self::$hookCalls = [];
  432. $this->service->updateStorage($storage);
  433. $this->assertCount(count($expectedCalls), self::$hookCalls);
  434. foreach ($expectedCalls as $index => $call) {
  435. $this->assertHookCall(
  436. self::$hookCalls[$index],
  437. $call[0],
  438. '/mountpoint',
  439. $call[1],
  440. $call[2]
  441. );
  442. }
  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. 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. }