Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 16 additions & 18 deletions apps/federation/tests/BackgroundJob/GetSharedSecretTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
Expand Down Expand Up @@ -77,12 +78,9 @@ protected function setUp(): void {

/**
* @dataProvider dataTestExecute
*
* @param bool $isTrustedServer
* @param bool $retainBackgroundJob
*/
public function testExecute(bool $isTrustedServer, bool $retainBackgroundJob): void {
/** @var GetSharedSecret |\PHPUnit\Framework\MockObject\MockObject $getSharedSecret */
/** @var GetSharedSecret&MockObject $getSharedSecret */
$getSharedSecret = $this->getMockBuilder(GetSharedSecret::class)
->setConstructorArgs(
[
Expand All @@ -95,8 +93,10 @@ public function testExecute(bool $isTrustedServer, bool $retainBackgroundJob): v
$this->timeFactory,
$this->config,
]
)->setMethods(['parentStart'])->getMock();
$this->invokePrivate($getSharedSecret, 'argument', [['url' => 'url', 'token' => 'token']]);
)
->onlyMethods(['parentStart'])
->getMock();
self::invokePrivate($getSharedSecret, 'argument', [['url' => 'url', 'token' => 'token']]);

$this->trustedServers->expects($this->once())->method('isTrustedServer')
->with('url')->willReturn($isTrustedServer);
Expand All @@ -105,7 +105,7 @@ public function testExecute(bool $isTrustedServer, bool $retainBackgroundJob): v
} else {
$getSharedSecret->expects($this->never())->method('parentStart');
}
$this->invokePrivate($getSharedSecret, 'retainJob', [$retainBackgroundJob]);
self::invokePrivate($getSharedSecret, 'retainJob', [$retainBackgroundJob]);
$this->jobList->expects($this->once())->method('remove');

$this->timeFactory->method('getTime')->willReturn(42);
Expand All @@ -128,7 +128,7 @@ public function testExecute(bool $isTrustedServer, bool $retainBackgroundJob): v
$getSharedSecret->start($this->jobList);
}

public function dataTestExecute() {
public static function dataTestExecute(): array {
return [
[true, true],
[true, false],
Expand All @@ -138,10 +138,8 @@ public function dataTestExecute() {

/**
* @dataProvider dataTestRun
*
* @param int $statusCode
*/
public function testRun($statusCode): void {
public function testRun(int $statusCode): void {
$target = 'targetURL';
$source = 'sourceURL';
$token = 'token';
Expand Down Expand Up @@ -181,18 +179,18 @@ public function testRun($statusCode): void {
$this->trustedServers->expects($this->never())->method('addSharedSecret');
}

$this->invokePrivate($this->getSharedSecret, 'run', [$argument]);
self::invokePrivate($this->getSharedSecret, 'run', [$argument]);
if (
$statusCode !== Http::STATUS_OK
&& $statusCode !== Http::STATUS_FORBIDDEN
) {
$this->assertTrue($this->invokePrivate($this->getSharedSecret, 'retainJob'));
$this->assertTrue(self::invokePrivate($this->getSharedSecret, 'retainJob'));
} else {
$this->assertFalse($this->invokePrivate($this->getSharedSecret, 'retainJob'));
$this->assertFalse(self::invokePrivate($this->getSharedSecret, 'retainJob'));
}
}

public function dataTestRun() {
public static function dataTestRun(): array {
return [
[Http::STATUS_OK],
[Http::STATUS_FORBIDDEN],
Expand Down Expand Up @@ -227,7 +225,7 @@ public function testRunExpired(): void {
TrustedServers::STATUS_FAILURE
);

$this->invokePrivate($this->getSharedSecret, 'run', [$argument]);
self::invokePrivate($this->getSharedSecret, 'run', [$argument]);
}

public function testRunConnectionError(): void {
Expand Down Expand Up @@ -263,8 +261,8 @@ public function testRunConnectionError(): void {

$this->trustedServers->expects($this->never())->method('addSharedSecret');

$this->invokePrivate($this->getSharedSecret, 'run', [$argument]);
self::invokePrivate($this->getSharedSecret, 'run', [$argument]);

$this->assertTrue($this->invokePrivate($this->getSharedSecret, 'retainJob'));
$this->assertTrue(self::invokePrivate($this->getSharedSecret, 'retainJob'));
}
}
92 changes: 34 additions & 58 deletions apps/federation/tests/BackgroundJob/RequestSharedSecretTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
Expand All @@ -24,50 +25,28 @@
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 MockObject|IConfig */
private $config;

/** @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->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);
Expand All @@ -89,13 +68,10 @@ protected function setUp(): void {

/**
* @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')
public function testStart(bool $isTrustedServer, bool $retainBackgroundJob): void {
/** @var RequestSharedSecret&MockObject $requestSharedSecret */
$requestSharedSecret = $this->getMockBuilder(RequestSharedSecret::class)
->setConstructorArgs(
[
$this->httpClientService,
Expand All @@ -107,8 +83,10 @@ public function testStart($isTrustedServer, $retainBackgroundJob): void {
$this->timeFactory,
$this->config,
]
)->setMethods(['parentStart'])->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);
Expand All @@ -117,7 +95,7 @@ public function testStart($isTrustedServer, $retainBackgroundJob): void {
} else {
$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);
Expand All @@ -141,7 +119,7 @@ public function testStart($isTrustedServer, $retainBackgroundJob): void {
$requestSharedSecret->start($this->jobList);
}

public function dataTestStart() {
public static function dataTestStart(): array {
return [
[true, true],
[true, false],
Expand All @@ -151,8 +129,6 @@ public function dataTestStart() {

/**
* @dataProvider dataTestRun
*
* @param int $statusCode
*/
public function testRun(int $statusCode, int $attempt = 0): void {
$target = 'targetURL';
Expand Down Expand Up @@ -184,18 +160,18 @@ public function testRun(int $statusCode, int $attempt = 0): void {
$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 || $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],
Expand Down Expand Up @@ -231,7 +207,7 @@ public function testRunExpired(): void {
TrustedServers::STATUS_FAILURE
);

$this->invokePrivate($this->requestSharedSecret, 'run', [$argument]);
self::invokePrivate($this->requestSharedSecret, 'run', [$argument]);
}

public function testRunConnectionError(): void {
Expand Down Expand Up @@ -267,7 +243,7 @@ public function testRunConnectionError(): void {
]
)->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'));
}
}
49 changes: 19 additions & 30 deletions apps/federation/tests/Controller/OCSAuthAPIControllerTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
Expand All @@ -8,6 +9,7 @@
namespace OCA\Federation\Tests\Controller;

use OC\BackgroundJob\JobList;
use OCA\Federation\BackgroundJob\GetSharedSecret;
use OCA\Federation\Controller\OCSAuthAPIController;
use OCA\Federation\DbHandler;
use OCA\Federation\TrustedServers;
Expand All @@ -16,34 +18,19 @@
use OCP\IRequest;
use OCP\Security\Bruteforce\IThrottler;
use OCP\Security\ISecureRandom;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Test\TestCase;

class OCSAuthAPIControllerTest extends TestCase {
/** @var \PHPUnit\Framework\MockObject\MockObject|IRequest */
private $request;

/** @var \PHPUnit\Framework\MockObject\MockObject|ISecureRandom */
private $secureRandom;

/** @var \PHPUnit\Framework\MockObject\MockObject|JobList */
private $jobList;

/** @var \PHPUnit\Framework\MockObject\MockObject|TrustedServers */
private $trustedServers;

/** @var \PHPUnit\Framework\MockObject\MockObject|DbHandler */
private $dbHandler;

/** @var \PHPUnit\Framework\MockObject\MockObject|LoggerInterface */
private $logger;

/** @var \PHPUnit\Framework\MockObject\MockObject|ITimeFactory */
private $timeFactory;

/** @var \PHPUnit\Framework\MockObject\MockObject|IThrottler */
private $throttler;

private IRequest&MockObject $request;
private ISecureRandom&MockObject $secureRandom;
private JobList&MockObject $jobList;
private TrustedServers&MockObject $trustedServers;
private DbHandler&MockObject $dbHandler;
private LoggerInterface&MockObject $logger;
private ITimeFactory&MockObject $timeFactory;
private IThrottler&MockObject $throttler;
private OCSAuthAPIController $ocsAuthApi;

/** @var int simulated timestamp */
Expand Down Expand Up @@ -91,7 +78,7 @@ public function testRequestSharedSecret(string $token, string $localToken, bool

if ($ok) {
$this->jobList->expects($this->once())->method('add')
->with('OCA\Federation\BackgroundJob\GetSharedSecret', ['url' => $url, 'token' => $token, 'created' => $this->currentTime]);
->with(GetSharedSecret::class, ['url' => $url, 'token' => $token, 'created' => $this->currentTime]);
} else {
$this->jobList->expects($this->never())->method('add');
$this->jobList->expects($this->never())->method('remove');
Expand All @@ -111,7 +98,7 @@ public function testRequestSharedSecret(string $token, string $localToken, bool
}
}

public function dataTestRequestSharedSecret() {
public static function dataTestRequestSharedSecret(): array {
return [
['token2', 'token1', true, true],
['token1', 'token2', false, false],
Expand All @@ -126,8 +113,8 @@ public function testGetSharedSecret(bool $isTrustedServer, bool $isValidToken, b
$url = 'url';
$token = 'token';

/** @var OCSAuthAPIController | \PHPUnit\Framework\MockObject\MockObject $ocsAuthApi */
$ocsAuthApi = $this->getMockBuilder('OCA\Federation\Controller\OCSAuthAPIController')
/** @var OCSAuthAPIController&MockObject $ocsAuthApi */
$ocsAuthApi = $this->getMockBuilder(OCSAuthAPIController::class)
->setConstructorArgs(
[
'federation',
Expand All @@ -140,7 +127,9 @@ public function testGetSharedSecret(bool $isTrustedServer, bool $isValidToken, b
$this->timeFactory,
$this->throttler
]
)->setMethods(['isValidToken'])->getMock();
)
->onlyMethods(['isValidToken'])
->getMock();

$this->trustedServers
->expects($this->any())
Expand Down Expand Up @@ -171,7 +160,7 @@ public function testGetSharedSecret(bool $isTrustedServer, bool $isValidToken, b
}
}

public function dataTestGetSharedSecret() {
public static function dataTestGetSharedSecret(): array {
return [
[true, true, true],
[false, true, false],
Expand Down
Loading
Loading