UserGlobalStoragesServiceTest.php 11 KB

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