123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984 |
- <?php
- /**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- *
- * @author Bjoern Schiessle <bjoern@schiessle.org>
- * @author Björn Schießle <bjoern@schiessle.org>
- * @author Christoph Wurst <christoph@winzerhof-wurst.at>
- * @author Daniel Calviño Sánchez <danxuliu@gmail.com>
- * @author Georg Ehrke <oc.list@georgehrke.com>
- * @author Joas Schilling <coding@schilljs.com>
- * @author John Molakvoæ <skjnldsv@protonmail.com>
- * @author Julius Härtl <jus@bitgrid.net>
- * @author Lukas Reschke <lukas@statuscode.ch>
- * @author Michael Weimann <mail@michael-weimann.eu>
- * @author Morris Jobke <hey@morrisjobke.de>
- * @author Robin Appelman <robin@icewind.nl>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- * @author Thomas Müller <thomas.mueller@tmit.eu>
- * @author Vincent Petry <vincent@nextcloud.com>
- *
- * @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,
- * along with this program. If not, see <http://www.gnu.org/licenses/>
- *
- */
- namespace OCA\Files_Sharing\Tests\Controllers;
- use OC\Files\Filesystem;
- use OC\Files\Node\Folder;
- use OCA\FederatedFileSharing\FederatedShareProvider;
- use OCA\Files_Sharing\Controller\ShareController;
- use OCP\Accounts\IAccount;
- use OCP\Accounts\IAccountManager;
- use OCP\Accounts\IAccountProperty;
- use OCP\AppFramework\Http\DataResponse;
- use OCP\AppFramework\Http\Template\ExternalShareMenuAction;
- use OCP\AppFramework\Http\Template\LinkMenuAction;
- use OCP\AppFramework\Http\Template\PublicTemplateResponse;
- use OCP\AppFramework\Http\Template\SimpleMenuAction;
- use OCP\Constants;
- use OCP\EventDispatcher\IEventDispatcher;
- use OCP\Files\File;
- use OCP\Files\NotFoundException;
- use OCP\Files\Storage;
- use OCP\IConfig;
- use OCP\IL10N;
- use OCP\IPreview;
- use OCP\IRequest;
- use OCP\ISession;
- use OCP\IURLGenerator;
- use OCP\IUser;
- use OCP\IUserManager;
- use OCP\Security\ISecureRandom;
- use OCP\Share\Exceptions\ShareNotFound;
- use OCP\Share\IShare;
- use PHPUnit\Framework\MockObject\MockObject;
- use OCP\Activity\IManager;
- use OCP\Files\IRootFolder;
- use OCP\Defaults;
- use OC\Share20\Manager;
- use OCA\Files_Sharing\DefaultPublicShareTemplateProvider;
- use OCP\Share\IPublicShareTemplateFactory;
- /**
- * @group DB
- *
- * @package OCA\Files_Sharing\Controllers
- */
- class ShareControllerTest extends \Test\TestCase {
- /** @var string */
- private $user;
- /** @var string */
- private $oldUser;
- /** @var string */
- private $appName = 'files_sharing';
- /** @var ShareController */
- private $shareController;
- /** @var IURLGenerator|MockObject */
- private $urlGenerator;
- /** @var ISession|MockObject */
- private $session;
- /** @var \OCP\IPreview|MockObject */
- private $previewManager;
- /** @var \OCP\IConfig|MockObject */
- private $config;
- /** @var \OC\Share20\Manager|MockObject */
- private $shareManager;
- /** @var IUserManager|MockObject */
- private $userManager;
- /** @var FederatedShareProvider|MockObject */
- private $federatedShareProvider;
- /** @var IAccountManager|MockObject */
- private $accountManager;
- /** @var IEventDispatcher|MockObject */
- private $eventDispatcher;
- /** @var IL10N */
- private $l10n;
- /** @var ISecureRandom */
- private $secureRandom;
- /** @var Defaults|MockObject */
- private $defaults;
- /** @var IPublicShareTemplateFactory|MockObject */
- private $publicShareTemplateFactory;
- protected function setUp(): void {
- parent::setUp();
- $this->appName = 'files_sharing';
- $this->shareManager = $this->createMock(Manager::class);
- $this->urlGenerator = $this->createMock(IURLGenerator::class);
- $this->session = $this->createMock(ISession::class);
- $this->previewManager = $this->createMock(IPreview::class);
- $this->config = $this->createMock(IConfig::class);
- $this->userManager = $this->createMock(IUserManager::class);
- $this->federatedShareProvider = $this->createMock(FederatedShareProvider::class);
- $this->federatedShareProvider->expects($this->any())
- ->method('isOutgoingServer2serverShareEnabled')->willReturn(true);
- $this->federatedShareProvider->expects($this->any())
- ->method('isIncomingServer2serverShareEnabled')->willReturn(true);
- $this->accountManager = $this->createMock(IAccountManager::class);
- $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
- $this->l10n = $this->createMock(IL10N::class);
- $this->secureRandom = $this->createMock(ISecureRandom::class);
- $this->defaults = $this->createMock(Defaults::class);
- $this->publicShareTemplateFactory = $this->createMock(IPublicShareTemplateFactory::class);
- $this->publicShareTemplateFactory
- ->expects($this->any())
- ->method('getProvider')
- ->willReturn(
- new DefaultPublicShareTemplateProvider(
- $this->userManager,
- $this->accountManager,
- $this->previewManager,
- $this->federatedShareProvider,
- $this->urlGenerator,
- $this->eventDispatcher,
- $this->l10n,
- $this->defaults,
- $this->config,
- $this->createMock(IRequest::class),
- )
- );
- $this->shareController = new \OCA\Files_Sharing\Controller\ShareController(
- $this->appName,
- $this->createMock(IRequest::class),
- $this->config,
- $this->urlGenerator,
- $this->userManager,
- $this->createMock(IManager::class),
- $this->shareManager,
- $this->session,
- $this->previewManager,
- $this->createMock(IRootFolder::class),
- $this->federatedShareProvider,
- $this->accountManager,
- $this->eventDispatcher,
- $this->l10n,
- $this->secureRandom,
- $this->defaults,
- $this->publicShareTemplateFactory,
- );
- // Store current user
- $this->oldUser = \OC_User::getUser();
- // Create a dummy user
- $this->user = \OC::$server->getSecureRandom()->generate(12, ISecureRandom::CHAR_LOWER);
- \OC::$server->getUserManager()->createUser($this->user, $this->user);
- \OC_Util::tearDownFS();
- $this->loginAsUser($this->user);
- }
- protected function tearDown(): void {
- \OC_Util::tearDownFS();
- \OC_User::setUserId('');
- Filesystem::tearDown();
- $user = \OC::$server->getUserManager()->get($this->user);
- if ($user !== null) {
- $user->delete();
- }
- \OC_User::setIncognitoMode(false);
- \OC::$server->getSession()->set('public_link_authenticated', '');
- // Set old user
- \OC_User::setUserId($this->oldUser);
- \OC_Util::setupFS($this->oldUser);
- parent::tearDown();
- }
- public function testShowShareInvalidToken() {
- $this->shareController->setToken('invalidtoken');
- $this->shareManager
- ->expects($this->once())
- ->method('getShareByToken')
- ->with('invalidtoken')
- ->will($this->throwException(new ShareNotFound()));
- $this->expectException(NotFoundException::class);
- // Test without a not existing token
- $this->shareController->showShare();
- }
- public function testShowShareNotAuthenticated() {
- $this->shareController->setToken('validtoken');
- $share = \OC::$server->getShareManager()->newShare();
- $share->setPassword('password');
- $this->shareManager
- ->expects($this->once())
- ->method('getShareByToken')
- ->with('validtoken')
- ->willReturn($share);
- $this->expectException(NotFoundException::class);
- // Test without a not existing token
- $this->shareController->showShare();
- }
- public function testShowShare() {
- $note = 'personal note';
- $filename = 'file1.txt';
- $this->shareController->setToken('token');
- $owner = $this->createMock(IUser::class);
- $owner->method('getDisplayName')->willReturn('ownerDisplay');
- $owner->method('getUID')->willReturn('ownerUID');
- $owner->method('isEnabled')->willReturn(true);
- $initiator = $this->createMock(IUser::class);
- $initiator->method('getDisplayName')->willReturn('initiatorDisplay');
- $initiator->method('getUID')->willReturn('initiatorUID');
- $initiator->method('isEnabled')->willReturn(true);
- $file = $this->createMock(File::class);
- $file->method('getName')->willReturn($filename);
- $file->method('getMimetype')->willReturn('text/plain');
- $file->method('getSize')->willReturn(33);
- $file->method('isReadable')->willReturn(true);
- $file->method('isShareable')->willReturn(true);
- $accountName = $this->createMock(IAccountProperty::class);
- $accountName->method('getScope')
- ->willReturn(IAccountManager::SCOPE_PUBLISHED);
- $account = $this->createMock(IAccount::class);
- $account->method('getProperty')
- ->with(IAccountManager::PROPERTY_DISPLAYNAME)
- ->willReturn($accountName);
- $this->accountManager->expects($this->once())
- ->method('getAccount')
- ->with($owner)
- ->willReturn($account);
- $share = \OC::$server->getShareManager()->newShare();
- $share->setId(42);
- $share->setPassword('password')
- ->setShareOwner('ownerUID')
- ->setSharedBy('initiatorUID')
- ->setNode($file)
- ->setNote($note)
- ->setTarget("/$filename");
- $this->session->method('exists')->with('public_link_authenticated')->willReturn(true);
- $this->session->method('get')->with('public_link_authenticated')->willReturn('42');
- $this->urlGenerator->expects($this->exactly(3))
- ->method('linkToRouteAbsolute')
- ->withConsecutive(
- ['files_sharing.sharecontroller.downloadShare', ['token' => 'token', 'filename' => $filename]],
- ['files_sharing.sharecontroller.showShare', ['token' => 'token']],
- ['files_sharing.PublicPreview.getPreview', ['token' => 'token', 'x' => 200, 'y' => 200, 'file' => '/'.$filename]],
- )->willReturnOnConsecutiveCalls(
- 'downloadURL',
- 'shareUrl',
- 'previewImage',
- );
- $this->previewManager->method('isMimeSupported')->with('text/plain')->willReturn(true);
- $this->config->method('getSystemValue')
- ->willReturnMap(
- [
- ['max_filesize_animated_gifs_public_sharing', 10, 10],
- ['enable_previews', true, true],
- ['preview_max_x', 1024, 1024],
- ['preview_max_y', 1024, 1024],
- ]
- );
- $shareTmpl['maxSizeAnimateGif'] = $this->config->getSystemValue('max_filesize_animated_gifs_public_sharing', 10);
- $shareTmpl['previewEnabled'] = $this->config->getSystemValue('enable_previews', true);
- $this->shareManager
- ->expects($this->once())
- ->method('getShareByToken')
- ->with('token')
- ->willReturn($share);
- $this->config
- ->expects($this->once())
- ->method('getAppValue')
- ->with('core', 'shareapi_public_link_disclaimertext', null)
- ->willReturn('My disclaimer text');
- $this->userManager->method('get')->willReturnCallback(function (string $uid) use ($owner, $initiator) {
- if ($uid === 'ownerUID') {
- return $owner;
- }
- if ($uid === 'initiatorUID') {
- return $initiator;
- }
- return null;
- });
- $this->eventDispatcher->expects($this->exactly(2))
- ->method('dispatchTyped')
- ->with(
- $this->callback(function ($event) use ($share) {
- return $event->getShare() === $share;
- })
- );
- $this->l10n->expects($this->any())
- ->method('t')
- ->willReturnCallback(function ($text, $parameters) {
- return vsprintf($text, $parameters);
- });
- $this->defaults->expects(self::any())
- ->method('getProductName')
- ->willReturn('Nextcloud');
- $response = $this->shareController->showShare();
- $sharedTmplParams = [
- 'owner' => 'ownerUID',
- 'filename' => $filename,
- 'directory_path' => "/$filename",
- 'mimetype' => 'text/plain',
- 'dirToken' => 'token',
- 'sharingToken' => 'token',
- 'server2serversharing' => true,
- 'protected' => 'true',
- 'dir' => '',
- 'downloadURL' => 'downloadURL',
- 'fileSize' => '33 B',
- 'nonHumanFileSize' => 33,
- 'maxSizeAnimateGif' => 10,
- 'previewSupported' => true,
- 'previewEnabled' => true,
- 'previewMaxX' => 1024,
- 'previewMaxY' => 1024,
- 'hideFileList' => false,
- 'shareOwner' => 'ownerDisplay',
- 'disclaimer' => 'My disclaimer text',
- 'shareUrl' => 'shareUrl',
- 'previewImage' => 'previewImage',
- 'previewURL' => 'downloadURL',
- 'note' => $note,
- 'hideDownload' => false,
- 'showgridview' => false
- ];
- $csp = new \OCP\AppFramework\Http\ContentSecurityPolicy();
- $csp->addAllowedFrameDomain('\'self\'');
- $expectedResponse = new PublicTemplateResponse($this->appName, 'public', $sharedTmplParams);
- $expectedResponse->setContentSecurityPolicy($csp);
- $expectedResponse->setHeaderTitle($sharedTmplParams['filename']);
- $expectedResponse->setHeaderDetails('shared by ' . $sharedTmplParams['shareOwner']);
- $expectedResponse->setHeaderActions([
- new SimpleMenuAction('download', $this->l10n->t('Download'), 'icon-download-white', $sharedTmplParams['downloadURL'], 0),
- new SimpleMenuAction('download', $this->l10n->t('Download'), 'icon-download', $sharedTmplParams['downloadURL'], 10, $sharedTmplParams['fileSize']),
- new LinkMenuAction($this->l10n->t('Direct link'), 'icon-public', $sharedTmplParams['previewURL']),
- new ExternalShareMenuAction($this->l10n->t('Add to your Nextcloud'), 'icon-external', $sharedTmplParams['owner'], $sharedTmplParams['shareOwner'], $sharedTmplParams['filename']),
- ]);
- $this->assertEquals($expectedResponse, $response);
- }
- public function testShowShareWithPrivateName() {
- $note = 'personal note';
- $filename = 'file1.txt';
- $this->shareController->setToken('token');
- $owner = $this->createMock(IUser::class);
- $owner->method('getDisplayName')->willReturn('ownerDisplay');
- $owner->method('getUID')->willReturn('ownerUID');
- $owner->method('isEnabled')->willReturn(true);
- $initiator = $this->createMock(IUser::class);
- $initiator->method('getDisplayName')->willReturn('initiatorDisplay');
- $initiator->method('getUID')->willReturn('initiatorUID');
- $initiator->method('isEnabled')->willReturn(true);
- $file = $this->createMock(File::class);
- $file->method('getName')->willReturn($filename);
- $file->method('getMimetype')->willReturn('text/plain');
- $file->method('getSize')->willReturn(33);
- $file->method('isReadable')->willReturn(true);
- $file->method('isShareable')->willReturn(true);
- $accountName = $this->createMock(IAccountProperty::class);
- $accountName->method('getScope')
- ->willReturn(IAccountManager::SCOPE_LOCAL);
- $account = $this->createMock(IAccount::class);
- $account->method('getProperty')
- ->with(IAccountManager::PROPERTY_DISPLAYNAME)
- ->willReturn($accountName);
- $this->accountManager->expects($this->once())
- ->method('getAccount')
- ->with($owner)
- ->willReturn($account);
- $share = \OC::$server->getShareManager()->newShare();
- $share->setId(42);
- $share->setPassword('password')
- ->setShareOwner('ownerUID')
- ->setSharedBy('initiatorUID')
- ->setNode($file)
- ->setNote($note)
- ->setTarget("/$filename");
- $this->session->method('exists')->with('public_link_authenticated')->willReturn(true);
- $this->session->method('get')->with('public_link_authenticated')->willReturn('42');
- $this->urlGenerator->expects($this->exactly(3))
- ->method('linkToRouteAbsolute')
- ->withConsecutive(
- ['files_sharing.sharecontroller.downloadShare', ['token' => 'token', 'filename' => $filename]],
- ['files_sharing.sharecontroller.showShare', ['token' => 'token']],
- ['files_sharing.PublicPreview.getPreview', ['token' => 'token', 'x' => 200, 'y' => 200, 'file' => '/'.$filename]],
- )->willReturnOnConsecutiveCalls(
- 'downloadURL',
- 'shareUrl',
- 'previewImage',
- );
- $this->previewManager->method('isMimeSupported')->with('text/plain')->willReturn(true);
- $this->config->method('getSystemValue')
- ->willReturnMap(
- [
- ['max_filesize_animated_gifs_public_sharing', 10, 10],
- ['enable_previews', true, true],
- ['preview_max_x', 1024, 1024],
- ['preview_max_y', 1024, 1024],
- ]
- );
- $shareTmpl['maxSizeAnimateGif'] = $this->config->getSystemValue('max_filesize_animated_gifs_public_sharing', 10);
- $shareTmpl['previewEnabled'] = $this->config->getSystemValue('enable_previews', true);
- $this->shareManager
- ->expects($this->once())
- ->method('getShareByToken')
- ->with('token')
- ->willReturn($share);
- $this->config
- ->expects($this->once())
- ->method('getAppValue')
- ->with('core', 'shareapi_public_link_disclaimertext', null)
- ->willReturn('My disclaimer text');
- $this->userManager->method('get')->willReturnCallback(function (string $uid) use ($owner, $initiator) {
- if ($uid === 'ownerUID') {
- return $owner;
- }
- if ($uid === 'initiatorUID') {
- return $initiator;
- }
- return null;
- });
- $this->eventDispatcher->expects($this->exactly(2))
- ->method('dispatchTyped')
- ->with(
- $this->callback(function ($event) use ($share) {
- return $event->getShare() === $share;
- })
- );
- $this->l10n->expects($this->any())
- ->method('t')
- ->will($this->returnCallback(function ($text, $parameters) {
- return vsprintf($text, $parameters);
- }));
- $this->defaults->expects(self::any())
- ->method('getProductName')
- ->willReturn('Nextcloud');
- $response = $this->shareController->showShare();
- $sharedTmplParams = [
- 'owner' => '',
- 'filename' => $filename,
- 'directory_path' => "/$filename",
- 'mimetype' => 'text/plain',
- 'dirToken' => 'token',
- 'sharingToken' => 'token',
- 'server2serversharing' => true,
- 'protected' => 'true',
- 'dir' => '',
- 'downloadURL' => 'downloadURL',
- 'fileSize' => '33 B',
- 'nonHumanFileSize' => 33,
- 'maxSizeAnimateGif' => 10,
- 'previewSupported' => true,
- 'previewEnabled' => true,
- 'previewMaxX' => 1024,
- 'previewMaxY' => 1024,
- 'hideFileList' => false,
- 'shareOwner' => '',
- 'disclaimer' => 'My disclaimer text',
- 'shareUrl' => 'shareUrl',
- 'previewImage' => 'previewImage',
- 'previewURL' => 'downloadURL',
- 'note' => $note,
- 'hideDownload' => false,
- 'showgridview' => false
- ];
- $csp = new \OCP\AppFramework\Http\ContentSecurityPolicy();
- $csp->addAllowedFrameDomain('\'self\'');
- $expectedResponse = new PublicTemplateResponse($this->appName, 'public', $sharedTmplParams);
- $expectedResponse->setContentSecurityPolicy($csp);
- $expectedResponse->setHeaderTitle($sharedTmplParams['filename']);
- $expectedResponse->setHeaderDetails('');
- $expectedResponse->setHeaderActions([
- new SimpleMenuAction('download', $this->l10n->t('Download'), 'icon-download-white', $sharedTmplParams['downloadURL'], 0),
- new SimpleMenuAction('download', $this->l10n->t('Download'), 'icon-download', $sharedTmplParams['downloadURL'], 10, $sharedTmplParams['fileSize']),
- new LinkMenuAction($this->l10n->t('Direct link'), 'icon-public', $sharedTmplParams['previewURL']),
- new ExternalShareMenuAction($this->l10n->t('Add to your Nextcloud'), 'icon-external', $sharedTmplParams['owner'], $sharedTmplParams['shareOwner'], $sharedTmplParams['filename']),
- ]);
- $this->assertEquals($expectedResponse, $response);
- }
- public function testShowShareHideDownload() {
- $note = 'personal note';
- $filename = 'file1.txt';
- $this->shareController->setToken('token');
- $owner = $this->getMockBuilder(IUser::class)->getMock();
- $owner->method('getDisplayName')->willReturn('ownerDisplay');
- $owner->method('getUID')->willReturn('ownerUID');
- $owner->method('isEnabled')->willReturn(true);
- $initiator = $this->createMock(IUser::class);
- $initiator->method('getDisplayName')->willReturn('initiatorDisplay');
- $initiator->method('getUID')->willReturn('initiatorUID');
- $initiator->method('isEnabled')->willReturn(true);
- $file = $this->getMockBuilder('OCP\Files\File')->getMock();
- $file->method('getName')->willReturn($filename);
- $file->method('getMimetype')->willReturn('text/plain');
- $file->method('getSize')->willReturn(33);
- $file->method('isReadable')->willReturn(true);
- $file->method('isShareable')->willReturn(true);
- $accountName = $this->createMock(IAccountProperty::class);
- $accountName->method('getScope')
- ->willReturn(IAccountManager::SCOPE_PUBLISHED);
- $account = $this->createMock(IAccount::class);
- $account->method('getProperty')
- ->with(IAccountManager::PROPERTY_DISPLAYNAME)
- ->willReturn($accountName);
- $this->accountManager->expects($this->once())
- ->method('getAccount')
- ->with($owner)
- ->willReturn($account);
- $share = \OC::$server->getShareManager()->newShare();
- $share->setId(42);
- $share->setPassword('password')
- ->setShareOwner('ownerUID')
- ->setSharedBy('initiatorUID')
- ->setNode($file)
- ->setNote($note)
- ->setTarget("/$filename")
- ->setHideDownload(true);
- $this->session->method('exists')->with('public_link_authenticated')->willReturn(true);
- $this->session->method('get')->with('public_link_authenticated')->willReturn('42');
- // Even if downloads are disabled the "downloadURL" parameter is
- // provided to the template, as it is needed to preview audio and GIF
- // files.
- $this->urlGenerator->expects($this->exactly(3))
- ->method('linkToRouteAbsolute')
- ->withConsecutive(
- ['files_sharing.sharecontroller.downloadShare', ['token' => 'token', 'filename' => $filename]],
- ['files_sharing.sharecontroller.showShare', ['token' => 'token']],
- ['files_sharing.PublicPreview.getPreview', ['token' => 'token', 'x' => 200, 'y' => 200, 'file' => '/'.$filename]],
- )->willReturnOnConsecutiveCalls(
- 'downloadURL',
- 'shareUrl',
- 'previewImage',
- );
- $this->previewManager->method('isMimeSupported')->with('text/plain')->willReturn(true);
- $this->config->method('getSystemValue')
- ->willReturnMap(
- [
- ['max_filesize_animated_gifs_public_sharing', 10, 10],
- ['enable_previews', true, true],
- ['preview_max_x', 1024, 1024],
- ['preview_max_y', 1024, 1024],
- ]
- );
- $shareTmpl['maxSizeAnimateGif'] = $this->config->getSystemValue('max_filesize_animated_gifs_public_sharing', 10);
- $shareTmpl['previewEnabled'] = $this->config->getSystemValue('enable_previews', true);
- $this->shareManager
- ->expects($this->once())
- ->method('getShareByToken')
- ->with('token')
- ->willReturn($share);
- $this->config
- ->expects($this->once())
- ->method('getAppValue')
- ->with('core', 'shareapi_public_link_disclaimertext', null)
- ->willReturn('My disclaimer text');
- $this->userManager->method('get')->willReturnCallback(function (string $uid) use ($owner, $initiator) {
- if ($uid === 'ownerUID') {
- return $owner;
- }
- if ($uid === 'initiatorUID') {
- return $initiator;
- }
- return null;
- });
- $this->eventDispatcher->expects($this->exactly(2))
- ->method('dispatchTyped')
- ->with(
- $this->callback(function ($event) use ($share) {
- return $event->getShare() === $share;
- })
- );
- $this->l10n->expects($this->any())
- ->method('t')
- ->willReturnCallback(function ($text, $parameters) {
- return vsprintf($text, $parameters);
- });
- $response = $this->shareController->showShare();
- $sharedTmplParams = [
- 'owner' => 'ownerUID',
- 'filename' => $filename,
- 'directory_path' => "/$filename",
- 'mimetype' => 'text/plain',
- 'dirToken' => 'token',
- 'sharingToken' => 'token',
- 'server2serversharing' => true,
- 'protected' => 'true',
- 'dir' => '',
- 'downloadURL' => 'downloadURL',
- 'fileSize' => '33 B',
- 'nonHumanFileSize' => 33,
- 'maxSizeAnimateGif' => 10,
- 'previewSupported' => true,
- 'previewEnabled' => true,
- 'previewMaxX' => 1024,
- 'previewMaxY' => 1024,
- 'hideFileList' => false,
- 'shareOwner' => 'ownerDisplay',
- 'disclaimer' => 'My disclaimer text',
- 'shareUrl' => 'shareUrl',
- 'previewImage' => 'previewImage',
- 'previewURL' => 'downloadURL',
- 'note' => $note,
- 'hideDownload' => true,
- 'showgridview' => false
- ];
- $csp = new \OCP\AppFramework\Http\ContentSecurityPolicy();
- $csp->addAllowedFrameDomain('\'self\'');
- $expectedResponse = new PublicTemplateResponse($this->appName, 'public', $sharedTmplParams);
- $expectedResponse->setContentSecurityPolicy($csp);
- $expectedResponse->setHeaderTitle($sharedTmplParams['filename']);
- $expectedResponse->setHeaderDetails('shared by ' . $sharedTmplParams['shareOwner']);
- $expectedResponse->setHeaderActions([]);
- $this->assertEquals($expectedResponse, $response);
- }
- /**
- * Checks file drop shares:
- * - there must not be any header action
- * - the template param "hideFileList" should be true
- *
- * @test
- * @return void
- */
- public function testShareFileDrop() {
- $this->shareController->setToken('token');
- $owner = $this->getMockBuilder(IUser::class)->getMock();
- $owner->method('getDisplayName')->willReturn('ownerDisplay');
- $owner->method('getUID')->willReturn('ownerUID');
- $owner->method('isEnabled')->willReturn(true);
- $initiator = $this->createMock(IUser::class);
- $initiator->method('getDisplayName')->willReturn('initiatorDisplay');
- $initiator->method('getUID')->willReturn('initiatorUID');
- $initiator->method('isEnabled')->willReturn(true);
- /* @var MockObject|Storage $storage */
- $storage = $this->getMockBuilder(Storage::class)
- ->disableOriginalConstructor()
- ->getMock();
- /* @var MockObject|Folder $folder */
- $folder = $this->getMockBuilder(Folder::class)
- ->disableOriginalConstructor()
- ->getMock();
- $folder->method('getName')->willReturn('/fileDrop');
- $folder->method('isReadable')->willReturn(true);
- $folder->method('isShareable')->willReturn(true);
- $folder->method('getStorage')->willReturn($storage);
- $folder->method('get')->with('')->willReturn($folder);
- $folder->method('getSize')->willReturn(1337);
- $accountName = $this->createMock(IAccountProperty::class);
- $accountName->method('getScope')
- ->willReturn(IAccountManager::SCOPE_PUBLISHED);
- $account = $this->createMock(IAccount::class);
- $account->method('getProperty')
- ->with(IAccountManager::PROPERTY_DISPLAYNAME)
- ->willReturn($accountName);
- $this->accountManager->expects($this->once())
- ->method('getAccount')
- ->with($owner)
- ->willReturn($account);
- $share = \OC::$server->getShareManager()->newShare();
- $share->setId(42);
- $share->setPermissions(Constants::PERMISSION_CREATE)
- ->setShareOwner('ownerUID')
- ->setSharedBy('initiatorUID')
- ->setNode($folder)
- ->setTarget('/fileDrop');
- $this->shareManager
- ->expects($this->once())
- ->method('getShareByToken')
- ->with('token')
- ->willReturn($share);
- $this->userManager->method('get')->willReturnCallback(function (string $uid) use ($owner, $initiator) {
- if ($uid === 'ownerUID') {
- return $owner;
- }
- if ($uid === 'initiatorUID') {
- return $initiator;
- }
- return null;
- });
- $this->l10n->expects($this->any())
- ->method('t')
- ->willReturnCallback(function ($text, $parameters) {
- return vsprintf($text, $parameters);
- });
- $response = $this->shareController->showShare();
- // skip the "folder" param for tests
- $responseParams = $response->getParams();
- unset($responseParams['folder']);
- $response->setParams($responseParams);
- $sharedTmplParams = [
- 'owner' => 'ownerUID',
- 'filename' => '/fileDrop',
- 'directory_path' => '/fileDrop',
- 'mimetype' => null,
- 'dirToken' => 'token',
- 'sharingToken' => 'token',
- 'server2serversharing' => true,
- 'protected' => 'false',
- 'dir' => null,
- 'downloadURL' => '',
- 'fileSize' => '1 KB',
- 'nonHumanFileSize' => 1337,
- 'maxSizeAnimateGif' => null,
- 'previewSupported' => null,
- 'previewEnabled' => null,
- 'previewMaxX' => null,
- 'previewMaxY' => null,
- 'hideFileList' => true,
- 'shareOwner' => 'ownerDisplay',
- 'disclaimer' => null,
- 'shareUrl' => '',
- 'previewImage' => '',
- 'previewURL' => '',
- 'note' => '',
- 'hideDownload' => false,
- 'showgridview' => false
- ];
- $csp = new \OCP\AppFramework\Http\ContentSecurityPolicy();
- $csp->addAllowedFrameDomain('\'self\'');
- $expectedResponse = new PublicTemplateResponse($this->appName, 'public', $sharedTmplParams);
- $expectedResponse->setContentSecurityPolicy($csp);
- $expectedResponse->setHeaderTitle($sharedTmplParams['filename']);
- $expectedResponse->setHeaderDetails('shared by ' . $sharedTmplParams['shareOwner']);
- self::assertEquals($expectedResponse, $response);
- }
- public function testShowShareInvalid() {
- $this->expectException(\OCP\Files\NotFoundException::class);
- $filename = 'file1.txt';
- $this->shareController->setToken('token');
- $owner = $this->getMockBuilder(IUser::class)->getMock();
- $owner->method('getDisplayName')->willReturn('ownerDisplay');
- $owner->method('getUID')->willReturn('ownerUID');
- $file = $this->getMockBuilder('OCP\Files\File')->getMock();
- $file->method('getName')->willReturn($filename);
- $file->method('getMimetype')->willReturn('text/plain');
- $file->method('getSize')->willReturn(33);
- $file->method('isShareable')->willReturn(false);
- $file->method('isReadable')->willReturn(true);
- $share = \OC::$server->getShareManager()->newShare();
- $share->setId(42);
- $share->setPassword('password')
- ->setShareOwner('ownerUID')
- ->setNode($file)
- ->setTarget("/$filename");
- $this->session->method('exists')->with('public_link_authenticated')->willReturn(true);
- $this->session->method('get')->with('public_link_authenticated')->willReturn('42');
- $this->previewManager->method('isMimeSupported')->with('text/plain')->willReturn(true);
- $this->config->method('getSystemValue')
- ->willReturnMap(
- [
- ['max_filesize_animated_gifs_public_sharing', 10, 10],
- ['enable_previews', true, true],
- ]
- );
- $shareTmpl['maxSizeAnimateGif'] = $this->config->getSystemValue('max_filesize_animated_gifs_public_sharing', 10);
- $shareTmpl['previewEnabled'] = $this->config->getSystemValue('enable_previews', true);
- $this->shareManager
- ->expects($this->once())
- ->method('getShareByToken')
- ->with('token')
- ->willReturn($share);
- $this->userManager->method('get')->with('ownerUID')->willReturn($owner);
- $this->shareController->showShare();
- }
- public function testDownloadShareWithCreateOnlyShare() {
- $share = $this->getMockBuilder(IShare::class)->getMock();
- $share->method('getPassword')->willReturn('password');
- $share
- ->expects($this->once())
- ->method('getPermissions')
- ->willReturn(\OCP\Constants::PERMISSION_CREATE);
- $this->shareManager
- ->expects($this->once())
- ->method('getShareByToken')
- ->with('validtoken')
- ->willReturn($share);
- // Test with a password protected share and no authentication
- $response = $this->shareController->downloadShare('validtoken');
- $expectedResponse = new DataResponse('Share has no read permission');
- $this->assertEquals($expectedResponse, $response);
- }
- public function testDisabledOwner() {
- $this->shareController->setToken('token');
- $owner = $this->getMockBuilder(IUser::class)->getMock();
- $owner->method('isEnabled')->willReturn(false);
- $initiator = $this->createMock(IUser::class);
- $initiator->method('isEnabled')->willReturn(false);
- /* @var MockObject|Folder $folder */
- $folder = $this->createMock(Folder::class);
- $share = \OC::$server->getShareManager()->newShare();
- $share->setId(42);
- $share->setPermissions(Constants::PERMISSION_CREATE)
- ->setShareOwner('ownerUID')
- ->setSharedBy('initiatorUID')
- ->setNode($folder)
- ->setTarget('/share');
- $this->shareManager
- ->expects($this->once())
- ->method('getShareByToken')
- ->with('token')
- ->willReturn($share);
- $this->userManager->method('get')->willReturnCallback(function (string $uid) use ($owner, $initiator) {
- if ($uid === 'ownerUID') {
- return $owner;
- }
- if ($uid === 'initiatorUID') {
- return $initiator;
- }
- return null;
- });
- $this->expectException(NotFoundException::class);
- $this->shareController->showShare();
- }
- public function testDisabledInitiator() {
- $this->shareController->setToken('token');
- $owner = $this->getMockBuilder(IUser::class)->getMock();
- $owner->method('isEnabled')->willReturn(false);
- $initiator = $this->createMock(IUser::class);
- $initiator->method('isEnabled')->willReturn(true);
- /* @var MockObject|Folder $folder */
- $folder = $this->createMock(Folder::class);
- $share = \OC::$server->getShareManager()->newShare();
- $share->setId(42);
- $share->setPermissions(Constants::PERMISSION_CREATE)
- ->setShareOwner('ownerUID')
- ->setSharedBy('initiatorUID')
- ->setNode($folder)
- ->setTarget('/share');
- $this->shareManager
- ->expects($this->once())
- ->method('getShareByToken')
- ->with('token')
- ->willReturn($share);
- $this->userManager->method('get')->willReturnCallback(function (string $uid) use ($owner, $initiator) {
- if ($uid === 'ownerUID') {
- return $owner;
- }
- if ($uid === 'initiatorUID') {
- return $initiator;
- }
- return null;
- });
- $this->expectException(NotFoundException::class);
- $this->shareController->showShare();
- }
- }
|