FederatedShareProviderTest.php 31 KB

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