FederatedShareProviderTest.php 29 KB

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