2017-09-12 23:38:36 +03:00
< ? php
/**
* @ copyright Copyright ( c ) 2017 Arthur Schiwon < blizzz @ arthur - schiwon . de >
*
* @ author Arthur Schiwon < blizzz @ arthur - schiwon . de >
*
* @ license GNU AGPL version 3 or any later version
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation , either version 3 of the
* License , or ( at your option ) any later version .
*
* 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
* along with this program . If not , see < http :// www . gnu . org / licenses />.
*
*/
namespace Test\Collaboration\Collaborators ;
use OC\Collaboration\Collaborators\RemotePlugin ;
use OC\Collaboration\Collaborators\SearchResult ;
use OC\Federation\CloudIdManager ;
use OCP\Collaboration\Collaborators\SearchResultType ;
use OCP\Contacts\IManager ;
use OCP\Federation\ICloudIdManager ;
use OCP\IConfig ;
2018-10-30 13:41:56 +03:00
use OCP\IUser ;
2018-10-17 22:20:15 +03:00
use OCP\IUserManager ;
2018-10-30 13:41:56 +03:00
use OCP\IUserSession ;
2017-09-12 23:38:36 +03:00
use OCP\Share ;
use Test\TestCase ;
class RemotePluginTest extends TestCase {
2018-10-17 22:20:15 +03:00
/** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
protected $userManager ;
2017-09-12 23:38:36 +03:00
/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
protected $config ;
/** @var IManager|\PHPUnit_Framework_MockObject_MockObject */
protected $contactsManager ;
/** @var ICloudIdManager|\PHPUnit_Framework_MockObject_MockObject */
protected $cloudIdManager ;
/** @var RemotePlugin */
protected $plugin ;
/** @var SearchResult */
protected $searchResult ;
2019-11-27 17:27:18 +03:00
protected function setUp () : void {
2017-09-12 23:38:36 +03:00
parent :: setUp ();
2018-10-17 22:20:15 +03:00
$this -> userManager = $this -> createMock ( IUserManager :: class );
2017-09-12 23:38:36 +03:00
$this -> config = $this -> createMock ( IConfig :: class );
$this -> contactsManager = $this -> createMock ( IManager :: class );
2020-11-16 19:56:44 +03:00
$this -> cloudIdManager = new CloudIdManager ( $this -> contactsManager );
2017-09-12 23:38:36 +03:00
$this -> searchResult = new SearchResult ();
}
public function instantiatePlugin () {
2018-10-30 13:41:56 +03:00
$user = $this -> createMock ( IUser :: class );
$user -> expects ( $this -> any ())
-> method ( 'getUID' )
-> willReturn ( 'admin' );
$userSession = $this -> createMock ( IUserSession :: class );
$userSession -> expects ( $this -> any ())
-> method ( 'getUser' )
-> willReturn ( $user );
$this -> plugin = new RemotePlugin ( $this -> contactsManager , $this -> cloudIdManager , $this -> config , $this -> userManager , $userSession );
2017-09-12 23:38:36 +03:00
}
/**
* @ dataProvider dataGetRemote
*
* @ param string $searchTerm
* @ param array $contacts
* @ param bool $shareeEnumeration
* @ param array $expected
* @ param bool $exactIdMatch
* @ param bool $reachedEnd
*/
public function testSearch ( $searchTerm , array $contacts , $shareeEnumeration , array $expected , $exactIdMatch , $reachedEnd ) {
$this -> config -> expects ( $this -> any ())
-> method ( 'getAppValue' )
-> willReturnCallback (
2020-04-10 15:19:56 +03:00
function ( $appName , $key , $default ) use ( $shareeEnumeration ) {
2017-09-12 23:38:36 +03:00
if ( $appName === 'core' && $key === 'shareapi_allow_share_dialog_user_enumeration' ) {
return $shareeEnumeration ? 'yes' : 'no' ;
}
return $default ;
}
);
$this -> instantiatePlugin ();
$this -> contactsManager -> expects ( $this -> any ())
-> method ( 'search' )
2020-11-16 19:56:44 +03:00
-> willReturnCallback ( function ( $search , $searchAttributes ) use ( $searchTerm , $contacts ) {
if ( $search === $searchTerm ) {
return $contacts ;
}
return [];
});
2017-09-12 23:38:36 +03:00
2018-02-06 20:11:44 +03:00
$moreResults = $this -> plugin -> search ( $searchTerm , 2 , 0 , $this -> searchResult );
2017-09-12 23:38:36 +03:00
$result = $this -> searchResult -> asArray ();
$this -> assertSame ( $exactIdMatch , $this -> searchResult -> hasExactIdMatch ( new SearchResultType ( 'remotes' )));
$this -> assertEquals ( $expected , $result );
$this -> assertSame ( $reachedEnd , $moreResults );
}
2017-09-13 15:39:04 +03:00
/**
* @ dataProvider dataTestSplitUserRemote
*
* @ param string $remote
* @ param string $expectedUser
* @ param string $expectedUrl
*/
public function testSplitUserRemote ( $remote , $expectedUser , $expectedUrl ) {
$this -> instantiatePlugin ();
2020-11-16 19:56:44 +03:00
$this -> contactsManager -> expects ( $this -> any ())
-> method ( 'search' )
-> willReturn ([]);
2017-09-13 15:39:04 +03:00
list ( $remoteUser , $remoteUrl ) = $this -> plugin -> splitUserRemote ( $remote );
$this -> assertSame ( $expectedUser , $remoteUser );
$this -> assertSame ( $expectedUrl , $remoteUrl );
}
/**
* @ dataProvider dataTestSplitUserRemoteError
*
* @ param string $id
*/
public function testSplitUserRemoteError ( $id ) {
2019-11-27 17:27:18 +03:00
$this -> expectException ( \Exception :: class );
2017-09-13 15:39:04 +03:00
$this -> instantiatePlugin ();
$this -> plugin -> splitUserRemote ( $id );
}
2017-09-12 23:38:36 +03:00
public function dataGetRemote () {
return [
[ 'test' , [], true , [ 'remotes' => [], 'exact' => [ 'remotes' => []]], false , true ],
[ 'test' , [], false , [ 'remotes' => [], 'exact' => [ 'remotes' => []]], false , true ],
[
'test@remote' ,
[],
true ,
2019-05-23 18:03:04 +03:00
[ 'remotes' => [], 'exact' => [ 'remotes' => [[ 'label' => 'test (remote)' , 'value' => [ 'shareType' => Share :: SHARE_TYPE_REMOTE , 'shareWith' => 'test@remote' , 'server' => 'remote' ], 'uuid' => 'test' , 'name' => 'test' ]]]],
2017-09-12 23:38:36 +03:00
false ,
true ,
],
[
'test@remote' ,
[],
false ,
2019-05-23 18:03:04 +03:00
[ 'remotes' => [], 'exact' => [ 'remotes' => [[ 'label' => 'test (remote)' , 'value' => [ 'shareType' => Share :: SHARE_TYPE_REMOTE , 'shareWith' => 'test@remote' , 'server' => 'remote' ], 'uuid' => 'test' , 'name' => 'test' ]]]],
2017-09-12 23:38:36 +03:00
false ,
true ,
],
[
'test' ,
[
[
2018-10-29 19:00:09 +03:00
'UID' => 'uid' ,
2017-09-12 23:38:36 +03:00
'FN' => 'User3 @ Localhost' ,
],
[
2018-10-29 19:00:09 +03:00
'UID' => 'uid' ,
2017-09-12 23:38:36 +03:00
'FN' => 'User2 @ Localhost' ,
'CLOUD' => [
],
],
[
2018-10-29 19:00:09 +03:00
'UID' => 'uid1' ,
2017-09-12 23:38:36 +03:00
'FN' => 'User @ Localhost' ,
'CLOUD' => [
'username@localhost' ,
],
],
],
true ,
2018-10-30 12:18:58 +03:00
[ 'remotes' => [[ 'name' => 'User @ Localhost' , 'label' => 'User @ Localhost (username@localhost)' , 'uuid' => 'uid1' , 'type' => '' , 'value' => [ 'shareType' => Share :: SHARE_TYPE_REMOTE , 'shareWith' => 'username@localhost' , 'server' => 'localhost' ]]], 'exact' => [ 'remotes' => []]],
2017-09-12 23:38:36 +03:00
false ,
true ,
],
[
'test' ,
[
[
2018-10-29 19:00:09 +03:00
'UID' => 'uid' ,
2017-09-12 23:38:36 +03:00
'FN' => 'User3 @ Localhost' ,
],
[
2018-10-29 19:00:09 +03:00
'UID' => 'uid' ,
2017-09-12 23:38:36 +03:00
'FN' => 'User2 @ Localhost' ,
'CLOUD' => [
],
],
[
2018-10-29 19:00:09 +03:00
'UID' => 'uid' ,
2017-09-12 23:38:36 +03:00
'FN' => 'User @ Localhost' ,
'CLOUD' => [
'username@localhost' ,
],
],
],
false ,
[ 'remotes' => [], 'exact' => [ 'remotes' => []]],
false ,
true ,
],
[
'test@remote' ,
[
[
2018-10-29 19:00:09 +03:00
'UID' => 'uid' ,
2017-09-12 23:38:36 +03:00
'FN' => 'User3 @ Localhost' ,
],
[
2018-10-29 19:00:09 +03:00
'UID' => 'uid' ,
2017-09-12 23:38:36 +03:00
'FN' => 'User2 @ Localhost' ,
'CLOUD' => [
],
],
[
2018-10-29 19:00:09 +03:00
'UID' => 'uid' ,
2017-09-12 23:38:36 +03:00
'FN' => 'User @ Localhost' ,
'CLOUD' => [
'username@localhost' ,
],
],
],
true ,
2019-05-23 18:03:04 +03:00
[ 'remotes' => [[ 'name' => 'User @ Localhost' , 'label' => 'User @ Localhost (username@localhost)' , 'uuid' => 'uid' , 'type' => '' , 'value' => [ 'shareType' => Share :: SHARE_TYPE_REMOTE , 'shareWith' => 'username@localhost' , 'server' => 'localhost' ]]], 'exact' => [ 'remotes' => [[ 'label' => 'test (remote)' , 'value' => [ 'shareType' => Share :: SHARE_TYPE_REMOTE , 'shareWith' => 'test@remote' , 'server' => 'remote' ], 'uuid' => 'test' , 'name' => 'test' ]]]],
2017-09-12 23:38:36 +03:00
false ,
true ,
],
[
'test@remote' ,
[
[
2018-10-29 19:00:09 +03:00
'UID' => 'uid' ,
2017-09-12 23:38:36 +03:00
'FN' => 'User3 @ Localhost' ,
],
[
2018-10-29 19:00:09 +03:00
'UID' => 'uid' ,
2017-09-12 23:38:36 +03:00
'FN' => 'User2 @ Localhost' ,
'CLOUD' => [
],
],
[
2018-10-29 19:00:09 +03:00
'UID' => 'uid' ,
2017-09-12 23:38:36 +03:00
'FN' => 'User @ Localhost' ,
'CLOUD' => [
'username@localhost' ,
],
],
],
false ,
2019-05-23 18:03:04 +03:00
[ 'remotes' => [], 'exact' => [ 'remotes' => [[ 'label' => 'test (remote)' , 'value' => [ 'shareType' => Share :: SHARE_TYPE_REMOTE , 'shareWith' => 'test@remote' , 'server' => 'remote' ], 'uuid' => 'test' , 'name' => 'test' ]]]],
2017-09-12 23:38:36 +03:00
false ,
true ,
],
[
'username@localhost' ,
[
[
2018-10-29 19:00:09 +03:00
'UID' => 'uid3' ,
2017-09-12 23:38:36 +03:00
'FN' => 'User3 @ Localhost' ,
],
[
2018-10-29 19:00:09 +03:00
'UID' => '2' ,
2017-09-12 23:38:36 +03:00
'FN' => 'User2 @ Localhost' ,
'CLOUD' => [
],
],
[
2018-10-29 19:00:09 +03:00
'UID' => 'uid1' ,
2017-09-12 23:38:36 +03:00
'FN' => 'User @ Localhost' ,
'CLOUD' => [
'username@localhost' ,
],
],
],
true ,
2018-10-30 12:18:58 +03:00
[ 'remotes' => [], 'exact' => [ 'remotes' => [[ 'name' => 'User @ Localhost' , 'label' => 'User @ Localhost (username@localhost)' , 'uuid' => 'uid1' , 'type' => '' , 'value' => [ 'shareType' => Share :: SHARE_TYPE_REMOTE , 'shareWith' => 'username@localhost' , 'server' => 'localhost' ]]]]],
2017-09-12 23:38:36 +03:00
true ,
true ,
],
[
'username@localhost' ,
[
[
2018-10-29 19:00:09 +03:00
'UID' => 'uid3' ,
2017-09-12 23:38:36 +03:00
'FN' => 'User3 @ Localhost' ,
],
[
2018-10-29 19:00:09 +03:00
'UID' => 'uid2' ,
2017-09-12 23:38:36 +03:00
'FN' => 'User2 @ Localhost' ,
'CLOUD' => [
],
],
[
2018-10-29 19:00:09 +03:00
'UID' => 'uid1' ,
2017-09-12 23:38:36 +03:00
'FN' => 'User @ Localhost' ,
'CLOUD' => [
'username@localhost' ,
],
],
],
false ,
2018-10-30 12:18:58 +03:00
[ 'remotes' => [], 'exact' => [ 'remotes' => [[ 'name' => 'User @ Localhost' , 'label' => 'User @ Localhost (username@localhost)' , 'uuid' => 'uid1' , 'type' => '' , 'value' => [ 'shareType' => Share :: SHARE_TYPE_REMOTE , 'shareWith' => 'username@localhost' , 'server' => 'localhost' ]]]]],
2017-09-12 23:38:36 +03:00
true ,
true ,
],
// contact with space
[
'user name@localhost' ,
[
[
2018-10-29 19:00:09 +03:00
'UID' => 'uid1' ,
2017-09-12 23:38:36 +03:00
'FN' => 'User3 @ Localhost' ,
],
[
2018-10-29 19:00:09 +03:00
'UID' => 'uid2' ,
2017-09-12 23:38:36 +03:00
'FN' => 'User2 @ Localhost' ,
'CLOUD' => [
],
],
[
2018-10-29 19:00:09 +03:00
'UID' => 'uid3' ,
2017-09-12 23:38:36 +03:00
'FN' => 'User Name @ Localhost' ,
'CLOUD' => [
'user name@localhost' ,
],
],
],
false ,
2018-10-30 12:18:58 +03:00
[ 'remotes' => [], 'exact' => [ 'remotes' => [[ 'name' => 'User Name @ Localhost' , 'label' => 'User Name @ Localhost (user name@localhost)' , 'uuid' => 'uid3' , 'type' => '' , 'value' => [ 'shareType' => Share :: SHARE_TYPE_REMOTE , 'shareWith' => 'user name@localhost' , 'server' => 'localhost' ]]]]],
2017-09-12 23:38:36 +03:00
true ,
true ,
],
// remote with space, no contact
[
'user space@remote' ,
[
[
2018-10-29 19:00:09 +03:00
'UID' => 'uid3' ,
2017-09-12 23:38:36 +03:00
'FN' => 'User3 @ Localhost' ,
],
[
2018-10-29 19:00:09 +03:00
'UID' => 'uid2' ,
2017-09-12 23:38:36 +03:00
'FN' => 'User2 @ Localhost' ,
'CLOUD' => [
],
],
[
2018-10-29 19:00:09 +03:00
'UID' => 'uid1' ,
2017-09-12 23:38:36 +03:00
'FN' => 'User @ Localhost' ,
'CLOUD' => [
'username@localhost' ,
],
],
],
false ,
2019-05-23 18:03:04 +03:00
[ 'remotes' => [], 'exact' => [ 'remotes' => [[ 'label' => 'user space (remote)' , 'value' => [ 'shareType' => Share :: SHARE_TYPE_REMOTE , 'shareWith' => 'user space@remote' , 'server' => 'remote' ], 'uuid' => 'user space' , 'name' => 'user space' ]]]],
2017-09-12 23:38:36 +03:00
false ,
true ,
],
];
}
2017-09-13 15:39:04 +03:00
public function dataTestSplitUserRemote () {
$userPrefix = [ 'user@name' , 'username' ];
$protocols = [ '' , 'http://' , 'https://' ];
$remotes = [
'localhost' ,
'local.host' ,
'dev.local.host' ,
'dev.local.host/path' ,
'dev.local.host/at@inpath' ,
'127.0.0.1' ,
'::1' ,
'::192.0.2.128' ,
'::192.0.2.128/at@inpath' ,
];
$testCases = [];
foreach ( $userPrefix as $user ) {
foreach ( $remotes as $remote ) {
foreach ( $protocols as $protocol ) {
$baseUrl = $user . '@' . $protocol . $remote ;
$testCases [] = [ $baseUrl , $user , $protocol . $remote ];
$testCases [] = [ $baseUrl . '/' , $user , $protocol . $remote ];
$testCases [] = [ $baseUrl . '/index.php' , $user , $protocol . $remote ];
$testCases [] = [ $baseUrl . '/index.php/s/token' , $user , $protocol . $remote ];
}
}
}
return $testCases ;
}
public function dataTestSplitUserRemoteError () {
2020-03-26 11:30:18 +03:00
return [
2017-09-13 15:39:04 +03:00
// Invalid path
2020-03-26 11:30:18 +03:00
[ 'user@' ],
2017-09-13 15:39:04 +03:00
// Invalid user
2020-03-26 11:30:18 +03:00
[ '@server' ],
[ 'us/er@server' ],
[ 'us:er@server' ],
2017-09-13 15:39:04 +03:00
// Invalid splitting
2020-03-26 11:30:18 +03:00
[ 'user' ],
[ '' ],
[ 'us/erserver' ],
[ 'us:erserver' ],
];
2017-09-13 15:39:04 +03:00
}
2017-09-12 23:38:36 +03:00
}