123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- <?php
- /**
- * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
- * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
- * SPDX-License-Identifier: AGPL-3.0-only
- */
- namespace OCA\Federation\Tests\BackgroundJob;
- use GuzzleHttp\Exception\ConnectException;
- use OCA\Federation\BackgroundJob\RequestSharedSecret;
- use OCA\Federation\TrustedServers;
- use OCP\AppFramework\Http;
- use OCP\AppFramework\Utility\ITimeFactory;
- use OCP\BackgroundJob\IJobList;
- use OCP\Http\Client\IClient;
- use OCP\Http\Client\IClientService;
- use OCP\Http\Client\IResponse;
- use OCP\IURLGenerator;
- use OCP\OCS\IDiscoveryService;
- use PHPUnit\Framework\MockObject\MockObject;
- use Psr\Log\LoggerInterface;
- use Test\TestCase;
- class RequestSharedSecretTest extends TestCase {
- /** @var MockObject|IClientService */
- private $httpClientService;
- /** @var MockObject|IClient */
- private $httpClient;
- /** @var MockObject|IJobList */
- private $jobList;
- /** @var MockObject|IURLGenerator */
- private $urlGenerator;
- /** @var MockObject|TrustedServers */
- private $trustedServers;
- /** @var MockObject|IResponse */
- private $response;
- /** @var MockObject|IDiscoveryService */
- private $discoveryService;
- /** @var MockObject|LoggerInterface */
- private $logger;
- /** @var MockObject|ITimeFactory */
- private $timeFactory;
- /** @var RequestSharedSecret */
- private $requestSharedSecret;
- protected function setUp(): void {
- 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)
- ->disableOriginalConstructor()->getMock();
- $this->response = $this->getMockBuilder(IResponse::class)->getMock();
- $this->discoveryService = $this->getMockBuilder(IDiscoveryService::class)->getMock();
- $this->logger = $this->createMock(LoggerInterface::class);
- $this->timeFactory = $this->createMock(ITimeFactory::class);
- $this->discoveryService->expects($this->any())->method('discover')->willReturn([]);
- $this->httpClientService->expects($this->any())->method('newClient')->willReturn($this->httpClient);
- $this->requestSharedSecret = new RequestSharedSecret(
- $this->httpClientService,
- $this->urlGenerator,
- $this->jobList,
- $this->trustedServers,
- $this->discoveryService,
- $this->logger,
- $this->timeFactory
- );
- }
- /**
- * @dataProvider dataTestStart
- *
- * @param bool $isTrustedServer
- * @param bool $retainBackgroundJob
- */
- public function testStart($isTrustedServer, $retainBackgroundJob): void {
- /** @var RequestSharedSecret |MockObject $requestSharedSecret */
- $requestSharedSecret = $this->getMockBuilder('OCA\Federation\BackgroundJob\RequestSharedSecret')
- ->setConstructorArgs(
- [
- $this->httpClientService,
- $this->urlGenerator,
- $this->jobList,
- $this->trustedServers,
- $this->discoveryService,
- $this->logger,
- $this->timeFactory
- ]
- )->setMethods(['parentStart'])->getMock();
- $this->invokePrivate($requestSharedSecret, 'argument', [['url' => 'url', 'token' => 'token']]);
- $this->trustedServers->expects($this->once())->method('isTrustedServer')
- ->with('url')->willReturn($isTrustedServer);
- if ($isTrustedServer) {
- $requestSharedSecret->expects($this->once())->method('parentStart');
- } else {
- $requestSharedSecret->expects($this->never())->method('parentStart');
- }
- $this->invokePrivate($requestSharedSecret, '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(
- RequestSharedSecret::class,
- [
- 'url' => 'url',
- 'token' => 'token',
- 'created' => 42,
- 'attempt' => 1,
- ]
- );
- } else {
- $this->jobList->expects($this->never())->method('add');
- }
- $requestSharedSecret->start($this->jobList);
- }
- public function dataTestStart() {
- return [
- [true, true],
- [true, false],
- [false, false],
- ];
- }
- /**
- * @dataProvider dataTestRun
- *
- * @param int $statusCode
- */
- public function testRun(int $statusCode, int $attempt = 0): void {
- $target = 'targetURL';
- $source = 'sourceURL';
- $token = 'token';
- $argument = ['url' => $target, 'token' => $token, 'attempt' => $attempt];
- $this->timeFactory->method('getTime')->willReturn(42);
- $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',
- [
- 'body' =>
- [
- 'url' => $source,
- 'token' => $token,
- 'format' => 'json',
- ],
- 'timeout' => 3,
- 'connect_timeout' => 3,
- ]
- )->willReturn($this->response);
- $this->response->expects($this->once())->method('getStatusCode')
- ->willReturn($statusCode);
- $this->invokePrivate($this->requestSharedSecret, 'run', [$argument]);
- if (
- $statusCode !== Http::STATUS_OK
- && ($statusCode !== Http::STATUS_FORBIDDEN || $attempt < 5)
- ) {
- $this->assertTrue($this->invokePrivate($this->requestSharedSecret, 'retainJob'));
- } else {
- $this->assertFalse($this->invokePrivate($this->requestSharedSecret, 'retainJob'));
- }
- }
- public function dataTestRun() {
- return [
- [Http::STATUS_OK],
- [Http::STATUS_FORBIDDEN, 5],
- [Http::STATUS_FORBIDDEN],
- [Http::STATUS_CONFLICT],
- ];
- }
- public function testRunExpired(): void {
- $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]);
- }
- public function testRunConnectionError(): void {
- $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('post')
- ->with(
- $target . '/ocs/v2.php/apps/federation/api/v1/request-shared-secret',
- [
- 'body' =>
- [
- 'url' => $source,
- 'token' => $token,
- 'format' => 'json',
- ],
- 'timeout' => 3,
- 'connect_timeout' => 3,
- ]
- )->willThrowException($this->createMock(ConnectException::class));
- $this->invokePrivate($this->requestSharedSecret, 'run', [$argument]);
- $this->assertTrue($this->invokePrivate($this->requestSharedSecret, 'retainJob'));
- }
- }
|