UserGlobalStoragesServiceTest.php 11 KB

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