2015-11-19 19:49:43 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2015-11-19 19:49:43 +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/>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
namespace OCA\Federation\Tests\BackgroundJob;
|
|
|
|
|
|
|
|
|
|
|
|
use OCA\Federation\BackgroundJob\RequestSharedSecret;
|
2016-05-09 16:37:51 +03:00
|
|
|
use OCA\Federation\DbHandler;
|
|
|
|
use OCA\Federation\TrustedServers;
|
2015-11-19 19:49:43 +03:00
|
|
|
use OCP\AppFramework\Http;
|
2017-08-01 12:23:40 +03:00
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
2016-05-09 16:37:51 +03:00
|
|
|
use OCP\BackgroundJob\IJobList;
|
|
|
|
use OCP\Http\Client\IClient;
|
2017-07-28 16:37:09 +03:00
|
|
|
use OCP\Http\Client\IClientService;
|
2016-05-09 16:37:51 +03:00
|
|
|
use OCP\Http\Client\IResponse;
|
2017-07-28 16:37:09 +03:00
|
|
|
use OCP\ILogger;
|
2016-05-09 16:37:51 +03:00
|
|
|
use OCP\IURLGenerator;
|
2017-03-09 13:36:54 +03:00
|
|
|
use OCP\OCS\IDiscoveryService;
|
2015-11-19 19:49:43 +03:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class RequestSharedSecretTest extends TestCase {
|
|
|
|
|
2017-08-01 12:23:40 +03:00
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|IClientService */
|
2017-07-28 16:37:09 +03:00
|
|
|
private $httpClientService;
|
|
|
|
|
2017-08-01 12:23:40 +03:00
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|IClient */
|
2015-11-19 19:49:43 +03:00
|
|
|
private $httpClient;
|
|
|
|
|
2017-08-01 12:23:40 +03:00
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|IJobList */
|
2015-11-19 19:49:43 +03:00
|
|
|
private $jobList;
|
|
|
|
|
2017-08-01 12:23:40 +03:00
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|IURLGenerator */
|
2015-11-19 19:49:43 +03:00
|
|
|
private $urlGenerator;
|
|
|
|
|
2017-08-01 12:23:40 +03:00
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|DbHandler */
|
2015-11-19 19:49:43 +03:00
|
|
|
private $dbHandler;
|
|
|
|
|
2017-08-01 12:23:40 +03:00
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|TrustedServers */
|
2015-11-19 19:49:43 +03:00
|
|
|
private $trustedServers;
|
|
|
|
|
2017-08-01 12:23:40 +03:00
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|IResponse */
|
2015-11-19 19:49:43 +03:00
|
|
|
private $response;
|
|
|
|
|
2017-08-01 12:23:40 +03:00
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|IDiscoveryService */
|
2017-03-09 13:36:54 +03:00
|
|
|
private $discoveryService;
|
|
|
|
|
2017-08-01 12:23:40 +03:00
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|ILogger */
|
2017-07-28 16:37:09 +03:00
|
|
|
private $logger;
|
|
|
|
|
2017-08-01 12:23:40 +03:00
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|ITimeFactory */
|
|
|
|
private $timeFactory;
|
|
|
|
|
2015-11-19 19:49:43 +03:00
|
|
|
/** @var RequestSharedSecret */
|
|
|
|
private $requestSharedSecret;
|
|
|
|
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
2017-07-28 16:37:09 +03:00
|
|
|
$this->httpClientService = $this->createMock(IClientService::class);
|
2016-08-31 21:54:17 +03:00
|
|
|
$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 19:49:43 +03:00
|
|
|
->disableOriginalConstructor()->getMock();
|
2016-08-31 21:54:17 +03:00
|
|
|
$this->dbHandler = $this->getMockBuilder(DbHandler::class)
|
2015-11-19 19:49:43 +03:00
|
|
|
->disableOriginalConstructor()->getMock();
|
2016-08-31 21:54:17 +03:00
|
|
|
$this->response = $this->getMockBuilder(IResponse::class)->getMock();
|
2017-03-09 13:36:54 +03:00
|
|
|
$this->discoveryService = $this->getMockBuilder(IDiscoveryService::class)->getMock();
|
2017-07-28 16:37:09 +03:00
|
|
|
$this->logger = $this->createMock(ILogger::class);
|
2017-08-01 12:23:40 +03:00
|
|
|
$this->timeFactory = $this->createMock(ITimeFactory::class);
|
2017-03-09 13:36:54 +03:00
|
|
|
|
|
|
|
$this->discoveryService->expects($this->any())->method('discover')->willReturn([]);
|
2017-07-28 16:37:09 +03:00
|
|
|
$this->httpClientService->expects($this->any())->method('newClient')->willReturn($this->httpClient);
|
2015-11-19 19:49:43 +03:00
|
|
|
|
|
|
|
$this->requestSharedSecret = new RequestSharedSecret(
|
2017-07-28 16:37:09 +03:00
|
|
|
$this->httpClientService,
|
2015-11-19 19:49:43 +03:00
|
|
|
$this->urlGenerator,
|
|
|
|
$this->jobList,
|
|
|
|
$this->trustedServers,
|
2017-03-09 13:36:54 +03:00
|
|
|
$this->dbHandler,
|
2017-07-28 16:37:09 +03:00
|
|
|
$this->discoveryService,
|
2017-08-01 12:23:40 +03:00
|
|
|
$this->logger,
|
|
|
|
$this->timeFactory
|
2015-11-19 19:49:43 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataTestExecute
|
|
|
|
*
|
|
|
|
* @param bool $isTrustedServer
|
2016-03-18 15:22:05 +03:00
|
|
|
* @param bool $retainBackgroundJob
|
2015-11-19 19:49:43 +03:00
|
|
|
*/
|
2016-03-18 15:22:05 +03:00
|
|
|
public function testExecute($isTrustedServer, $retainBackgroundJob) {
|
2015-11-19 19:49:43 +03:00
|
|
|
/** @var RequestSharedSecret |\PHPUnit_Framework_MockObject_MockObject $requestSharedSecret */
|
|
|
|
$requestSharedSecret = $this->getMockBuilder('OCA\Federation\BackgroundJob\RequestSharedSecret')
|
|
|
|
->setConstructorArgs(
|
|
|
|
[
|
2017-07-28 16:37:09 +03:00
|
|
|
$this->httpClientService,
|
2015-11-19 19:49:43 +03:00
|
|
|
$this->urlGenerator,
|
|
|
|
$this->jobList,
|
|
|
|
$this->trustedServers,
|
2017-03-09 13:36:54 +03:00
|
|
|
$this->dbHandler,
|
2017-07-28 16:37:09 +03:00
|
|
|
$this->discoveryService,
|
2017-08-01 12:23:40 +03:00
|
|
|
$this->logger,
|
|
|
|
$this->timeFactory
|
2015-11-19 19:49:43 +03:00
|
|
|
]
|
2017-08-01 12:23:40 +03:00
|
|
|
)->setMethods(['parentExecute'])->getMock();
|
|
|
|
$this->invokePrivate($requestSharedSecret, 'argument', [['url' => 'url', 'token' => 'token']]);
|
2015-11-19 19:49:43 +03:00
|
|
|
|
|
|
|
$this->trustedServers->expects($this->once())->method('isTrustedServer')
|
|
|
|
->with('url')->willReturn($isTrustedServer);
|
|
|
|
if ($isTrustedServer) {
|
|
|
|
$requestSharedSecret->expects($this->once())->method('parentExecute');
|
|
|
|
} else {
|
|
|
|
$requestSharedSecret->expects($this->never())->method('parentExecute');
|
|
|
|
}
|
2016-03-18 15:22:05 +03:00
|
|
|
$this->invokePrivate($requestSharedSecret, 'retainJob', [$retainBackgroundJob]);
|
2017-07-28 16:37:09 +03:00
|
|
|
$this->jobList->expects($this->once())->method('remove');
|
2017-08-01 12:23:40 +03:00
|
|
|
|
|
|
|
$this->timeFactory->method('getTime')->willReturn(42);
|
|
|
|
|
2016-03-18 15:22:05 +03:00
|
|
|
if ($retainBackgroundJob) {
|
2017-08-01 12:23:40 +03:00
|
|
|
$this->jobList->expects($this->once())
|
|
|
|
->method('add')
|
|
|
|
->with(
|
|
|
|
RequestSharedSecret::class,
|
|
|
|
[
|
|
|
|
'url' => 'url',
|
|
|
|
'token' => 'token',
|
|
|
|
'created' => 42,
|
|
|
|
]
|
|
|
|
);
|
2016-03-18 15:22:05 +03:00
|
|
|
} else {
|
2017-08-01 12:23:40 +03:00
|
|
|
$this->jobList->expects($this->never())->method('add');
|
2016-03-18 15:22:05 +03:00
|
|
|
}
|
2015-11-19 19:49:43 +03:00
|
|
|
|
|
|
|
$requestSharedSecret->execute($this->jobList);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataTestExecute() {
|
|
|
|
return [
|
2016-03-18 15:22:05 +03:00
|
|
|
[true, true],
|
|
|
|
[true, false],
|
|
|
|
[false, false],
|
2015-11-19 19:49:43 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataTestRun
|
|
|
|
*
|
|
|
|
* @param int $statusCode
|
|
|
|
*/
|
|
|
|
public function testRun($statusCode) {
|
|
|
|
|
|
|
|
$target = 'targetURL';
|
|
|
|
$source = 'sourceURL';
|
|
|
|
$token = 'token';
|
|
|
|
|
|
|
|
$argument = ['url' => $target, 'token' => $token];
|
|
|
|
|
2017-08-01 12:23:40 +03:00
|
|
|
$this->timeFactory->method('getTime')->willReturn(42);
|
|
|
|
|
2015-11-19 19:49:43 +03:00
|
|
|
$this->urlGenerator->expects($this->once())->method('getAbsoluteURL')->with('/')
|
|
|
|
->willReturn($source);
|
|
|
|
$this->httpClient->expects($this->once())->method('post')
|
|
|
|
->with(
|
|
|
|
$target . '/ocs/v2.php/apps/federation/api/v1/request-shared-secret?format=json',
|
|
|
|
[
|
|
|
|
'body' =>
|
|
|
|
[
|
|
|
|
'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');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($statusCode === Http::STATUS_FORBIDDEN) {
|
|
|
|
$this->dbHandler->expects($this->once())->method('addToken')->with($target, '');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->invokePrivate($this->requestSharedSecret, 'run', [$argument]);
|
2016-03-18 15:22:05 +03:00
|
|
|
if (
|
|
|
|
$statusCode !== Http::STATUS_OK
|
|
|
|
&& $statusCode !== Http::STATUS_FORBIDDEN
|
|
|
|
) {
|
|
|
|
$this->assertTrue($this->invokePrivate($this->requestSharedSecret, 'retainJob'));
|
|
|
|
} else {
|
|
|
|
$this->assertFalse($this->invokePrivate($this->requestSharedSecret, 'retainJob'));
|
|
|
|
}
|
2015-11-19 19:49:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function dataTestRun() {
|
|
|
|
return [
|
|
|
|
[Http::STATUS_OK],
|
|
|
|
[Http::STATUS_FORBIDDEN],
|
|
|
|
[Http::STATUS_CONFLICT],
|
|
|
|
];
|
|
|
|
}
|
2017-08-01 12:23:40 +03:00
|
|
|
|
|
|
|
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->requestSharedSecret, 'run', [$argument]);
|
|
|
|
}
|
2015-11-19 19:49:43 +03:00
|
|
|
}
|