You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FederatedShareProviderTest.php 27KB

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