You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FederatedShareProviderTest.php 31KB

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