2014-11-24 17:31:52 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Bjoern Schiessle <bjoern@schiessle.org>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
2020-04-29 12:57:22 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 19:13:36 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2016-01-12 17:02:16 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2015-02-26 13:37:37 +03:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* @license AGPL-3.0
|
2014-11-24 17:31:52 +03:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
2014-11-24 17:31:52 +03:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2014-11-24 17:31:52 +03:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-03-26 13:44:34 +03:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
2014-11-24 17:31:52 +03:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2014-11-24 17:31:52 +03:00
|
|
|
*
|
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2016-05-04 12:03:40 +03:00
|
|
|
namespace OCA\FederatedFileSharing\Tests;
|
|
|
|
|
2016-08-19 13:01:13 +03:00
|
|
|
use OCA\FederatedFileSharing\Controller\RequestHandlerController;
|
2018-06-11 16:30:04 +03:00
|
|
|
use OCP\AppFramework\Http\DataResponse;
|
|
|
|
use OCP\Federation\ICloudFederationFactory;
|
|
|
|
use OCP\Federation\ICloudFederationProvider;
|
|
|
|
use OCP\Federation\ICloudFederationProviderManager;
|
|
|
|
use OCP\Federation\ICloudFederationShare;
|
2017-01-27 14:52:17 +03:00
|
|
|
use OCP\Federation\ICloudIdManager;
|
2018-06-11 16:30:04 +03:00
|
|
|
use OCP\IDBConnection;
|
|
|
|
use OCP\IRequest;
|
2016-05-04 16:26:30 +03:00
|
|
|
use OCP\IUserManager;
|
2018-06-11 16:30:04 +03:00
|
|
|
use OCP\Share;
|
2016-05-04 16:26:30 +03:00
|
|
|
use OCP\Share\IShare;
|
2021-03-09 22:42:47 +03:00
|
|
|
use Psr\Log\LoggerInterface;
|
2014-11-24 17:31:52 +03:00
|
|
|
|
|
|
|
/**
|
2016-05-04 12:03:40 +03:00
|
|
|
* Class RequestHandlerTest
|
2015-11-03 03:52:41 +03:00
|
|
|
*
|
2016-05-04 12:03:40 +03:00
|
|
|
* @package OCA\FederatedFileSharing\Tests
|
2015-11-03 03:52:41 +03:00
|
|
|
* @group DB
|
2014-11-24 17:31:52 +03:00
|
|
|
*/
|
2018-06-11 16:30:04 +03:00
|
|
|
class RequestHandlerControllerTest extends \Test\TestCase {
|
|
|
|
private $owner = 'owner';
|
|
|
|
private $user1 = 'user1';
|
|
|
|
private $user2 = 'user2';
|
|
|
|
private $ownerCloudId = 'owner@server0.org';
|
|
|
|
private $user1CloudId = 'user1@server1.org';
|
|
|
|
private $user2CloudId = 'user2@server2.org';
|
2014-11-24 17:31:52 +03:00
|
|
|
|
2018-06-11 16:30:04 +03:00
|
|
|
/** @var RequestHandlerController */
|
|
|
|
private $requestHandler;
|
2014-11-24 17:31:52 +03:00
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \OCA\FederatedFileSharing\FederatedShareProvider|\PHPUnit\Framework\MockObject\MockObject */
|
2016-04-18 19:17:08 +03:00
|
|
|
private $federatedShareProvider;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \OCA\FederatedFileSharing\Notifications|\PHPUnit\Framework\MockObject\MockObject */
|
2016-05-04 16:26:30 +03:00
|
|
|
private $notifications;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \OCA\FederatedFileSharing\AddressHandler|\PHPUnit\Framework\MockObject\MockObject */
|
2016-05-04 16:26:30 +03:00
|
|
|
private $addressHandler;
|
2017-03-30 18:03:04 +03:00
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
|
2016-05-04 16:26:30 +03:00
|
|
|
private $userManager;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IShare|\PHPUnit\Framework\MockObject\MockObject */
|
2016-05-04 16:26:30 +03:00
|
|
|
private $share;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var ICloudIdManager|\PHPUnit\Framework\MockObject\MockObject */
|
2017-01-27 14:52:17 +03:00
|
|
|
private $cloudIdManager;
|
|
|
|
|
2021-03-09 22:42:47 +03:00
|
|
|
/** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
|
2018-04-23 11:44:49 +03:00
|
|
|
private $logger;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
|
2018-06-11 16:30:04 +03:00
|
|
|
private $request;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IDBConnection|\PHPUnit\Framework\MockObject\MockObject */
|
2018-06-11 16:30:04 +03:00
|
|
|
private $connection;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var Share\IManager|\PHPUnit\Framework\MockObject\MockObject */
|
2018-06-11 16:30:04 +03:00
|
|
|
private $shareManager;
|
2014-11-24 17:31:52 +03:00
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var ICloudFederationFactory|\PHPUnit\Framework\MockObject\MockObject */
|
2018-06-11 16:30:04 +03:00
|
|
|
private $cloudFederationFactory;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var ICloudFederationProviderManager|\PHPUnit\Framework\MockObject\MockObject */
|
2018-06-11 16:30:04 +03:00
|
|
|
private $cloudFederationProviderManager;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var ICloudFederationProvider|\PHPUnit\Framework\MockObject\MockObject */
|
2018-06-11 16:30:04 +03:00
|
|
|
private $cloudFederationProvider;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var ICloudFederationShare|\PHPUnit\Framework\MockObject\MockObject */
|
2018-06-11 16:30:04 +03:00
|
|
|
private $cloudFederationShare;
|
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function setUp(): void {
|
2017-10-25 01:03:28 +03:00
|
|
|
$this->share = $this->getMockBuilder(IShare::class)->getMock();
|
2016-04-18 19:17:08 +03:00
|
|
|
$this->federatedShareProvider = $this->getMockBuilder('OCA\FederatedFileSharing\FederatedShareProvider')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
$this->federatedShareProvider->expects($this->any())
|
|
|
|
->method('isOutgoingServer2serverShareEnabled')->willReturn(true);
|
|
|
|
$this->federatedShareProvider->expects($this->any())
|
|
|
|
->method('isIncomingServer2serverShareEnabled')->willReturn(true);
|
2016-05-04 16:26:30 +03:00
|
|
|
$this->federatedShareProvider->expects($this->any())->method('getShareById')
|
|
|
|
->willReturn($this->share);
|
2014-12-04 21:51:04 +03:00
|
|
|
|
2016-05-04 16:26:30 +03:00
|
|
|
$this->notifications = $this->getMockBuilder('OCA\FederatedFileSharing\Notifications')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
$this->addressHandler = $this->getMockBuilder('OCA\FederatedFileSharing\AddressHandler')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
2017-10-24 16:26:53 +03:00
|
|
|
$this->userManager = $this->getMockBuilder(IUserManager::class)->getMock();
|
2018-06-11 16:30:04 +03:00
|
|
|
$this->cloudIdManager = $this->createMock(ICloudIdManager::class);
|
|
|
|
$this->request = $this->createMock(IRequest::class);
|
|
|
|
$this->connection = $this->createMock(IDBConnection::class);
|
|
|
|
$this->shareManager = $this->createMock(Share\IManager::class);
|
|
|
|
$this->cloudFederationFactory = $this->createMock(ICloudFederationFactory::class);
|
|
|
|
$this->cloudFederationProviderManager = $this->createMock(ICloudFederationProviderManager::class);
|
|
|
|
$this->cloudFederationProvider = $this->createMock(ICloudFederationProvider::class);
|
|
|
|
$this->cloudFederationShare = $this->createMock(ICloudFederationShare::class);
|
2017-01-27 14:52:17 +03:00
|
|
|
|
2017-03-30 18:03:04 +03:00
|
|
|
|
2021-03-09 22:42:47 +03:00
|
|
|
$this->logger = $this->createMock(LoggerInterface::class);
|
2018-04-23 11:44:49 +03:00
|
|
|
|
2018-06-11 16:30:04 +03:00
|
|
|
$this->requestHandler = new RequestHandlerController(
|
2016-08-19 13:01:13 +03:00
|
|
|
'federatedfilesharing',
|
2018-06-11 16:30:04 +03:00
|
|
|
$this->request,
|
2016-05-04 16:26:30 +03:00
|
|
|
$this->federatedShareProvider,
|
2018-06-11 16:30:04 +03:00
|
|
|
$this->connection,
|
|
|
|
$this->shareManager,
|
2016-05-04 16:26:30 +03:00
|
|
|
$this->notifications,
|
|
|
|
$this->addressHandler,
|
2017-01-27 14:52:17 +03:00
|
|
|
$this->userManager,
|
2018-04-23 11:44:49 +03:00
|
|
|
$this->cloudIdManager,
|
2018-06-11 16:30:04 +03:00
|
|
|
$this->logger,
|
|
|
|
$this->cloudFederationFactory,
|
|
|
|
$this->cloudFederationProviderManager
|
2016-05-04 16:26:30 +03:00
|
|
|
);
|
2014-11-24 17:31:52 +03:00
|
|
|
}
|
|
|
|
|
2020-04-10 17:51:06 +03:00
|
|
|
public function testCreateShare() {
|
2014-11-24 17:31:52 +03:00
|
|
|
// simulate a post request
|
|
|
|
$_POST['remote'] = 'localhost';
|
|
|
|
$_POST['token'] = 'token';
|
|
|
|
$_POST['name'] = 'name';
|
2018-06-11 16:30:04 +03:00
|
|
|
$_POST['owner'] = $this->owner;
|
|
|
|
$_POST['sharedBy'] = $this->user1;
|
|
|
|
$_POST['shareWith'] = $this->user2;
|
2014-12-04 21:51:04 +03:00
|
|
|
$_POST['remoteId'] = 1;
|
2018-06-11 16:30:04 +03:00
|
|
|
$_POST['sharedByFederatedId'] = $this->user1CloudId;
|
|
|
|
$_POST['ownerFederatedId'] = $this->ownerCloudId;
|
|
|
|
|
|
|
|
$this->cloudFederationFactory->expects($this->once())->method('getCloudFederationShare')
|
|
|
|
->with(
|
|
|
|
$this->user2,
|
|
|
|
'name',
|
|
|
|
'',
|
|
|
|
1,
|
|
|
|
$this->ownerCloudId,
|
|
|
|
$this->owner,
|
|
|
|
$this->user1CloudId,
|
|
|
|
$this->user1,
|
|
|
|
'token',
|
|
|
|
'user',
|
|
|
|
'file'
|
|
|
|
)->willReturn($this->cloudFederationShare);
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var ICloudFederationProvider|\PHPUnit\Framework\MockObject\MockObject $provider */
|
2018-06-11 16:30:04 +03:00
|
|
|
$this->cloudFederationProviderManager->expects($this->once())
|
|
|
|
->method('getCloudFederationProvider')
|
|
|
|
->with('file')
|
|
|
|
->willReturn($this->cloudFederationProvider);
|
|
|
|
|
|
|
|
$this->cloudFederationProvider->expects($this->once())->method('shareReceived')
|
|
|
|
->with($this->cloudFederationShare);
|
|
|
|
|
|
|
|
$result = $this->requestHandler->createShare();
|
|
|
|
|
|
|
|
$this->assertInstanceOf(DataResponse::class, $result);
|
2014-11-24 17:31:52 +03:00
|
|
|
}
|
|
|
|
|
2020-04-10 17:51:06 +03:00
|
|
|
public function testDeclineShare() {
|
2018-06-11 16:30:04 +03:00
|
|
|
$id = 42;
|
2014-11-24 17:31:52 +03:00
|
|
|
$_POST['token'] = 'token';
|
|
|
|
|
2018-06-11 16:30:04 +03:00
|
|
|
$notification = [
|
|
|
|
'sharedSecret' => 'token',
|
|
|
|
'message' => 'Recipient declined the share'
|
|
|
|
];
|
2015-01-21 02:11:15 +03:00
|
|
|
|
2018-06-11 16:30:04 +03:00
|
|
|
$this->cloudFederationProviderManager->expects($this->once())
|
|
|
|
->method('getCloudFederationProvider')
|
|
|
|
->with('file')
|
|
|
|
->willReturn($this->cloudFederationProvider);
|
2015-01-21 02:11:15 +03:00
|
|
|
|
2018-06-11 16:30:04 +03:00
|
|
|
$this->cloudFederationProvider->expects($this->once())
|
|
|
|
->method('notificationReceived')
|
|
|
|
->with('SHARE_DECLINED', $id, $notification);
|
2015-01-21 02:11:15 +03:00
|
|
|
|
2018-06-11 16:30:04 +03:00
|
|
|
$result = $this->requestHandler->declineShare($id);
|
2015-01-21 02:11:15 +03:00
|
|
|
|
2018-06-11 16:30:04 +03:00
|
|
|
$this->assertInstanceOf(DataResponse::class, $result);
|
2015-01-21 02:11:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-10 17:51:06 +03:00
|
|
|
public function testAcceptShare() {
|
2018-06-11 16:30:04 +03:00
|
|
|
$id = 42;
|
|
|
|
$_POST['token'] = 'token';
|
2015-01-21 02:11:15 +03:00
|
|
|
|
2018-06-11 16:30:04 +03:00
|
|
|
$notification = [
|
|
|
|
'sharedSecret' => 'token',
|
|
|
|
'message' => 'Recipient accept the share'
|
|
|
|
];
|
2015-01-21 02:11:15 +03:00
|
|
|
|
2018-06-11 16:30:04 +03:00
|
|
|
$this->cloudFederationProviderManager->expects($this->once())
|
|
|
|
->method('getCloudFederationProvider')
|
|
|
|
->with('file')
|
|
|
|
->willReturn($this->cloudFederationProvider);
|
2015-01-21 02:11:15 +03:00
|
|
|
|
2018-06-11 16:30:04 +03:00
|
|
|
$this->cloudFederationProvider->expects($this->once())
|
|
|
|
->method('notificationReceived')
|
|
|
|
->with('SHARE_ACCEPTED', $id, $notification);
|
2016-05-04 13:16:02 +03:00
|
|
|
|
2018-06-11 16:30:04 +03:00
|
|
|
$result = $this->requestHandler->acceptShare($id);
|
2016-05-04 13:16:02 +03:00
|
|
|
|
2018-06-11 16:30:04 +03:00
|
|
|
$this->assertInstanceOf(DataResponse::class, $result);
|
2016-05-04 13:16:02 +03:00
|
|
|
}
|
2014-11-24 17:31:52 +03:00
|
|
|
}
|