FederatedShareProviderTest.php 30 KB

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