Use ITimeFactory

* Inject the timefacotry so we can mock it properly in the tests.
* Extended unit tests to cover the new paths

Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
This commit is contained in:
Roeland Jago Douma 2017-08-01 11:23:40 +02:00
parent 3ffff08819
commit bc54e6ff7c
No known key found for this signature in database
GPG Key ID: F941078878347C0C
9 changed files with 230 additions and 102 deletions

View File

@ -32,6 +32,7 @@ use OCA\Federation\Middleware\AddServerMiddleware;
use OCA\Federation\SyncFederationAddressBooks; use OCA\Federation\SyncFederationAddressBooks;
use OCA\Federation\TrustedServers; use OCA\Federation\TrustedServers;
use OCP\AppFramework\IAppContainer; use OCP\AppFramework\IAppContainer;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\SabrePluginEvent; use OCP\SabrePluginEvent;
use OCP\Util; use OCP\Util;
use Sabre\DAV\Auth\Plugin; use Sabre\DAV\Auth\Plugin;
@ -74,7 +75,8 @@ class Application extends \OCP\AppFramework\App {
$server->getJobList(), $server->getJobList(),
$server->getSecureRandom(), $server->getSecureRandom(),
$server->getConfig(), $server->getConfig(),
$server->getEventDispatcher() $server->getEventDispatcher(),
$server->query(ITimeFactory::class)
); );
}); });

View File

@ -32,6 +32,7 @@ use OC\BackgroundJob\Job;
use OCA\Federation\DbHandler; use OCA\Federation\DbHandler;
use OCA\Federation\TrustedServers; use OCA\Federation\TrustedServers;
use OCP\AppFramework\Http; use OCP\AppFramework\Http;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList; use OCP\BackgroundJob\IJobList;
use OCP\Http\Client\IClient; use OCP\Http\Client\IClient;
use OCP\Http\Client\IClientService; use OCP\Http\Client\IClientService;
@ -70,6 +71,9 @@ class GetSharedSecret extends Job{
/** @var ILogger */ /** @var ILogger */
private $logger; private $logger;
/** @var ITimeFactory */
private $timeFactory;
/** @var bool */ /** @var bool */
protected $retainJob = false; protected $retainJob = false;
@ -90,6 +94,7 @@ class GetSharedSecret extends Job{
* @param ILogger $logger * @param ILogger $logger
* @param DbHandler $dbHandler * @param DbHandler $dbHandler
* @param IDiscoveryService $ocsDiscoveryService * @param IDiscoveryService $ocsDiscoveryService
* @param ITimeFactory $timeFactory
*/ */
public function __construct( public function __construct(
IClientService $httpClientService, IClientService $httpClientService,
@ -98,7 +103,8 @@ class GetSharedSecret extends Job{
TrustedServers $trustedServers, TrustedServers $trustedServers,
ILogger $logger, ILogger $logger,
DbHandler $dbHandler, DbHandler $dbHandler,
IDiscoveryService $ocsDiscoveryService IDiscoveryService $ocsDiscoveryService,
ITimeFactory $timeFactory
) { ) {
$this->logger = $logger; $this->logger = $logger;
$this->httpClient = $httpClientService->newClient(); $this->httpClient = $httpClientService->newClient();
@ -107,6 +113,7 @@ class GetSharedSecret extends Job{
$this->dbHandler = $dbHandler; $this->dbHandler = $dbHandler;
$this->ocsDiscoveryService = $ocsDiscoveryService; $this->ocsDiscoveryService = $ocsDiscoveryService;
$this->trustedServers = $trustedServers; $this->trustedServers = $trustedServers;
$this->timeFactory = $timeFactory;
} }
/** /**
@ -125,7 +132,7 @@ class GetSharedSecret extends Job{
$jobList->remove($this, $this->argument); $jobList->remove($this, $this->argument);
if ($this->retainJob) { if ($this->retainJob) {
$this->reAddJob($jobList, $this->argument); $this->reAddJob($this->argument);
} }
} }
@ -141,8 +148,8 @@ class GetSharedSecret extends Job{
protected function run($argument) { protected function run($argument) {
$target = $argument['url']; $target = $argument['url'];
$created = isset($argument['created']) ? (int)$argument['created'] : time(); $created = isset($argument['created']) ? (int)$argument['created'] : $this->timeFactory->getTime();
$currentTime = time(); $currentTime = $this->timeFactory->getTime();
$source = $this->urlGenerator->getAbsoluteURL('/'); $source = $this->urlGenerator->getAbsoluteURL('/');
$source = rtrim($source, '/'); $source = rtrim($source, '/');
$token = $argument['token']; $token = $argument['token'];
@ -158,7 +165,7 @@ class GetSharedSecret extends Job{
$endPoints = $this->ocsDiscoveryService->discover($target, 'FEDERATED_SHARING'); $endPoints = $this->ocsDiscoveryService->discover($target, 'FEDERATED_SHARING');
$endPoint = isset($endPoints['shared-secret']) ? $endPoints['shared-secret'] : $this->defaultEndPoint; $endPoint = isset($endPoints['shared-secret']) ? $endPoints['shared-secret'] : $this->defaultEndPoint;
// make sure that we have a well formated url // make sure that we have a well formatted url
$url = rtrim($target, '/') . '/' . trim($endPoint, '/') . $this->format; $url = rtrim($target, '/') . '/' . trim($endPoint, '/') . $this->format;
$result = null; $result = null;
@ -223,12 +230,11 @@ class GetSharedSecret extends Job{
/** /**
* re-add background job * re-add background job
* *
* @param IJobList $jobList
* @param array $argument * @param array $argument
*/ */
protected function reAddJob(IJobList $jobList, array $argument) { protected function reAddJob(array $argument) {
$url = $argument['url']; $url = $argument['url'];
$created = isset($argument['created']) ? (int)$argument['created'] : time(); $created = isset($argument['created']) ? (int)$argument['created'] : $this->timeFactory->getTime();
$token = $argument['token']; $token = $argument['token'];
$this->jobList->add( $this->jobList->add(
GetSharedSecret::class, GetSharedSecret::class,

View File

@ -33,6 +33,7 @@ use OC\BackgroundJob\Job;
use OCA\Federation\DbHandler; use OCA\Federation\DbHandler;
use OCA\Federation\TrustedServers; use OCA\Federation\TrustedServers;
use OCP\AppFramework\Http; use OCP\AppFramework\Http;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList; use OCP\BackgroundJob\IJobList;
use OCP\Http\Client\IClient; use OCP\Http\Client\IClient;
use OCP\Http\Client\IClientService; use OCP\Http\Client\IClientService;
@ -70,6 +71,9 @@ class RequestSharedSecret extends Job {
/** @var ILogger */ /** @var ILogger */
private $logger; private $logger;
/** @var ITimeFactory */
private $timeFactory;
/** @var bool */ /** @var bool */
protected $retainJob = false; protected $retainJob = false;
@ -90,6 +94,7 @@ class RequestSharedSecret extends Job {
* @param DbHandler $dbHandler * @param DbHandler $dbHandler
* @param IDiscoveryService $ocsDiscoveryService * @param IDiscoveryService $ocsDiscoveryService
* @param ILogger $logger * @param ILogger $logger
* @param ITimeFactory $timeFactory
*/ */
public function __construct( public function __construct(
IClientService $httpClientService, IClientService $httpClientService,
@ -98,7 +103,8 @@ class RequestSharedSecret extends Job {
TrustedServers $trustedServers, TrustedServers $trustedServers,
DbHandler $dbHandler, DbHandler $dbHandler,
IDiscoveryService $ocsDiscoveryService, IDiscoveryService $ocsDiscoveryService,
ILogger $logger ILogger $logger,
ITimeFactory $timeFactory
) { ) {
$this->httpClient = $httpClientService->newClient(); $this->httpClient = $httpClientService->newClient();
$this->jobList = $jobList; $this->jobList = $jobList;
@ -107,6 +113,7 @@ class RequestSharedSecret extends Job {
$this->logger = $logger; $this->logger = $logger;
$this->ocsDiscoveryService = $ocsDiscoveryService; $this->ocsDiscoveryService = $ocsDiscoveryService;
$this->trustedServers = $trustedServers; $this->trustedServers = $trustedServers;
$this->timeFactory = $timeFactory;
} }
@ -126,7 +133,7 @@ class RequestSharedSecret extends Job {
$jobList->remove($this, $this->argument); $jobList->remove($this, $this->argument);
if ($this->retainJob) { if ($this->retainJob) {
$this->reAddJob($jobList, $this->argument); $this->reAddJob($this->argument);
} }
} }
@ -143,8 +150,8 @@ class RequestSharedSecret extends Job {
protected function run($argument) { protected function run($argument) {
$target = $argument['url']; $target = $argument['url'];
$created = isset($argument['created']) ? (int)$argument['created'] : time(); $created = isset($argument['created']) ? (int)$argument['created'] : $this->timeFactory->getTime();
$currentTime = time(); $currentTime = $this->timeFactory->getTime();
$source = $this->urlGenerator->getAbsoluteURL('/'); $source = $this->urlGenerator->getAbsoluteURL('/');
$source = rtrim($source, '/'); $source = rtrim($source, '/');
$token = $argument['token']; $token = $argument['token'];
@ -208,16 +215,14 @@ class RequestSharedSecret extends Job {
/** /**
* re-add background job * re-add background job
* *
* @param IJobList $jobList
* @param array $argument * @param array $argument
*/ */
protected function reAddJob(IJobList $jobList, array $argument) { protected function reAddJob(array $argument) {
$url = $argument['url']; $url = $argument['url'];
$created = isset($argument['created']) ? (int)$argument['created'] : time(); $created = isset($argument['created']) ? (int)$argument['created'] : $this->timeFactory->getTime();
$token = $argument['token']; $token = $argument['token'];
$jobList->add( $this->jobList->add(
RequestSharedSecret::class, RequestSharedSecret::class,
[ [
'url' => $url, 'url' => $url,

View File

@ -32,6 +32,7 @@ use OCA\Federation\TrustedServers;
use OCP\AppFramework\Http; use OCP\AppFramework\Http;
use OCP\AppFramework\OCS\OCSForbiddenException; use OCP\AppFramework\OCS\OCSForbiddenException;
use OCP\AppFramework\OCSController; use OCP\AppFramework\OCSController;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList; use OCP\BackgroundJob\IJobList;
use OCP\ILogger; use OCP\ILogger;
use OCP\IRequest; use OCP\IRequest;
@ -61,6 +62,9 @@ class OCSAuthAPIController extends OCSController{
/** @var ILogger */ /** @var ILogger */
private $logger; private $logger;
/** @var ITimeFactory */
private $timeFactory;
/** /**
* OCSAuthAPI constructor. * OCSAuthAPI constructor.
* *
@ -71,6 +75,7 @@ class OCSAuthAPIController extends OCSController{
* @param TrustedServers $trustedServers * @param TrustedServers $trustedServers
* @param DbHandler $dbHandler * @param DbHandler $dbHandler
* @param ILogger $logger * @param ILogger $logger
* @param ITimeFactory $timeFactory
*/ */
public function __construct( public function __construct(
$appName, $appName,
@ -79,7 +84,8 @@ class OCSAuthAPIController extends OCSController{
IJobList $jobList, IJobList $jobList,
TrustedServers $trustedServers, TrustedServers $trustedServers,
DbHandler $dbHandler, DbHandler $dbHandler,
ILogger $logger ILogger $logger,
ITimeFactory $timeFactory
) { ) {
parent::__construct($appName, $request); parent::__construct($appName, $request);
@ -88,6 +94,7 @@ class OCSAuthAPIController extends OCSController{
$this->trustedServers = $trustedServers; $this->trustedServers = $trustedServers;
$this->dbHandler = $dbHandler; $this->dbHandler = $dbHandler;
$this->logger = $logger; $this->logger = $logger;
$this->timeFactory = $timeFactory;
} }
/** /**
@ -154,7 +161,7 @@ class OCSAuthAPIController extends OCSController{
[ [
'url' => $url, 'url' => $url,
'token' => $token, 'token' => $token,
'created' => $this->getTimestamp() 'created' => $this->timeFactory->getTime()
] ]
); );
@ -202,9 +209,4 @@ class OCSAuthAPIController extends OCSController{
$storedToken = $this->dbHandler->getToken($url); $storedToken = $this->dbHandler->getToken($url);
return hash_equals($storedToken, $token); return hash_equals($storedToken, $token);
} }
protected function getTimestamp() {
return time();
}
} }

View File

@ -28,6 +28,7 @@ namespace OCA\Federation;
use OC\HintException; use OC\HintException;
use OCP\AppFramework\Http; use OCP\AppFramework\Http;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList; use OCP\BackgroundJob\IJobList;
use OCP\Http\Client\IClientService; use OCP\Http\Client\IClientService;
use OCP\IConfig; use OCP\IConfig;
@ -68,6 +69,9 @@ class TrustedServers {
/** @var EventDispatcherInterface */ /** @var EventDispatcherInterface */
private $dispatcher; private $dispatcher;
/** @var ITimeFactory */
private $timeFactory;
/** /**
* @param DbHandler $dbHandler * @param DbHandler $dbHandler
* @param IClientService $httpClientService * @param IClientService $httpClientService
@ -76,6 +80,7 @@ class TrustedServers {
* @param ISecureRandom $secureRandom * @param ISecureRandom $secureRandom
* @param IConfig $config * @param IConfig $config
* @param EventDispatcherInterface $dispatcher * @param EventDispatcherInterface $dispatcher
* @param ITimeFactory $timeFactory
*/ */
public function __construct( public function __construct(
DbHandler $dbHandler, DbHandler $dbHandler,
@ -84,7 +89,8 @@ class TrustedServers {
IJobList $jobList, IJobList $jobList,
ISecureRandom $secureRandom, ISecureRandom $secureRandom,
IConfig $config, IConfig $config,
EventDispatcherInterface $dispatcher EventDispatcherInterface $dispatcher,
ITimeFactory $timeFactory
) { ) {
$this->dbHandler = $dbHandler; $this->dbHandler = $dbHandler;
$this->httpClientService = $httpClientService; $this->httpClientService = $httpClientService;
@ -93,6 +99,7 @@ class TrustedServers {
$this->secureRandom = $secureRandom; $this->secureRandom = $secureRandom;
$this->config = $config; $this->config = $config;
$this->dispatcher = $dispatcher; $this->dispatcher = $dispatcher;
$this->timeFactory = $timeFactory;
} }
/** /**
@ -112,7 +119,7 @@ class TrustedServers {
[ [
'url' => $url, 'url' => $url,
'token' => $token, 'token' => $token,
'created' => $this->getTimestamp() 'created' => $this->timeFactory->getTime()
] ]
); );
} }
@ -276,8 +283,4 @@ class TrustedServers {
return 'https://' . $url; return 'https://' . $url;
} }
protected function getTimestamp() {
return time();
}
} }

View File

@ -31,6 +31,7 @@ use OCA\Files_Sharing\Tests\TestCase;
use OCA\Federation\DbHandler; use OCA\Federation\DbHandler;
use OCA\Federation\TrustedServers; use OCA\Federation\TrustedServers;
use OCP\AppFramework\Http; use OCP\AppFramework\Http;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList; use OCP\BackgroundJob\IJobList;
use OCP\Http\Client\IClient; use OCP\Http\Client\IClient;
use OCP\Http\Client\IClientService; use OCP\Http\Client\IClientService;
@ -75,6 +76,9 @@ class GetSharedSecretTest extends TestCase {
/** @var \PHPUnit_Framework_MockObject_MockObject|IDiscoveryService */ /** @var \PHPUnit_Framework_MockObject_MockObject|IDiscoveryService */
private $discoverService; private $discoverService;
/** @var \PHPUnit_Framework_MockObject_MockObject|ITimeFactory */
private $timeFactory;
/** @var GetSharedSecret */ /** @var GetSharedSecret */
private $getSharedSecret; private $getSharedSecret;
@ -92,6 +96,7 @@ class GetSharedSecretTest extends TestCase {
$this->logger = $this->getMockBuilder(ILogger::class)->getMock(); $this->logger = $this->getMockBuilder(ILogger::class)->getMock();
$this->response = $this->getMockBuilder(IResponse::class)->getMock(); $this->response = $this->getMockBuilder(IResponse::class)->getMock();
$this->discoverService = $this->getMockBuilder(IDiscoveryService::class)->getMock(); $this->discoverService = $this->getMockBuilder(IDiscoveryService::class)->getMock();
$this->timeFactory = $this->createMock(ITimeFactory::class);
$this->discoverService->expects($this->any())->method('discover')->willReturn([]); $this->discoverService->expects($this->any())->method('discover')->willReturn([]);
$this->httpClientService->expects($this->any())->method('newClient')->willReturn($this->httpClient); $this->httpClientService->expects($this->any())->method('newClient')->willReturn($this->httpClient);
@ -103,7 +108,8 @@ class GetSharedSecretTest extends TestCase {
$this->trustedServers, $this->trustedServers,
$this->logger, $this->logger,
$this->dbHandler, $this->dbHandler,
$this->discoverService $this->discoverService,
$this->timeFactory
); );
} }
@ -124,10 +130,11 @@ class GetSharedSecretTest extends TestCase {
$this->trustedServers, $this->trustedServers,
$this->logger, $this->logger,
$this->dbHandler, $this->dbHandler,
$this->discoverService $this->discoverService,
$this->timeFactory
] ]
)->setMethods(['parentExecute', 'reAddJob'])->getMock(); )->setMethods(['parentExecute'])->getMock();
$this->invokePrivate($getSharedSecret, 'argument', [['url' => 'url']]); $this->invokePrivate($getSharedSecret, 'argument', [['url' => 'url', 'token' => 'token']]);
$this->trustedServers->expects($this->once())->method('isTrustedServer') $this->trustedServers->expects($this->once())->method('isTrustedServer')
->with('url')->willReturn($isTrustedServer); ->with('url')->willReturn($isTrustedServer);
@ -138,11 +145,22 @@ class GetSharedSecretTest extends TestCase {
} }
$this->invokePrivate($getSharedSecret, 'retainJob', [$retainBackgroundJob]); $this->invokePrivate($getSharedSecret, 'retainJob', [$retainBackgroundJob]);
$this->jobList->expects($this->once())->method('remove'); $this->jobList->expects($this->once())->method('remove');
if ($retainBackgroundJob) {
$getSharedSecret->expects($this->once())->method('reAddJob');
} else {
$getSharedSecret->expects($this->never())->method('reAddJob');
$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');
} }
$getSharedSecret->execute($this->jobList); $getSharedSecret->execute($this->jobList);
@ -163,13 +181,15 @@ class GetSharedSecretTest extends TestCase {
* @param int $statusCode * @param int $statusCode
*/ */
public function testRun($statusCode) { public function testRun($statusCode) {
$target = 'targetURL'; $target = 'targetURL';
$source = 'sourceURL'; $source = 'sourceURL';
$token = 'token'; $token = 'token';
$argument = ['url' => $target, 'token' => $token]; $argument = ['url' => $target, 'token' => $token];
$this->timeFactory->method('getTime')
->willReturn(42);
$this->urlGenerator->expects($this->once())->method('getAbsoluteURL')->with('/') $this->urlGenerator->expects($this->once())->method('getAbsoluteURL')->with('/')
->willReturn($source); ->willReturn($source);
$this->httpClient->expects($this->once())->method('get') $this->httpClient->expects($this->once())->method('get')
@ -216,7 +236,6 @@ class GetSharedSecretTest extends TestCase {
} else { } else {
$this->assertFalse($this->invokePrivate($this->getSharedSecret, 'retainJob')); $this->assertFalse($this->invokePrivate($this->getSharedSecret, 'retainJob'));
} }
} }
public function dataTestRun() { public function dataTestRun() {
@ -227,4 +246,33 @@ class GetSharedSecretTest extends TestCase {
]; ];
} }
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]);
}
} }

View File

@ -29,6 +29,7 @@ use OCA\Federation\BackgroundJob\RequestSharedSecret;
use OCA\Federation\DbHandler; use OCA\Federation\DbHandler;
use OCA\Federation\TrustedServers; use OCA\Federation\TrustedServers;
use OCP\AppFramework\Http; use OCP\AppFramework\Http;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList; use OCP\BackgroundJob\IJobList;
use OCP\Http\Client\IClient; use OCP\Http\Client\IClient;
use OCP\Http\Client\IClientService; use OCP\Http\Client\IClientService;
@ -67,6 +68,9 @@ class RequestSharedSecretTest extends TestCase {
/** @var \PHPUnit_Framework_MockObject_MockObject|ILogger */ /** @var \PHPUnit_Framework_MockObject_MockObject|ILogger */
private $logger; private $logger;
/** @var \PHPUnit_Framework_MockObject_MockObject|ITimeFactory */
private $timeFactory;
/** @var RequestSharedSecret */ /** @var RequestSharedSecret */
private $requestSharedSecret; private $requestSharedSecret;
@ -84,6 +88,7 @@ class RequestSharedSecretTest extends TestCase {
$this->response = $this->getMockBuilder(IResponse::class)->getMock(); $this->response = $this->getMockBuilder(IResponse::class)->getMock();
$this->discoveryService = $this->getMockBuilder(IDiscoveryService::class)->getMock(); $this->discoveryService = $this->getMockBuilder(IDiscoveryService::class)->getMock();
$this->logger = $this->createMock(ILogger::class); $this->logger = $this->createMock(ILogger::class);
$this->timeFactory = $this->createMock(ITimeFactory::class);
$this->discoveryService->expects($this->any())->method('discover')->willReturn([]); $this->discoveryService->expects($this->any())->method('discover')->willReturn([]);
$this->httpClientService->expects($this->any())->method('newClient')->willReturn($this->httpClient); $this->httpClientService->expects($this->any())->method('newClient')->willReturn($this->httpClient);
@ -95,7 +100,8 @@ class RequestSharedSecretTest extends TestCase {
$this->trustedServers, $this->trustedServers,
$this->dbHandler, $this->dbHandler,
$this->discoveryService, $this->discoveryService,
$this->logger $this->logger,
$this->timeFactory
); );
} }
@ -116,10 +122,11 @@ class RequestSharedSecretTest extends TestCase {
$this->trustedServers, $this->trustedServers,
$this->dbHandler, $this->dbHandler,
$this->discoveryService, $this->discoveryService,
$this->logger $this->logger,
$this->timeFactory
] ]
)->setMethods(['parentExecute', 'reAddJob'])->getMock(); )->setMethods(['parentExecute'])->getMock();
$this->invokePrivate($requestSharedSecret, 'argument', [['url' => 'url']]); $this->invokePrivate($requestSharedSecret, 'argument', [['url' => 'url', 'token' => 'token']]);
$this->trustedServers->expects($this->once())->method('isTrustedServer') $this->trustedServers->expects($this->once())->method('isTrustedServer')
->with('url')->willReturn($isTrustedServer); ->with('url')->willReturn($isTrustedServer);
@ -130,10 +137,22 @@ class RequestSharedSecretTest extends TestCase {
} }
$this->invokePrivate($requestSharedSecret, 'retainJob', [$retainBackgroundJob]); $this->invokePrivate($requestSharedSecret, 'retainJob', [$retainBackgroundJob]);
$this->jobList->expects($this->once())->method('remove'); $this->jobList->expects($this->once())->method('remove');
$this->timeFactory->method('getTime')->willReturn(42);
if ($retainBackgroundJob) { if ($retainBackgroundJob) {
$requestSharedSecret->expects($this->once())->method('reAddJob'); $this->jobList->expects($this->once())
->method('add')
->with(
RequestSharedSecret::class,
[
'url' => 'url',
'token' => 'token',
'created' => 42,
]
);
} else { } else {
$requestSharedSecret->expects($this->never())->method('reAddJob'); $this->jobList->expects($this->never())->method('add');
} }
$requestSharedSecret->execute($this->jobList); $requestSharedSecret->execute($this->jobList);
@ -161,6 +180,8 @@ class RequestSharedSecretTest extends TestCase {
$argument = ['url' => $target, 'token' => $token]; $argument = ['url' => $target, 'token' => $token];
$this->timeFactory->method('getTime')->willReturn(42);
$this->urlGenerator->expects($this->once())->method('getAbsoluteURL')->with('/') $this->urlGenerator->expects($this->once())->method('getAbsoluteURL')->with('/')
->willReturn($source); ->willReturn($source);
$this->httpClient->expects($this->once())->method('post') $this->httpClient->expects($this->once())->method('post')
@ -209,4 +230,34 @@ class RequestSharedSecretTest extends TestCase {
[Http::STATUS_CONFLICT], [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->requestSharedSecret, 'run', [$argument]);
}
} }

View File

@ -29,8 +29,8 @@ use OC\BackgroundJob\JobList;
use OCA\Federation\Controller\OCSAuthAPIController; use OCA\Federation\Controller\OCSAuthAPIController;
use OCA\Federation\DbHandler; use OCA\Federation\DbHandler;
use OCA\Federation\TrustedServers; use OCA\Federation\TrustedServers;
use OCP\AppFramework\Http;
use OCP\AppFramework\OCS\OCSForbiddenException; use OCP\AppFramework\OCS\OCSForbiddenException;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\ILogger; use OCP\ILogger;
use OCP\IRequest; use OCP\IRequest;
use OCP\Security\ISecureRandom; use OCP\Security\ISecureRandom;
@ -56,6 +56,10 @@ class OCSAuthAPIControllerTest extends TestCase {
/** @var \PHPUnit_Framework_MockObject_MockObject|ILogger */ /** @var \PHPUnit_Framework_MockObject_MockObject|ILogger */
private $logger; private $logger;
/** @var \PHPUnit_Framework_MockObject_MockObject|ITimeFactory */
private $timeFactory;
/** @var OCSAuthAPIController */ /** @var OCSAuthAPIController */
private $ocsAuthApi; private $ocsAuthApi;
@ -65,31 +69,28 @@ class OCSAuthAPIControllerTest extends TestCase {
public function setUp() { public function setUp() {
parent::setUp(); parent::setUp();
$this->request = $this->getMockBuilder('OCP\IRequest')->getMock(); $this->request = $this->createMock(IRequest::class);
$this->secureRandom = $this->getMockBuilder('OCP\Security\ISecureRandom')->getMock(); $this->secureRandom = $this->createMock(ISecureRandom::class);
$this->trustedServers = $this->getMockBuilder('OCA\Federation\TrustedServers') $this->trustedServers = $this->createMock(TrustedServers::class);
->disableOriginalConstructor()->getMock(); $this->dbHandler = $this->createMock(DbHandler::class);
$this->dbHandler = $this->getMockBuilder('OCA\Federation\DbHandler') $this->jobList = $this->createMock(JobList::class);
->disableOriginalConstructor()->getMock(); $this->logger = $this->createMock(ILogger::class);
$this->jobList = $this->getMockBuilder('OC\BackgroundJob\JobList') $this->timeFactory = $this->createMock(ITimeFactory::class);
->disableOriginalConstructor()->getMock();
$this->logger = $this->getMockBuilder('OCP\ILogger')
->disableOriginalConstructor()->getMock();
$this->ocsAuthApi = $this->getMockBuilder(OCSAuthAPIController::class)
->setConstructorArgs( $this->ocsAuthApi = new OCSAuthAPIController(
[
'federation', 'federation',
$this->request, $this->request,
$this->secureRandom, $this->secureRandom,
$this->jobList, $this->jobList,
$this->trustedServers, $this->trustedServers,
$this->dbHandler, $this->dbHandler,
$this->logger $this->logger,
] $this->timeFactory
)->setMethods(['getTimestamp'])->getMock(); );
$this->ocsAuthApi->expects($this->any())->method('getTimestamp')->willReturn($this->currentTime); $this->timeFactory->method('getTime')
->willReturn($this->currentTime);
} }
@ -157,7 +158,8 @@ class OCSAuthAPIControllerTest extends TestCase {
$this->jobList, $this->jobList,
$this->trustedServers, $this->trustedServers,
$this->dbHandler, $this->dbHandler,
$this->logger $this->logger,
$this->timeFactory
] ]
)->setMethods(['isValidToken'])->getMock(); )->setMethods(['isValidToken'])->getMock();

View File

@ -29,6 +29,7 @@ namespace OCA\Federation\Tests;
use OCA\Federation\DbHandler; use OCA\Federation\DbHandler;
use OCA\Federation\TrustedServers; use OCA\Federation\TrustedServers;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList; use OCP\BackgroundJob\IJobList;
use OCP\Http\Client\IClient; use OCP\Http\Client\IClient;
use OCP\Http\Client\IClientService; use OCP\Http\Client\IClientService;
@ -71,6 +72,9 @@ class TrustedServersTest extends TestCase {
/** @var \PHPUnit_Framework_MockObject_MockObject | EventDispatcherInterface */ /** @var \PHPUnit_Framework_MockObject_MockObject | EventDispatcherInterface */
private $dispatcher; private $dispatcher;
/** @var \PHPUnit_Framework_MockObject_MockObject|ITimeFactory */
private $timeFactory;
public function setUp() { public function setUp() {
parent::setUp(); parent::setUp();
@ -85,6 +89,7 @@ class TrustedServersTest extends TestCase {
$this->jobList = $this->getMockBuilder(IJobList::class)->getMock(); $this->jobList = $this->getMockBuilder(IJobList::class)->getMock();
$this->secureRandom = $this->getMockBuilder(ISecureRandom::class)->getMock(); $this->secureRandom = $this->getMockBuilder(ISecureRandom::class)->getMock();
$this->config = $this->getMockBuilder(IConfig::class)->getMock(); $this->config = $this->getMockBuilder(IConfig::class)->getMock();
$this->timeFactory = $this->createMock(ITimeFactory::class);
$this->trustedServers = new TrustedServers( $this->trustedServers = new TrustedServers(
$this->dbHandler, $this->dbHandler,
@ -93,7 +98,8 @@ class TrustedServersTest extends TestCase {
$this->jobList, $this->jobList,
$this->secureRandom, $this->secureRandom,
$this->config, $this->config,
$this->dispatcher $this->dispatcher,
$this->timeFactory
); );
} }
@ -114,14 +120,16 @@ class TrustedServersTest extends TestCase {
$this->jobList, $this->jobList,
$this->secureRandom, $this->secureRandom,
$this->config, $this->config,
$this->dispatcher $this->dispatcher,
$this->timeFactory
] ]
) )
->setMethods(['normalizeUrl', 'updateProtocol', 'getTimestamp']) ->setMethods(['normalizeUrl', 'updateProtocol'])
->getMock(); ->getMock();
$trustedServers->expects($this->once())->method('updateProtocol') $trustedServers->expects($this->once())->method('updateProtocol')
->with('url')->willReturn('https://url'); ->with('url')->willReturn('https://url');
$trustedServers->expects($this->any())->method('getTimestamp')->willReturn(1234567); $this->timeFactory->method('getTime')
->willReturn(1234567);
$this->dbHandler->expects($this->once())->method('addServer')->with('https://url') $this->dbHandler->expects($this->once())->method('addServer')->with('https://url')
->willReturn($success); ->willReturn($success);
@ -273,7 +281,8 @@ class TrustedServersTest extends TestCase {
$this->jobList, $this->jobList,
$this->secureRandom, $this->secureRandom,
$this->config, $this->config,
$this->dispatcher $this->dispatcher,
$this->timeFactory
] ]
) )
->setMethods(['checkOwnCloudVersion']) ->setMethods(['checkOwnCloudVersion'])