1
0

UserGlobalStoragesServiceTest.php 11 KB

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