2016-02-04 18:08:35 +03:00
< ? php
2016-03-01 19:52:32 +03:00
/**
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 >
2017-11-06 17:56:42 +03:00
* @ author Joas Schilling < coding @ schilljs . com >
2016-05-26 20:56:05 +03:00
* @ author Lukas Reschke < lukas @ statuscode . ch >
2017-11-06 17:56:42 +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-03-01 19:52:32 +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 ,
* along with this program . If not , see < http :// www . gnu . org / licenses />
*
*/
2016-02-04 18:08:35 +03:00
namespace OCA\FederatedFileSharing\Tests ;
2017-01-27 14:52:17 +03:00
use OC\Federation\CloudIdManager ;
2016-02-04 18:08:35 +03:00
use OCA\FederatedFileSharing\AddressHandler ;
use OCA\FederatedFileSharing\FederatedShareProvider ;
use OCA\FederatedFileSharing\Notifications ;
use OCA\FederatedFileSharing\TokenHandler ;
2018-07-04 11:39:13 +03:00
use OCP\Federation\ICloudFederationProviderManager ;
2017-01-27 14:52:17 +03:00
use OCP\Federation\ICloudIdManager ;
2017-10-24 16:26:53 +03:00
use OCP\Files\File ;
2016-02-04 18:08:35 +03:00
use OCP\Files\IRootFolder ;
2016-04-18 19:17:08 +03:00
use OCP\IConfig ;
2016-02-04 18:08:35 +03:00
use OCP\IDBConnection ;
use OCP\IL10N ;
use OCP\ILogger ;
2016-05-11 21:48:27 +03:00
use OCP\IUserManager ;
2016-02-04 18:08:35 +03:00
use OCP\Share\IManager ;
/**
* Class FederatedShareProviderTest
*
* @ package OCA\FederatedFileSharing\Tests
* @ group DB
*/
2016-05-04 16:26:30 +03:00
class FederatedShareProviderTest extends \Test\TestCase {
2016-02-04 18:08:35 +03:00
/** @var IDBConnection */
protected $connection ;
/** @var AddressHandler | \PHPUnit_Framework_MockObject_MockObject */
protected $addressHandler ;
/** @var Notifications | \PHPUnit_Framework_MockObject_MockObject */
protected $notifications ;
2017-04-10 16:40:30 +03:00
/** @var TokenHandler|\PHPUnit_Framework_MockObject_MockObject */
2016-02-04 18:08:35 +03:00
protected $tokenHandler ;
/** @var IL10N */
protected $l ;
/** @var ILogger */
protected $logger ;
/** @var IRootFolder | \PHPUnit_Framework_MockObject_MockObject */
protected $rootFolder ;
2016-04-18 19:17:08 +03:00
/** @var IConfig | \PHPUnit_Framework_MockObject_MockObject */
protected $config ;
2016-05-11 21:48:27 +03:00
/** @var IUserManager | \PHPUnit_Framework_MockObject_MockObject */
protected $userManager ;
2017-05-24 10:07:58 +03:00
/** @var \OCP\GlobalScale\IConfig|\PHPUnit_Framework_MockObject_MockObject */
protected $gsConfig ;
2016-02-04 18:08:35 +03:00
/** @var IManager */
protected $shareManager ;
/** @var FederatedShareProvider */
protected $provider ;
2017-01-27 14:52:17 +03:00
/** @var ICloudIdManager */
private $cloudIdManager ;
2018-07-04 11:39:13 +03:00
/** @var \PHPUnit_Framework_MockObject_MockObject|ICloudFederationProviderManager */
private $cloudFederationProviderManager ;
2016-02-04 18:08:35 +03:00
public function setUp () {
parent :: setUp ();
$this -> connection = \OC :: $server -> getDatabaseConnection ();
$this -> notifications = $this -> getMockBuilder ( 'OCA\FederatedFileSharing\Notifications' )
-> disableOriginalConstructor ()
-> getMock ();
$this -> tokenHandler = $this -> getMockBuilder ( 'OCA\FederatedFileSharing\TokenHandler' )
-> disableOriginalConstructor ()
-> getMock ();
2017-10-24 16:26:53 +03:00
$this -> l = $this -> getMockBuilder ( IL10N :: class ) -> getMock ();
2016-02-04 18:08:35 +03:00
$this -> l -> method ( 't' )
-> will ( $this -> returnCallback ( function ( $text , $parameters = []) {
return vsprintf ( $text , $parameters );
}));
2017-10-24 16:26:53 +03:00
$this -> logger = $this -> getMockBuilder ( ILogger :: class ) -> getMock ();
2016-08-31 15:06:51 +03:00
$this -> rootFolder = $this -> getMockBuilder ( 'OCP\Files\IRootFolder' ) -> getMock ();
2017-10-24 16:26:53 +03:00
$this -> config = $this -> getMockBuilder ( IConfig :: class ) -> getMock ();
$this -> userManager = $this -> getMockBuilder ( IUserManager :: class ) -> getMock ();
2016-05-11 21:48:27 +03:00
//$this->addressHandler = new AddressHandler(\OC::$server->getURLGenerator(), $this->l);
$this -> addressHandler = $this -> getMockBuilder ( 'OCA\FederatedFileSharing\AddressHandler' ) -> disableOriginalConstructor () -> getMock ();
2017-05-24 10:07:58 +03:00
$this -> cloudIdManager = new CloudIdManager ();
$this -> gsConfig = $this -> createMock ( \OCP\GlobalScale\IConfig :: class );
2016-02-04 18:08:35 +03:00
2016-05-04 16:26:30 +03:00
$this -> userManager -> expects ( $this -> any ()) -> method ( 'userExists' ) -> willReturn ( true );
2018-07-04 11:39:13 +03:00
$this -> cloudFederationProviderManager = $this -> createMock ( ICloudFederationProviderManager :: class );
2016-02-04 18:08:35 +03:00
$this -> provider = new FederatedShareProvider (
$this -> connection ,
$this -> addressHandler ,
$this -> notifications ,
$this -> tokenHandler ,
$this -> l ,
$this -> logger ,
2016-04-18 19:17:08 +03:00
$this -> rootFolder ,
2016-05-11 21:48:27 +03:00
$this -> config ,
2017-01-27 14:52:17 +03:00
$this -> userManager ,
2017-05-24 10:07:58 +03:00
$this -> cloudIdManager ,
2018-07-04 11:39:13 +03:00
$this -> gsConfig ,
$this -> cloudFederationProviderManager
2016-02-04 18:08:35 +03:00
);
$this -> shareManager = \OC :: $server -> getShareManager ();
}
public function tearDown () {
$this -> connection -> getQueryBuilder () -> delete ( 'share' ) -> execute ();
return parent :: tearDown ();
}
public function testCreate () {
$share = $this -> shareManager -> newShare ();
2017-10-24 16:26:53 +03:00
$node = $this -> getMockBuilder ( File :: class ) -> getMock ();
2016-02-04 18:08:35 +03:00
$node -> method ( 'getId' ) -> willReturn ( 42 );
$node -> method ( 'getName' ) -> willReturn ( 'myFile' );
$share -> setSharedWith ( 'user@server.com' )
-> setSharedBy ( 'sharedBy' )
-> setShareOwner ( 'shareOwner' )
-> setPermissions ( 19 )
2018-07-04 11:39:13 +03:00
-> setShareType ( \OCP\Share :: SHARE_TYPE_REMOTE )
2016-02-04 18:08:35 +03:00
-> setNode ( $node );
$this -> tokenHandler -> method ( 'generateToken' ) -> willReturn ( 'token' );
2016-05-11 21:48:27 +03:00
$this -> addressHandler -> expects ( $this -> any ()) -> method ( 'generateRemoteURL' )
-> willReturn ( 'http://localhost/' );
$this -> addressHandler -> expects ( $this -> any ()) -> method ( 'splitUserRemote' )
-> willReturn ([ 'user' , 'server.com' ]);
2016-02-04 18:08:35 +03:00
$this -> notifications -> expects ( $this -> once ())
-> method ( 'sendRemoteShare' )
-> with (
$this -> equalTo ( 'token' ),
$this -> equalTo ( 'user@server.com' ),
$this -> equalTo ( 'myFile' ),
$this -> anything (),
2016-05-04 16:26:30 +03:00
'shareOwner' ,
'shareOwner@http://localhost/' ,
'sharedBy' ,
'sharedBy@http://localhost/'
2016-02-04 18:08:35 +03:00
) -> willReturn ( true );
$this -> rootFolder -> expects ( $this -> never ()) -> method ( $this -> anything ());
$share = $this -> provider -> create ( $share );
$qb = $this -> connection -> getQueryBuilder ();
$stmt = $qb -> select ( '*' )
-> from ( 'share' )
-> where ( $qb -> expr () -> eq ( 'id' , $qb -> createNamedParameter ( $share -> getId ())))
-> execute ();
$data = $stmt -> fetch ();
$stmt -> closeCursor ();
$expected = [
'share_type' => \OCP\Share :: SHARE_TYPE_REMOTE ,
'share_with' => 'user@server.com' ,
'uid_owner' => 'shareOwner' ,
'uid_initiator' => 'sharedBy' ,
'item_type' => 'file' ,
'item_source' => 42 ,
'file_source' => 42 ,
'permissions' => 19 ,
'accepted' => 0 ,
'token' => 'token' ,
];
$this -> assertArraySubset ( $expected , $data );
$this -> assertEquals ( $data [ 'id' ], $share -> getId ());
$this -> assertEquals ( \OCP\Share :: SHARE_TYPE_REMOTE , $share -> getShareType ());
$this -> assertEquals ( 'user@server.com' , $share -> getSharedWith ());
$this -> assertEquals ( 'sharedBy' , $share -> getSharedBy ());
$this -> assertEquals ( 'shareOwner' , $share -> getShareOwner ());
$this -> assertEquals ( 'file' , $share -> getNodeType ());
$this -> assertEquals ( 42 , $share -> getNodeId ());
$this -> assertEquals ( 19 , $share -> getPermissions ());
$this -> assertEquals ( 'token' , $share -> getToken ());
}
public function testCreateCouldNotFindServer () {
$share = $this -> shareManager -> newShare ();
2017-10-24 16:26:53 +03:00
$node = $this -> getMockBuilder ( File :: class ) -> getMock ();
2016-02-04 18:08:35 +03:00
$node -> method ( 'getId' ) -> willReturn ( 42 );
$node -> method ( 'getName' ) -> willReturn ( 'myFile' );
$share -> setSharedWith ( 'user@server.com' )
-> setSharedBy ( 'sharedBy' )
-> setShareOwner ( 'shareOwner' )
-> setPermissions ( 19 )
2018-07-04 11:39:13 +03:00
-> setShareType ( \OCP\Share :: SHARE_TYPE_REMOTE )
2016-02-04 18:08:35 +03:00
-> setNode ( $node );
$this -> tokenHandler -> method ( 'generateToken' ) -> willReturn ( 'token' );
2016-05-11 21:48:27 +03:00
$this -> addressHandler -> expects ( $this -> any ()) -> method ( 'generateRemoteURL' )
-> willReturn ( 'http://localhost/' );
$this -> addressHandler -> expects ( $this -> any ()) -> method ( 'splitUserRemote' )
-> willReturn ([ 'user' , 'server.com' ]);
2016-02-04 18:08:35 +03:00
$this -> notifications -> expects ( $this -> once ())
-> method ( 'sendRemoteShare' )
-> with (
$this -> equalTo ( 'token' ),
$this -> equalTo ( 'user@server.com' ),
$this -> equalTo ( 'myFile' ),
$this -> anything (),
2016-05-04 16:26:30 +03:00
'shareOwner' ,
'shareOwner@http://localhost/' ,
'sharedBy' ,
'sharedBy@http://localhost/'
2016-02-04 18:08:35 +03:00
) -> willReturn ( false );
$this -> rootFolder -> method ( 'getById' )
-> with ( '42' )
-> willReturn ([ $node ]);
try {
$share = $this -> provider -> create ( $share );
$this -> fail ();
} catch ( \Exception $e ) {
2016-11-02 20:05:14 +03:00
$this -> assertEquals ( 'Sharing myFile failed, could not find user@server.com, maybe the server is currently unreachable or uses a self-signed certificate.' , $e -> getMessage ());
2016-02-04 18:08:35 +03:00
}
$qb = $this -> connection -> getQueryBuilder ();
$stmt = $qb -> select ( '*' )
2016-06-24 16:59:32 +03:00
-> from ( 'share' )
-> where ( $qb -> expr () -> eq ( 'id' , $qb -> createNamedParameter ( $share -> getId ())))
-> execute ();
$data = $stmt -> fetch ();
$stmt -> closeCursor ();
$this -> assertFalse ( $data );
}
public function testCreateException () {
$share = $this -> shareManager -> newShare ();
2017-10-24 16:26:53 +03:00
$node = $this -> getMockBuilder ( File :: class ) -> getMock ();
2016-06-24 16:59:32 +03:00
$node -> method ( 'getId' ) -> willReturn ( 42 );
$node -> method ( 'getName' ) -> willReturn ( 'myFile' );
$share -> setSharedWith ( 'user@server.com' )
-> setSharedBy ( 'sharedBy' )
-> setShareOwner ( 'shareOwner' )
-> setPermissions ( 19 )
2018-07-04 11:39:13 +03:00
-> setShareType ( \OCP\Share :: SHARE_TYPE_REMOTE )
2016-06-24 16:59:32 +03:00
-> setNode ( $node );
$this -> tokenHandler -> method ( 'generateToken' ) -> willReturn ( 'token' );
$this -> addressHandler -> expects ( $this -> any ()) -> method ( 'generateRemoteURL' )
-> willReturn ( 'http://localhost/' );
$this -> addressHandler -> expects ( $this -> any ()) -> method ( 'splitUserRemote' )
-> willReturn ([ 'user' , 'server.com' ]);
$this -> notifications -> expects ( $this -> once ())
-> method ( 'sendRemoteShare' )
-> with (
$this -> equalTo ( 'token' ),
$this -> equalTo ( 'user@server.com' ),
$this -> equalTo ( 'myFile' ),
$this -> anything (),
'shareOwner' ,
'shareOwner@http://localhost/' ,
'sharedBy' ,
'sharedBy@http://localhost/'
) -> willThrowException ( new \Exception ( 'dummy' ));
$this -> rootFolder -> method ( 'getById' )
-> with ( '42' )
-> willReturn ([ $node ]);
try {
$share = $this -> provider -> create ( $share );
$this -> fail ();
} catch ( \Exception $e ) {
2016-11-02 20:05:14 +03:00
$this -> assertEquals ( 'Sharing myFile failed, could not find user@server.com, maybe the server is currently unreachable or uses a self-signed certificate.' , $e -> getMessage ());
2016-06-24 16:59:32 +03:00
}
$qb = $this -> connection -> getQueryBuilder ();
$stmt = $qb -> select ( '*' )
2016-02-04 18:08:35 +03:00
-> from ( 'share' )
-> where ( $qb -> expr () -> eq ( 'id' , $qb -> createNamedParameter ( $share -> getId ())))
-> execute ();
$data = $stmt -> fetch ();
$stmt -> closeCursor ();
$this -> assertFalse ( $data );
}
public function testCreateShareWithSelf () {
$share = $this -> shareManager -> newShare ();
2017-10-24 16:26:53 +03:00
$node = $this -> getMockBuilder ( File :: class ) -> getMock ();
2016-02-04 18:08:35 +03:00
$node -> method ( 'getId' ) -> willReturn ( 42 );
$node -> method ( 'getName' ) -> willReturn ( 'myFile' );
2016-05-11 21:48:27 +03:00
$this -> addressHandler -> expects ( $this -> any ()) -> method ( 'compareAddresses' )
-> willReturn ( true );
$shareWith = 'sharedBy@localhost' ;
2016-02-04 18:08:35 +03:00
$share -> setSharedWith ( $shareWith )
-> setSharedBy ( 'sharedBy' )
-> setShareOwner ( 'shareOwner' )
-> setPermissions ( 19 )
-> setNode ( $node );
$this -> rootFolder -> expects ( $this -> never ()) -> method ( $this -> anything ());
try {
$share = $this -> provider -> create ( $share );
$this -> fail ();
} catch ( \Exception $e ) {
$this -> assertEquals ( 'Not allowed to create a federated share with the same user' , $e -> getMessage ());
}
$qb = $this -> connection -> getQueryBuilder ();
$stmt = $qb -> select ( '*' )
-> from ( 'share' )
-> where ( $qb -> expr () -> eq ( 'id' , $qb -> createNamedParameter ( $share -> getId ())))
-> execute ();
$data = $stmt -> fetch ();
$stmt -> closeCursor ();
$this -> assertFalse ( $data );
}
public function testCreateAlreadyShared () {
$share = $this -> shareManager -> newShare ();
2017-10-24 16:26:53 +03:00
$node = $this -> getMockBuilder ( File :: class ) -> getMock ();
2016-02-04 18:08:35 +03:00
$node -> method ( 'getId' ) -> willReturn ( 42 );
$node -> method ( 'getName' ) -> willReturn ( 'myFile' );
2016-05-11 21:48:27 +03:00
$this -> addressHandler -> expects ( $this -> any ()) -> method ( 'splitUserRemote' )
-> willReturn ([ 'user' , 'server.com' ]);
2016-02-04 18:08:35 +03:00
$share -> setSharedWith ( 'user@server.com' )
-> setSharedBy ( 'sharedBy' )
-> setShareOwner ( 'shareOwner' )
-> setPermissions ( 19 )
2018-07-04 11:39:13 +03:00
-> setShareType ( \OCP\Share :: SHARE_TYPE_REMOTE )
2016-02-04 18:08:35 +03:00
-> setNode ( $node );
$this -> tokenHandler -> method ( 'generateToken' ) -> willReturn ( 'token' );
2016-05-11 21:48:27 +03:00
$this -> addressHandler -> expects ( $this -> any ()) -> method ( 'generateRemoteURL' )
-> willReturn ( 'http://localhost/' );
2016-02-04 18:08:35 +03:00
$this -> notifications -> expects ( $this -> once ())
-> method ( 'sendRemoteShare' )
-> with (
$this -> equalTo ( 'token' ),
$this -> equalTo ( 'user@server.com' ),
$this -> equalTo ( 'myFile' ),
$this -> anything (),
2016-05-04 16:26:30 +03:00
'shareOwner' ,
'shareOwner@http://localhost/' ,
'sharedBy' ,
'sharedBy@http://localhost/'
2016-02-04 18:08:35 +03:00
) -> willReturn ( true );
$this -> rootFolder -> expects ( $this -> never ()) -> method ( $this -> anything ());
$this -> provider -> create ( $share );
try {
$this -> provider -> create ( $share );
} catch ( \Exception $e ) {
$this -> assertEquals ( 'Sharing myFile failed, because this item is already shared with user@server.com' , $e -> getMessage ());
}
}
2016-05-04 16:26:30 +03:00
/**
* @ dataProvider datatTestUpdate
*
*/
public function testUpdate ( $owner , $sharedBy ) {
$this -> provider = $this -> getMockBuilder ( 'OCA\FederatedFileSharing\FederatedShareProvider' )
-> setConstructorArgs (
[
$this -> connection ,
$this -> addressHandler ,
$this -> notifications ,
$this -> tokenHandler ,
$this -> l ,
$this -> logger ,
$this -> rootFolder ,
$this -> config ,
2017-01-27 14:52:17 +03:00
$this -> userManager ,
2017-05-24 10:07:58 +03:00
$this -> cloudIdManager ,
2018-07-04 11:39:13 +03:00
$this -> gsConfig ,
$this -> cloudFederationProviderManager
2016-05-04 16:26:30 +03:00
]
) -> setMethods ([ 'sendPermissionUpdate' ]) -> getMock ();
2016-02-04 18:08:35 +03:00
$share = $this -> shareManager -> newShare ();
2017-10-24 16:26:53 +03:00
$node = $this -> getMockBuilder ( File :: class ) -> getMock ();
2016-02-04 18:08:35 +03:00
$node -> method ( 'getId' ) -> willReturn ( 42 );
$node -> method ( 'getName' ) -> willReturn ( 'myFile' );
2016-05-11 21:48:27 +03:00
$this -> addressHandler -> expects ( $this -> any ()) -> method ( 'splitUserRemote' )
-> willReturn ([ 'user' , 'server.com' ]);
2016-02-04 18:08:35 +03:00
$share -> setSharedWith ( 'user@server.com' )
2016-05-04 16:26:30 +03:00
-> setSharedBy ( $sharedBy )
-> setShareOwner ( $owner )
2016-02-04 18:08:35 +03:00
-> setPermissions ( 19 )
2018-07-04 11:39:13 +03:00
-> setShareType ( \OCP\Share :: SHARE_TYPE_REMOTE )
2016-02-04 18:08:35 +03:00
-> setNode ( $node );
$this -> tokenHandler -> method ( 'generateToken' ) -> willReturn ( 'token' );
2016-05-11 21:48:27 +03:00
$this -> addressHandler -> expects ( $this -> any ()) -> method ( 'generateRemoteURL' )
-> willReturn ( 'http://localhost/' );
2016-02-04 18:08:35 +03:00
$this -> notifications -> expects ( $this -> once ())
-> method ( 'sendRemoteShare' )
-> with (
$this -> equalTo ( 'token' ),
$this -> equalTo ( 'user@server.com' ),
$this -> equalTo ( 'myFile' ),
$this -> anything (),
2016-05-04 16:26:30 +03:00
$owner ,
$owner . '@http://localhost/' ,
$sharedBy ,
$sharedBy . '@http://localhost/'
2016-02-04 18:08:35 +03:00
) -> willReturn ( true );
2016-05-04 16:26:30 +03:00
if ( $owner === $sharedBy ) {
$this -> provider -> expects ( $this -> never ()) -> method ( 'sendPermissionUpdate' );
} else {
$this -> provider -> expects ( $this -> once ()) -> method ( 'sendPermissionUpdate' );
}
2016-02-04 18:08:35 +03:00
$this -> rootFolder -> expects ( $this -> never ()) -> method ( $this -> anything ());
$share = $this -> provider -> create ( $share );
$share -> setPermissions ( 1 );
$this -> provider -> update ( $share );
$share = $this -> provider -> getShareById ( $share -> getId ());
$this -> assertEquals ( 1 , $share -> getPermissions ());
}
2016-05-04 16:26:30 +03:00
public function datatTestUpdate () {
return [
[ 'sharedBy' , 'shareOwner' ],
[ 'shareOwner' , 'shareOwner' ]
];
}
2016-02-04 18:08:35 +03:00
public function testGetSharedBy () {
2017-10-24 16:26:53 +03:00
$node = $this -> getMockBuilder ( File :: class ) -> getMock ();
2016-02-04 18:08:35 +03:00
$node -> method ( 'getId' ) -> willReturn ( 42 );
$node -> method ( 'getName' ) -> willReturn ( 'myFile' );
2016-05-11 21:48:27 +03:00
$this -> addressHandler -> expects ( $this -> at ( 0 )) -> method ( 'splitUserRemote' )
-> willReturn ([ 'user' , 'server.com' ]);
$this -> addressHandler -> expects ( $this -> at ( 1 )) -> method ( 'splitUserRemote' )
-> willReturn ([ 'user2' , 'server.com' ]);
2018-01-16 21:24:52 +03:00
$this -> addressHandler -> method ( 'generateRemoteURL' )
-> willReturn ( 'remoteurl.com' );
2016-02-04 18:08:35 +03:00
$this -> tokenHandler -> method ( 'generateToken' ) -> willReturn ( 'token' );
$this -> notifications
-> method ( 'sendRemoteShare' )
-> willReturn ( true );
$this -> rootFolder -> expects ( $this -> never ()) -> method ( $this -> anything ());
$share = $this -> shareManager -> newShare ();
$share -> setSharedWith ( 'user@server.com' )
-> setSharedBy ( 'sharedBy' )
-> setShareOwner ( 'shareOwner' )
-> setPermissions ( 19 )
2018-07-04 11:39:13 +03:00
-> setShareType ( \OCP\Share :: SHARE_TYPE_REMOTE )
2016-02-04 18:08:35 +03:00
-> setNode ( $node );
$this -> provider -> create ( $share );
$share2 = $this -> shareManager -> newShare ();
$share2 -> setSharedWith ( 'user2@server.com' )
-> setSharedBy ( 'sharedBy2' )
-> setShareOwner ( 'shareOwner' )
-> setPermissions ( 19 )
2018-07-04 11:39:13 +03:00
-> setShareType ( \OCP\Share :: SHARE_TYPE_REMOTE )
2016-02-04 18:08:35 +03:00
-> setNode ( $node );
$this -> provider -> create ( $share2 );
$shares = $this -> provider -> getSharesBy ( 'sharedBy' , \OCP\Share :: SHARE_TYPE_REMOTE , null , false , - 1 , 0 );
$this -> assertCount ( 1 , $shares );
$this -> assertEquals ( 'user@server.com' , $shares [ 0 ] -> getSharedWith ());
$this -> assertEquals ( 'sharedBy' , $shares [ 0 ] -> getSharedBy ());
}
public function testGetSharedByWithNode () {
2017-10-24 16:26:53 +03:00
$node = $this -> getMockBuilder ( File :: class ) -> getMock ();
2016-02-04 18:08:35 +03:00
$node -> method ( 'getId' ) -> willReturn ( 42 );
$node -> method ( 'getName' ) -> willReturn ( 'myFile' );
$this -> tokenHandler -> method ( 'generateToken' ) -> willReturn ( 'token' );
$this -> notifications
-> method ( 'sendRemoteShare' )
-> willReturn ( true );
$this -> rootFolder -> expects ( $this -> never ()) -> method ( $this -> anything ());
2018-01-16 21:24:52 +03:00
$this -> addressHandler -> method ( 'generateRemoteURL' )
-> willReturn ( 'remoteurl.com' );
2016-02-04 18:08:35 +03:00
$share = $this -> shareManager -> newShare ();
$share -> setSharedWith ( 'user@server.com' )
-> setSharedBy ( 'sharedBy' )
-> setShareOwner ( 'shareOwner' )
-> setPermissions ( 19 )
2018-07-04 11:39:13 +03:00
-> setShareType ( \OCP\Share :: SHARE_TYPE_REMOTE )
2016-02-04 18:08:35 +03:00
-> setNode ( $node );
$this -> provider -> create ( $share );
2017-10-24 16:26:53 +03:00
$node2 = $this -> getMockBuilder ( File :: class ) -> getMock ();
2016-02-04 18:08:35 +03:00
$node2 -> method ( 'getId' ) -> willReturn ( 43 );
$node2 -> method ( 'getName' ) -> willReturn ( 'myOtherFile' );
$share2 = $this -> shareManager -> newShare ();
$share2 -> setSharedWith ( 'user@server.com' )
-> setSharedBy ( 'sharedBy' )
-> setShareOwner ( 'shareOwner' )
-> setPermissions ( 19 )
2018-07-04 11:39:13 +03:00
-> setShareType ( \OCP\Share :: SHARE_TYPE_REMOTE )
2016-02-04 18:08:35 +03:00
-> setNode ( $node2 );
$this -> provider -> create ( $share2 );
$shares = $this -> provider -> getSharesBy ( 'sharedBy' , \OCP\Share :: SHARE_TYPE_REMOTE , $node2 , false , - 1 , 0 );
$this -> assertCount ( 1 , $shares );
$this -> assertEquals ( 43 , $shares [ 0 ] -> getNodeId ());
}
public function testGetSharedByWithReshares () {
2017-10-24 16:26:53 +03:00
$node = $this -> getMockBuilder ( File :: class ) -> getMock ();
2016-02-04 18:08:35 +03:00
$node -> method ( 'getId' ) -> willReturn ( 42 );
$node -> method ( 'getName' ) -> willReturn ( 'myFile' );
$this -> tokenHandler -> method ( 'generateToken' ) -> willReturn ( 'token' );
$this -> notifications
-> method ( 'sendRemoteShare' )
-> willReturn ( true );
$this -> rootFolder -> expects ( $this -> never ()) -> method ( $this -> anything ());
2018-01-16 21:24:52 +03:00
$this -> addressHandler -> method ( 'generateRemoteURL' )
-> willReturn ( 'remoteurl.com' );
2016-02-04 18:08:35 +03:00
$share = $this -> shareManager -> newShare ();
$share -> setSharedWith ( 'user@server.com' )
-> setSharedBy ( 'shareOwner' )
-> setShareOwner ( 'shareOwner' )
-> setPermissions ( 19 )
2018-07-04 11:39:13 +03:00
-> setShareType ( \OCP\Share :: SHARE_TYPE_REMOTE )
2016-02-04 18:08:35 +03:00
-> setNode ( $node );
$this -> provider -> create ( $share );
$share2 = $this -> shareManager -> newShare ();
$share2 -> setSharedWith ( 'user2@server.com' )
-> setSharedBy ( 'sharedBy' )
-> setShareOwner ( 'shareOwner' )
-> setPermissions ( 19 )
2018-07-04 11:39:13 +03:00
-> setShareType ( \OCP\Share :: SHARE_TYPE_REMOTE )
2016-02-04 18:08:35 +03:00
-> setNode ( $node );
$this -> provider -> create ( $share2 );
$shares = $this -> provider -> getSharesBy ( 'shareOwner' , \OCP\Share :: SHARE_TYPE_REMOTE , null , true , - 1 , 0 );
$this -> assertCount ( 2 , $shares );
}
public function testGetSharedByWithLimit () {
2017-10-24 16:26:53 +03:00
$node = $this -> getMockBuilder ( File :: class ) -> getMock ();
2016-02-04 18:08:35 +03:00
$node -> method ( 'getId' ) -> willReturn ( 42 );
$node -> method ( 'getName' ) -> willReturn ( 'myFile' );
2016-05-11 21:48:27 +03:00
$this -> addressHandler -> expects ( $this -> any ()) -> method ( 'splitUserRemote' )
-> willReturnCallback ( function ( $uid ) {
if ( $uid === 'user@server.com' ) {
return [ 'user' , 'server.com' ];
}
return [ 'user2' , 'server.com' ];
});
2016-02-04 18:08:35 +03:00
$this -> tokenHandler -> method ( 'generateToken' ) -> willReturn ( 'token' );
$this -> notifications
-> method ( 'sendRemoteShare' )
-> willReturn ( true );
$this -> rootFolder -> expects ( $this -> never ()) -> method ( $this -> anything ());
2018-01-16 21:24:52 +03:00
$this -> addressHandler -> method ( 'generateRemoteURL' )
-> willReturn ( 'remoteurl.com' );
2016-02-04 18:08:35 +03:00
$share = $this -> shareManager -> newShare ();
$share -> setSharedWith ( 'user@server.com' )
-> setSharedBy ( 'sharedBy' )
-> setShareOwner ( 'shareOwner' )
-> setPermissions ( 19 )
2018-07-04 11:39:13 +03:00
-> setShareType ( \OCP\Share :: SHARE_TYPE_REMOTE )
2016-02-04 18:08:35 +03:00
-> setNode ( $node );
$this -> provider -> create ( $share );
$share2 = $this -> shareManager -> newShare ();
$share2 -> setSharedWith ( 'user2@server.com' )
-> setSharedBy ( 'sharedBy' )
-> setShareOwner ( 'shareOwner' )
-> setPermissions ( 19 )
2018-07-04 11:39:13 +03:00
-> setShareType ( \OCP\Share :: SHARE_TYPE_REMOTE )
2016-02-04 18:08:35 +03:00
-> setNode ( $node );
$this -> provider -> create ( $share2 );
$shares = $this -> provider -> getSharesBy ( 'shareOwner' , \OCP\Share :: SHARE_TYPE_REMOTE , null , true , 1 , 1 );
$this -> assertCount ( 1 , $shares );
$this -> assertEquals ( 'user2@server.com' , $shares [ 0 ] -> getSharedWith ());
}
2016-04-04 13:28:19 +03:00
public function dataDeleteUser () {
return [
[ 'a' , 'b' , 'c' , 'a' , true ],
[ 'a' , 'b' , 'c' , 'b' , false ],
// The recipient is non local.
[ 'a' , 'b' , 'c' , 'c' , false ],
[ 'a' , 'b' , 'c' , 'd' , false ],
];
}
/**
* @ dataProvider dataDeleteUser
*
* @ param string $owner The owner of the share ( uid )
* @ param string $initiator The initiator of the share ( uid )
* @ param string $recipient The recipient of the share ( uid / gid / pass )
* @ param string $deletedUser The user that is deleted
* @ param bool $rowDeleted Is the row deleted in this setup
*/
public function testDeleteUser ( $owner , $initiator , $recipient , $deletedUser , $rowDeleted ) {
$qb = $this -> connection -> getQueryBuilder ();
$qb -> insert ( 'share' )
-> setValue ( 'share_type' , $qb -> createNamedParameter ( \OCP\Share :: SHARE_TYPE_REMOTE ))
-> setValue ( 'uid_owner' , $qb -> createNamedParameter ( $owner ))
-> setValue ( 'uid_initiator' , $qb -> createNamedParameter ( $initiator ))
-> setValue ( 'share_with' , $qb -> createNamedParameter ( $recipient ))
-> setValue ( 'item_type' , $qb -> createNamedParameter ( 'file' ))
-> setValue ( 'item_source' , $qb -> createNamedParameter ( 42 ))
-> setValue ( 'file_source' , $qb -> createNamedParameter ( 42 ))
-> execute ();
$id = $qb -> getLastInsertId ();
$this -> provider -> userDeleted ( $deletedUser , \OCP\Share :: SHARE_TYPE_REMOTE );
$qb = $this -> connection -> getQueryBuilder ();
$qb -> select ( '*' )
-> from ( 'share' )
-> where (
$qb -> expr () -> eq ( 'id' , $qb -> createNamedParameter ( $id ))
);
$cursor = $qb -> execute ();
$data = $cursor -> fetchAll ();
$cursor -> closeCursor ();
$this -> assertCount ( $rowDeleted ? 0 : 1 , $data );
}
2016-04-18 19:17:08 +03:00
/**
2017-05-24 10:07:58 +03:00
* @ dataProvider dataTestIsOutgoingServer2serverShareEnabled
2016-04-18 19:17:08 +03:00
*
* @ param string $isEnabled
* @ param bool $expected
*/
2017-05-24 10:07:58 +03:00
public function testIsOutgoingServer2serverShareEnabled ( $internalOnly , $isEnabled , $expected ) {
$this -> gsConfig -> expects ( $this -> once ()) -> method ( 'onlyInternalFederation' )
-> willReturn ( $internalOnly );
$this -> config -> expects ( $this -> any ()) -> method ( 'getAppValue' )
2016-04-18 19:17:08 +03:00
-> with ( 'files_sharing' , 'outgoing_server2server_share_enabled' , 'yes' )
-> willReturn ( $isEnabled );
$this -> assertSame ( $expected ,
$this -> provider -> isOutgoingServer2serverShareEnabled ()
);
}
2017-05-24 10:07:58 +03:00
public function dataTestIsOutgoingServer2serverShareEnabled () {
return [
[ false , 'yes' , true ],
[ false , 'no' , false ],
[ true , 'yes' , false ],
[ true , 'no' , false ],
];
}
2016-04-18 19:17:08 +03:00
/**
2017-05-24 10:07:58 +03:00
* @ dataProvider dataTestIsIncomingServer2serverShareEnabled
2016-04-18 19:17:08 +03:00
*
* @ param string $isEnabled
* @ param bool $expected
*/
2017-05-24 10:07:58 +03:00
public function testIsIncomingServer2serverShareEnabled ( $onlyInternal , $isEnabled , $expected ) {
$this -> gsConfig -> expects ( $this -> once ()) -> method ( 'onlyInternalFederation' )
-> willReturn ( $onlyInternal );
$this -> config -> expects ( $this -> any ()) -> method ( 'getAppValue' )
2016-04-18 19:17:08 +03:00
-> with ( 'files_sharing' , 'incoming_server2server_share_enabled' , 'yes' )
-> willReturn ( $isEnabled );
$this -> assertSame ( $expected ,
$this -> provider -> isIncomingServer2serverShareEnabled ()
);
}
2017-05-24 10:07:58 +03:00
public function dataTestIsIncomingServer2serverShareEnabled () {
return [
[ false , 'yes' , true ],
[ false , 'no' , false ],
[ true , 'yes' , false ],
[ true , 'no' , false ],
];
}
2017-04-07 12:42:33 +03:00
/**
2017-05-24 10:07:58 +03:00
* @ dataProvider dataTestIsLookupServerQueriesEnabled
2017-04-07 12:42:33 +03:00
*
* @ param string $isEnabled
* @ param bool $expected
*/
2017-05-24 10:07:58 +03:00
public function testIsLookupServerQueriesEnabled ( $gsEnabled , $isEnabled , $expected ) {
$this -> gsConfig -> expects ( $this -> once ()) -> method ( 'isGlobalScaleEnabled' )
-> willReturn ( $gsEnabled );
$this -> config -> expects ( $this -> any ()) -> method ( 'getAppValue' )
2017-04-07 12:42:33 +03:00
-> with ( 'files_sharing' , 'lookupServerEnabled' , 'no' )
-> willReturn ( $isEnabled );
$this -> assertSame ( $expected ,
$this -> provider -> isLookupServerQueriesEnabled ()
);
}
2017-05-24 10:07:58 +03:00
public function dataTestIsLookupServerQueriesEnabled () {
return [
[ false , 'yes' , true ],
[ false , 'no' , false ],
[ true , 'yes' , true ],
[ true , 'no' , true ],
];
}
2017-04-07 12:42:33 +03:00
/**
2017-05-24 10:07:58 +03:00
* @ dataProvider dataTestIsLookupServerUploadEnabled
2017-04-07 12:42:33 +03:00
*
* @ param string $isEnabled
* @ param bool $expected
*/
2017-05-24 10:07:58 +03:00
public function testIsLookupServerUploadEnabled ( $gsEnabled , $isEnabled , $expected ) {
$this -> gsConfig -> expects ( $this -> once ()) -> method ( 'isGlobalScaleEnabled' )
-> willReturn ( $gsEnabled );
$this -> config -> expects ( $this -> any ()) -> method ( 'getAppValue' )
2017-04-07 12:42:33 +03:00
-> with ( 'files_sharing' , 'lookupServerUploadEnabled' , 'yes' )
-> willReturn ( $isEnabled );
$this -> assertSame ( $expected ,
$this -> provider -> isLookupServerUploadEnabled ()
);
}
2017-05-24 10:07:58 +03:00
public function dataTestIsLookupServerUploadEnabled () {
2016-04-18 19:17:08 +03:00
return [
2017-05-24 10:07:58 +03:00
[ false , 'yes' , true ],
[ false , 'no' , false ],
[ true , 'yes' , false ],
[ true , 'no' , false ],
2016-04-18 19:17:08 +03:00
];
}
2016-11-01 15:22:52 +03:00
public function testGetSharesInFolder () {
$userManager = \OC :: $server -> getUserManager ();
$rootFolder = \OC :: $server -> getRootFolder ();
2016-11-02 20:05:14 +03:00
$u1 = $userManager -> createUser ( 'testFed' , md5 ( time ()));
$u2 = $userManager -> createUser ( 'testFed2' , md5 ( time ()));
2016-11-01 15:22:52 +03:00
$folder1 = $rootFolder -> getUserFolder ( $u1 -> getUID ()) -> newFolder ( 'foo' );
$file1 = $folder1 -> newFile ( 'bar1' );
$file2 = $folder1 -> newFile ( 'bar2' );
$this -> tokenHandler -> method ( 'generateToken' ) -> willReturn ( 'token' );
$this -> notifications
-> method ( 'sendRemoteShare' )
-> willReturn ( true );
2018-01-16 21:24:52 +03:00
$this -> addressHandler -> method ( 'generateRemoteURL' )
-> willReturn ( 'remoteurl.com' );
2016-11-01 15:22:52 +03:00
$share1 = $this -> shareManager -> newShare ();
$share1 -> setSharedWith ( 'user@server.com' )
-> setSharedBy ( $u1 -> getUID ())
-> setShareOwner ( $u1 -> getUID ())
-> setPermissions ( \OCP\Constants :: PERMISSION_READ )
2018-07-04 11:39:13 +03:00
-> setShareType ( \OCP\Share :: SHARE_TYPE_REMOTE )
2016-11-01 15:22:52 +03:00
-> setNode ( $file1 );
$this -> provider -> create ( $share1 );
$share2 = $this -> shareManager -> newShare ();
$share2 -> setSharedWith ( 'user@server.com' )
-> setSharedBy ( $u2 -> getUID ())
-> setShareOwner ( $u1 -> getUID ())
-> setPermissions ( \OCP\Constants :: PERMISSION_READ )
2018-07-04 11:39:13 +03:00
-> setShareType ( \OCP\Share :: SHARE_TYPE_REMOTE )
2016-11-01 15:22:52 +03:00
-> setNode ( $file2 );
$this -> provider -> create ( $share2 );
$result = $this -> provider -> getSharesInFolder ( $u1 -> getUID (), $folder1 , false );
$this -> assertCount ( 1 , $result );
$this -> assertCount ( 1 , $result [ $file1 -> getId ()]);
$result = $this -> provider -> getSharesInFolder ( $u1 -> getUID (), $folder1 , true );
$this -> assertCount ( 2 , $result );
$this -> assertCount ( 1 , $result [ $file1 -> getId ()]);
$this -> assertCount ( 1 , $result [ $file2 -> getId ()]);
$u1 -> delete ();
$u2 -> delete ();
}
2017-01-04 10:59:43 +03:00
public function testGetAccessList () {
$userManager = \OC :: $server -> getUserManager ();
$rootFolder = \OC :: $server -> getRootFolder ();
$u1 = $userManager -> createUser ( 'testFed' , md5 ( time ()));
$folder1 = $rootFolder -> getUserFolder ( $u1 -> getUID ()) -> newFolder ( 'foo' );
$file1 = $folder1 -> newFile ( 'bar1' );
2017-04-10 16:40:30 +03:00
$this -> tokenHandler -> expects ( $this -> exactly ( 2 ))
-> method ( 'generateToken' )
-> willReturnOnConsecutiveCalls ( 'token1' , 'token2' );
$this -> notifications -> expects ( $this -> atLeastOnce ())
2017-01-04 10:59:43 +03:00
-> method ( 'sendRemoteShare' )
-> willReturn ( true );
2017-04-11 13:40:36 +03:00
$result = $this -> provider -> getAccessList ([ $file1 ], true );
$this -> assertEquals ([ 'remote' => []], $result );
$result = $this -> provider -> getAccessList ([ $file1 ], false );
$this -> assertEquals ([ 'remote' => false ], $result );
2018-01-16 21:24:52 +03:00
$this -> addressHandler -> method ( 'generateRemoteURL' )
-> willReturn ( 'remoteurl.com' );
2017-01-04 10:59:43 +03:00
$share1 = $this -> shareManager -> newShare ();
$share1 -> setSharedWith ( 'user@server.com' )
-> setSharedBy ( $u1 -> getUID ())
-> setShareOwner ( $u1 -> getUID ())
-> setPermissions ( \OCP\Constants :: PERMISSION_READ )
2018-07-04 11:39:13 +03:00
-> setShareType ( \OCP\Share :: SHARE_TYPE_REMOTE )
2017-01-04 10:59:43 +03:00
-> setNode ( $file1 );
$this -> provider -> create ( $share1 );
2017-04-10 16:40:30 +03:00
$share2 = $this -> shareManager -> newShare ();
$share2 -> setSharedWith ( 'foobar@localhost' )
-> setSharedBy ( $u1 -> getUID ())
-> setShareOwner ( $u1 -> getUID ())
-> setPermissions ( \OCP\Constants :: PERMISSION_READ )
2018-07-04 11:39:13 +03:00
-> setShareType ( \OCP\Share :: SHARE_TYPE_REMOTE )
2017-04-10 16:40:30 +03:00
-> setNode ( $file1 );
$this -> provider -> create ( $share2 );
2017-01-04 10:59:43 +03:00
$result = $this -> provider -> getAccessList ([ $file1 ], true );
2017-04-10 16:40:30 +03:00
$this -> assertEquals ([ 'remote' => [
'user@server.com' => [
'token' => 'token1' ,
'node_id' => $file1 -> getId (),
],
'foobar@localhost' => [
'token' => 'token2' ,
'node_id' => $file1 -> getId (),
],
]], $result );
2017-04-11 13:40:36 +03:00
$result = $this -> provider -> getAccessList ([ $file1 ], false );
$this -> assertEquals ([ 'remote' => true ], $result );
2017-01-04 10:59:43 +03:00
$u1 -> delete ();
}
2016-02-04 18:08:35 +03:00
}