FederatedShareProviderTest.php 29 KB

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