userstoragesservicetest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /**
  3. * @author Robin Appelman <icewind@owncloud.com>
  4. * @author Robin McCorkell <robin@mccorkell.me.uk>
  5. * @author Vincent Petry <pvince81@owncloud.com>
  6. *
  7. * @copyright Copyright (c) 2016, ownCloud, Inc.
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\Files_external\Tests\Service;
  24. use \OC\Files\Filesystem;
  25. use OCA\Files_external\Service\GlobalStoragesService;
  26. use OCA\Files_external\Service\StoragesService;
  27. use \OCA\Files_external\Service\UserStoragesService;
  28. use \OCA\Files_external\NotFoundException;
  29. use \OCA\Files_external\Lib\StorageConfig;
  30. use Test\Traits\UserTrait;
  31. /**
  32. * @group DB
  33. */
  34. class UserStoragesServiceTest extends StoragesServiceTest {
  35. use UserTrait;
  36. private $user;
  37. private $userId;
  38. /**
  39. * @var StoragesService
  40. */
  41. protected $globalStoragesService;
  42. public function setUp() {
  43. parent::setUp();
  44. $this->globalStoragesService = new GlobalStoragesService($this->backendService, $this->dbConfig, $this->mountCache);
  45. $this->userId = $this->getUniqueID('user_');
  46. $this->createUser($this->userId, $this->userId);
  47. $this->user = \OC::$server->getUserManager()->get($this->userId);
  48. /** @var \OCP\IUserSession|\PHPUnit_Framework_MockObject_MockObject $userSession */
  49. $userSession = $this->getMock('\OCP\IUserSession');
  50. $userSession
  51. ->expects($this->any())
  52. ->method('getUser')
  53. ->will($this->returnValue($this->user));
  54. $this->service = new UserStoragesService($this->backendService, $this->dbConfig, $userSession, $this->mountCache);
  55. }
  56. private function makeTestStorageData() {
  57. return $this->makeStorageConfig([
  58. 'mountPoint' => 'mountpoint',
  59. 'backendIdentifier' => 'identifier:\OCA\Files_External\Lib\Backend\SMB',
  60. 'authMechanismIdentifier' => 'identifier:\Auth\Mechanism',
  61. 'backendOptions' => [
  62. 'option1' => 'value1',
  63. 'option2' => 'value2',
  64. 'password' => 'testPassword',
  65. ],
  66. 'mountOptions' => [
  67. 'preview' => false,
  68. ]
  69. ]);
  70. }
  71. public function testAddStorage() {
  72. $storage = $this->makeTestStorageData();
  73. $newStorage = $this->service->addStorage($storage);
  74. $id = $newStorage->getId();
  75. $newStorage = $this->service->getStorage($id);
  76. $this->assertEquals($storage->getMountPoint(), $newStorage->getMountPoint());
  77. $this->assertEquals($storage->getBackend(), $newStorage->getBackend());
  78. $this->assertEquals($storage->getAuthMechanism(), $newStorage->getAuthMechanism());
  79. $this->assertEquals($storage->getBackendOptions(), $newStorage->getBackendOptions());
  80. $this->assertEquals(0, $newStorage->getStatus());
  81. // hook called once for user
  82. $this->assertHookCall(
  83. current(self::$hookCalls),
  84. Filesystem::signal_create_mount,
  85. $storage->getMountPoint(),
  86. \OC_Mount_Config::MOUNT_TYPE_USER,
  87. $this->userId
  88. );
  89. $nextStorage = $this->service->addStorage($storage);
  90. $this->assertEquals($id + 1, $nextStorage->getId());
  91. }
  92. public function testUpdateStorage() {
  93. $storage = $this->makeStorageConfig([
  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. ]);
  103. $newStorage = $this->service->addStorage($storage);
  104. $backendOptions = $newStorage->getBackendOptions();
  105. $backendOptions['password'] = 'anotherPassword';
  106. $newStorage->setBackendOptions($backendOptions);
  107. self::$hookCalls = [];
  108. $newStorage = $this->service->updateStorage($newStorage);
  109. $this->assertEquals('anotherPassword', $newStorage->getBackendOptions()['password']);
  110. $this->assertEquals([$this->userId], $newStorage->getApplicableUsers());
  111. // these attributes are unused for user storages
  112. $this->assertEmpty($newStorage->getApplicableGroups());
  113. $this->assertEquals(0, $newStorage->getStatus());
  114. // no hook calls
  115. $this->assertEmpty(self::$hookCalls);
  116. }
  117. /**
  118. * @dataProvider deleteStorageDataProvider
  119. */
  120. public function testDeleteStorage($backendOptions, $rustyStorageId, $expectedCountAfterDeletion) {
  121. parent::testDeleteStorage($backendOptions, $rustyStorageId, $expectedCountAfterDeletion);
  122. // hook called once for user (first one was during test creation)
  123. $this->assertHookCall(
  124. self::$hookCalls[1],
  125. Filesystem::signal_delete_mount,
  126. '/mountpoint',
  127. \OC_Mount_Config::MOUNT_TYPE_USER,
  128. $this->userId
  129. );
  130. }
  131. public function testHooksRenameMountPoint() {
  132. $storage = $this->makeTestStorageData();
  133. $storage = $this->service->addStorage($storage);
  134. $storage->setMountPoint('renamedMountpoint');
  135. // reset calls
  136. self::$hookCalls = [];
  137. $this->service->updateStorage($storage);
  138. // hook called twice
  139. $this->assertHookCall(
  140. self::$hookCalls[0],
  141. Filesystem::signal_delete_mount,
  142. '/mountpoint',
  143. \OC_Mount_Config::MOUNT_TYPE_USER,
  144. $this->userId
  145. );
  146. $this->assertHookCall(
  147. self::$hookCalls[1],
  148. Filesystem::signal_create_mount,
  149. '/renamedMountpoint',
  150. \OC_Mount_Config::MOUNT_TYPE_USER,
  151. $this->userId
  152. );
  153. }
  154. /**
  155. * @expectedException \OCA\Files_external\NotFoundException
  156. */
  157. public function testGetAdminStorage() {
  158. $backend = $this->backendService->getBackend('identifier:\OCA\Files_External\Lib\Backend\SMB');
  159. $authMechanism = $this->backendService->getAuthMechanism('identifier:\Auth\Mechanism');
  160. $storage = new StorageConfig();
  161. $storage->setMountPoint('mountpoint');
  162. $storage->setBackend($backend);
  163. $storage->setAuthMechanism($authMechanism);
  164. $storage->setBackendOptions(['password' => 'testPassword']);
  165. $storage->setApplicableUsers([$this->userId]);
  166. $newStorage = $this->globalStoragesService->addStorage($storage);
  167. $this->assertInstanceOf('\OCA\Files_external\Lib\StorageConfig', $this->globalStoragesService->getStorage($newStorage->getId()));
  168. $this->service->getStorage($newStorage->getId());
  169. }
  170. }