1
0

FederatedShareProviderTest.php 23 KB

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