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

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