UserGlobalStoragesServiceTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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 OCA\Files_External\Lib\StorageConfig;
  30. use OCA\Files_External\NotFoundException;
  31. use OCA\Files_External\Service\StoragesService;
  32. use OCA\Files_External\Service\UserGlobalStoragesService;
  33. use OCP\IGroupManager;
  34. use OCP\IUser;
  35. use OCP\IUserSession;
  36. use Test\Traits\UserTrait;
  37. /**
  38. * @group DB
  39. */
  40. class UserGlobalStoragesServiceTest extends GlobalStoragesServiceTest {
  41. use UserTrait;
  42. /** @var \OCP\IGroupManager|\PHPUnit\Framework\MockObject\MockObject groupManager */
  43. protected $groupManager;
  44. /**
  45. * @var StoragesService
  46. */
  47. protected $globalStoragesService;
  48. /**
  49. * @var UserGlobalStoragesService
  50. */
  51. protected $service;
  52. protected $user;
  53. public const USER_ID = 'test_user';
  54. public const GROUP_ID = 'test_group';
  55. public const GROUP_ID2 = 'test_group2';
  56. protected function setUp(): void {
  57. parent::setUp();
  58. $this->globalStoragesService = $this->service;
  59. $this->user = new \OC\User\User(self::USER_ID, null, \OC::$server->getEventDispatcher());
  60. /** @var \OCP\IUserSession|\PHPUnit\Framework\MockObject\MockObject $userSession */
  61. $userSession = $this->createMock(IUserSession::class);
  62. $userSession
  63. ->expects($this->any())
  64. ->method('getUser')
  65. ->willReturn($this->user);
  66. $this->groupManager = $this->createMock(IGroupManager::class);
  67. $this->groupManager->method('isInGroup')
  68. ->willReturnCallback(function ($userId, $groupId) {
  69. if ($userId === self::USER_ID) {
  70. switch ($groupId) {
  71. case self::GROUP_ID:
  72. case self::GROUP_ID2:
  73. return true;
  74. }
  75. }
  76. return false;
  77. });
  78. $this->groupManager->method('getUserGroupIds')
  79. ->willReturnCallback(function (IUser $user) {
  80. if ($user->getUID() === self::USER_ID) {
  81. return [self::GROUP_ID, self::GROUP_ID2];
  82. } else {
  83. return [];
  84. }
  85. });
  86. $this->service = new UserGlobalStoragesService(
  87. $this->backendService,
  88. $this->dbConfig,
  89. $userSession,
  90. $this->groupManager,
  91. $this->mountCache,
  92. $this->eventDispatcher,
  93. );
  94. }
  95. public function applicableStorageProvider() {
  96. return [
  97. [[], [], true],
  98. // not applicable cases
  99. [['user1'], [], false],
  100. [[], ['group1'], false],
  101. [['user1'], ['group1'], false],
  102. // applicable cases
  103. [[self::USER_ID], [], true],
  104. [[], [self::GROUP_ID], true],
  105. [[self::USER_ID], ['group1'], true],
  106. [['user1'], [self::GROUP_ID], true],
  107. // sanity checks
  108. [['user1', 'user2', self::USER_ID, 'user3'], [], true],
  109. ];
  110. }
  111. /**
  112. * @dataProvider applicableStorageProvider
  113. */
  114. public function testGetStorageWithApplicable($applicableUsers, $applicableGroups, $isVisible) {
  115. $backend = $this->backendService->getBackend('identifier:\OCA\Files_External\Lib\Backend\SMB');
  116. $authMechanism = $this->backendService->getAuthMechanism('identifier:\Auth\Mechanism');
  117. $storage = new StorageConfig();
  118. $storage->setMountPoint('mountpoint');
  119. $storage->setBackend($backend);
  120. $storage->setAuthMechanism($authMechanism);
  121. $storage->setBackendOptions(['password' => 'testPassword']);
  122. $storage->setApplicableUsers($applicableUsers);
  123. $storage->setApplicableGroups($applicableGroups);
  124. $newStorage = $this->globalStoragesService->addStorage($storage);
  125. $storages = $this->service->getAllStorages();
  126. if ($isVisible) {
  127. $this->assertEquals(1, count($storages));
  128. $retrievedStorage = $this->service->getStorage($newStorage->getId());
  129. $this->assertEquals('/mountpoint', $retrievedStorage->getMountPoint());
  130. } else {
  131. $this->assertEquals(0, count($storages));
  132. try {
  133. $this->service->getStorage($newStorage->getId());
  134. $this->fail('Failed asserting that storage can\'t be accessed by id');
  135. } catch (NotFoundException $e) {
  136. }
  137. }
  138. }
  139. public function testAddStorage($storageParams = null) {
  140. $this->expectException(\DomainException::class);
  141. $backend = $this->backendService->getBackend('identifier:\OCA\Files_External\Lib\Backend\SMB');
  142. $authMechanism = $this->backendService->getAuthMechanism('identifier:\Auth\Mechanism');
  143. $storage = new StorageConfig(255);
  144. $storage->setMountPoint('mountpoint');
  145. $storage->setBackend($backend);
  146. $storage->setAuthMechanism($authMechanism);
  147. $storage->setBackendOptions(['password' => 'testPassword']);
  148. $this->service->addStorage($storage);
  149. }
  150. public function testUpdateStorage($storageParams = null) {
  151. $this->expectException(\DomainException::class);
  152. $backend = $this->backendService->getBackend('identifier:\OCA\Files_External\Lib\Backend\SMB');
  153. $authMechanism = $this->backendService->getAuthMechanism('identifier:\Auth\Mechanism');
  154. $storage = new StorageConfig(255);
  155. $storage->setMountPoint('mountpoint');
  156. $storage->setBackend($backend);
  157. $storage->setAuthMechanism($authMechanism);
  158. $storage->setBackendOptions(['password' => 'testPassword']);
  159. $newStorage = $this->globalStoragesService->addStorage($storage);
  160. $retrievedStorage = $this->service->getStorage($newStorage->getId());
  161. $retrievedStorage->setMountPoint('abc');
  162. $this->service->updateStorage($retrievedStorage);
  163. }
  164. public function testNonExistingStorage() {
  165. $this->expectException(\DomainException::class);
  166. $this->ActualNonExistingStorageTest();
  167. }
  168. /**
  169. * @dataProvider deleteStorageDataProvider
  170. */
  171. public function testDeleteStorage($backendOptions, $rustyStorageId) {
  172. $this->expectException(\DomainException::class);
  173. $backend = $this->backendService->getBackend('identifier:\OCA\Files_External\Lib\Backend\SMB');
  174. $authMechanism = $this->backendService->getAuthMechanism('identifier:\Auth\Mechanism');
  175. $storage = new StorageConfig(255);
  176. $storage->setMountPoint('mountpoint');
  177. $storage->setBackend($backend);
  178. $storage->setAuthMechanism($authMechanism);
  179. $storage->setBackendOptions($backendOptions);
  180. $newStorage = $this->globalStoragesService->addStorage($storage);
  181. $id = $newStorage->getId();
  182. $this->service->removeStorage($id);
  183. }
  184. public function testDeleteUnexistingStorage() {
  185. $this->expectException(\DomainException::class);
  186. $this->actualDeletedUnexistingStorageTest();
  187. }
  188. public function getUniqueStoragesProvider() {
  189. return [
  190. // 'all' vs group
  191. [100, [], [], 100, [], [self::GROUP_ID], 2],
  192. [100, [], [self::GROUP_ID], 100, [], [], 1],
  193. // 'all' vs user
  194. [100, [], [], 100, [self::USER_ID], [], 2],
  195. [100, [self::USER_ID], [], 100, [], [], 1],
  196. // group vs user
  197. [100, [], [self::GROUP_ID], 100, [self::USER_ID], [], 2],
  198. [100, [self::USER_ID], [], 100, [], [self::GROUP_ID], 1],
  199. // group+user vs group
  200. [100, [], [self::GROUP_ID2], 100, [self::USER_ID], [self::GROUP_ID], 2],
  201. [100, [self::USER_ID], [self::GROUP_ID], 100, [], [self::GROUP_ID2], 1],
  202. // user vs 'all' (higher priority)
  203. [200, [], [], 100, [self::USER_ID], [], 2],
  204. [100, [self::USER_ID], [], 200, [], [], 1],
  205. // group vs group (higher priority)
  206. [100, [], [self::GROUP_ID2], 200, [], [self::GROUP_ID], 2],
  207. [200, [], [self::GROUP_ID], 100, [], [self::GROUP_ID2], 1],
  208. ];
  209. }
  210. /**
  211. * @dataProvider getUniqueStoragesProvider
  212. */
  213. public function testGetUniqueStorages(
  214. $priority1, $applicableUsers1, $applicableGroups1,
  215. $priority2, $applicableUsers2, $applicableGroups2,
  216. $expectedPrecedence
  217. ) {
  218. $backend = $this->backendService->getBackend('identifier:\OCA\Files_External\Lib\Backend\SMB');
  219. $backend->method('isVisibleFor')
  220. ->willReturn(true);
  221. $authMechanism = $this->backendService->getAuthMechanism('identifier:\Auth\Mechanism');
  222. $authMechanism->method('isVisibleFor')
  223. ->willReturn(true);
  224. $storage1 = new StorageConfig();
  225. $storage1->setMountPoint('mountpoint');
  226. $storage1->setBackend($backend);
  227. $storage1->setAuthMechanism($authMechanism);
  228. $storage1->setBackendOptions(['password' => 'testPassword']);
  229. $storage1->setPriority($priority1);
  230. $storage1->setApplicableUsers($applicableUsers1);
  231. $storage1->setApplicableGroups($applicableGroups1);
  232. $storage1 = $this->globalStoragesService->addStorage($storage1);
  233. $storage2 = new StorageConfig();
  234. $storage2->setMountPoint('mountpoint');
  235. $storage2->setBackend($backend);
  236. $storage2->setAuthMechanism($authMechanism);
  237. $storage2->setBackendOptions(['password' => 'testPassword']);
  238. $storage2->setPriority($priority2);
  239. $storage2->setApplicableUsers($applicableUsers2);
  240. $storage2->setApplicableGroups($applicableGroups2);
  241. $storage2 = $this->globalStoragesService->addStorage($storage2);
  242. $storages = $this->service->getUniqueStorages();
  243. $this->assertCount(1, $storages);
  244. if ($expectedPrecedence === 1) {
  245. $this->assertArrayHasKey($storage1->getID(), $storages);
  246. } elseif ($expectedPrecedence === 2) {
  247. $this->assertArrayHasKey($storage2->getID(), $storages);
  248. }
  249. }
  250. public function testGetStoragesBackendNotVisible() {
  251. // we don't test this here
  252. $this->addToAssertionCount(1);
  253. }
  254. public function testGetStoragesAuthMechanismNotVisible() {
  255. // we don't test this here
  256. $this->addToAssertionCount(1);
  257. }
  258. public function testHooksAddStorage($a = null, $b = null, $c = null) {
  259. // we don't test this here
  260. $this->addToAssertionCount(1);
  261. }
  262. public function testHooksUpdateStorage($a = null, $b = null, $c = null, $d = null, $e = null) {
  263. // we don't test this here
  264. $this->addToAssertionCount(1);
  265. }
  266. public function testHooksRenameMountPoint() {
  267. // we don't test this here
  268. $this->addToAssertionCount(1);
  269. }
  270. public function testHooksDeleteStorage($a = null, $b = null, $c = null) {
  271. // we don't test this here
  272. $this->addToAssertionCount(1);
  273. }
  274. public function testLegacyConfigConversionApplicableAll() {
  275. // we don't test this here
  276. $this->addToAssertionCount(1);
  277. }
  278. public function testLegacyConfigConversionApplicableUserAndGroup() {
  279. // we don't test this here
  280. $this->addToAssertionCount(1);
  281. }
  282. public function testReadLegacyConfigAndGenerateConfigId() {
  283. // we don't test this here
  284. $this->addToAssertionCount(1);
  285. }
  286. public function testReadLegacyConfigNoAuthMechanism() {
  287. // we don't test this here
  288. $this->addToAssertionCount(1);
  289. }
  290. public function testReadLegacyConfigClass() {
  291. // we don't test this here
  292. $this->addToAssertionCount(1);
  293. }
  294. public function testReadEmptyMountPoint() {
  295. // we don't test this here
  296. $this->addToAssertionCount(1);
  297. }
  298. public function testUpdateStorageMountPoint() {
  299. // we don't test this here
  300. $this->addToAssertionCount(1);
  301. }
  302. }