FederatedShareProviderTest.php 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Bjoern Schiessle <bjoern@schiessle.org>
  7. * @author Björn Schießle <bjoern@schiessle.org>
  8. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  9. * @author Georg Ehrke <oc.list@georgehrke.com>
  10. * @author Joas Schilling <coding@schilljs.com>
  11. * @author Lukas Reschke <lukas@statuscode.ch>
  12. * @author Morris Jobke <hey@morrisjobke.de>
  13. * @author Robin Appelman <robin@icewind.nl>
  14. * @author Roeland Jago Douma <roeland@famdouma.nl>
  15. * @author Valdnet <47037905+Valdnet@users.noreply.github.com>
  16. * @author Vincent Petry <vincent@nextcloud.com>
  17. *
  18. * @license AGPL-3.0
  19. *
  20. * This code is free software: you can redistribute it and/or modify
  21. * it under the terms of the GNU Affero General Public License, version 3,
  22. * as published by the Free Software Foundation.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU Affero General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Affero General Public License, version 3,
  30. * along with this program. If not, see <http://www.gnu.org/licenses/>
  31. *
  32. */
  33. namespace OCA\FederatedFileSharing\Tests;
  34. use OC\Federation\CloudIdManager;
  35. use OCA\FederatedFileSharing\AddressHandler;
  36. use OCA\FederatedFileSharing\FederatedShareProvider;
  37. use OCA\FederatedFileSharing\Notifications;
  38. use OCA\FederatedFileSharing\TokenHandler;
  39. use OCP\Contacts\IManager as IContactsManager;
  40. use OCP\EventDispatcher\IEventDispatcher;
  41. use OCP\Federation\ICloudFederationProviderManager;
  42. use OCP\Federation\ICloudIdManager;
  43. use OCP\Files\File;
  44. use OCP\Files\IRootFolder;
  45. use OCP\ICacheFactory;
  46. use OCP\IConfig;
  47. use OCP\IDBConnection;
  48. use OCP\IL10N;
  49. use OCP\ILogger;
  50. use OCP\IURLGenerator;
  51. use OCP\IUserManager;
  52. use OCP\Share\IManager;
  53. use OCP\Share\IShare;
  54. use PHPUnit\Framework\MockObject\MockObject;
  55. /**
  56. * Class FederatedShareProviderTest
  57. *
  58. * @package OCA\FederatedFileSharing\Tests
  59. * @group DB
  60. */
  61. class FederatedShareProviderTest extends \Test\TestCase {
  62. /** @var IDBConnection */
  63. protected $connection;
  64. /** @var AddressHandler | \PHPUnit\Framework\MockObject\MockObject */
  65. protected $addressHandler;
  66. /** @var Notifications | \PHPUnit\Framework\MockObject\MockObject */
  67. protected $notifications;
  68. /** @var TokenHandler|\PHPUnit\Framework\MockObject\MockObject */
  69. protected $tokenHandler;
  70. /** @var IL10N */
  71. protected $l;
  72. /** @var ILogger */
  73. protected $logger;
  74. /** @var IRootFolder | \PHPUnit\Framework\MockObject\MockObject */
  75. protected $rootFolder;
  76. /** @var IConfig | \PHPUnit\Framework\MockObject\MockObject */
  77. protected $config;
  78. /** @var IUserManager | \PHPUnit\Framework\MockObject\MockObject */
  79. protected $userManager;
  80. /** @var \OCP\GlobalScale\IConfig|\PHPUnit\Framework\MockObject\MockObject */
  81. protected $gsConfig;
  82. /** @var IManager */
  83. protected $shareManager;
  84. /** @var FederatedShareProvider */
  85. protected $provider;
  86. /** @var IContactsManager|\PHPUnit\Framework\MockObject\MockObject */
  87. protected $contactsManager;
  88. /** @var ICloudIdManager */
  89. private $cloudIdManager;
  90. /** @var \PHPUnit\Framework\MockObject\MockObject|ICloudFederationProviderManager */
  91. private $cloudFederationProviderManager;
  92. protected function setUp(): void {
  93. parent::setUp();
  94. $this->connection = \OC::$server->getDatabaseConnection();
  95. $this->notifications = $this->getMockBuilder('OCA\FederatedFileSharing\Notifications')
  96. ->disableOriginalConstructor()
  97. ->getMock();
  98. $this->tokenHandler = $this->getMockBuilder('OCA\FederatedFileSharing\TokenHandler')
  99. ->disableOriginalConstructor()
  100. ->getMock();
  101. $this->l = $this->getMockBuilder(IL10N::class)->getMock();
  102. $this->l->method('t')
  103. ->willReturnCallback(function ($text, $parameters = []) {
  104. return vsprintf($text, $parameters);
  105. });
  106. $this->logger = $this->getMockBuilder(ILogger::class)->getMock();
  107. $this->rootFolder = $this->getMockBuilder('OCP\Files\IRootFolder')->getMock();
  108. $this->config = $this->getMockBuilder(IConfig::class)->getMock();
  109. $this->userManager = $this->getMockBuilder(IUserManager::class)->getMock();
  110. //$this->addressHandler = new AddressHandler(\OC::$server->getURLGenerator(), $this->l);
  111. $this->addressHandler = $this->getMockBuilder('OCA\FederatedFileSharing\AddressHandler')->disableOriginalConstructor()->getMock();
  112. $this->contactsManager = $this->createMock(IContactsManager::class);
  113. $this->cloudIdManager = new CloudIdManager(
  114. $this->contactsManager,
  115. $this->createMock(IURLGenerator::class),
  116. $this->userManager,
  117. $this->createMock(ICacheFactory::class),
  118. $this->createMock(IEventDispatcher::class)
  119. );
  120. $this->gsConfig = $this->createMock(\OCP\GlobalScale\IConfig::class);
  121. $this->userManager->expects($this->any())->method('userExists')->willReturn(true);
  122. $this->cloudFederationProviderManager = $this->createMock(ICloudFederationProviderManager::class);
  123. $this->provider = new FederatedShareProvider(
  124. $this->connection,
  125. $this->addressHandler,
  126. $this->notifications,
  127. $this->tokenHandler,
  128. $this->l,
  129. $this->logger,
  130. $this->rootFolder,
  131. $this->config,
  132. $this->userManager,
  133. $this->cloudIdManager,
  134. $this->gsConfig,
  135. $this->cloudFederationProviderManager
  136. );
  137. $this->shareManager = \OC::$server->getShareManager();
  138. }
  139. protected function tearDown(): void {
  140. $this->connection->getQueryBuilder()->delete('share')->execute();
  141. parent::tearDown();
  142. }
  143. public function dataTestCreate() {
  144. return [
  145. [null, null],
  146. [new \DateTime('2020-03-01T01:02:03'), '2020-03-01 01:02:03'],
  147. ];
  148. }
  149. /**
  150. * @dataProvider dataTestCreate
  151. */
  152. public function testCreate($expirationDate, $expectedDataDate) {
  153. $share = $this->shareManager->newShare();
  154. /** @var File|MockObject $node */
  155. $node = $this->getMockBuilder(File::class)->getMock();
  156. $node->method('getId')->willReturn(42);
  157. $node->method('getName')->willReturn('myFile');
  158. $share->setSharedWith('user@server.com')
  159. ->setSharedBy('sharedBy')
  160. ->setShareOwner('shareOwner')
  161. ->setPermissions(19)
  162. ->setShareType(IShare::TYPE_REMOTE)
  163. ->setExpirationDate($expirationDate)
  164. ->setNode($node);
  165. $this->tokenHandler->method('generateToken')->willReturn('token');
  166. $this->addressHandler->expects($this->any())->method('generateRemoteURL')
  167. ->willReturn('http://localhost/');
  168. $this->addressHandler->expects($this->any())->method('splitUserRemote')
  169. ->willReturn(['user', 'server.com']);
  170. $this->notifications->expects($this->once())
  171. ->method('sendRemoteShare')
  172. ->with(
  173. $this->equalTo('token'),
  174. $this->equalTo('user@server.com'),
  175. $this->equalTo('myFile'),
  176. $this->anything(),
  177. 'shareOwner',
  178. 'shareOwner@http://localhost/',
  179. 'sharedBy',
  180. 'sharedBy@http://localhost/'
  181. )
  182. ->willReturn(true);
  183. $this->rootFolder->expects($this->never())->method($this->anything());
  184. $this->contactsManager->expects($this->any())
  185. ->method('search')
  186. ->willReturn([]);
  187. $share = $this->provider->create($share);
  188. $qb = $this->connection->getQueryBuilder();
  189. $stmt = $qb->select('*')
  190. ->from('share')
  191. ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
  192. ->execute();
  193. $data = $stmt->fetch();
  194. $stmt->closeCursor();
  195. $expected = [
  196. 'share_type' => IShare::TYPE_REMOTE,
  197. 'share_with' => 'user@server.com',
  198. 'uid_owner' => 'shareOwner',
  199. 'uid_initiator' => 'sharedBy',
  200. 'item_type' => 'file',
  201. 'item_source' => 42,
  202. 'file_source' => 42,
  203. 'permissions' => 19,
  204. 'accepted' => 0,
  205. 'token' => 'token',
  206. 'expiration' => $expectedDataDate,
  207. ];
  208. foreach (array_keys($expected) as $key) {
  209. $this->assertEquals($expected[$key], $data[$key], "Assert that value for key '$key' is the same");
  210. }
  211. $this->assertEquals($data['id'], $share->getId());
  212. $this->assertEquals(IShare::TYPE_REMOTE, $share->getShareType());
  213. $this->assertEquals('user@server.com', $share->getSharedWith());
  214. $this->assertEquals('sharedBy', $share->getSharedBy());
  215. $this->assertEquals('shareOwner', $share->getShareOwner());
  216. $this->assertEquals('file', $share->getNodeType());
  217. $this->assertEquals(42, $share->getNodeId());
  218. $this->assertEquals(19, $share->getPermissions());
  219. $this->assertEquals('token', $share->getToken());
  220. $this->assertEquals($expirationDate, $share->getExpirationDate());
  221. }
  222. public function testCreateCouldNotFindServer() {
  223. $share = $this->shareManager->newShare();
  224. $node = $this->getMockBuilder(File::class)->getMock();
  225. $node->method('getId')->willReturn(42);
  226. $node->method('getName')->willReturn('myFile');
  227. $share->setSharedWith('user@server.com')
  228. ->setSharedBy('sharedBy')
  229. ->setShareOwner('shareOwner')
  230. ->setPermissions(19)
  231. ->setShareType(IShare::TYPE_REMOTE)
  232. ->setNode($node);
  233. $this->tokenHandler->method('generateToken')->willReturn('token');
  234. $this->addressHandler->expects($this->any())->method('generateRemoteURL')
  235. ->willReturn('http://localhost/');
  236. $this->addressHandler->expects($this->any())->method('splitUserRemote')
  237. ->willReturn(['user', 'server.com']);
  238. $this->notifications->expects($this->once())
  239. ->method('sendRemoteShare')
  240. ->with(
  241. $this->equalTo('token'),
  242. $this->equalTo('user@server.com'),
  243. $this->equalTo('myFile'),
  244. $this->anything(),
  245. 'shareOwner',
  246. 'shareOwner@http://localhost/',
  247. 'sharedBy',
  248. 'sharedBy@http://localhost/'
  249. )->willReturn(false);
  250. $this->rootFolder->method('getById')
  251. ->with('42')
  252. ->willReturn([$node]);
  253. $this->contactsManager->expects($this->any())
  254. ->method('search')
  255. ->willReturn([]);
  256. try {
  257. $share = $this->provider->create($share);
  258. $this->fail();
  259. } catch (\Exception $e) {
  260. $this->assertEquals('Sharing myFile failed, could not find user@server.com, maybe the server is currently unreachable or uses a self-signed certificate.', $e->getMessage());
  261. }
  262. $qb = $this->connection->getQueryBuilder();
  263. $stmt = $qb->select('*')
  264. ->from('share')
  265. ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
  266. ->execute();
  267. $data = $stmt->fetch();
  268. $stmt->closeCursor();
  269. $this->assertFalse($data);
  270. }
  271. public function testCreateException() {
  272. $share = $this->shareManager->newShare();
  273. $node = $this->getMockBuilder(File::class)->getMock();
  274. $node->method('getId')->willReturn(42);
  275. $node->method('getName')->willReturn('myFile');
  276. $share->setSharedWith('user@server.com')
  277. ->setSharedBy('sharedBy')
  278. ->setShareOwner('shareOwner')
  279. ->setPermissions(19)
  280. ->setShareType(IShare::TYPE_REMOTE)
  281. ->setNode($node);
  282. $this->tokenHandler->method('generateToken')->willReturn('token');
  283. $this->addressHandler->expects($this->any())->method('generateRemoteURL')
  284. ->willReturn('http://localhost/');
  285. $this->addressHandler->expects($this->any())->method('splitUserRemote')
  286. ->willReturn(['user', 'server.com']);
  287. $this->notifications->expects($this->once())
  288. ->method('sendRemoteShare')
  289. ->with(
  290. $this->equalTo('token'),
  291. $this->equalTo('user@server.com'),
  292. $this->equalTo('myFile'),
  293. $this->anything(),
  294. 'shareOwner',
  295. 'shareOwner@http://localhost/',
  296. 'sharedBy',
  297. 'sharedBy@http://localhost/'
  298. )->willThrowException(new \Exception('dummy'));
  299. $this->rootFolder->method('getById')
  300. ->with('42')
  301. ->willReturn([$node]);
  302. $this->contactsManager->expects($this->any())
  303. ->method('search')
  304. ->willReturn([]);
  305. try {
  306. $share = $this->provider->create($share);
  307. $this->fail();
  308. } catch (\Exception $e) {
  309. $this->assertEquals('Sharing myFile failed, could not find user@server.com, maybe the server is currently unreachable or uses a self-signed certificate.', $e->getMessage());
  310. }
  311. $qb = $this->connection->getQueryBuilder();
  312. $stmt = $qb->select('*')
  313. ->from('share')
  314. ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
  315. ->execute();
  316. $data = $stmt->fetch();
  317. $stmt->closeCursor();
  318. $this->assertFalse($data);
  319. }
  320. public function testCreateShareWithSelf() {
  321. $share = $this->shareManager->newShare();
  322. $node = $this->getMockBuilder(File::class)->getMock();
  323. $node->method('getId')->willReturn(42);
  324. $node->method('getName')->willReturn('myFile');
  325. $this->addressHandler->expects($this->any())->method('compareAddresses')
  326. ->willReturn(true);
  327. $shareWith = 'sharedBy@localhost';
  328. $share->setSharedWith($shareWith)
  329. ->setSharedBy('sharedBy')
  330. ->setShareOwner('shareOwner')
  331. ->setPermissions(19)
  332. ->setNode($node);
  333. $this->contactsManager->expects($this->any())
  334. ->method('search')
  335. ->willReturn([]);
  336. $this->rootFolder->expects($this->never())->method($this->anything());
  337. try {
  338. $share = $this->provider->create($share);
  339. $this->fail();
  340. } catch (\Exception $e) {
  341. $this->assertEquals('Not allowed to create a federated share with the same user', $e->getMessage());
  342. }
  343. $qb = $this->connection->getQueryBuilder();
  344. $stmt = $qb->select('*')
  345. ->from('share')
  346. ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
  347. ->execute();
  348. $data = $stmt->fetch();
  349. $stmt->closeCursor();
  350. $this->assertFalse($data);
  351. }
  352. public function testCreateAlreadyShared() {
  353. $share = $this->shareManager->newShare();
  354. $node = $this->getMockBuilder(File::class)->getMock();
  355. $node->method('getId')->willReturn(42);
  356. $node->method('getName')->willReturn('myFile');
  357. $this->addressHandler->expects($this->any())->method('splitUserRemote')
  358. ->willReturn(['user', 'server.com']);
  359. $share->setSharedWith('user@server.com')
  360. ->setSharedBy('sharedBy')
  361. ->setShareOwner('shareOwner')
  362. ->setPermissions(19)
  363. ->setShareType(IShare::TYPE_REMOTE)
  364. ->setNode($node);
  365. $this->tokenHandler->method('generateToken')->willReturn('token');
  366. $this->addressHandler->expects($this->any())->method('generateRemoteURL')
  367. ->willReturn('http://localhost/');
  368. $this->notifications->expects($this->once())
  369. ->method('sendRemoteShare')
  370. ->with(
  371. $this->equalTo('token'),
  372. $this->equalTo('user@server.com'),
  373. $this->equalTo('myFile'),
  374. $this->anything(),
  375. 'shareOwner',
  376. 'shareOwner@http://localhost/',
  377. 'sharedBy',
  378. 'sharedBy@http://localhost/'
  379. )->willReturn(true);
  380. $this->rootFolder->expects($this->never())->method($this->anything());
  381. $this->contactsManager->expects($this->any())
  382. ->method('search')
  383. ->willReturn([]);
  384. $this->provider->create($share);
  385. try {
  386. $this->provider->create($share);
  387. } catch (\Exception $e) {
  388. $this->assertEquals('Sharing myFile failed, because this item is already shared with user user@server.com', $e->getMessage());
  389. }
  390. }
  391. /**
  392. * @dataProvider dataTestUpdate
  393. */
  394. public function testUpdate($owner, $sharedBy, $expirationDate) {
  395. $this->provider = $this->getMockBuilder('OCA\FederatedFileSharing\FederatedShareProvider')
  396. ->setConstructorArgs(
  397. [
  398. $this->connection,
  399. $this->addressHandler,
  400. $this->notifications,
  401. $this->tokenHandler,
  402. $this->l,
  403. $this->logger,
  404. $this->rootFolder,
  405. $this->config,
  406. $this->userManager,
  407. $this->cloudIdManager,
  408. $this->gsConfig,
  409. $this->cloudFederationProviderManager
  410. ]
  411. )->setMethods(['sendPermissionUpdate'])->getMock();
  412. $share = $this->shareManager->newShare();
  413. $node = $this->getMockBuilder(File::class)->getMock();
  414. $node->method('getId')->willReturn(42);
  415. $node->method('getName')->willReturn('myFile');
  416. $this->addressHandler->expects($this->any())->method('splitUserRemote')
  417. ->willReturn(['user', 'server.com']);
  418. $share->setSharedWith('user@server.com')
  419. ->setSharedBy($sharedBy)
  420. ->setShareOwner($owner)
  421. ->setPermissions(19)
  422. ->setShareType(IShare::TYPE_REMOTE)
  423. ->setExpirationDate(new \DateTime('2019-02-01T01:02:03'))
  424. ->setNode($node);
  425. $this->tokenHandler->method('generateToken')->willReturn('token');
  426. $this->addressHandler->expects($this->any())->method('generateRemoteURL')
  427. ->willReturn('http://localhost/');
  428. $this->notifications->expects($this->once())
  429. ->method('sendRemoteShare')
  430. ->with(
  431. $this->equalTo('token'),
  432. $this->equalTo('user@server.com'),
  433. $this->equalTo('myFile'),
  434. $this->anything(),
  435. $owner,
  436. $owner . '@http://localhost/',
  437. $sharedBy,
  438. $sharedBy . '@http://localhost/'
  439. )->willReturn(true);
  440. if ($owner === $sharedBy) {
  441. $this->provider->expects($this->never())->method('sendPermissionUpdate');
  442. } else {
  443. $this->provider->expects($this->once())->method('sendPermissionUpdate');
  444. }
  445. $this->rootFolder->expects($this->never())->method($this->anything());
  446. $this->contactsManager->expects($this->any())
  447. ->method('search')
  448. ->willReturn([]);
  449. $share = $this->provider->create($share);
  450. $share->setPermissions(1);
  451. $share->setExpirationDate($expirationDate);
  452. $this->provider->update($share);
  453. $share = $this->provider->getShareById($share->getId());
  454. $this->assertEquals(1, $share->getPermissions());
  455. $this->assertEquals($expirationDate, $share->getExpirationDate());
  456. }
  457. public function dataTestUpdate() {
  458. return [
  459. ['sharedBy', 'shareOwner', new \DateTime('2020-03-01T01:02:03')],
  460. ['shareOwner', 'shareOwner', null],
  461. ];
  462. }
  463. public function testGetSharedBy() {
  464. $node = $this->getMockBuilder(File::class)->getMock();
  465. $node->method('getId')->willReturn(42);
  466. $node->method('getName')->willReturn('myFile');
  467. $this->addressHandler->expects($this->never())->method('splitUserRemote');
  468. $this->addressHandler->method('generateRemoteURL')
  469. ->willReturn('remoteurl.com');
  470. $this->tokenHandler->method('generateToken')->willReturn('token');
  471. $this->notifications
  472. ->method('sendRemoteShare')
  473. ->willReturn(true);
  474. $this->rootFolder->expects($this->never())->method($this->anything());
  475. $this->contactsManager->expects($this->any())
  476. ->method('search')
  477. ->willReturn([]);
  478. $share = $this->shareManager->newShare();
  479. $share->setSharedWith('user@server.com')
  480. ->setSharedBy('sharedBy')
  481. ->setShareOwner('shareOwner')
  482. ->setPermissions(19)
  483. ->setShareType(IShare::TYPE_REMOTE)
  484. ->setNode($node);
  485. $this->provider->create($share);
  486. $share2 = $this->shareManager->newShare();
  487. $share2->setSharedWith('user2@server.com')
  488. ->setSharedBy('sharedBy2')
  489. ->setShareOwner('shareOwner')
  490. ->setPermissions(19)
  491. ->setShareType(IShare::TYPE_REMOTE)
  492. ->setNode($node);
  493. $this->provider->create($share2);
  494. $shares = $this->provider->getSharesBy('sharedBy', IShare::TYPE_REMOTE, null, false, -1, 0);
  495. $this->assertCount(1, $shares);
  496. $this->assertEquals('user@server.com', $shares[0]->getSharedWith());
  497. $this->assertEquals('sharedBy', $shares[0]->getSharedBy());
  498. }
  499. public function testGetSharedByWithNode() {
  500. $node = $this->getMockBuilder(File::class)->getMock();
  501. $node->method('getId')->willReturn(42);
  502. $node->method('getName')->willReturn('myFile');
  503. $this->tokenHandler->method('generateToken')->willReturn('token');
  504. $this->notifications
  505. ->method('sendRemoteShare')
  506. ->willReturn(true);
  507. $this->rootFolder->expects($this->never())->method($this->anything());
  508. $this->addressHandler->method('generateRemoteURL')
  509. ->willReturn('remoteurl.com');
  510. $this->contactsManager->expects($this->any())
  511. ->method('search')
  512. ->willReturn([]);
  513. $share = $this->shareManager->newShare();
  514. $share->setSharedWith('user@server.com')
  515. ->setSharedBy('sharedBy')
  516. ->setShareOwner('shareOwner')
  517. ->setPermissions(19)
  518. ->setShareType(IShare::TYPE_REMOTE)
  519. ->setNode($node);
  520. $this->provider->create($share);
  521. $node2 = $this->getMockBuilder(File::class)->getMock();
  522. $node2->method('getId')->willReturn(43);
  523. $node2->method('getName')->willReturn('myOtherFile');
  524. $share2 = $this->shareManager->newShare();
  525. $share2->setSharedWith('user@server.com')
  526. ->setSharedBy('sharedBy')
  527. ->setShareOwner('shareOwner')
  528. ->setPermissions(19)
  529. ->setShareType(IShare::TYPE_REMOTE)
  530. ->setNode($node2);
  531. $this->provider->create($share2);
  532. $shares = $this->provider->getSharesBy('sharedBy', IShare::TYPE_REMOTE, $node2, false, -1, 0);
  533. $this->assertCount(1, $shares);
  534. $this->assertEquals(43, $shares[0]->getNodeId());
  535. }
  536. public function testGetSharedByWithReshares() {
  537. $node = $this->getMockBuilder(File::class)->getMock();
  538. $node->method('getId')->willReturn(42);
  539. $node->method('getName')->willReturn('myFile');
  540. $this->tokenHandler->method('generateToken')->willReturn('token');
  541. $this->notifications
  542. ->method('sendRemoteShare')
  543. ->willReturn(true);
  544. $this->rootFolder->expects($this->never())->method($this->anything());
  545. $this->addressHandler->method('generateRemoteURL')
  546. ->willReturn('remoteurl.com');
  547. $this->contactsManager->expects($this->any())
  548. ->method('search')
  549. ->willReturn([]);
  550. $share = $this->shareManager->newShare();
  551. $share->setSharedWith('user@server.com')
  552. ->setSharedBy('shareOwner')
  553. ->setShareOwner('shareOwner')
  554. ->setPermissions(19)
  555. ->setShareType(IShare::TYPE_REMOTE)
  556. ->setNode($node);
  557. $this->provider->create($share);
  558. $share2 = $this->shareManager->newShare();
  559. $share2->setSharedWith('user2@server.com')
  560. ->setSharedBy('sharedBy')
  561. ->setShareOwner('shareOwner')
  562. ->setPermissions(19)
  563. ->setShareType(IShare::TYPE_REMOTE)
  564. ->setNode($node);
  565. $this->provider->create($share2);
  566. $shares = $this->provider->getSharesBy('shareOwner', IShare::TYPE_REMOTE, null, true, -1, 0);
  567. $this->assertCount(2, $shares);
  568. }
  569. public function testGetSharedByWithLimit() {
  570. $node = $this->getMockBuilder(File::class)->getMock();
  571. $node->method('getId')->willReturn(42);
  572. $node->method('getName')->willReturn('myFile');
  573. $this->addressHandler->expects($this->any())->method('splitUserRemote')
  574. ->willReturnCallback(function ($uid) {
  575. if ($uid === 'user@server.com') {
  576. return ['user', 'server.com'];
  577. }
  578. return ['user2', 'server.com'];
  579. });
  580. $this->tokenHandler->method('generateToken')->willReturn('token');
  581. $this->notifications
  582. ->method('sendRemoteShare')
  583. ->willReturn(true);
  584. $this->rootFolder->expects($this->never())->method($this->anything());
  585. $this->addressHandler->method('generateRemoteURL')
  586. ->willReturn('remoteurl.com');
  587. $this->contactsManager->expects($this->any())
  588. ->method('search')
  589. ->willReturn([]);
  590. $share = $this->shareManager->newShare();
  591. $share->setSharedWith('user@server.com')
  592. ->setSharedBy('sharedBy')
  593. ->setShareOwner('shareOwner')
  594. ->setPermissions(19)
  595. ->setShareType(IShare::TYPE_REMOTE)
  596. ->setNode($node);
  597. $this->provider->create($share);
  598. $share2 = $this->shareManager->newShare();
  599. $share2->setSharedWith('user2@server.com')
  600. ->setSharedBy('sharedBy')
  601. ->setShareOwner('shareOwner')
  602. ->setPermissions(19)
  603. ->setShareType(IShare::TYPE_REMOTE)
  604. ->setNode($node);
  605. $this->provider->create($share2);
  606. $shares = $this->provider->getSharesBy('shareOwner', IShare::TYPE_REMOTE, null, true, 1, 1);
  607. $this->assertCount(1, $shares);
  608. $this->assertEquals('user2@server.com', $shares[0]->getSharedWith());
  609. }
  610. public function dataDeleteUser() {
  611. return [
  612. ['a', 'b', 'c', 'a', true],
  613. ['a', 'b', 'c', 'b', false],
  614. // The recipient is non local.
  615. ['a', 'b', 'c', 'c', false],
  616. ['a', 'b', 'c', 'd', false],
  617. ];
  618. }
  619. /**
  620. * @dataProvider dataDeleteUser
  621. *
  622. * @param string $owner The owner of the share (uid)
  623. * @param string $initiator The initiator of the share (uid)
  624. * @param string $recipient The recipient of the share (uid/gid/pass)
  625. * @param string $deletedUser The user that is deleted
  626. * @param bool $rowDeleted Is the row deleted in this setup
  627. */
  628. public function testDeleteUser($owner, $initiator, $recipient, $deletedUser, $rowDeleted) {
  629. $qb = $this->connection->getQueryBuilder();
  630. $qb->insert('share')
  631. ->setValue('share_type', $qb->createNamedParameter(IShare::TYPE_REMOTE))
  632. ->setValue('uid_owner', $qb->createNamedParameter($owner))
  633. ->setValue('uid_initiator', $qb->createNamedParameter($initiator))
  634. ->setValue('share_with', $qb->createNamedParameter($recipient))
  635. ->setValue('item_type', $qb->createNamedParameter('file'))
  636. ->setValue('item_source', $qb->createNamedParameter(42))
  637. ->setValue('file_source', $qb->createNamedParameter(42))
  638. ->execute();
  639. $id = $qb->getLastInsertId();
  640. $this->provider->userDeleted($deletedUser, IShare::TYPE_REMOTE);
  641. $qb = $this->connection->getQueryBuilder();
  642. $qb->select('*')
  643. ->from('share')
  644. ->where(
  645. $qb->expr()->eq('id', $qb->createNamedParameter($id))
  646. );
  647. $cursor = $qb->execute();
  648. $data = $cursor->fetchAll();
  649. $cursor->closeCursor();
  650. $this->assertCount($rowDeleted ? 0 : 1, $data);
  651. }
  652. /**
  653. * @dataProvider dataTestIsOutgoingServer2serverShareEnabled
  654. *
  655. * @param string $isEnabled
  656. * @param bool $expected
  657. */
  658. public function testIsOutgoingServer2serverShareEnabled($internalOnly, $isEnabled, $expected) {
  659. $this->gsConfig->expects($this->once())->method('onlyInternalFederation')
  660. ->willReturn($internalOnly);
  661. $this->config->expects($this->any())->method('getAppValue')
  662. ->with('files_sharing', 'outgoing_server2server_share_enabled', 'yes')
  663. ->willReturn($isEnabled);
  664. $this->assertSame($expected,
  665. $this->provider->isOutgoingServer2serverShareEnabled()
  666. );
  667. }
  668. public function dataTestIsOutgoingServer2serverShareEnabled() {
  669. return [
  670. [false, 'yes', true],
  671. [false, 'no', false],
  672. [true, 'yes', false],
  673. [true, 'no', false],
  674. ];
  675. }
  676. /**
  677. * @dataProvider dataTestIsIncomingServer2serverShareEnabled
  678. *
  679. * @param string $isEnabled
  680. * @param bool $expected
  681. */
  682. public function testIsIncomingServer2serverShareEnabled($onlyInternal, $isEnabled, $expected) {
  683. $this->gsConfig->expects($this->once())->method('onlyInternalFederation')
  684. ->willReturn($onlyInternal);
  685. $this->config->expects($this->any())->method('getAppValue')
  686. ->with('files_sharing', 'incoming_server2server_share_enabled', 'yes')
  687. ->willReturn($isEnabled);
  688. $this->assertSame($expected,
  689. $this->provider->isIncomingServer2serverShareEnabled()
  690. );
  691. }
  692. public function dataTestIsIncomingServer2serverShareEnabled() {
  693. return [
  694. [false, 'yes', true],
  695. [false, 'no', false],
  696. [true, 'yes', false],
  697. [true, 'no', false],
  698. ];
  699. }
  700. /**
  701. * @dataProvider dataTestIsLookupServerQueriesEnabled
  702. *
  703. * @param string $isEnabled
  704. * @param bool $expected
  705. */
  706. public function testIsLookupServerQueriesEnabled($gsEnabled, $isEnabled, $expected) {
  707. $this->gsConfig->expects($this->once())->method('isGlobalScaleEnabled')
  708. ->willReturn($gsEnabled);
  709. $this->config->expects($this->any())->method('getAppValue')
  710. ->with('files_sharing', 'lookupServerEnabled', 'yes')
  711. ->willReturn($isEnabled);
  712. $this->assertSame($expected,
  713. $this->provider->isLookupServerQueriesEnabled()
  714. );
  715. }
  716. public function dataTestIsLookupServerQueriesEnabled() {
  717. return [
  718. [false, 'yes', true],
  719. [false, 'no', false],
  720. [true, 'yes', true],
  721. [true, 'no', true],
  722. ];
  723. }
  724. /**
  725. * @dataProvider dataTestIsLookupServerUploadEnabled
  726. *
  727. * @param string $isEnabled
  728. * @param bool $expected
  729. */
  730. public function testIsLookupServerUploadEnabled($gsEnabled, $isEnabled, $expected) {
  731. $this->gsConfig->expects($this->once())->method('isGlobalScaleEnabled')
  732. ->willReturn($gsEnabled);
  733. $this->config->expects($this->any())->method('getAppValue')
  734. ->with('files_sharing', 'lookupServerUploadEnabled', 'yes')
  735. ->willReturn($isEnabled);
  736. $this->assertSame($expected,
  737. $this->provider->isLookupServerUploadEnabled()
  738. );
  739. }
  740. public function dataTestIsLookupServerUploadEnabled() {
  741. return [
  742. [false, 'yes', true],
  743. [false, 'no', false],
  744. [true, 'yes', false],
  745. [true, 'no', false],
  746. ];
  747. }
  748. public function testGetSharesInFolder() {
  749. $userManager = \OC::$server->getUserManager();
  750. $rootFolder = \OC::$server->getRootFolder();
  751. $u1 = $userManager->createUser('testFed', md5(time()));
  752. $u2 = $userManager->createUser('testFed2', md5(time()));
  753. $folder1 = $rootFolder->getUserFolder($u1->getUID())->newFolder('foo');
  754. $file1 = $folder1->newFile('bar1');
  755. $file2 = $folder1->newFile('bar2');
  756. $this->tokenHandler->method('generateToken')->willReturn('token');
  757. $this->notifications
  758. ->method('sendRemoteShare')
  759. ->willReturn(true);
  760. $this->addressHandler->method('generateRemoteURL')
  761. ->willReturn('remoteurl.com');
  762. $this->contactsManager->expects($this->any())
  763. ->method('search')
  764. ->willReturn([]);
  765. $share1 = $this->shareManager->newShare();
  766. $share1->setSharedWith('user@server.com')
  767. ->setSharedBy($u1->getUID())
  768. ->setShareOwner($u1->getUID())
  769. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  770. ->setShareType(IShare::TYPE_REMOTE)
  771. ->setNode($file1);
  772. $this->provider->create($share1);
  773. $share2 = $this->shareManager->newShare();
  774. $share2->setSharedWith('user@server.com')
  775. ->setSharedBy($u2->getUID())
  776. ->setShareOwner($u1->getUID())
  777. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  778. ->setShareType(IShare::TYPE_REMOTE)
  779. ->setNode($file2);
  780. $this->provider->create($share2);
  781. $result = $this->provider->getSharesInFolder($u1->getUID(), $folder1, false);
  782. $this->assertCount(1, $result);
  783. $this->assertCount(1, $result[$file1->getId()]);
  784. $result = $this->provider->getSharesInFolder($u1->getUID(), $folder1, true);
  785. $this->assertCount(2, $result);
  786. $this->assertCount(1, $result[$file1->getId()]);
  787. $this->assertCount(1, $result[$file2->getId()]);
  788. $u1->delete();
  789. $u2->delete();
  790. }
  791. public function testGetAccessList() {
  792. $userManager = \OC::$server->getUserManager();
  793. $rootFolder = \OC::$server->getRootFolder();
  794. $u1 = $userManager->createUser('testFed', md5(time()));
  795. $folder1 = $rootFolder->getUserFolder($u1->getUID())->newFolder('foo');
  796. $file1 = $folder1->newFile('bar1');
  797. $this->tokenHandler->expects($this->exactly(2))
  798. ->method('generateToken')
  799. ->willReturnOnConsecutiveCalls('token1', 'token2');
  800. $this->notifications->expects($this->atLeastOnce())
  801. ->method('sendRemoteShare')
  802. ->willReturn(true);
  803. $this->contactsManager->expects($this->any())
  804. ->method('search')
  805. ->willReturn([]);
  806. $result = $this->provider->getAccessList([$file1], true);
  807. $this->assertEquals(['remote' => []], $result);
  808. $result = $this->provider->getAccessList([$file1], false);
  809. $this->assertEquals(['remote' => false], $result);
  810. $this->addressHandler->method('generateRemoteURL')
  811. ->willReturn('remoteurl.com');
  812. $share1 = $this->shareManager->newShare();
  813. $share1->setSharedWith('user@server.com')
  814. ->setSharedBy($u1->getUID())
  815. ->setShareOwner($u1->getUID())
  816. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  817. ->setShareType(IShare::TYPE_REMOTE)
  818. ->setNode($file1);
  819. $this->provider->create($share1);
  820. $share2 = $this->shareManager->newShare();
  821. $share2->setSharedWith('foobar@localhost')
  822. ->setSharedBy($u1->getUID())
  823. ->setShareOwner($u1->getUID())
  824. ->setPermissions(\OCP\Constants::PERMISSION_READ)
  825. ->setShareType(IShare::TYPE_REMOTE)
  826. ->setNode($file1);
  827. $this->provider->create($share2);
  828. $result = $this->provider->getAccessList([$file1], true);
  829. $this->assertEquals(['remote' => [
  830. 'user@server.com' => [
  831. 'token' => 'token1',
  832. 'node_id' => $file1->getId(),
  833. ],
  834. 'foobar@localhost' => [
  835. 'token' => 'token2',
  836. 'node_id' => $file1->getId(),
  837. ],
  838. ]], $result);
  839. $result = $this->provider->getAccessList([$file1], false);
  840. $this->assertEquals(['remote' => true], $result);
  841. $u1->delete();
  842. }
  843. }