FederatedShareProviderTest.php 29 KB

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