2014-10-15 13:58:44 +04:00
< ? php
/**
2016-07-21 17:49:16 +03:00
* @ copyright Copyright ( c ) 2016 , ownCloud , Inc .
*
* @ author Bjoern Schiessle < bjoern @ schiessle . org >
2016-05-26 20:56:05 +03:00
* @ author Björn Schießle < bjoern @ schiessle . org >
2020-03-31 11:49:10 +03:00
* @ author Christoph Wurst < christoph @ winzerhof - wurst . at >
2019-12-03 21:57:53 +03:00
* @ author Daniel Calviño Sánchez < danxuliu @ gmail . com >
2017-11-06 22:15:27 +03:00
* @ author Georg Ehrke < oc . list @ georgehrke . com >
2016-07-21 17:49:16 +03:00
* @ author Joas Schilling < coding @ schilljs . com >
2019-12-03 21:57:53 +03:00
* @ author John Molakvoæ ( skjnldsv ) < skjnldsv @ protonmail . com >
* @ author Julius Härtl < jus @ bitgrid . net >
2016-05-26 20:56:05 +03:00
* @ author Lukas Reschke < lukas @ statuscode . ch >
2019-12-03 21:57:53 +03:00
* @ author Michael Weimann < mail @ michael - weimann . eu >
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 >
2016-07-21 17:49:16 +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-03-26 13:44:34 +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 />
2014-10-15 13:58:44 +04:00
*
*/
2015-02-26 13:37:37 +03:00
2016-05-17 12:42:03 +03:00
namespace OCA\Files_Sharing\Tests\Controllers ;
2014-10-15 13:58:44 +04:00
use OC\Files\Filesystem ;
2018-10-03 01:06:12 +03:00
use OC\Files\Node\Folder ;
2016-04-18 19:17:08 +03:00
use OCA\FederatedFileSharing\FederatedShareProvider ;
2016-10-24 12:46:25 +03:00
use OCA\Files_Sharing\Controller\ShareController ;
2020-04-01 17:02:34 +03:00
use OCP\Accounts\IAccount ;
use OCP\Accounts\IAccountManager ;
use OCP\Accounts\IAccountProperty ;
2016-06-09 19:29:13 +03:00
use OCP\AppFramework\Http\DataResponse ;
2018-04-05 16:40:58 +03:00
use OCP\AppFramework\Http\Template\ExternalShareMenuAction ;
use OCP\AppFramework\Http\Template\LinkMenuAction ;
2018-02-09 13:34:34 +03:00
use OCP\AppFramework\Http\Template\PublicTemplateResponse ;
use OCP\AppFramework\Http\Template\SimpleMenuAction ;
2018-10-03 01:06:12 +03:00
use OCP\Constants ;
2019-09-09 23:33:03 +03:00
use OCP\Files\File ;
2018-06-14 11:20:10 +03:00
use OCP\Files\NotFoundException ;
2018-10-03 01:06:12 +03:00
use OCP\Files\Storage ;
2017-10-24 16:26:53 +03:00
use OCP\IConfig ;
use OCP\IL10N ;
use OCP\ILogger ;
use OCP\IPreview ;
use OCP\IRequest ;
2016-01-15 11:41:51 +03:00
use OCP\ISession ;
2019-11-22 22:52:10 +03:00
use OCP\IURLGenerator ;
use OCP\IUser ;
2016-02-03 10:25:57 +03:00
use OCP\IUserManager ;
2014-10-15 13:58:44 +04:00
use OCP\Security\ISecureRandom ;
2019-11-22 22:52:10 +03:00
use OCP\Share\Exceptions\ShareNotFound ;
2017-10-25 01:03:28 +03:00
use OCP\Share\IShare ;
2018-10-03 01:06:12 +03:00
use PHPUnit\Framework\MockObject\MockObject ;
2016-07-15 16:01:14 +03:00
use Symfony\Component\EventDispatcher\EventDispatcherInterface ;
2020-04-01 17:02:34 +03:00
use OCP\Activity\IManager ;
use OCP\Files\IRootFolder ;
use OCP\Defaults ;
use OC\Share20\Manager ;
2014-10-15 13:58:44 +04:00
/**
2015-11-20 13:27:11 +03:00
* @ group DB
*
2014-10-15 13:58:44 +04:00
* @ package OCA\Files_Sharing\Controllers
*/
2015-02-25 14:52:16 +03:00
class ShareControllerTest extends \Test\TestCase {
2014-10-15 13:58:44 +04:00
/** @var string */
private $user ;
/** @var string */
private $oldUser ;
2016-01-20 12:14:03 +03:00
/** @var string */
private $appName = 'files_sharing' ;
2014-10-15 13:58:44 +04:00
/** @var ShareController */
private $shareController ;
2020-04-01 17:02:34 +03:00
/** @var IURLGenerator|MockObject */
2014-10-15 13:58:44 +04:00
private $urlGenerator ;
2020-04-01 17:02:34 +03:00
/** @var ISession|MockObject */
2016-01-15 11:41:51 +03:00
private $session ;
2020-04-01 17:02:34 +03:00
/** @var \OCP\IPreview|MockObject */
2016-01-20 12:14:03 +03:00
private $previewManager ;
2020-04-01 17:02:34 +03:00
/** @var \OCP\IConfig|MockObject */
2016-01-20 12:14:03 +03:00
private $config ;
2020-04-01 17:02:34 +03:00
/** @var \OC\Share20\Manager|MockObject */
2016-01-15 11:41:51 +03:00
private $shareManager ;
2020-04-01 17:02:34 +03:00
/** @var IUserManager|MockObject */
2016-02-03 10:25:57 +03:00
private $userManager ;
2020-04-01 17:02:34 +03:00
/** @var FederatedShareProvider|MockObject */
2016-04-18 19:17:08 +03:00
private $federatedShareProvider ;
2020-04-01 17:02:34 +03:00
/** @var IAccountManager|MockObject */
private $accountManager ;
/** @var EventDispatcherInterface|MockObject */
2016-07-15 16:01:14 +03:00
private $eventDispatcher ;
2018-02-09 13:34:34 +03:00
/** @var IL10N */
private $l10n ;
2014-10-15 13:58:44 +04:00
2019-11-21 18:40:38 +03:00
protected function setUp () : void {
2016-05-17 12:42:03 +03:00
parent :: setUp ();
2016-01-20 12:14:03 +03:00
$this -> appName = 'files_sharing' ;
2020-04-01 17:02:34 +03:00
$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 );
2016-04-18 19:17:08 +03:00
$this -> federatedShareProvider -> expects ( $this -> any ())
-> method ( 'isOutgoingServer2serverShareEnabled' ) -> willReturn ( true );
$this -> federatedShareProvider -> expects ( $this -> any ())
-> method ( 'isIncomingServer2serverShareEnabled' ) -> willReturn ( true );
2020-04-01 17:02:34 +03:00
$this -> accountManager = $this -> createMock ( IAccountManager :: class );
$this -> eventDispatcher = $this -> createMock ( 'Symfony\Component\EventDispatcher\EventDispatcherInterface' );
2018-02-09 13:34:34 +03:00
$this -> l10n = $this -> createMock ( IL10N :: class );
2016-01-20 12:14:03 +03:00
2016-10-24 12:46:25 +03:00
$this -> shareController = new \OCA\Files_Sharing\Controller\ShareController (
2016-01-20 12:14:03 +03:00
$this -> appName ,
2020-04-01 17:02:34 +03:00
$this -> createMock ( IRequest :: class ),
2016-01-20 12:14:03 +03:00
$this -> config ,
$this -> urlGenerator ,
2016-02-03 10:25:57 +03:00
$this -> userManager ,
2020-04-01 17:02:34 +03:00
$this -> createMock ( ILogger :: class ),
$this -> createMock ( IManager :: class ),
2016-01-20 12:14:03 +03:00
$this -> shareManager ,
$this -> session ,
$this -> previewManager ,
2020-04-01 17:02:34 +03:00
$this -> createMock ( IRootFolder :: class ),
2016-07-15 16:01:14 +03:00
$this -> federatedShareProvider ,
2020-04-01 17:02:34 +03:00
$this -> accountManager ,
2016-07-22 17:13:26 +03:00
$this -> eventDispatcher ,
2018-02-09 13:34:34 +03:00
$this -> l10n ,
2020-04-01 17:02:34 +03:00
$this -> createMock ( Defaults :: class )
2016-01-20 12:14:03 +03:00
);
2014-10-15 13:58:44 +04:00
// Store current user
$this -> oldUser = \OC_User :: getUser ();
// Create a dummy user
2016-01-11 21:59:15 +03:00
$this -> user = \OC :: $server -> getSecureRandom () -> generate ( 12 , ISecureRandom :: CHAR_LOWER );
2014-10-15 13:58:44 +04:00
2015-12-17 17:10:11 +03:00
\OC :: $server -> getUserManager () -> createUser ( $this -> user , $this -> user );
2014-10-15 13:58:44 +04:00
\OC_Util :: tearDownFS ();
2015-05-11 16:22:05 +03:00
$this -> loginAsUser ( $this -> user );
2014-10-15 13:58:44 +04:00
}
2019-11-21 18:40:38 +03:00
protected function tearDown () : void {
2014-10-15 13:58:44 +04:00
\OC_Util :: tearDownFS ();
\OC_User :: setUserId ( '' );
Filesystem :: tearDown ();
2015-12-17 17:59:23 +03:00
$user = \OC :: $server -> getUserManager () -> get ( $this -> user );
2020-04-10 15:19:56 +03:00
if ( $user !== null ) {
$user -> delete ();
}
2014-10-15 13:58:44 +04:00
\OC_User :: setIncognitoMode ( false );
\OC :: $server -> getSession () -> set ( 'public_link_authenticated' , '' );
// Set old user
\OC_User :: setUserId ( $this -> oldUser );
\OC_Util :: setupFS ( $this -> oldUser );
2016-05-17 12:42:03 +03:00
parent :: tearDown ();
2014-10-15 13:58:44 +04:00
}
2016-01-15 11:41:51 +03:00
public function testShowShareInvalidToken () {
2018-06-14 11:20:10 +03:00
$this -> shareController -> setToken ( 'invalidtoken' );
2016-01-15 11:41:51 +03:00
$this -> shareManager
-> expects ( $this -> once ())
-> method ( 'getShareByToken' )
-> with ( 'invalidtoken' )
-> will ( $this -> throwException ( new ShareNotFound ()));
2015-03-24 13:21:58 +03:00
2018-06-14 11:20:10 +03:00
$this -> expectException ( NotFoundException :: class );
2014-10-15 13:58:44 +04:00
// Test without a not existing token
2018-06-14 11:20:10 +03:00
$this -> shareController -> showShare ();
2016-01-15 11:41:51 +03:00
}
2014-10-15 13:58:44 +04:00
2016-01-15 11:41:51 +03:00
public function testShowShareNotAuthenticated () {
2018-06-14 11:20:10 +03:00
$this -> shareController -> setToken ( 'validtoken' );
2016-02-03 10:25:57 +03:00
$share = \OC :: $server -> getShareManager () -> newShare ();
$share -> setPassword ( 'password' );
2016-01-15 11:41:51 +03:00
$this -> shareManager
-> expects ( $this -> once ())
-> method ( 'getShareByToken' )
-> with ( 'validtoken' )
-> willReturn ( $share );
2018-06-14 11:20:10 +03:00
$this -> expectException ( NotFoundException :: class );
2016-01-20 12:14:03 +03:00
2016-01-15 11:41:51 +03:00
// Test without a not existing token
2018-06-14 11:20:10 +03:00
$this -> shareController -> showShare ();
2016-01-15 11:41:51 +03:00
}
2014-10-15 13:58:44 +04:00
2016-01-15 11:41:51 +03:00
public function testShowShare () {
2018-07-13 18:34:32 +03:00
$note = 'personal note' ;
2018-06-14 11:20:10 +03:00
$this -> shareController -> setToken ( 'token' );
2019-09-09 23:33:03 +03:00
$owner = $this -> createMock ( IUser :: class );
2016-01-15 11:41:51 +03:00
$owner -> method ( 'getDisplayName' ) -> willReturn ( 'ownerDisplay' );
$owner -> method ( 'getUID' ) -> willReturn ( 'ownerUID' );
2019-09-09 23:33:03 +03:00
$owner -> method ( 'isEnabled' ) -> willReturn ( true );
2016-01-15 11:41:51 +03:00
2019-09-09 23:33:03 +03:00
$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 );
2016-01-15 11:41:51 +03:00
$file -> method ( 'getName' ) -> willReturn ( 'file1.txt' );
$file -> method ( 'getMimetype' ) -> willReturn ( 'text/plain' );
$file -> method ( 'getSize' ) -> willReturn ( 33 );
2016-02-09 16:56:47 +03:00
$file -> method ( 'isReadable' ) -> willReturn ( true );
$file -> method ( 'isShareable' ) -> willReturn ( true );
2016-01-15 11:41:51 +03:00
2020-04-01 17:02:34 +03:00
$accountName = $this -> createMock ( IAccountProperty :: class );
$accountName -> method ( 'getScope' )
-> willReturn ( IAccountManager :: VISIBILITY_PUBLIC );
$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 );
2016-02-03 10:25:57 +03:00
$share = \OC :: $server -> getShareManager () -> newShare ();
$share -> setId ( 42 );
$share -> setPassword ( 'password' )
-> setShareOwner ( 'ownerUID' )
2019-09-09 23:33:03 +03:00
-> setSharedBy ( 'initiatorUID' )
2016-02-03 10:25:57 +03:00
-> setNode ( $file )
2018-07-13 18:34:32 +03:00
-> setNote ( $note )
2016-02-03 10:25:57 +03:00
-> setTarget ( '/file1.txt' );
2016-01-15 11:41:51 +03:00
$this -> session -> method ( 'exists' ) -> with ( 'public_link_authenticated' ) -> willReturn ( true );
$this -> session -> method ( 'get' ) -> with ( 'public_link_authenticated' ) -> willReturn ( '42' );
2018-02-09 13:34:34 +03:00
$this -> urlGenerator -> expects ( $this -> at ( 0 ))
-> method ( 'linkToRouteAbsolute' )
-> with ( 'files_sharing.sharecontroller.downloadShare' , [ 'token' => 'token' ])
-> willReturn ( 'downloadURL' );
2016-01-20 12:14:03 +03:00
$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 ],
2016-05-25 11:21:07 +03:00
[ 'preview_max_x' , 1024 , 1024 ],
[ 'preview_max_y' , 1024 , 1024 ],
2016-01-20 12:14:03 +03:00
]
);
$shareTmpl [ 'maxSizeAnimateGif' ] = $this -> config -> getSystemValue ( 'max_filesize_animated_gifs_public_sharing' , 10 );
$shareTmpl [ 'previewEnabled' ] = $this -> config -> getSystemValue ( 'enable_previews' , true );
2016-01-15 11:41:51 +03:00
$this -> shareManager
-> expects ( $this -> once ())
-> method ( 'getShareByToken' )
-> with ( 'token' )
-> willReturn ( $share );
2016-09-08 21:34:04 +03:00
$this -> config
-> expects ( $this -> once ())
-> method ( 'getAppValue' )
-> with ( 'core' , 'shareapi_public_link_disclaimertext' , null )
-> willReturn ( 'My disclaimer text' );
2016-01-15 11:41:51 +03:00
2020-04-09 14:53:40 +03:00
$this -> userManager -> method ( 'get' ) -> willReturnCallback ( function ( string $uid ) use ( $owner , $initiator ) {
2019-09-09 23:33:03 +03:00
if ( $uid === 'ownerUID' ) {
return $owner ;
}
if ( $uid === 'initiatorUID' ) {
return $initiator ;
}
return null ;
});
2016-02-03 10:25:57 +03:00
2016-07-15 16:01:14 +03:00
$this -> eventDispatcher -> expects ( $this -> once ())
-> method ( 'dispatch' )
2019-10-17 11:27:13 +03:00
-> with (
'OCA\Files_Sharing::loadAdditionalScripts' ,
2020-04-09 14:53:40 +03:00
$this -> callback ( function ( $event ) use ( $share ) {
2019-10-17 11:27:13 +03:00
return $event -> getArgument ( 'share' ) === $share ;
})
);
2016-07-15 16:01:14 +03:00
2018-02-09 13:34:34 +03:00
$this -> l10n -> expects ( $this -> any ())
-> method ( 't' )
2020-04-09 14:53:40 +03:00
-> willReturnCallback ( function ( $text , $parameters ) {
2018-02-09 13:34:34 +03:00
return vsprintf ( $text , $parameters );
2020-03-26 00:21:27 +03:00
});
2018-02-09 13:34:34 +03:00
2018-06-14 11:20:10 +03:00
$response = $this -> shareController -> showShare ();
2020-03-26 11:30:18 +03:00
$sharedTmplParams = [
2016-01-15 11:41:51 +03:00
'owner' => 'ownerUID' ,
2014-10-15 13:58:44 +04:00
'filename' => 'file1.txt' ,
'directory_path' => '/file1.txt' ,
'mimetype' => 'text/plain' ,
2016-01-15 11:41:51 +03:00
'dirToken' => 'token' ,
'sharingToken' => 'token' ,
2014-10-15 13:58:44 +04:00
'server2serversharing' => true ,
'protected' => 'true' ,
2014-11-18 16:54:08 +03:00
'dir' => '' ,
2018-02-09 13:34:34 +03:00
'downloadURL' => 'downloadURL' ,
2015-01-17 14:11:52 +03:00
'fileSize' => '33 B' ,
'nonHumanFileSize' => 33 ,
'maxSizeAnimateGif' => 10 ,
2015-03-16 14:09:03 +03:00
'previewSupported' => true ,
2015-06-09 17:33:26 +03:00
'previewEnabled' => true ,
2016-05-25 11:21:07 +03:00
'previewMaxX' => 1024 ,
'previewMaxY' => 1024 ,
2016-06-07 13:28:02 +03:00
'hideFileList' => false ,
2016-09-08 21:34:04 +03:00
'shareOwner' => 'ownerDisplay' ,
'disclaimer' => 'My disclaimer text' ,
2016-07-22 17:13:26 +03:00
'shareUrl' => null ,
2017-09-25 22:44:42 +03:00
'previewImage' => null ,
2018-02-09 13:34:34 +03:00
'previewURL' => 'downloadURL' ,
2018-10-18 13:43:08 +03:00
'note' => $note ,
2018-11-06 17:52:49 +03:00
'hideDownload' => false ,
2018-11-15 22:29:10 +03:00
'showgridview' => false
2020-03-26 11:30:18 +03:00
];
2015-03-10 12:06:15 +03:00
$csp = new \OCP\AppFramework\Http\ContentSecurityPolicy ();
$csp -> addAllowedFrameDomain ( '\'self\'' );
2018-02-09 13:34:34 +03:00
$expectedResponse = new PublicTemplateResponse ( $this -> appName , 'public' , $sharedTmplParams );
2015-03-10 12:06:15 +03:00
$expectedResponse -> setContentSecurityPolicy ( $csp );
2018-02-09 13:34:34 +03:00
$expectedResponse -> setHeaderTitle ( $sharedTmplParams [ 'filename' ]);
2020-04-01 17:02:34 +03:00
$expectedResponse -> setHeaderDetails ( 'shared by ' . $sharedTmplParams [ 'shareOwner' ]);
2018-02-09 13:34:34 +03:00
$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' ]),
2020-04-01 17:02:34 +03:00
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' ;
$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 ( 'file1.txt' );
$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 :: VISIBILITY_PRIVATE );
$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 ( '/file1.txt' );
$this -> session -> method ( 'exists' ) -> with ( 'public_link_authenticated' ) -> willReturn ( true );
$this -> session -> method ( 'get' ) -> with ( 'public_link_authenticated' ) -> willReturn ( '42' );
$this -> urlGenerator -> expects ( $this -> at ( 0 ))
-> method ( 'linkToRouteAbsolute' )
-> with ( 'files_sharing.sharecontroller.downloadShare' , [ 'token' => 'token' ])
-> willReturn ( 'downloadURL' );
$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' );
2020-04-09 14:53:40 +03:00
$this -> userManager -> method ( 'get' ) -> willReturnCallback ( function ( string $uid ) use ( $owner , $initiator ) {
2020-04-01 17:02:34 +03:00
if ( $uid === 'ownerUID' ) {
return $owner ;
}
if ( $uid === 'initiatorUID' ) {
return $initiator ;
}
return null ;
});
$this -> eventDispatcher -> expects ( $this -> once ())
-> method ( 'dispatch' )
-> with (
'OCA\Files_Sharing::loadAdditionalScripts' ,
2020-04-09 14:53:40 +03:00
$this -> callback ( function ( $event ) use ( $share ) {
2020-04-01 17:02:34 +03:00
return $event -> getArgument ( 'share' ) === $share ;
})
);
$this -> l10n -> expects ( $this -> any ())
-> method ( 't' )
2020-04-09 14:53:40 +03:00
-> will ( $this -> returnCallback ( function ( $text , $parameters ) {
2020-04-01 17:02:34 +03:00
return vsprintf ( $text , $parameters );
}));
$response = $this -> shareController -> showShare ();
$sharedTmplParams = [
'owner' => '' ,
'filename' => 'file1.txt' ,
'directory_path' => '/file1.txt' ,
'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' => null ,
'previewImage' => null ,
'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' ]),
2018-02-09 13:34:34 +03:00
]);
2015-03-10 12:06:15 +03:00
2014-10-15 13:58:44 +04:00
$this -> assertEquals ( $expectedResponse , $response );
}
2018-10-22 11:54:01 +03:00
public function testShowShareHideDownload () {
$note = 'personal note' ;
$this -> shareController -> setToken ( 'token' );
$owner = $this -> getMockBuilder ( IUser :: class ) -> getMock ();
$owner -> method ( 'getDisplayName' ) -> willReturn ( 'ownerDisplay' );
$owner -> method ( 'getUID' ) -> willReturn ( 'ownerUID' );
2019-09-09 23:33:03 +03:00
$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 );
2018-10-22 11:54:01 +03:00
$file = $this -> getMockBuilder ( 'OCP\Files\File' ) -> getMock ();
$file -> method ( 'getName' ) -> willReturn ( 'file1.txt' );
$file -> method ( 'getMimetype' ) -> willReturn ( 'text/plain' );
$file -> method ( 'getSize' ) -> willReturn ( 33 );
$file -> method ( 'isReadable' ) -> willReturn ( true );
$file -> method ( 'isShareable' ) -> willReturn ( true );
2020-04-01 17:02:34 +03:00
$accountName = $this -> createMock ( IAccountProperty :: class );
$accountName -> method ( 'getScope' )
-> willReturn ( IAccountManager :: VISIBILITY_PUBLIC );
$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 );
2018-10-22 11:54:01 +03:00
$share = \OC :: $server -> getShareManager () -> newShare ();
$share -> setId ( 42 );
$share -> setPassword ( 'password' )
-> setShareOwner ( 'ownerUID' )
2019-09-09 23:33:03 +03:00
-> setSharedBy ( 'initiatorUID' )
2018-10-22 11:54:01 +03:00
-> setNode ( $file )
-> setNote ( $note )
-> setTarget ( '/file1.txt' )
-> 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 -> at ( 0 ))
-> method ( 'linkToRouteAbsolute' )
-> with ( 'files_sharing.sharecontroller.downloadShare' , [ 'token' => 'token' ])
-> willReturn ( 'downloadURL' );
$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' );
2020-04-09 14:53:40 +03:00
$this -> userManager -> method ( 'get' ) -> willReturnCallback ( function ( string $uid ) use ( $owner , $initiator ) {
2019-09-09 23:33:03 +03:00
if ( $uid === 'ownerUID' ) {
return $owner ;
}
if ( $uid === 'initiatorUID' ) {
return $initiator ;
}
return null ;
});
2018-10-22 11:54:01 +03:00
$this -> eventDispatcher -> expects ( $this -> once ())
-> method ( 'dispatch' )
2019-10-17 11:27:13 +03:00
-> with (
'OCA\Files_Sharing::loadAdditionalScripts' ,
2020-04-09 14:53:40 +03:00
$this -> callback ( function ( $event ) use ( $share ) {
2019-10-17 11:27:13 +03:00
return $event -> getArgument ( 'share' ) === $share ;
})
);
2018-10-22 11:54:01 +03:00
$this -> l10n -> expects ( $this -> any ())
-> method ( 't' )
2020-04-09 14:53:40 +03:00
-> willReturnCallback ( function ( $text , $parameters ) {
2018-10-22 11:54:01 +03:00
return vsprintf ( $text , $parameters );
2020-03-26 00:21:27 +03:00
});
2018-10-22 11:54:01 +03:00
$response = $this -> shareController -> showShare ();
2020-03-26 11:30:18 +03:00
$sharedTmplParams = [
2018-10-22 11:54:01 +03:00
'owner' => 'ownerUID' ,
'filename' => 'file1.txt' ,
'directory_path' => '/file1.txt' ,
'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' => null ,
'previewImage' => null ,
'previewURL' => 'downloadURL' ,
'note' => $note ,
2018-11-06 17:52:49 +03:00
'hideDownload' => true ,
2018-11-15 22:29:10 +03:00
'showgridview' => false
2020-03-26 11:30:18 +03:00
];
2018-10-22 11:54:01 +03:00
$csp = new \OCP\AppFramework\Http\ContentSecurityPolicy ();
$csp -> addAllowedFrameDomain ( '\'self\'' );
$expectedResponse = new PublicTemplateResponse ( $this -> appName , 'public' , $sharedTmplParams );
$expectedResponse -> setContentSecurityPolicy ( $csp );
$expectedResponse -> setHeaderTitle ( $sharedTmplParams [ 'filename' ]);
2020-04-01 17:02:34 +03:00
$expectedResponse -> setHeaderDetails ( 'shared by ' . $sharedTmplParams [ 'shareOwner' ]);
2018-10-22 11:54:01 +03:00
$expectedResponse -> setHeaderActions ([]);
$this -> assertEquals ( $expectedResponse , $response );
}
2018-10-03 01:06:12 +03:00
/**
* 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' );
2019-09-09 23:33:03 +03:00
$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 );
2018-10-03 01:06:12 +03:00
/* @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 );
2020-04-01 17:02:34 +03:00
$accountName = $this -> createMock ( IAccountProperty :: class );
$accountName -> method ( 'getScope' )
-> willReturn ( IAccountManager :: VISIBILITY_PUBLIC );
$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 );
2018-10-03 01:06:12 +03:00
$share = \OC :: $server -> getShareManager () -> newShare ();
$share -> setId ( 42 );
$share -> setPermissions ( Constants :: PERMISSION_CREATE )
-> setShareOwner ( 'ownerUID' )
2019-09-09 23:33:03 +03:00
-> setSharedBy ( 'initiatorUID' )
2018-10-03 01:06:12 +03:00
-> setNode ( $folder )
-> setTarget ( '/fileDrop' );
$this -> shareManager
-> expects ( $this -> once ())
-> method ( 'getShareByToken' )
-> with ( 'token' )
-> willReturn ( $share );
2020-04-09 14:53:40 +03:00
$this -> userManager -> method ( 'get' ) -> willReturnCallback ( function ( string $uid ) use ( $owner , $initiator ) {
2019-09-09 23:33:03 +03:00
if ( $uid === 'ownerUID' ) {
return $owner ;
}
if ( $uid === 'initiatorUID' ) {
return $initiator ;
}
return null ;
});
2018-10-03 01:06:12 +03:00
$this -> l10n -> expects ( $this -> any ())
-> method ( 't' )
2020-04-09 14:53:40 +03:00
-> willReturnCallback ( function ( $text , $parameters ) {
2018-10-03 01:06:12 +03:00
return vsprintf ( $text , $parameters );
2020-03-26 00:21:27 +03:00
});
2018-10-03 01:06:12 +03:00
$response = $this -> shareController -> showShare ();
// skip the "folder" param for tests
$responseParams = $response -> getParams ();
unset ( $responseParams [ 'folder' ]);
$response -> setParams ( $responseParams );
2020-03-26 11:30:18 +03:00
$sharedTmplParams = [
2018-10-03 01:06:12 +03:00
'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' => '' ,
2018-11-05 22:17:00 +03:00
'note' => '' ,
2018-11-07 12:25:36 +03:00
'hideDownload' => false ,
2018-11-15 22:29:10 +03:00
'showgridview' => false
2020-03-26 11:30:18 +03:00
];
2018-10-03 01:06:12 +03:00
$csp = new \OCP\AppFramework\Http\ContentSecurityPolicy ();
$csp -> addAllowedFrameDomain ( '\'self\'' );
$expectedResponse = new PublicTemplateResponse ( $this -> appName , 'public' , $sharedTmplParams );
$expectedResponse -> setContentSecurityPolicy ( $csp );
$expectedResponse -> setHeaderTitle ( $sharedTmplParams [ 'filename' ]);
2020-04-01 17:02:34 +03:00
$expectedResponse -> setHeaderDetails ( 'shared by ' . $sharedTmplParams [ 'shareOwner' ]);
2018-10-03 01:06:12 +03:00
self :: assertEquals ( $expectedResponse , $response );
}
2019-09-09 23:33:03 +03:00
2016-02-09 17:01:12 +03:00
public function testShowShareInvalid () {
2019-11-27 17:27:18 +03:00
$this -> expectException ( \OCP\Files\NotFoundException :: class );
2018-06-14 11:20:10 +03:00
$this -> shareController -> setToken ( 'token' );
2017-10-24 16:26:53 +03:00
$owner = $this -> getMockBuilder ( IUser :: class ) -> getMock ();
2016-02-09 17:01:12 +03:00
$owner -> method ( 'getDisplayName' ) -> willReturn ( 'ownerDisplay' );
$owner -> method ( 'getUID' ) -> willReturn ( 'ownerUID' );
2016-07-15 13:22:53 +03:00
$file = $this -> getMockBuilder ( 'OCP\Files\File' ) -> getMock ();
2016-02-09 17:01:12 +03:00
$file -> method ( 'getName' ) -> willReturn ( 'file1.txt' );
$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 ( '/file1.txt' );
$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 );
2018-06-14 11:20:10 +03:00
$this -> shareController -> showShare ();
2014-10-15 13:58:44 +04:00
}
2015-03-24 13:21:58 +03:00
2016-06-09 19:29:13 +03:00
public function testDownloadShareWithCreateOnlyShare () {
2017-10-25 01:03:28 +03:00
$share = $this -> getMockBuilder ( IShare :: class ) -> getMock ();
2016-06-09 19:29:13 +03:00
$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 is read-only' );
$this -> assertEquals ( $expectedResponse , $response );
}
2019-09-09 23:33:03 +03:00
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 );
2020-04-09 14:53:40 +03:00
$this -> userManager -> method ( 'get' ) -> willReturnCallback ( function ( string $uid ) use ( $owner , $initiator ) {
2019-09-09 23:33:03 +03:00
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 );
2020-04-09 14:53:40 +03:00
$this -> userManager -> method ( 'get' ) -> willReturnCallback ( function ( string $uid ) use ( $owner , $initiator ) {
2019-09-09 23:33:03 +03:00
if ( $uid === 'ownerUID' ) {
return $owner ;
}
if ( $uid === 'initiatorUID' ) {
return $initiator ;
}
return null ;
});
$this -> expectException ( NotFoundException :: class );
$this -> shareController -> showShare ();
}
2014-10-15 13:58:44 +04:00
}