123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530 |
- <?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\LookupPlugin;
- use OC\Federation\CloudId;
- use OCP\Collaboration\Collaborators\ISearchResult;
- use OCP\Collaboration\Collaborators\SearchResultType;
- use OCP\Federation\ICloudId;
- use OCP\Federation\ICloudIdManager;
- use OCP\Http\Client\IClient;
- use OCP\Http\Client\IClientService;
- use OCP\Http\Client\IResponse;
- use OCP\IConfig;
- use OCP\IUser;
- use OCP\IUserSession;
- use OCP\Share\IShare;
- use PHPUnit\Framework\MockObject\MockObject;
- use Psr\Log\LoggerInterface;
- use Test\TestCase;
- class LookupPluginTest extends TestCase {
- /** @var IConfig|MockObject */
- protected $config;
- /** @var IClientService|MockObject */
- protected $clientService;
- /** @var IUserSession|MockObject */
- protected $userSession;
- /** @var ICloudIdManager|MockObject */
- protected $cloudIdManager;
- /** @var LookupPlugin */
- protected $plugin;
- /** @var LoggerInterface|MockObject */
- protected $logger;
- protected function setUp(): void {
- parent::setUp();
- $this->userSession = $this->createMock(IUserSession::class);
- $this->cloudIdManager = $this->createMock(ICloudIdManager::class);
- $this->config = $this->createMock(IConfig::class);
- $this->logger = $this->createMock(LoggerInterface::class);
- $this->clientService = $this->createMock(IClientService::class);
- $cloudId = $this->createMock(ICloudId::class);
- $cloudId->expects($this->any())->method('getRemote')->willReturn('myNextcloud.net');
- $user = $this->createMock(IUser::class);
- $user->expects($this->any())->method('getCloudId')->willReturn('user@myNextcloud.net');
- $this->userSession->expects($this->any())->method('getUser')
- ->willReturn($user);
- $this->cloudIdManager->expects($this->any())->method('resolveCloudId')
- ->willReturnCallback(function ($cloudId) {
- if ($cloudId === 'user@myNextcloud.net') {
- return new CloudId('user@myNextcloud.net', 'user', 'myNextcloud.net');
- }
- return new CloudId('user@someNextcloud.net', 'user', 'someNextcloud.net');
- });
- $this->plugin = new LookupPlugin(
- $this->config,
- $this->clientService,
- $this->userSession,
- $this->cloudIdManager,
- $this->logger
- );
- }
- public function testSearchNoLookupServerURI() {
- $this->config->expects($this->once())
- ->method('getAppValue')
- ->with('files_sharing', 'lookupServerEnabled', 'yes')
- ->willReturn('yes');
- $this->config->expects($this->exactly(2))
- ->method('getSystemValueBool')
- ->withConsecutive(
- ['gs.enabled', false],
- ['has_internet_connection', true],
- )->willReturnOnConsecutiveCalls(
- false,
- true,
- );
- $this->config->expects($this->once())
- ->method('getSystemValueString')
- ->with('lookup_server', 'https://lookup.nextcloud.com')
- ->willReturn('');
- $this->clientService->expects($this->never())
- ->method('newClient');
- /** @var ISearchResult|MockObject $searchResult */
- $searchResult = $this->createMock(ISearchResult::class);
- $this->plugin->search('foobar', 10, 0, $searchResult);
- }
- public function testSearchNoInternet() {
- $this->config->expects($this->once())
- ->method('getAppValue')
- ->with('files_sharing', 'lookupServerEnabled', 'yes')
- ->willReturn('yes');
- $this->config->expects($this->exactly(2))
- ->method('getSystemValueBool')
- ->withConsecutive(
- ['gs.enabled', false],
- ['has_internet_connection', true],
- )->willReturnOnConsecutiveCalls(
- false,
- false,
- );
- $this->clientService->expects($this->never())
- ->method('newClient');
- /** @var ISearchResult|MockObject $searchResult */
- $searchResult = $this->createMock(ISearchResult::class);
- $this->plugin->search('foobar', 10, 0, $searchResult);
- }
- /**
- * @dataProvider searchDataProvider
- * @param array $searchParams
- */
- public function testSearch(array $searchParams) {
- $type = new SearchResultType('lookup');
- /** @var ISearchResult|MockObject $searchResult */
- $searchResult = $this->createMock(ISearchResult::class);
- $searchResult->expects($this->once())
- ->method('addResultSet')
- ->with($type, $searchParams['expectedResult'], []);
- $this->config->expects($this->once())
- ->method('getAppValue')
- ->with('files_sharing', 'lookupServerEnabled', 'yes')
- ->willReturn('yes');
- $this->config->expects($this->exactly(2))
- ->method('getSystemValueBool')
- ->withConsecutive(
- ['gs.enabled', false],
- ['has_internet_connection', true],
- )->willReturnOnConsecutiveCalls(
- false,
- true,
- );
- $this->config->expects($this->once())
- ->method('getSystemValueString')
- ->with('lookup_server', 'https://lookup.nextcloud.com')
- ->willReturn($searchParams['server']);
- $response = $this->createMock(IResponse::class);
- $response->expects($this->once())
- ->method('getBody')
- ->willReturn(json_encode($searchParams['resultBody']));
- $client = $this->createMock(IClient::class);
- $client->expects($this->once())
- ->method('get')
- ->willReturnCallback(function ($url) use ($searchParams, $response) {
- $this->assertSame(strpos($url, $searchParams['server'] . '/users?search='), 0);
- $this->assertNotFalse(strpos($url, urlencode($searchParams['search'])));
- return $response;
- });
- $this->clientService->expects($this->once())
- ->method('newClient')
- ->willReturn($client);
- $moreResults = $this->plugin->search(
- $searchParams['search'],
- $searchParams['limit'],
- $searchParams['offset'],
- $searchResult
- );
- $this->assertFalse($moreResults);
- }
- /**
- * @dataProvider dataSearchEnableDisableLookupServer
- * @param array $searchParams
- * @param bool $GSEnabled
- * @param bool $LookupEnabled
- */
- public function testSearchEnableDisableLookupServer(array $searchParams, $GSEnabled, $LookupEnabled) {
- $type = new SearchResultType('lookup');
- /** @var ISearchResult|MockObject $searchResult */
- $searchResult = $this->createMock(ISearchResult::class);
- $this->config->expects($this->once())
- ->method('getAppValue')
- ->with('files_sharing', 'lookupServerEnabled', 'yes')
- ->willReturn($LookupEnabled ? 'yes' : 'no');
- if ($GSEnabled || $LookupEnabled) {
- $searchResult->expects($this->once())
- ->method('addResultSet')
- ->with($type, $searchParams['expectedResult'], []);
- $this->config->expects($this->exactly(2))
- ->method('getSystemValueBool')
- ->withConsecutive(
- ['gs.enabled', false],
- ['has_internet_connection', true],
- )->willReturnOnConsecutiveCalls(
- $GSEnabled,
- true,
- );
- $this->config->expects($this->once())
- ->method('getSystemValueString')
- ->with('lookup_server', 'https://lookup.nextcloud.com')
- ->willReturn($searchParams['server']);
- $response = $this->createMock(IResponse::class);
- $response->expects($this->once())
- ->method('getBody')
- ->willReturn(json_encode($searchParams['resultBody']));
- $client = $this->createMock(IClient::class);
- $client->expects($this->once())
- ->method('get')
- ->willReturnCallback(function ($url) use ($searchParams, $response) {
- $this->assertSame(strpos($url, $searchParams['server'] . '/users?search='), 0);
- $this->assertNotFalse(strpos($url, urlencode($searchParams['search'])));
- return $response;
- });
- $this->clientService->expects($this->once())
- ->method('newClient')
- ->willReturn($client);
- } else {
- $searchResult->expects($this->never())->method('addResultSet');
- $this->config->expects($this->exactly(2))
- ->method('getSystemValueBool')
- ->withConsecutive(
- ['gs.enabled', false],
- ['has_internet_connection', true],
- )->willReturnOnConsecutiveCalls(
- $GSEnabled,
- true,
- );
- }
- $moreResults = $this->plugin->search(
- $searchParams['search'],
- $searchParams['limit'],
- $searchParams['offset'],
- $searchResult
- );
- $this->assertFalse($moreResults);
- }
- public function testSearchLookupServerDisabled() {
- $this->config->expects($this->once())
- ->method('getAppValue')
- ->with('files_sharing', 'lookupServerEnabled', 'yes')
- ->willReturn('no');
- /** @var ISearchResult|MockObject $searchResult */
- $searchResult = $this->createMock(ISearchResult::class);
- $searchResult->expects($this->never())
- ->method('addResultSet');
- $searchResult->expects($this->never())
- ->method('markExactIdMatch');
- $this->assertFalse($this->plugin->search('irr', 10, 0, $searchResult));
- }
- public function dataSearchEnableDisableLookupServer() {
- $fedIDs = [
- 'foo@enceladus.moon',
- 'foobar@enceladus.moon',
- 'foongus@enceladus.moon',
- ];
- return [
- [[
- 'search' => 'foo',
- 'limit' => 10,
- 'offset' => 0,
- 'server' => 'https://lookup.example.io',
- 'resultBody' => [
- ['federationId' => $fedIDs[0]],
- ['federationId' => $fedIDs[1]],
- ['federationId' => $fedIDs[2]],
- ],
- 'expectedResult' => [
- [
- 'label' => $fedIDs[0],
- 'value' => [
- 'shareType' => IShare::TYPE_REMOTE,
- 'globalScale' => true,
- 'shareWith' => $fedIDs[0]
- ],
- 'extra' => ['federationId' => $fedIDs[0]],
- ],
- [
- 'label' => $fedIDs[1],
- 'value' => [
- 'shareType' => IShare::TYPE_REMOTE,
- 'globalScale' => true,
- 'shareWith' => $fedIDs[1]
- ],
- 'extra' => ['federationId' => $fedIDs[1]],
- ],
- [
- 'label' => $fedIDs[2],
- 'value' => [
- 'shareType' => IShare::TYPE_REMOTE,
- 'globalScale' => true,
- 'shareWith' => $fedIDs[2]
- ],
- 'extra' => ['federationId' => $fedIDs[2]],
- ],
- ]
- ],// GS , Lookup
- true, true
- ],
- [[
- 'search' => 'foo',
- 'limit' => 10,
- 'offset' => 0,
- 'server' => 'https://lookup.example.io',
- 'resultBody' => [
- ['federationId' => $fedIDs[0]],
- ['federationId' => $fedIDs[1]],
- ['federationId' => $fedIDs[2]],
- ],
- 'expectedResult' => [
- [
- 'label' => $fedIDs[0],
- 'value' => [
- 'shareType' => IShare::TYPE_REMOTE,
- 'globalScale' => true,
- 'shareWith' => $fedIDs[0]
- ],
- 'extra' => ['federationId' => $fedIDs[0]],
- ],
- [
- 'label' => $fedIDs[1],
- 'value' => [
- 'shareType' => IShare::TYPE_REMOTE,
- 'globalScale' => true,
- 'shareWith' => $fedIDs[1]
- ],
- 'extra' => ['federationId' => $fedIDs[1]],
- ],
- [
- 'label' => $fedIDs[2],
- 'value' => [
- 'shareType' => IShare::TYPE_REMOTE,
- 'globalScale' => true,
- 'shareWith' => $fedIDs[2]
- ],
- 'extra' => ['federationId' => $fedIDs[2]],
- ],
- ]
- ],// GS , Lookup
- true, false
- ],
- [[
- 'search' => 'foo',
- 'limit' => 10,
- 'offset' => 0,
- 'server' => 'https://lookup.example.io',
- 'resultBody' => [
- ['federationId' => $fedIDs[0]],
- ['federationId' => $fedIDs[1]],
- ['federationId' => $fedIDs[2]],
- ],
- 'expectedResult' => [
- [
- 'label' => $fedIDs[0],
- 'value' => [
- 'shareType' => IShare::TYPE_REMOTE,
- 'globalScale' => false,
- 'shareWith' => $fedIDs[0]
- ],
- 'extra' => ['federationId' => $fedIDs[0]],
- ],
- [
- 'label' => $fedIDs[1],
- 'value' => [
- 'shareType' => IShare::TYPE_REMOTE,
- 'globalScale' => false,
- 'shareWith' => $fedIDs[1]
- ],
- 'extra' => ['federationId' => $fedIDs[1]],
- ],
- [
- 'label' => $fedIDs[2],
- 'value' => [
- 'shareType' => IShare::TYPE_REMOTE,
- 'globalScale' => false,
- 'shareWith' => $fedIDs[2]
- ],
- 'extra' => ['federationId' => $fedIDs[2]],
- ],
- ]
- ],// GS , Lookup
- false, true
- ],
- [[
- 'search' => 'foo',
- 'limit' => 10,
- 'offset' => 0,
- 'server' => 'https://lookup.example.io',
- 'resultBody' => [
- ['federationId' => $fedIDs[0]],
- ['federationId' => $fedIDs[1]],
- ['federationId' => $fedIDs[2]],
- ],
- 'expectedResult' => [
- [
- 'label' => $fedIDs[0],
- 'value' => [
- 'shareType' => IShare::TYPE_REMOTE,
- 'shareWith' => $fedIDs[0]
- ],
- 'extra' => ['federationId' => $fedIDs[0]],
- ],
- [
- 'label' => $fedIDs[1],
- 'value' => [
- 'shareType' => IShare::TYPE_REMOTE,
- 'shareWith' => $fedIDs[1]
- ],
- 'extra' => ['federationId' => $fedIDs[1]],
- ],
- [
- 'label' => $fedIDs[2],
- 'value' => [
- 'shareType' => IShare::TYPE_REMOTE,
- 'shareWith' => $fedIDs[2]
- ],
- 'extra' => ['federationId' => $fedIDs[2]],
- ],
- ]
- ],// GS , Lookup
- false, false
- ],
- ];
- }
- public function searchDataProvider() {
- $fedIDs = [
- 'foo@enceladus.moon',
- 'foobar@enceladus.moon',
- 'foongus@enceladus.moon',
- ];
- return [
- // #0, standard search with results
- [[
- 'search' => 'foo',
- 'limit' => 10,
- 'offset' => 0,
- 'server' => 'https://lookup.example.io',
- 'resultBody' => [
- ['federationId' => $fedIDs[0]],
- ['federationId' => $fedIDs[1]],
- ['federationId' => $fedIDs[2]],
- ],
- 'expectedResult' => [
- [
- 'label' => $fedIDs[0],
- 'value' => [
- 'shareType' => IShare::TYPE_REMOTE,
- 'globalScale' => false,
- 'shareWith' => $fedIDs[0]
- ],
- 'extra' => ['federationId' => $fedIDs[0]],
- ],
- [
- 'label' => $fedIDs[1],
- 'value' => [
- 'shareType' => IShare::TYPE_REMOTE,
- 'globalScale' => false,
- 'shareWith' => $fedIDs[1]
- ],
- 'extra' => ['federationId' => $fedIDs[1]],
- ],
- [
- 'label' => $fedIDs[2],
- 'value' => [
- 'shareType' => IShare::TYPE_REMOTE,
- 'globalScale' => false,
- 'shareWith' => $fedIDs[2]
- ],
- 'extra' => ['federationId' => $fedIDs[2]],
- ],
- ]
- ]],
- // #1, search without results
- [[
- 'search' => 'foo',
- 'limit' => 10,
- 'offset' => 0,
- 'server' => 'https://lookup.example.io',
- 'resultBody' => [],
- 'expectedResult' => [],
- ]],
- ];
- }
- }
|