diff options
Diffstat (limited to 'apps/federation/tests/BackgroundJob/RequestSharedSecretTest.php')
-rw-r--r-- | apps/federation/tests/BackgroundJob/RequestSharedSecretTest.php | 179 |
1 files changed, 71 insertions, 108 deletions
diff --git a/apps/federation/tests/BackgroundJob/RequestSharedSecretTest.php b/apps/federation/tests/BackgroundJob/RequestSharedSecretTest.php index d013ad221bc..6ef579c7483 100644 --- a/apps/federation/tests/BackgroundJob/RequestSharedSecretTest.php +++ b/apps/federation/tests/BackgroundJob/RequestSharedSecretTest.php @@ -1,27 +1,10 @@ <?php + +declare(strict_types=1); /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Bjoern Schiessle <bjoern@schiessle.org> - * @author Björn Schießle <bjoern@schiessle.org> - * @author Joas Schilling <coding@schilljs.com> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @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/> - * + * 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; @@ -34,56 +17,39 @@ use OCP\BackgroundJob\IJobList; use OCP\Http\Client\IClient; use OCP\Http\Client\IClientService; use OCP\Http\Client\IResponse; -use OCP\ILogger; +use OCP\IConfig; use OCP\IURLGenerator; use OCP\OCS\IDiscoveryService; +use PHPUnit\Framework\MockObject\MockObject; +use Psr\Log\LoggerInterface; use Test\TestCase; class RequestSharedSecretTest extends TestCase { - - /** @var \PHPUnit\Framework\MockObject\MockObject|IClientService */ - private $httpClientService; - - /** @var \PHPUnit\Framework\MockObject\MockObject|IClient */ - private $httpClient; - - /** @var \PHPUnit\Framework\MockObject\MockObject|IJobList */ - private $jobList; - - /** @var \PHPUnit\Framework\MockObject\MockObject|IURLGenerator */ - private $urlGenerator; - - /** @var \PHPUnit\Framework\MockObject\MockObject|TrustedServers */ - private $trustedServers; - - /** @var \PHPUnit\Framework\MockObject\MockObject|IResponse */ - private $response; - - /** @var \PHPUnit\Framework\MockObject\MockObject|IDiscoveryService */ - private $discoveryService; - - /** @var \PHPUnit\Framework\MockObject\MockObject|ILogger */ - private $logger; - - /** @var \PHPUnit\Framework\MockObject\MockObject|ITimeFactory */ - private $timeFactory; - - /** @var RequestSharedSecret */ - private $requestSharedSecret; + private IClientService&MockObject $httpClientService; + private IClient&MockObject $httpClient; + private IJobList&MockObject $jobList; + private IURLGenerator&MockObject $urlGenerator; + private TrustedServers&MockObject $trustedServers; + private IResponse&MockObject $response; + private IDiscoveryService&MockObject $discoveryService; + private LoggerInterface&MockObject $logger; + private ITimeFactory&MockObject $timeFactory; + private IConfig&MockObject $config; + private RequestSharedSecret $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(ILogger::class); + $this->httpClient = $this->createMock(IClient::class); + $this->jobList = $this->createMock(IJobList::class); + $this->urlGenerator = $this->createMock(IURLGenerator::class); + $this->trustedServers = $this->createMock(TrustedServers::class); + $this->response = $this->createMock(IResponse::class); + $this->discoveryService = $this->createMock(IDiscoveryService::class); + $this->logger = $this->createMock(LoggerInterface::class); $this->timeFactory = $this->createMock(ITimeFactory::class); + $this->config = $this->createMock(IConfig::class); $this->discoveryService->expects($this->any())->method('discover')->willReturn([]); $this->httpClientService->expects($this->any())->method('newClient')->willReturn($this->httpClient); @@ -95,19 +61,15 @@ class RequestSharedSecretTest extends TestCase { $this->trustedServers, $this->discoveryService, $this->logger, - $this->timeFactory + $this->timeFactory, + $this->config, ); } - /** - * @dataProvider dataTestExecute - * - * @param bool $isTrustedServer - * @param bool $retainBackgroundJob - */ - public function testExecute($isTrustedServer, $retainBackgroundJob) { - /** @var RequestSharedSecret |\PHPUnit\Framework\MockObject\MockObject $requestSharedSecret */ - $requestSharedSecret = $this->getMockBuilder('OCA\Federation\BackgroundJob\RequestSharedSecret') + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestStart')] + public function testStart(bool $isTrustedServer, bool $retainBackgroundJob): void { + /** @var RequestSharedSecret&MockObject $requestSharedSecret */ + $requestSharedSecret = $this->getMockBuilder(RequestSharedSecret::class) ->setConstructorArgs( [ $this->httpClientService, @@ -116,19 +78,22 @@ class RequestSharedSecretTest extends TestCase { $this->trustedServers, $this->discoveryService, $this->logger, - $this->timeFactory + $this->timeFactory, + $this->config, ] - )->setMethods(['parentExecute'])->getMock(); - $this->invokePrivate($requestSharedSecret, 'argument', [['url' => 'url', 'token' => 'token']]); + ) + ->onlyMethods(['parentStart']) + ->getMock(); + self::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('parentExecute'); + $requestSharedSecret->expects($this->once())->method('parentStart'); } else { - $requestSharedSecret->expects($this->never())->method('parentExecute'); + $requestSharedSecret->expects($this->never())->method('parentStart'); } - $this->invokePrivate($requestSharedSecret, 'retainJob', [$retainBackgroundJob]); + self::invokePrivate($requestSharedSecret, 'retainJob', [$retainBackgroundJob]); $this->jobList->expects($this->once())->method('remove'); $this->timeFactory->method('getTime')->willReturn(42); @@ -142,16 +107,17 @@ class RequestSharedSecretTest extends TestCase { 'url' => 'url', 'token' => 'token', 'created' => 42, + 'attempt' => 1, ] ); } else { $this->jobList->expects($this->never())->method('add'); } - $requestSharedSecret->execute($this->jobList); + $requestSharedSecret->start($this->jobList); } - public function dataTestExecute() { + public static function dataTestStart(): array { return [ [true, true], [true, false], @@ -159,17 +125,13 @@ class RequestSharedSecretTest extends TestCase { ]; } - /** - * @dataProvider dataTestRun - * - * @param int $statusCode - */ - public function testRun($statusCode) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestRun')] + public function testRun(int $statusCode, int $attempt = 0): void { $target = 'targetURL'; $source = 'sourceURL'; $token = 'token'; - $argument = ['url' => $target, 'token' => $token]; + $argument = ['url' => $target, 'token' => $token, 'attempt' => $attempt]; $this->timeFactory->method('getTime')->willReturn(42); @@ -179,40 +141,41 @@ class RequestSharedSecretTest extends TestCase { ->with( $target . '/ocs/v2.php/apps/federation/api/v1/request-shared-secret', [ - 'body' => - [ - 'url' => $source, - 'token' => $token, - 'format' => 'json', - ], + 'body' => [ + 'url' => $source, + 'token' => $token, + 'format' => 'json', + ], 'timeout' => 3, 'connect_timeout' => 3, + 'verify' => true, ] )->willReturn($this->response); $this->response->expects($this->once())->method('getStatusCode') ->willReturn($statusCode); - $this->invokePrivate($this->requestSharedSecret, 'run', [$argument]); + self::invokePrivate($this->requestSharedSecret, 'run', [$argument]); if ( $statusCode !== Http::STATUS_OK - && $statusCode !== Http::STATUS_FORBIDDEN + && ($statusCode !== Http::STATUS_FORBIDDEN || $attempt < 5) ) { - $this->assertTrue($this->invokePrivate($this->requestSharedSecret, 'retainJob')); + $this->assertTrue(self::invokePrivate($this->requestSharedSecret, 'retainJob')); } else { - $this->assertFalse($this->invokePrivate($this->requestSharedSecret, 'retainJob')); + $this->assertFalse(self::invokePrivate($this->requestSharedSecret, 'retainJob')); } } - public function dataTestRun() { + public static function dataTestRun(): array { return [ [Http::STATUS_OK], + [Http::STATUS_FORBIDDEN, 5], [Http::STATUS_FORBIDDEN], [Http::STATUS_CONFLICT], ]; } - public function testRunExpired() { + public function testRunExpired(): void { $target = 'targetURL'; $source = 'sourceURL'; $token = 'token'; @@ -239,10 +202,10 @@ class RequestSharedSecretTest extends TestCase { TrustedServers::STATUS_FAILURE ); - $this->invokePrivate($this->requestSharedSecret, 'run', [$argument]); + self::invokePrivate($this->requestSharedSecret, 'run', [$argument]); } - public function testRunConnectionError() { + public function testRunConnectionError(): void { $target = 'targetURL'; $source = 'sourceURL'; $token = 'token'; @@ -263,18 +226,18 @@ class RequestSharedSecretTest extends TestCase { ->with( $target . '/ocs/v2.php/apps/federation/api/v1/request-shared-secret', [ - 'body' => - [ - 'url' => $source, - 'token' => $token, - 'format' => 'json', - ], + 'body' => [ + 'url' => $source, + 'token' => $token, + 'format' => 'json', + ], 'timeout' => 3, 'connect_timeout' => 3, + 'verify' => true, ] )->willThrowException($this->createMock(ConnectException::class)); - $this->invokePrivate($this->requestSharedSecret, 'run', [$argument]); - $this->assertTrue($this->invokePrivate($this->requestSharedSecret, 'retainJob')); + self::invokePrivate($this->requestSharedSecret, 'run', [$argument]); + $this->assertTrue(self::invokePrivate($this->requestSharedSecret, 'retainJob')); } } |