userstoragesservicetest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. /**
  3. * @author Vincent Petry <pvince81@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2015, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace OCA\Files_external\Tests\Service;
  22. use \OC\Files\Filesystem;
  23. use \OCA\Files_external\Service\UserStoragesService;
  24. use \OCA\Files_external\NotFoundException;
  25. use \OCA\Files_external\Lib\StorageConfig;
  26. class UserStoragesServiceTest extends StoragesServiceTest {
  27. public function setUp() {
  28. parent::setUp();
  29. $this->userId = $this->getUniqueID('user_');
  30. $this->user = new \OC\User\User($this->userId, null);
  31. $userSession = $this->getMock('\OCP\IUserSession');
  32. $userSession
  33. ->expects($this->any())
  34. ->method('getUser')
  35. ->will($this->returnValue($this->user));
  36. $this->service = new UserStoragesService($userSession);
  37. // create home folder
  38. mkdir($this->dataDir . '/' . $this->userId . '/');
  39. }
  40. public function tearDown() {
  41. @unlink($this->dataDir . '/' . $this->userId . '/mount.json');
  42. parent::tearDown();
  43. }
  44. private function makeTestStorageData() {
  45. return $this->makeStorageConfig([
  46. 'mountPoint' => 'mountpoint',
  47. 'backendClass' => '\OC\Files\Storage\SMB',
  48. 'backendOptions' => [
  49. 'option1' => 'value1',
  50. 'option2' => 'value2',
  51. 'password' => 'testPassword',
  52. ],
  53. 'mountOptions' => [
  54. 'preview' => false,
  55. ]
  56. ]);
  57. }
  58. public function testAddStorage() {
  59. $storage = $this->makeTestStorageData();
  60. $newStorage = $this->service->addStorage($storage);
  61. $this->assertEquals(1, $newStorage->getId());
  62. $newStorage = $this->service->getStorage(1);
  63. $this->assertEquals($storage->getMountPoint(), $newStorage->getMountPoint());
  64. $this->assertEquals($storage->getBackendClass(), $newStorage->getBackendClass());
  65. $this->assertEquals($storage->getBackendOptions(), $newStorage->getBackendOptions());
  66. $this->assertEquals(1, $newStorage->getId());
  67. $this->assertEquals(0, $newStorage->getStatus());
  68. // hook called once for user
  69. $this->assertHookCall(
  70. current(self::$hookCalls),
  71. Filesystem::signal_create_mount,
  72. $storage->getMountPoint(),
  73. \OC_Mount_Config::MOUNT_TYPE_USER,
  74. $this->userId
  75. );
  76. // next one gets id 2
  77. $nextStorage = $this->service->addStorage($storage);
  78. $this->assertEquals(2, $nextStorage->getId());
  79. }
  80. public function testUpdateStorage() {
  81. $storage = $this->makeStorageConfig([
  82. 'mountPoint' => 'mountpoint',
  83. 'backendClass' => '\OC\Files\Storage\SMB',
  84. 'backendOptions' => [
  85. 'option1' => 'value1',
  86. 'option2' => 'value2',
  87. 'password' => 'testPassword',
  88. ],
  89. ]);
  90. $newStorage = $this->service->addStorage($storage);
  91. $this->assertEquals(1, $newStorage->getId());
  92. $backendOptions = $newStorage->getBackendOptions();
  93. $backendOptions['password'] = 'anotherPassword';
  94. $newStorage->setBackendOptions($backendOptions);
  95. self::$hookCalls = [];
  96. $newStorage = $this->service->updateStorage($newStorage);
  97. $this->assertEquals('anotherPassword', $newStorage->getBackendOptions()['password']);
  98. // these attributes are unused for user storages
  99. $this->assertEmpty($newStorage->getApplicableUsers());
  100. $this->assertEmpty($newStorage->getApplicableGroups());
  101. $this->assertEquals(1, $newStorage->getId());
  102. $this->assertEquals(0, $newStorage->getStatus());
  103. // no hook calls
  104. $this->assertEmpty(self::$hookCalls);
  105. }
  106. public function testDeleteStorage() {
  107. parent::testDeleteStorage();
  108. // hook called once for user (first one was during test creation)
  109. $this->assertHookCall(
  110. self::$hookCalls[1],
  111. Filesystem::signal_delete_mount,
  112. '/mountpoint',
  113. \OC_Mount_Config::MOUNT_TYPE_USER,
  114. $this->userId
  115. );
  116. }
  117. public function testHooksRenameMountPoint() {
  118. $storage = $this->makeTestStorageData();
  119. $storage = $this->service->addStorage($storage);
  120. $storage->setMountPoint('renamedMountpoint');
  121. // reset calls
  122. self::$hookCalls = [];
  123. $this->service->updateStorage($storage);
  124. // hook called twice
  125. $this->assertHookCall(
  126. self::$hookCalls[0],
  127. Filesystem::signal_delete_mount,
  128. '/mountpoint',
  129. \OC_Mount_Config::MOUNT_TYPE_USER,
  130. $this->userId
  131. );
  132. $this->assertHookCall(
  133. self::$hookCalls[1],
  134. Filesystem::signal_create_mount,
  135. '/renamedMountpoint',
  136. \OC_Mount_Config::MOUNT_TYPE_USER,
  137. $this->userId
  138. );
  139. }
  140. /**
  141. * Make sure it uses the correct format when reading/writing
  142. * the legacy config
  143. */
  144. public function testLegacyConfigConversion() {
  145. $configFile = $this->dataDir . '/' . $this->userId . '/mount.json';
  146. $storage = $this->makeTestStorageData();
  147. $storage = $this->service->addStorage($storage);
  148. $json = json_decode(file_get_contents($configFile), true);
  149. $this->assertCount(1, $json);
  150. $this->assertEquals([\OC_Mount_Config::MOUNT_TYPE_USER], array_keys($json));
  151. $this->assertEquals([$this->userId], array_keys($json[\OC_Mount_config::MOUNT_TYPE_USER]));
  152. $mountPointData = $json[\OC_Mount_config::MOUNT_TYPE_USER][$this->userId];
  153. $this->assertEquals(['/' . $this->userId . '/files/mountpoint'], array_keys($mountPointData));
  154. $mountPointOptions = current($mountPointData);
  155. $this->assertEquals(1, $mountPointOptions['id']);
  156. $this->assertEquals('\OC\Files\Storage\SMB', $mountPointOptions['class']);
  157. $this->assertEquals(false, $mountPointOptions['mountOptions']['preview']);
  158. $backendOptions = $mountPointOptions['options'];
  159. $this->assertEquals('value1', $backendOptions['option1']);
  160. $this->assertEquals('value2', $backendOptions['option2']);
  161. $this->assertEquals('', $backendOptions['password']);
  162. $this->assertNotEmpty($backendOptions['password_encrypted']);
  163. }
  164. /**
  165. * Test reading in a legacy config and generating config ids.
  166. */
  167. public function testReadLegacyConfigAndGenerateConfigId() {
  168. $configFile = $this->dataDir . '/' . $this->userId . '/mount.json';
  169. $legacyBackendOptions = [
  170. 'user' => 'someuser',
  171. 'password' => 'somepassword',
  172. ];
  173. $legacyBackendOptions = \OC_Mount_Config::encryptPasswords($legacyBackendOptions);
  174. $legacyConfig = [
  175. 'class' => '\OC\Files\Storage\SMB',
  176. 'options' => $legacyBackendOptions,
  177. 'mountOptions' => ['preview' => false],
  178. ];
  179. // different mount options
  180. $legacyConfig2 = [
  181. 'class' => '\OC\Files\Storage\SMB',
  182. 'options' => $legacyBackendOptions,
  183. 'mountOptions' => ['preview' => true],
  184. ];
  185. $json = ['user' => []];
  186. $json['user'][$this->userId] = [
  187. '/$user/files/somemount' => $legacyConfig,
  188. '/$user/files/anothermount' => $legacyConfig2,
  189. ];
  190. file_put_contents($configFile, json_encode($json));
  191. $allStorages = $this->service->getAllStorages();
  192. $this->assertCount(2, $allStorages);
  193. $storage1 = $allStorages[1];
  194. $storage2 = $allStorages[2];
  195. $this->assertEquals('/somemount', $storage1->getMountPoint());
  196. $this->assertEquals('someuser', $storage1->getBackendOptions()['user']);
  197. $this->assertEquals('somepassword', $storage1->getBackendOptions()['password']);
  198. $this->assertEquals(['preview' => false], $storage1->getMountOptions());
  199. $this->assertEquals('/anothermount', $storage2->getMountPoint());
  200. $this->assertEquals('someuser', $storage2->getBackendOptions()['user']);
  201. $this->assertEquals('somepassword', $storage2->getBackendOptions()['password']);
  202. $this->assertEquals(['preview' => true], $storage2->getMountOptions());
  203. }
  204. }