FederatedShareProviderTest.php 23 KB

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