unit tests

This commit is contained in:
Björn Schießle 2015-11-19 17:49:43 +01:00
parent f6446a64b6
commit a63a1043b6
7 changed files with 549 additions and 17 deletions

View File

@ -110,10 +110,20 @@ class GetSharedSecret extends QueuedJob{
$target = $this->argument['url'];
// only execute if target is still in the list of trusted domains
if ($this->trustedServers->isTrustedServer($target)) {
parent::execute($jobList, $logger);
$this->parentExecute($jobList, $logger);
}
}
/**
* call execute() method of parent
*
* @param JobList $jobList
* @param ILogger $logger
*/
protected function parentExecute($jobList, $logger) {
parent::execute($jobList, $logger);
}
protected function run($argument) {
$target = $argument['url'];
$source = $this->urlGenerator->getAbsoluteURL('/');
@ -146,7 +156,7 @@ class GetSharedSecret extends QueuedJob{
&& $status !== Http::STATUS_FORBIDDEN
) {
$this->jobList->add(
'OCA\Federation\Backgroundjob\GetSharedSecret',
'OCA\Federation\BackgroundJob\GetSharedSecret',
$argument
);
} else {

View File

@ -106,10 +106,18 @@ class RequestSharedSecret extends QueuedJob {
$target = $this->argument['url'];
// only execute if target is still in the list of trusted domains
if ($this->trustedServers->isTrustedServer($target)) {
parent::execute($jobList, $logger);
$this->parentExecute($jobList, $logger);
}
}
/**
* @param JobList $jobList
* @param ILogger $logger
*/
protected function parentExecute($jobList, $logger) {
parent::execute($jobList, $logger);
}
protected function run($argument) {
$target = $argument['url'];

View File

@ -82,7 +82,7 @@ class DbHandler {
$result = $query->execute();
if ($result) {
return $this->connection->lastInsertId('*PREFIX*'.$this->dbTable);
return (int)$this->connection->lastInsertId('*PREFIX*'.$this->dbTable);
} else {
$message = 'Internal failure, Could not add ownCloud as trusted server: ' . $url;
$message_t = $this->l->t('Could not add server');
@ -231,7 +231,7 @@ class DbHandler {
->setParameter('url_hash', $hash);
$result = $query->execute()->fetch();
return $result['status'];
return (int)$result['status'];
}
/**

View File

@ -0,0 +1,190 @@
<?php
/**
* @author Björn Schießle <schiessle@owncloud.com>
*
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @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\GetSharedSecret;
use OCA\Files_Sharing\Tests\TestCase;
use OCA\Federation\DbHandler;
use OCA\Federation\TrustedServers;
use OCP\AppFramework\Http;
use OCP\BackgroundJob\IJobList;
use OCP\Http\Client\IClient;
use OCP\Http\Client\IResponse;
use OCP\ILogger;
use OCP\IURLGenerator;
class GetSharedSecretTest extends TestCase {
/** @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 | DbHandler */
private $dbHandler;
/** @var \PHPUnit_Framework_MockObject_MockObject | ILogger */
private $logger;
/** @var \PHPUnit_Framework_MockObject_MockObject | IResponse */
private $response;
/** @var GetSharedSecret */
private $getSharedSecret;
public function setUp() {
parent::setUp();
$this->httpClient = $this->getMock('OCP\Http\Client\IClient');
$this->jobList = $this->getMock('OCP\BackgroundJob\IJobList');
$this->urlGenerator = $this->getMock('OCP\IURLGenerator');
$this->trustedServers = $this->getMockBuilder('OCA\Federation\TrustedServers')
->disableOriginalConstructor()->getMock();
$this->dbHandler = $this->getMockBuilder('OCA\Federation\DbHandler')
->disableOriginalConstructor()->getMock();
$this->logger = $this->getMock('OCP\ILogger');
$this->response = $this->getMock('OCP\Http\Client\IResponse');
$this->getSharedSecret = new GetSharedSecret(
$this->httpClient,
$this->urlGenerator,
$this->jobList,
$this->trustedServers,
$this->logger,
$this->dbHandler
);
}
/**
* @dataProvider dataTestExecute
*
* @param bool $isTrustedServer
*/
public function testExecute($isTrustedServer) {
/** @var GetSharedSecret |\PHPUnit_Framework_MockObject_MockObject $getSharedSecret */
$getSharedSecret = $this->getMockBuilder('OCA\Federation\BackgroundJob\GetSharedSecret')
->setConstructorArgs(
[
$this->httpClient,
$this->urlGenerator,
$this->jobList,
$this->trustedServers,
$this->logger,
$this->dbHandler
]
)->setMethods(['parentExecute'])->getMock();
$this->invokePrivate($getSharedSecret, 'argument', [['url' => 'url']]);
$this->jobList->expects($this->once())->method('remove');
$this->trustedServers->expects($this->once())->method('isTrustedServer')
->with('url')->willReturn($isTrustedServer);
if ($isTrustedServer) {
$getSharedSecret->expects($this->once())->method('parentExecute');
} else {
$getSharedSecret->expects($this->never())->method('parentExecute');
}
$getSharedSecret->execute($this->jobList);
}
public function dataTestExecute() {
return [
[true],
[false]
];
}
/**
* @dataProvider dataTestRun
*
* @param int $statusCode
*/
public function testRun($statusCode) {
$target = 'targetURL';
$source = 'sourceURL';
$token = 'token';
$argument = ['url' => $target, 'token' => $token];
$this->urlGenerator->expects($this->once())->method('getAbsoluteURL')->with('/')
->willReturn($source);
$this->httpClient->expects($this->once())->method('get')
->with(
$target . '/ocs/v2.php/apps/federation/api/v1/shared-secret?format=json',
[
'query' =>
[
'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->jobList->expects($this->once())->method('add')
->with('OCA\Federation\BackgroundJob\GetSharedSecret', $argument);
$this->dbHandler->expects($this->never())->method('addToken');
} else {
$this->dbHandler->expects($this->once())->method('addToken')->with($target, '');
$this->jobList->expects($this->never())->method('add');
}
if ($statusCode === Http::STATUS_OK) {
$this->response->expects($this->once())->method('getBody')
->willReturn('{"ocs":{"data":{"sharedSecret":"secret"}}}');
$this->trustedServers->expects($this->once())->method('addSharedSecret')
->with($target, 'secret');
} else {
$this->trustedServers->expects($this->never())->method('addSharedSecret');
}
$this->invokePrivate($this->getSharedSecret, 'run', [$argument]);
}
public function dataTestRun() {
return [
[Http::STATUS_OK],
[Http::STATUS_FORBIDDEN],
[Http::STATUS_CONFLICT],
];
}
}

View File

@ -0,0 +1,169 @@
<?php
/**
* @author Björn Schießle <schiessle@owncloud.com>
*
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @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;
use OCP\AppFramework\Http;
use Test\TestCase;
class RequestSharedSecretTest extends TestCase {
/** @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 | DbHandler */
private $dbHandler;
/** @var \PHPUnit_Framework_MockObject_MockObject | TrustedServers */
private $trustedServers;
/** @var \PHPUnit_Framework_MockObject_MockObject | IResponse */
private $response;
/** @var RequestSharedSecret */
private $requestSharedSecret;
public function setUp() {
parent::setUp();
$this->httpClient = $this->getMock('OCP\Http\Client\IClient');
$this->jobList = $this->getMock('OCP\BackgroundJob\IJobList');
$this->urlGenerator = $this->getMock('OCP\IURLGenerator');
$this->trustedServers = $this->getMockBuilder('OCA\Federation\TrustedServers')
->disableOriginalConstructor()->getMock();
$this->dbHandler = $this->getMockBuilder('OCA\Federation\DbHandler')
->disableOriginalConstructor()->getMock();
$this->response = $this->getMock('OCP\Http\Client\IResponse');
$this->requestSharedSecret = new RequestSharedSecret(
$this->httpClient,
$this->urlGenerator,
$this->jobList,
$this->trustedServers,
$this->dbHandler
);
}
/**
* @dataProvider dataTestExecute
*
* @param bool $isTrustedServer
*/
public function testExecute($isTrustedServer) {
/** @var RequestSharedSecret |\PHPUnit_Framework_MockObject_MockObject $requestSharedSecret */
$requestSharedSecret = $this->getMockBuilder('OCA\Federation\BackgroundJob\RequestSharedSecret')
->setConstructorArgs(
[
$this->httpClient,
$this->urlGenerator,
$this->jobList,
$this->trustedServers,
$this->dbHandler
]
)->setMethods(['parentExecute'])->getMock();
$this->invokePrivate($requestSharedSecret, 'argument', [['url' => 'url']]);
$this->jobList->expects($this->once())->method('remove');
$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');
}
$requestSharedSecret->execute($this->jobList);
}
public function dataTestExecute() {
return [
[true],
[false]
];
}
/**
* @dataProvider dataTestRun
*
* @param int $statusCode
*/
public function testRun($statusCode) {
$target = 'targetURL';
$source = 'sourceURL';
$token = 'token';
$argument = ['url' => $target, 'token' => $token];
$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->jobList->expects($this->once())->method('add')
->with('OCA\Federation\BackgroundJob\RequestSharedSecret', $argument);
$this->dbHandler->expects($this->never())->method('addToken');
}
if ($statusCode === Http::STATUS_FORBIDDEN) {
$this->jobList->expects($this->never())->method('add');
$this->dbHandler->expects($this->once())->method('addToken')->with($target, '');
}
$this->invokePrivate($this->requestSharedSecret, 'run', [$argument]);
}
public function dataTestRun() {
return [
[Http::STATUS_OK],
[Http::STATUS_FORBIDDEN],
[Http::STATUS_CONFLICT],
];
}
}

View File

@ -24,6 +24,7 @@ namespace OCA\Federation\Tests\lib;
use OCA\Federation\DbHandler;
use OCA\Federation\TrustedServers;
use OCP\IDBConnection;
use Test\TestCase;
@ -73,7 +74,8 @@ class DbHandlerTest extends TestCase {
$result = $query->execute()->fetchAll();
$this->assertSame(1, count($result));
$this->assertSame('server1', $result[0]['url']);
$this->assertSame($id, $result[0]['id']);
$this->assertSame($id, (int)$result[0]['id']);
$this->assertSame(TrustedServers::STATUS_PENDING, (int)$result[0]['status']);
}
public function testRemove() {
@ -85,15 +87,15 @@ class DbHandlerTest extends TestCase {
$this->assertSame(2, count($result));
$this->assertSame('server1', $result[0]['url']);
$this->assertSame('server2', $result[1]['url']);
$this->assertSame($id1, $result[0]['id']);
$this->assertSame($id2, $result[1]['id']);
$this->assertSame($id1, (int)$result[0]['id']);
$this->assertSame($id2, (int)$result[1]['id']);
$this->dbHandler->removeServer($id2);
$query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
$result = $query->execute()->fetchAll();
$this->assertSame(1, count($result));
$this->assertSame('server1', $result[0]['url']);
$this->assertSame($id1, $result[0]['id']);
$this->assertSame($id1, (int)$result[0]['id']);
}
public function testGetAll() {
@ -104,32 +106,118 @@ class DbHandlerTest extends TestCase {
$this->assertSame(2, count($result));
$this->assertSame('server1', $result[0]['url']);
$this->assertSame('server2', $result[1]['url']);
$this->assertSame($id1, $result[0]['id']);
$this->assertSame($id2, $result[1]['id']);
$this->assertSame($id1, (int)$result[0]['id']);
$this->assertSame($id2, (int)$result[1]['id']);
}
/**
* @dataProvider dataTestExists
* @dataProvider dataTestServerExists
*
* @param string $serverInTable
* @param string $checkForServer
* @param bool $expected
*/
public function testExists($serverInTable, $checkForServer, $expected) {
public function testServerExists($serverInTable, $checkForServer, $expected) {
$this->dbHandler->addServer($serverInTable);
$this->assertSame($expected,
$this->dbHandler->serverExists($checkForServer)
);
}
public function dataTestExists() {
public function dataTestServerExists() {
return [
['server1', 'server1', true],
['server1', 'server1', true],
['server1', 'http://server1', true],
['server1', 'server2', false]
];
}
public function testAddToken() {
$this->dbHandler->addServer('server1');
$query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
$result = $query->execute()->fetchAll();
$this->assertSame(1, count($result));
$this->assertSame(null, $result[0]['token']);
$this->dbHandler->addToken('http://server1', 'token');
$query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
$result = $query->execute()->fetchAll();
$this->assertSame(1, count($result));
$this->assertSame('token', $result[0]['token']);
}
public function testGetToken() {
$this->dbHandler->addServer('server1');
$this->dbHandler->addToken('http://server1', 'token');
$this->assertSame('token',
$this->dbHandler->getToken('https://server1')
);
}
public function testAddSharedSecret() {
$this->dbHandler->addServer('server1');
$query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
$result = $query->execute()->fetchAll();
$this->assertSame(1, count($result));
$this->assertSame(null, $result[0]['shared_secret']);
$this->dbHandler->addSharedSecret('http://server1', 'secret');
$query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
$result = $query->execute()->fetchAll();
$this->assertSame(1, count($result));
$this->assertSame('secret', $result[0]['shared_secret']);
}
public function testGetSharedSecret() {
$this->dbHandler->addServer('server1');
$this->dbHandler->addSharedSecret('http://server1', 'secret');
$this->assertSame('secret',
$this->dbHandler->getSharedSecret('https://server1')
);
}
public function testSetServerStatus() {
$this->dbHandler->addServer('server1');
$query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
$result = $query->execute()->fetchAll();
$this->assertSame(1, count($result));
$this->assertSame(TrustedServers::STATUS_PENDING, (int)$result[0]['status']);
$this->dbHandler->setServerStatus('http://server1', TrustedServers::STATUS_OK);
$query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
$result = $query->execute()->fetchAll();
$this->assertSame(1, count($result));
$this->assertSame(TrustedServers::STATUS_OK, (int)$result[0]['status']);
}
public function testGetServerStatus() {
$this->dbHandler->addServer('server1');
$this->dbHandler->setServerStatus('http://server1', TrustedServers::STATUS_OK);
$this->assertSame(TrustedServers::STATUS_OK,
$this->dbHandler->getServerStatus('https://server1')
);
}
/**
* hash should always be computed with the normalized URL
*
* @dataProvider dataTestHash
*
* @param string $url
* @param string $expected
*/
public function testHash($url, $expected) {
$this->assertSame($expected,
$this->invokePrivate($this->dbHandler, 'hash', [$url])
);
}
public function dataTestHash() {
return [
['server1', md5('server1')],
['http://server1', md5('server1')],
['https://server1', md5('server1')],
['http://server1/', md5('server1')],
];
}
/**
* @dataProvider dataTestNormalizeUrl
*

View File

@ -88,7 +88,7 @@ class TrustedServersTest extends TestCase {
}
/**
* @dataProvider dataTestAddServer
* @dataProvider dataTrueFalse
*
* @param bool $success
*/
@ -130,13 +130,66 @@ class TrustedServersTest extends TestCase {
);
}
public function dataTestAddServer() {
public function dataTrueFalse() {
return [
[true],
[false]
];
}
/**
* @dataProvider dataTrueFalse
*
* @param bool $status
*/
public function testSetAutoAddServers($status) {
if ($status) {
$this->config->expects($this->once())->method('setAppValue')
->with('federation', 'autoAddServers', '1');
} else {
$this->config->expects($this->once())->method('setAppValue')
->with('federation', 'autoAddServers', '0');
}
$this->trustedServers->setAutoAddServers($status);
}
/**
* @dataProvider dataTestGetAutoAddServers
*
* @param string $status
* @param bool $expected
*/
public function testGetAutoAddServers($status, $expected) {
$this->config->expects($this->once())->method('getAppValue')
->with('federation', 'autoAddServers', '1')->willReturn($status);
$this->assertSame($expected,
$this->trustedServers->getAutoAddServers($status)
);
}
public function dataTestGetAutoAddServers() {
return [
['1', true],
['0', false]
];
}
public function testAddSharedSecret() {
$this->dbHandler->expects($this->once())->method('addSharedSecret')
->with('url', 'secret');
$this->trustedServers->addSharedSecret('url', 'secret');
}
public function testGetSharedSecret() {
$this->dbHandler->expects($this->once())->method('getSharedSecret')
->with('url')->willReturn(true);
$this->assertTrue(
$this->trustedServers->getSharedSecret('url')
);
}
public function testRemoveServer() {
$id = 42;
$this->dbHandler->expects($this->once())->method('removeServer')->with($id);
@ -161,6 +214,20 @@ class TrustedServersTest extends TestCase {
);
}
public function testSetServerStatus() {
$this->dbHandler->expects($this->once())->method('setServerStatus')
->with('url', 'status');
$this->trustedServers->setServerStatus('url', 'status');
}
public function testGetServerStatus() {
$this->dbHandler->expects($this->once())->method('getServerStatus')
->with('url')->willReturn(true);
$this->assertTrue(
$this->trustedServers->getServerStatus('url')
);
}
/**
* @dataProvider dataTestIsOwnCloudServer
*