FederatedShareProviderTest.php 29 KB

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