UserGlobalStoragesServiceTest.php 11 KB

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