2016-05-23 15:57:22 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
2016-07-13 18:50:06 +03:00
|
|
|
* @copyright Copyright (c) 2016, Björn Schießle <bjoern@schiessle.org>
|
2016-07-14 12:01:25 +03:00
|
|
|
*
|
2020-12-16 16:54:15 +03:00
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Bjoern Schiessle <bjoern@schiessle.org>
|
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
2020-04-29 12:57:22 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2016-07-21 17:49:16 +03:00
|
|
|
*
|
2016-05-23 15:57:22 +03:00
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* 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/>
|
2016-05-23 15:57:22 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\FederatedFileSharing\Tests\Controller;
|
|
|
|
|
2017-01-27 14:52:17 +03:00
|
|
|
use OC\Federation\CloudIdManager;
|
2016-05-23 15:57:22 +03:00
|
|
|
use OC\HintException;
|
|
|
|
use OCA\FederatedFileSharing\AddressHandler;
|
2016-07-14 12:01:25 +03:00
|
|
|
use OCA\FederatedFileSharing\Controller\MountPublicLinkController;
|
2016-05-23 15:57:22 +03:00
|
|
|
use OCA\FederatedFileSharing\FederatedShareProvider;
|
|
|
|
use OCP\AppFramework\Http;
|
2020-11-16 19:56:44 +03:00
|
|
|
use OCP\Contacts\IManager as IContactsManager;
|
2017-01-27 14:52:17 +03:00
|
|
|
use OCP\Federation\ICloudIdManager;
|
2016-05-23 15:57:22 +03:00
|
|
|
use OCP\Files\IRootFolder;
|
2016-07-12 19:48:00 +03:00
|
|
|
use OCP\Http\Client\IClientService;
|
|
|
|
use OCP\IL10N;
|
2017-10-24 16:26:53 +03:00
|
|
|
use OCP\IRequest;
|
2016-07-12 15:03:29 +03:00
|
|
|
use OCP\ISession;
|
2016-05-23 15:57:22 +03:00
|
|
|
use OCP\IUserManager;
|
2016-07-12 19:48:00 +03:00
|
|
|
use OCP\IUserSession;
|
2016-05-23 15:57:22 +03:00
|
|
|
use OCP\Share\IManager;
|
|
|
|
use OCP\Share\IShare;
|
|
|
|
|
2016-07-14 12:01:25 +03:00
|
|
|
class MountPublicLinkControllerTest extends \Test\TestCase {
|
2020-11-16 19:56:44 +03:00
|
|
|
/** @var IContactsManager|\PHPUnit\Framework\MockObject\MockObject */
|
|
|
|
protected $contactsManager;
|
2016-05-23 15:57:22 +03:00
|
|
|
|
2016-07-14 12:01:25 +03:00
|
|
|
/** @var MountPublicLinkController */
|
2016-05-23 15:57:22 +03:00
|
|
|
private $controller;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \OCP\IRequest | \PHPUnit\Framework\MockObject\MockObject */
|
2016-05-23 15:57:22 +03:00
|
|
|
private $request;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var FederatedShareProvider | \PHPUnit\Framework\MockObject\MockObject */
|
2016-05-23 15:57:22 +03:00
|
|
|
private $federatedShareProvider;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IManager | \PHPUnit\Framework\MockObject\MockObject */
|
2016-05-23 15:57:22 +03:00
|
|
|
private $shareManager;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var AddressHandler | \PHPUnit\Framework\MockObject\MockObject */
|
2016-05-23 15:57:22 +03:00
|
|
|
private $addressHandler;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IRootFolder | \PHPUnit\Framework\MockObject\MockObject */
|
2016-05-23 15:57:22 +03:00
|
|
|
private $rootFolder;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IUserManager | \PHPUnit\Framework\MockObject\MockObject */
|
2016-05-23 15:57:22 +03:00
|
|
|
private $userManager;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var ISession | \PHPUnit\Framework\MockObject\MockObject */
|
2016-07-12 15:03:29 +03:00
|
|
|
private $session;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IL10N | \PHPUnit\Framework\MockObject\MockObject */
|
2016-07-12 19:48:00 +03:00
|
|
|
private $l10n;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IUserSession | \PHPUnit\Framework\MockObject\MockObject */
|
2016-07-12 19:48:00 +03:00
|
|
|
private $userSession;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IClientService | \PHPUnit\Framework\MockObject\MockObject */
|
2016-07-12 19:48:00 +03:00
|
|
|
private $clientService;
|
|
|
|
|
2016-05-23 15:57:22 +03:00
|
|
|
/** @var IShare */
|
|
|
|
private $share;
|
|
|
|
|
2017-01-27 14:52:17 +03:00
|
|
|
/** @var ICloudIdManager */
|
|
|
|
private $cloudIdManager;
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2016-05-23 15:57:22 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$this->request = $this->getMockBuilder(IRequest::class)->disableOriginalConstructor()->getMock();
|
2016-05-23 15:57:22 +03:00
|
|
|
$this->federatedShareProvider = $this->getMockBuilder('OCA\FederatedFileSharing\FederatedShareProvider')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
2017-10-25 01:03:28 +03:00
|
|
|
$this->shareManager = $this->getMockBuilder(IManager::class)->disableOriginalConstructor()->getMock();
|
2016-05-23 15:57:22 +03:00
|
|
|
$this->addressHandler = $this->getMockBuilder('OCA\FederatedFileSharing\AddressHandler')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
2016-07-12 19:48:00 +03:00
|
|
|
$this->rootFolder = $this->getMockBuilder('OCP\Files\IRootFolder')->disableOriginalConstructor()->getMock();
|
2017-10-24 16:26:53 +03:00
|
|
|
$this->userManager = $this->getMockBuilder(IUserManager::class)->disableOriginalConstructor()->getMock();
|
2016-05-23 15:57:22 +03:00
|
|
|
$this->share = new \OC\Share20\Share($this->rootFolder, $this->userManager);
|
2017-10-26 14:46:16 +03:00
|
|
|
$this->session = $this->getMockBuilder(ISession::class)->disableOriginalConstructor()->getMock();
|
2017-10-24 16:26:53 +03:00
|
|
|
$this->l10n = $this->getMockBuilder(IL10N::class)->disableOriginalConstructor()->getMock();
|
2017-10-26 14:46:16 +03:00
|
|
|
$this->userSession = $this->getMockBuilder(IUserSession::class)->disableOriginalConstructor()->getMock();
|
2016-07-12 19:48:00 +03:00
|
|
|
$this->clientService = $this->getMockBuilder('OCP\Http\Client\IClientService')->disableOriginalConstructor()->getMock();
|
2020-11-16 19:56:44 +03:00
|
|
|
$this->contactsManager = $this->createMock(IContactsManager::class);
|
|
|
|
$this->cloudIdManager = new CloudIdManager($this->contactsManager);
|
2016-05-23 15:57:22 +03:00
|
|
|
|
2016-07-14 12:01:25 +03:00
|
|
|
$this->controller = new MountPublicLinkController(
|
2016-05-23 15:57:22 +03:00
|
|
|
'federatedfilesharing', $this->request,
|
|
|
|
$this->federatedShareProvider,
|
|
|
|
$this->shareManager,
|
2016-07-12 15:03:29 +03:00
|
|
|
$this->addressHandler,
|
2016-07-12 19:48:00 +03:00
|
|
|
$this->session,
|
|
|
|
$this->l10n,
|
|
|
|
$this->userSession,
|
2017-01-27 14:52:17 +03:00
|
|
|
$this->clientService,
|
|
|
|
$this->cloudIdManager
|
2016-05-23 15:57:22 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-07-14 12:01:25 +03:00
|
|
|
* @dataProvider dataTestCreateFederatedShare
|
2016-05-23 15:57:22 +03:00
|
|
|
*
|
|
|
|
* @param string $shareWith
|
2016-07-14 21:41:10 +03:00
|
|
|
* @param bool $outgoingSharesAllowed
|
2016-05-23 15:57:22 +03:00
|
|
|
* @param bool $validShareWith
|
|
|
|
* @param string $token
|
|
|
|
* @param bool $validToken
|
|
|
|
* @param bool $createSuccessful
|
|
|
|
* @param string $expectedReturnData
|
|
|
|
*/
|
2016-07-14 21:41:10 +03:00
|
|
|
public function testCreateFederatedShare($shareWith,
|
|
|
|
$outgoingSharesAllowed,
|
|
|
|
$validShareWith,
|
|
|
|
$token,
|
|
|
|
$validToken,
|
|
|
|
$createSuccessful,
|
2021-04-19 10:08:32 +03:00
|
|
|
$expectedReturnData,
|
|
|
|
$permissions
|
2016-07-14 21:41:10 +03:00
|
|
|
) {
|
|
|
|
$this->federatedShareProvider->expects($this->any())
|
|
|
|
->method('isOutgoingServer2serverShareEnabled')
|
|
|
|
->willReturn($outgoingSharesAllowed);
|
|
|
|
|
2016-05-23 15:57:22 +03:00
|
|
|
$this->addressHandler->expects($this->any())->method('splitUserRemote')
|
|
|
|
->with($shareWith)
|
|
|
|
->willReturnCallback(
|
2020-04-09 14:53:40 +03:00
|
|
|
function ($shareWith) use ($validShareWith, $expectedReturnData) {
|
2016-05-23 15:57:22 +03:00
|
|
|
if ($validShareWith) {
|
|
|
|
return ['user', 'server'];
|
|
|
|
}
|
|
|
|
throw new HintException($expectedReturnData, $expectedReturnData);
|
|
|
|
}
|
|
|
|
);
|
2016-07-12 15:03:29 +03:00
|
|
|
|
2016-05-23 15:57:22 +03:00
|
|
|
$share = $this->share;
|
2021-04-19 10:08:32 +03:00
|
|
|
$share->setPermissions($permissions);
|
2016-07-12 15:03:29 +03:00
|
|
|
|
2016-05-23 15:57:22 +03:00
|
|
|
$this->shareManager->expects($this->any())->method('getShareByToken')
|
|
|
|
->with($token)
|
|
|
|
->willReturnCallback(
|
|
|
|
function ($token) use ($validToken, $share, $expectedReturnData) {
|
|
|
|
if ($validToken) {
|
|
|
|
return $share;
|
|
|
|
}
|
|
|
|
throw new HintException($expectedReturnData, $expectedReturnData);
|
|
|
|
}
|
|
|
|
);
|
2016-07-12 15:03:29 +03:00
|
|
|
|
2016-05-23 15:57:22 +03:00
|
|
|
$this->federatedShareProvider->expects($this->any())->method('create')
|
|
|
|
->with($share)
|
|
|
|
->willReturnCallback(
|
|
|
|
function (IShare $share) use ($createSuccessful, $shareWith, $expectedReturnData) {
|
|
|
|
$this->assertEquals($shareWith, $share->getSharedWith());
|
|
|
|
if ($createSuccessful) {
|
|
|
|
return $share;
|
|
|
|
}
|
|
|
|
throw new HintException($expectedReturnData, $expectedReturnData);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2016-07-14 12:01:25 +03:00
|
|
|
$result = $this->controller->createFederatedShare($shareWith, $token);
|
2016-05-23 15:57:22 +03:00
|
|
|
|
2016-07-14 21:41:10 +03:00
|
|
|
$errorCase = !$validShareWith || !$validToken || !$createSuccessful || !$outgoingSharesAllowed;
|
2016-05-23 15:57:22 +03:00
|
|
|
|
|
|
|
if ($errorCase) {
|
|
|
|
$this->assertSame(Http::STATUS_BAD_REQUEST, $result->getStatus());
|
|
|
|
$this->assertTrue(isset($result->getData()['message']));
|
|
|
|
$this->assertSame($expectedReturnData, $result->getData()['message']);
|
|
|
|
} else {
|
|
|
|
$this->assertSame(Http::STATUS_OK, $result->getStatus());
|
|
|
|
$this->assertTrue(isset($result->getData()['remoteUrl']));
|
|
|
|
$this->assertSame($expectedReturnData, $result->getData()['remoteUrl']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-14 12:01:25 +03:00
|
|
|
public function dataTestCreateFederatedShare() {
|
2016-05-23 15:57:22 +03:00
|
|
|
return [
|
2016-07-14 21:41:10 +03:00
|
|
|
//shareWith, outgoingSharesAllowed, validShareWith, token, validToken, createSuccessful, expectedReturnData
|
2021-04-19 10:08:32 +03:00
|
|
|
['user@server', true, true, 'token', true, true, 'server', 31],
|
|
|
|
['user@server', true, true, 'token', false, false, 'server', 4],
|
|
|
|
['user@server', true, false, 'token', true, true, 'invalid federated cloud id', 31],
|
|
|
|
['user@server', true, false, 'token', false, true, 'invalid federated cloud id', 31],
|
|
|
|
['user@server', true, false, 'token', false, false, 'invalid federated cloud id', 31],
|
|
|
|
['user@server', true, false, 'token', true, false, 'invalid federated cloud id', 31],
|
|
|
|
['user@server', true, true, 'token', false, true, 'invalid token', 31],
|
|
|
|
['user@server', true, true, 'token', false, false, 'invalid token', 31],
|
|
|
|
['user@server', true, true, 'token', true, false, 'can not create share', 31],
|
|
|
|
['user@server', false, true, 'token', true, true, 'This server doesn\'t support outgoing federated shares', 31],
|
2016-05-23 15:57:22 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|