server/apps/federation/tests/BackgroundJob/GetSharedSecretTest.php

357 lines
10 KiB
PHP
Raw Normal View History

2015-11-19 16:49:43 +00:00
<?php
/**
2016-07-21 14:49:16 +00:00
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Bjoern Schiessle <bjoern@schiessle.org>
2016-05-26 17:56:05 +00:00
* @author Björn Schießle <bjoern@schiessle.org>
2016-07-21 14:49:16 +00:00
* @author Joas Schilling <coding@schilljs.com>
* @author Roeland Jago Douma <roeland@famdouma.nl>
2016-01-12 14:02:16 +00:00
* @author Thomas Müller <thomas.mueller@tmit.eu>
2015-11-19 16:49:43 +00: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/>
*
*/
namespace OCA\Federation\Tests\BackgroundJob;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Ring\Exception\RingException;
2015-11-19 16:49:43 +00:00
use OCA\Federation\BackgroundJob\GetSharedSecret;
use OCA\Files_Sharing\Tests\TestCase;
use OCA\Federation\DbHandler;
use OCA\Federation\TrustedServers;
use OCP\AppFramework\Http;
use OCP\AppFramework\Utility\ITimeFactory;
2015-11-19 16:49:43 +00:00
use OCP\BackgroundJob\IJobList;
use OCP\Http\Client\IClient;
use OCP\Http\Client\IClientService;
2015-11-19 16:49:43 +00:00
use OCP\Http\Client\IResponse;
use OCP\ILogger;
use OCP\IURLGenerator;
use OCP\OCS\IDiscoveryService;
2015-11-19 16:49:43 +00:00
/**
* Class GetSharedSecretTest
*
* @group DB
*
* @package OCA\Federation\Tests\BackgroundJob
*/
2015-11-19 16:49:43 +00:00
class GetSharedSecretTest extends TestCase {
/** @var \PHPUnit_Framework_MockObject_MockObject|IClient */
2015-11-19 16:49:43 +00:00
private $httpClient;
/** @var \PHPUnit_Framework_MockObject_MockObject|IClientService */
private $httpClientService;
/** @var \PHPUnit_Framework_MockObject_MockObject|IJobList */
2015-11-19 16:49:43 +00:00
private $jobList;
/** @var \PHPUnit_Framework_MockObject_MockObject|IURLGenerator */
2015-11-19 16:49:43 +00:00
private $urlGenerator;
/** @var \PHPUnit_Framework_MockObject_MockObject|TrustedServers */
2015-11-19 16:49:43 +00:00
private $trustedServers;
/** @var \PHPUnit_Framework_MockObject_MockObject|DbHandler */
2015-11-19 16:49:43 +00:00
private $dbHandler;
/** @var \PHPUnit_Framework_MockObject_MockObject|ILogger */
2015-11-19 16:49:43 +00:00
private $logger;
/** @var \PHPUnit_Framework_MockObject_MockObject|IResponse */
2015-11-19 16:49:43 +00:00
private $response;
/** @var \PHPUnit_Framework_MockObject_MockObject|IDiscoveryService */
private $discoverService;
/** @var \PHPUnit_Framework_MockObject_MockObject|ITimeFactory */
private $timeFactory;
2015-11-19 16:49:43 +00:00
/** @var GetSharedSecret */
private $getSharedSecret;
public function setUp() {
parent::setUp();
$this->httpClientService = $this->createMock(IClientService::class);
$this->httpClient = $this->getMockBuilder(IClient::class)->getMock();
$this->jobList = $this->getMockBuilder(IJobList::class)->getMock();
$this->urlGenerator = $this->getMockBuilder(IURLGenerator::class)->getMock();
$this->trustedServers = $this->getMockBuilder(TrustedServers::class)
2015-11-19 16:49:43 +00:00
->disableOriginalConstructor()->getMock();
$this->dbHandler = $this->getMockBuilder(DbHandler::class)
2015-11-19 16:49:43 +00:00
->disableOriginalConstructor()->getMock();
$this->logger = $this->getMockBuilder(ILogger::class)->getMock();
$this->response = $this->getMockBuilder(IResponse::class)->getMock();
$this->discoverService = $this->getMockBuilder(IDiscoveryService::class)->getMock();
$this->timeFactory = $this->createMock(ITimeFactory::class);
$this->discoverService->expects($this->any())->method('discover')->willReturn([]);
$this->httpClientService->expects($this->any())->method('newClient')->willReturn($this->httpClient);
2015-11-19 16:49:43 +00:00
$this->getSharedSecret = new GetSharedSecret(
$this->httpClientService,
2015-11-19 16:49:43 +00:00
$this->urlGenerator,
$this->jobList,
$this->trustedServers,
$this->logger,
$this->dbHandler,
$this->discoverService,
$this->timeFactory
2015-11-19 16:49:43 +00:00
);
}
/**
* @dataProvider dataTestExecute
*
* @param bool $isTrustedServer
* @param bool $retainBackgroundJob
2015-11-19 16:49:43 +00:00
*/
public function testExecute($isTrustedServer, $retainBackgroundJob) {
2015-11-19 16:49:43 +00:00
/** @var GetSharedSecret |\PHPUnit_Framework_MockObject_MockObject $getSharedSecret */
$getSharedSecret = $this->getMockBuilder('OCA\Federation\BackgroundJob\GetSharedSecret')
->setConstructorArgs(
[
$this->httpClientService,
2015-11-19 16:49:43 +00:00
$this->urlGenerator,
$this->jobList,
$this->trustedServers,
$this->logger,
$this->dbHandler,
$this->discoverService,
$this->timeFactory
2015-11-19 16:49:43 +00:00
]
)->setMethods(['parentExecute'])->getMock();
$this->invokePrivate($getSharedSecret, 'argument', [['url' => 'url', 'token' => 'token']]);
2015-11-19 16:49:43 +00:00
$this->trustedServers->expects($this->once())->method('isTrustedServer')
->with('url')->willReturn($isTrustedServer);
if ($isTrustedServer) {
$getSharedSecret->expects($this->once())->method('parentExecute');
} else {
$getSharedSecret->expects($this->never())->method('parentExecute');
}
$this->invokePrivate($getSharedSecret, 'retainJob', [$retainBackgroundJob]);
$this->jobList->expects($this->once())->method('remove');
$this->timeFactory->method('getTime')->willReturn(42);
if ($retainBackgroundJob) {
$this->jobList->expects($this->once())
->method('add')
->with(
GetSharedSecret::class,
[
'url' => 'url',
'token' => 'token',
'created' => 42,
]
);
} else {
$this->jobList->expects($this->never())->method('add');
}
2015-11-19 16:49:43 +00:00
$getSharedSecret->execute($this->jobList);
}
public function dataTestExecute() {
return [
[true, true],
[true, false],
[false, false],
2015-11-19 16:49:43 +00:00
];
}
/**
* @dataProvider dataTestRun
*
* @param int $statusCode
*/
public function testRun($statusCode) {
$target = 'targetURL';
$source = 'sourceURL';
$token = 'token';
$argument = ['url' => $target, 'token' => $token];
$this->timeFactory->method('getTime')
->willReturn(42);
2015-11-19 16:49:43 +00:00
$this->urlGenerator->expects($this->once())->method('getAbsoluteURL')->with('/')
->willReturn($source);
$this->httpClient->expects($this->once())->method('get')
->with(
$target . '/ocs/v2.php/apps/federation/api/v1/shared-secret?format=json',
[
'query' =>
[
'url' => $source,
'token' => $token
],
'timeout' => 3,
'connect_timeout' => 3,
]
)->willReturn($this->response);
$this->response->expects($this->once())->method('getStatusCode')
->willReturn($statusCode);
if (
$statusCode !== Http::STATUS_OK
&& $statusCode !== Http::STATUS_FORBIDDEN
) {
$this->dbHandler->expects($this->never())->method('addToken');
} else {
$this->dbHandler->expects($this->once())->method('addToken')->with($target, '');
}
if ($statusCode === Http::STATUS_OK) {
$this->response->expects($this->once())->method('getBody')
->willReturn('{"ocs":{"data":{"sharedSecret":"secret"}}}');
$this->trustedServers->expects($this->once())->method('addSharedSecret')
->with($target, 'secret');
} else {
$this->trustedServers->expects($this->never())->method('addSharedSecret');
}
$this->invokePrivate($this->getSharedSecret, 'run', [$argument]);
if (
$statusCode !== Http::STATUS_OK
&& $statusCode !== Http::STATUS_FORBIDDEN
) {
$this->assertTrue($this->invokePrivate($this->getSharedSecret, 'retainJob'));
} else {
$this->assertFalse($this->invokePrivate($this->getSharedSecret, 'retainJob'));
}
2015-11-19 16:49:43 +00:00
}
public function dataTestRun() {
return [
[Http::STATUS_OK],
[Http::STATUS_FORBIDDEN],
[Http::STATUS_CONFLICT],
];
}
public function testRunExpired() {
$target = 'targetURL';
$source = 'sourceURL';
$token = 'token';
$created = 42;
$argument = [
'url' => $target,
'token' => $token,
'created' => $created,
];
$this->urlGenerator->expects($this->once())
->method('getAbsoluteURL')
->with('/')
->willReturn($source);
$this->timeFactory->method('getTime')
->willReturn($created + 2592000 + 1);
$this->trustedServers->expects($this->once())
->method('setServerStatus')
->with(
$target,
TrustedServers::STATUS_FAILURE
);
$this->invokePrivate($this->getSharedSecret, 'run', [$argument]);
}
public function testRunConnectionError() {
$target = 'targetURL';
$source = 'sourceURL';
$token = 'token';
$argument = ['url' => $target, 'token' => $token];
$this->timeFactory->method('getTime')
->willReturn(42);
$this->urlGenerator
->expects($this->once())
->method('getAbsoluteURL')
->with('/')
->willReturn($source);
$this->httpClient->expects($this->once())->method('get')
->with(
$target . '/ocs/v2.php/apps/federation/api/v1/shared-secret?format=json',
[
'query' =>
[
'url' => $source,
'token' => $token
],
'timeout' => 3,
'connect_timeout' => 3,
]
)->willThrowException($this->createMock(ConnectException::class));
$this->dbHandler->expects($this->never())->method('addToken');
$this->trustedServers->expects($this->never())->method('addSharedSecret');
$this->invokePrivate($this->getSharedSecret, 'run', [$argument]);
$this->assertTrue($this->invokePrivate($this->getSharedSecret, 'retainJob'));
}
public function testRunRingException() {
$target = 'targetURL';
$source = 'sourceURL';
$token = 'token';
$argument = ['url' => $target, 'token' => $token];
$this->timeFactory->method('getTime')
->willReturn(42);
$this->urlGenerator
->expects($this->once())
->method('getAbsoluteURL')
->with('/')
->willReturn($source);
$this->httpClient->expects($this->once())->method('get')
->with(
$target . '/ocs/v2.php/apps/federation/api/v1/shared-secret?format=json',
[
'query' =>
[
'url' => $source,
'token' => $token
],
'timeout' => 3,
'connect_timeout' => 3,
]
)->willThrowException($this->createMock(RingException::class));
$this->dbHandler->expects($this->never())->method('addToken');
$this->trustedServers->expects($this->never())->method('addSharedSecret');
$this->invokePrivate($this->getSharedSecret, 'run', [$argument]);
$this->assertTrue($this->invokePrivate($this->getSharedSecret, 'retainJob'));
}
2015-11-19 16:49:43 +00:00
}