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>
|
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
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;
|
|
|
|
|
2018-06-11 16:30:04 +03:00
|
|
|
use OC\AppFramework\Http;
|
2017-01-27 14:52:17 +03:00
|
|
|
use OC\Federation\CloudIdManager;
|
2016-05-04 12:03:40 +03:00
|
|
|
use OC\Files\Filesystem;
|
2016-08-19 13:01:13 +03:00
|
|
|
use OCA\FederatedFileSharing\Controller\RequestHandlerController;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCA\FederatedFileSharing\FederatedShareProvider;
|
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;
|
2016-09-29 01:03:29 +03:00
|
|
|
use OCP\Http\Client\IClient;
|
|
|
|
use OCP\Http\Client\IClientService;
|
|
|
|
use OCP\Http\Client\IResponse;
|
2017-10-24 16:26:53 +03:00
|
|
|
use OCP\IConfig;
|
2018-06-11 16:30:04 +03:00
|
|
|
use OCP\IDBConnection;
|
2018-04-23 11:44:49 +03:00
|
|
|
use OCP\ILogger;
|
2018-06-11 16:30:04 +03:00
|
|
|
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;
|
2018-06-11 16:30:04 +03:00
|
|
|
use PHPUnit\Framework\MockObject\Builder\InvocationMocker;
|
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 {
|
2014-11-24 17:31:52 +03:00
|
|
|
|
2018-06-11 16:30:04 +03:00
|
|
|
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
|
|
|
|
2016-08-19 13:01:13 +03:00
|
|
|
/** @var \OCA\FederatedFileSharing\FederatedShareProvider|\PHPUnit_Framework_MockObject_MockObject */
|
2016-04-18 19:17:08 +03:00
|
|
|
private $federatedShareProvider;
|
|
|
|
|
2016-08-19 13:01:13 +03:00
|
|
|
/** @var \OCA\FederatedFileSharing\Notifications|\PHPUnit_Framework_MockObject_MockObject */
|
2016-05-04 16:26:30 +03:00
|
|
|
private $notifications;
|
|
|
|
|
2016-08-19 13:01:13 +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
|
|
|
|
2016-08-19 13:01:13 +03:00
|
|
|
/** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
|
2016-05-04 16:26:30 +03:00
|
|
|
private $userManager;
|
|
|
|
|
2016-08-19 13:01:13 +03:00
|
|
|
/** @var IShare|\PHPUnit_Framework_MockObject_MockObject */
|
2016-05-04 16:26:30 +03:00
|
|
|
private $share;
|
|
|
|
|
2018-06-11 16:30:04 +03:00
|
|
|
/** @var ICloudIdManager|\PHPUnit_Framework_MockObject_MockObject */
|
2017-01-27 14:52:17 +03:00
|
|
|
private $cloudIdManager;
|
|
|
|
|
2018-04-23 11:44:49 +03:00
|
|
|
/** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $logger;
|
|
|
|
|
2018-06-11 16:30:04 +03:00
|
|
|
/** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $request;
|
|
|
|
|
|
|
|
/** @var IDBConnection|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $connection;
|
|
|
|
|
|
|
|
/** @var Share\IManager|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $shareManager;
|
2014-11-24 17:31:52 +03:00
|
|
|
|
2018-06-11 16:30:04 +03:00
|
|
|
/** @var ICloudFederationFactory|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $cloudFederationFactory;
|
|
|
|
|
|
|
|
/** @var ICloudFederationProviderManager|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $cloudFederationProviderManager;
|
|
|
|
|
|
|
|
/** @var ICloudFederationProvider|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $cloudFederationProvider;
|
|
|
|
|
|
|
|
/** @var ICloudFederationShare|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $cloudFederationShare;
|
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function setUp(): void {
|
2014-11-24 17:31:52 +03:00
|
|
|
|
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
|
|
|
|
2018-04-23 11:44:49 +03:00
|
|
|
$this->logger = $this->createMock(ILogger::class);
|
|
|
|
|
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
|
|
|
);
|
2015-01-21 02:11:15 +03:00
|
|
|
|
2014-11-24 17:31:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function testCreateShare() {
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
/** @var ICloudFederationProvider|\PHPUnit_Framework_MockObject_MockObject $provider */
|
|
|
|
$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
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-11 16:30:04 +03:00
|
|
|
function testAcceptShare() {
|
2015-01-21 02:11:15 +03:00
|
|
|
|
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
|
|
|
}
|