From a6b4ba9ba1d119280c7ce0cf439b804b298e3ce8 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 9 May 2016 15:37:51 +0200 Subject: Fix errors in the tests --- .../BackgroundJob/RequestSharedSecretTest.php | 6 + apps/federation/tests/DbHandlerTest.php | 293 ++++++++++++++++ apps/federation/tests/HooksTest.php | 79 +++++ apps/federation/tests/Lib/DbHandlerTest.php | 293 ---------------- apps/federation/tests/Lib/HooksTest.php | 79 ----- .../tests/Lib/SyncFederationAddressbooksTest.php | 88 ----- apps/federation/tests/Lib/TrustedServersTest.php | 372 -------------------- .../tests/SyncFederationAddressbooksTest.php | 90 +++++ apps/federation/tests/TrustedServersTest.php | 374 +++++++++++++++++++++ 9 files changed, 842 insertions(+), 832 deletions(-) create mode 100644 apps/federation/tests/DbHandlerTest.php create mode 100644 apps/federation/tests/HooksTest.php delete mode 100644 apps/federation/tests/Lib/DbHandlerTest.php delete mode 100644 apps/federation/tests/Lib/HooksTest.php delete mode 100644 apps/federation/tests/Lib/SyncFederationAddressbooksTest.php delete mode 100644 apps/federation/tests/Lib/TrustedServersTest.php create mode 100644 apps/federation/tests/SyncFederationAddressbooksTest.php create mode 100644 apps/federation/tests/TrustedServersTest.php diff --git a/apps/federation/tests/BackgroundJob/RequestSharedSecretTest.php b/apps/federation/tests/BackgroundJob/RequestSharedSecretTest.php index 5b4a1f87a5f..2fc4fe881a6 100644 --- a/apps/federation/tests/BackgroundJob/RequestSharedSecretTest.php +++ b/apps/federation/tests/BackgroundJob/RequestSharedSecretTest.php @@ -24,7 +24,13 @@ namespace OCA\Federation\Tests\BackgroundJob; use OCA\Federation\BackgroundJob\RequestSharedSecret; +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\IURLGenerator; use Test\TestCase; class RequestSharedSecretTest extends TestCase { diff --git a/apps/federation/tests/DbHandlerTest.php b/apps/federation/tests/DbHandlerTest.php new file mode 100644 index 00000000000..6e5b8f2f06c --- /dev/null +++ b/apps/federation/tests/DbHandlerTest.php @@ -0,0 +1,293 @@ + + * @author Thomas Müller + * + * @copyright Copyright (c) 2016, 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 + * + */ + + +namespace OCA\Federation\Tests; + + +use OCA\Federation\DbHandler; +use OCA\Federation\TrustedServers; +use OCP\IDBConnection; +use OCP\IL10N; +use Test\TestCase; + +/** + * @group DB + */ +class DbHandlerTest extends TestCase { + + /** @var DbHandler */ + private $dbHandler; + + /** @var IL10N | \PHPUnit_Framework_MockObject_MockObject */ + private $il10n; + + /** @var IDBConnection */ + private $connection; + + /** @var string */ + private $dbTable = 'trusted_servers'; + + public function setUp() { + parent::setUp(); + + $this->connection = \OC::$server->getDatabaseConnection(); + $this->il10n = $this->getMock('OCP\IL10N'); + + $this->dbHandler = new DbHandler( + $this->connection, + $this->il10n + ); + + $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable); + $result = $query->execute()->fetchAll(); + $this->assertEmpty($result, 'we need to start with a empty trusted_servers table'); + } + + public function tearDown() { + parent::tearDown(); + $query = $this->connection->getQueryBuilder()->delete($this->dbTable); + $query->execute(); + } + + /** + * @dataProvider dataTestAddServer + * + * @param string $url passed to the method + * @param string $expectedUrl the url we expect to be written to the db + * @param string $expectedHash the hash value we expect to be written to the db + */ + public function testAddServer($url, $expectedUrl, $expectedHash) { + $id = $this->dbHandler->addServer($url); + + $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable); + $result = $query->execute()->fetchAll(); + $this->assertSame(1, count($result)); + $this->assertSame($expectedUrl, $result[0]['url']); + $this->assertSame($id, (int)$result[0]['id']); + $this->assertSame($expectedHash, $result[0]['url_hash']); + $this->assertSame(TrustedServers::STATUS_PENDING, (int)$result[0]['status']); + } + + public function dataTestAddServer() { + return [ + ['http://owncloud.org', 'http://owncloud.org', sha1('owncloud.org')], + ['https://owncloud.org', 'https://owncloud.org', sha1('owncloud.org')], + ['http://owncloud.org/', 'http://owncloud.org', sha1('owncloud.org')], + ]; + } + + public function testRemove() { + $id1 = $this->dbHandler->addServer('server1'); + $id2 = $this->dbHandler->addServer('server2'); + + $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable); + $result = $query->execute()->fetchAll(); + $this->assertSame(2, count($result)); + $this->assertSame('server1', $result[0]['url']); + $this->assertSame('server2', $result[1]['url']); + $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, (int)$result[0]['id']); + } + + + public function testGetServerById() { + $this->dbHandler->addServer('server1'); + $id = $this->dbHandler->addServer('server2'); + + $result = $this->dbHandler->getServerById($id); + $this->assertSame('server2', $result['url']); + } + + public function testGetAll() { + $id1 = $this->dbHandler->addServer('server1'); + $id2 = $this->dbHandler->addServer('server2'); + + $result = $this->dbHandler->getAllServer(); + $this->assertSame(2, count($result)); + $this->assertSame('server1', $result[0]['url']); + $this->assertSame('server2', $result[1]['url']); + $this->assertSame($id1, (int)$result[0]['id']); + $this->assertSame($id2, (int)$result[1]['id']); + } + + /** + * @dataProvider dataTestServerExists + * + * @param string $serverInTable + * @param string $checkForServer + * @param bool $expected + */ + public function testServerExists($serverInTable, $checkForServer, $expected) { + $this->dbHandler->addServer($serverInTable); + $this->assertSame($expected, + $this->dbHandler->serverExists($checkForServer) + ); + } + + public function dataTestServerExists() { + return [ + ['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') + ); + + // test sync token + $this->dbHandler->setServerStatus('http://server1', TrustedServers::STATUS_OK, 'token1234567890'); + $servers = $this->dbHandler->getAllServer(); + $this->assertSame('token1234567890', $servers[0]['sync_token']); + } + + /** + * 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', sha1('server1')], + ['http://server1', sha1('server1')], + ['https://server1', sha1('server1')], + ['http://server1/', sha1('server1')], + ]; + } + + /** + * @dataProvider dataTestNormalizeUrl + * + * @param string $url + * @param string $expected + */ + public function testNormalizeUrl($url, $expected) { + $this->assertSame($expected, + $this->invokePrivate($this->dbHandler, 'normalizeUrl', [$url]) + ); + } + + public function dataTestNormalizeUrl() { + return [ + ['owncloud.org', 'owncloud.org'], + ['http://owncloud.org', 'owncloud.org'], + ['https://owncloud.org', 'owncloud.org'], + ['https://owncloud.org//mycloud', 'owncloud.org/mycloud'], + ['https://owncloud.org/mycloud/', 'owncloud.org/mycloud'], + ]; + } + + /** + * @dataProvider providesAuth + */ + public function testAuth($expectedResult, $user, $password) { + if ($expectedResult) { + $this->dbHandler->addServer('url1'); + $this->dbHandler->addSharedSecret('url1', $password); + } + $result = $this->dbHandler->auth($user, $password); + $this->assertEquals($expectedResult, $result); + } + + public function providesAuth() { + return [ + [false, 'foo', ''], + [true, 'system', '123456789'], + ]; + } +} diff --git a/apps/federation/tests/HooksTest.php b/apps/federation/tests/HooksTest.php new file mode 100644 index 00000000000..014829d9cf6 --- /dev/null +++ b/apps/federation/tests/HooksTest.php @@ -0,0 +1,79 @@ + + * + * @copyright Copyright (c) 2016, 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 + * + */ + + +namespace OCA\Federation\Tests; + + +use OCA\Federation\Hooks; +use OCA\Federation\TrustedServers; +use Test\TestCase; + +class HooksTest extends TestCase { + + /** @var \PHPUnit_Framework_MockObject_MockObject | TrustedServers */ + private $trustedServers; + + /** @var Hooks */ + private $hooks; + + public function setUp() { + parent::setUp(); + + $this->trustedServers = $this->getMockBuilder('OCA\Federation\TrustedServers') + ->disableOriginalConstructor()->getMock(); + + $this->hooks = new Hooks($this->trustedServers); + } + + /** + * @dataProvider dataTestAddServerHook + * + * @param bool $autoAddEnabled is auto-add enabled + * @param bool $isTrustedServer is the server already in the list of trusted servers + * @param bool $addServer should the server be added + */ + public function testAddServerHook($autoAddEnabled, $isTrustedServer, $addServer) { + $this->trustedServers->expects($this->any())->method('getAutoAddServers') + ->willReturn($autoAddEnabled); + $this->trustedServers->expects($this->any())->method('isTrustedServer') + ->with('url')->willReturn($isTrustedServer); + + if ($addServer) { + $this->trustedServers->expects($this->once())->method('addServer') + ->with('url'); + } else { + $this->trustedServers->expects($this->never())->method('addServer'); + } + + $this->hooks->addServerHook(['server' => 'url']); + + } + + public function dataTestAddServerHook() { + return [ + [true, true, false], + [false, true, false], + [true, false, true], + [false, false, false], + ]; + } +} diff --git a/apps/federation/tests/Lib/DbHandlerTest.php b/apps/federation/tests/Lib/DbHandlerTest.php deleted file mode 100644 index 03a740b0277..00000000000 --- a/apps/federation/tests/Lib/DbHandlerTest.php +++ /dev/null @@ -1,293 +0,0 @@ - - * @author Thomas Müller - * - * @copyright Copyright (c) 2016, 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 - * - */ - - -namespace OCA\Federation\Tests\Lib; - - -use OCA\Federation\DbHandler; -use OCA\Federation\TrustedServers; -use OCP\IDBConnection; -use OCP\IL10N; -use Test\TestCase; - -/** - * @group DB - */ -class DbHandlerTest extends TestCase { - - /** @var DbHandler */ - private $dbHandler; - - /** @var IL10N | \PHPUnit_Framework_MockObject_MockObject */ - private $il10n; - - /** @var IDBConnection */ - private $connection; - - /** @var string */ - private $dbTable = 'trusted_servers'; - - public function setUp() { - parent::setUp(); - - $this->connection = \OC::$server->getDatabaseConnection(); - $this->il10n = $this->getMock('OCP\IL10N'); - - $this->dbHandler = new DbHandler( - $this->connection, - $this->il10n - ); - - $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable); - $result = $query->execute()->fetchAll(); - $this->assertEmpty($result, 'we need to start with a empty trusted_servers table'); - } - - public function tearDown() { - parent::tearDown(); - $query = $this->connection->getQueryBuilder()->delete($this->dbTable); - $query->execute(); - } - - /** - * @dataProvider dataTestAddServer - * - * @param string $url passed to the method - * @param string $expectedUrl the url we expect to be written to the db - * @param string $expectedHash the hash value we expect to be written to the db - */ - public function testAddServer($url, $expectedUrl, $expectedHash) { - $id = $this->dbHandler->addServer($url); - - $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable); - $result = $query->execute()->fetchAll(); - $this->assertSame(1, count($result)); - $this->assertSame($expectedUrl, $result[0]['url']); - $this->assertSame($id, (int)$result[0]['id']); - $this->assertSame($expectedHash, $result[0]['url_hash']); - $this->assertSame(TrustedServers::STATUS_PENDING, (int)$result[0]['status']); - } - - public function dataTestAddServer() { - return [ - ['http://owncloud.org', 'http://owncloud.org', sha1('owncloud.org')], - ['https://owncloud.org', 'https://owncloud.org', sha1('owncloud.org')], - ['http://owncloud.org/', 'http://owncloud.org', sha1('owncloud.org')], - ]; - } - - public function testRemove() { - $id1 = $this->dbHandler->addServer('server1'); - $id2 = $this->dbHandler->addServer('server2'); - - $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable); - $result = $query->execute()->fetchAll(); - $this->assertSame(2, count($result)); - $this->assertSame('server1', $result[0]['url']); - $this->assertSame('server2', $result[1]['url']); - $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, (int)$result[0]['id']); - } - - - public function testGetServerById() { - $this->dbHandler->addServer('server1'); - $id = $this->dbHandler->addServer('server2'); - - $result = $this->dbHandler->getServerById($id); - $this->assertSame('server2', $result['url']); - } - - public function testGetAll() { - $id1 = $this->dbHandler->addServer('server1'); - $id2 = $this->dbHandler->addServer('server2'); - - $result = $this->dbHandler->getAllServer(); - $this->assertSame(2, count($result)); - $this->assertSame('server1', $result[0]['url']); - $this->assertSame('server2', $result[1]['url']); - $this->assertSame($id1, (int)$result[0]['id']); - $this->assertSame($id2, (int)$result[1]['id']); - } - - /** - * @dataProvider dataTestServerExists - * - * @param string $serverInTable - * @param string $checkForServer - * @param bool $expected - */ - public function testServerExists($serverInTable, $checkForServer, $expected) { - $this->dbHandler->addServer($serverInTable); - $this->assertSame($expected, - $this->dbHandler->serverExists($checkForServer) - ); - } - - public function dataTestServerExists() { - return [ - ['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') - ); - - // test sync token - $this->dbHandler->setServerStatus('http://server1', TrustedServers::STATUS_OK, 'token1234567890'); - $servers = $this->dbHandler->getAllServer(); - $this->assertSame('token1234567890', $servers[0]['sync_token']); - } - - /** - * 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', sha1('server1')], - ['http://server1', sha1('server1')], - ['https://server1', sha1('server1')], - ['http://server1/', sha1('server1')], - ]; - } - - /** - * @dataProvider dataTestNormalizeUrl - * - * @param string $url - * @param string $expected - */ - public function testNormalizeUrl($url, $expected) { - $this->assertSame($expected, - $this->invokePrivate($this->dbHandler, 'normalizeUrl', [$url]) - ); - } - - public function dataTestNormalizeUrl() { - return [ - ['owncloud.org', 'owncloud.org'], - ['http://owncloud.org', 'owncloud.org'], - ['https://owncloud.org', 'owncloud.org'], - ['https://owncloud.org//mycloud', 'owncloud.org/mycloud'], - ['https://owncloud.org/mycloud/', 'owncloud.org/mycloud'], - ]; - } - - /** - * @dataProvider providesAuth - */ - public function testAuth($expectedResult, $user, $password) { - if ($expectedResult) { - $this->dbHandler->addServer('url1'); - $this->dbHandler->addSharedSecret('url1', $password); - } - $result = $this->dbHandler->auth($user, $password); - $this->assertEquals($expectedResult, $result); - } - - public function providesAuth() { - return [ - [false, 'foo', ''], - [true, 'system', '123456789'], - ]; - } -} diff --git a/apps/federation/tests/Lib/HooksTest.php b/apps/federation/tests/Lib/HooksTest.php deleted file mode 100644 index 6f917b4bb62..00000000000 --- a/apps/federation/tests/Lib/HooksTest.php +++ /dev/null @@ -1,79 +0,0 @@ - - * - * @copyright Copyright (c) 2016, 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 - * - */ - - -namespace OCA\Federation\Tests\Lib; - - -use OCA\Federation\Hooks; -use OCA\Federation\TrustedServers; -use Test\TestCase; - -class HooksTest extends TestCase { - - /** @var \PHPUnit_Framework_MockObject_MockObject | TrustedServers */ - private $trustedServers; - - /** @var Hooks */ - private $hooks; - - public function setUp() { - parent::setUp(); - - $this->trustedServers = $this->getMockBuilder('OCA\Federation\TrustedServers') - ->disableOriginalConstructor()->getMock(); - - $this->hooks = new Hooks($this->trustedServers); - } - - /** - * @dataProvider dataTestAddServerHook - * - * @param bool $autoAddEnabled is auto-add enabled - * @param bool $isTrustedServer is the server already in the list of trusted servers - * @param bool $addServer should the server be added - */ - public function testAddServerHook($autoAddEnabled, $isTrustedServer, $addServer) { - $this->trustedServers->expects($this->any())->method('getAutoAddServers') - ->willReturn($autoAddEnabled); - $this->trustedServers->expects($this->any())->method('isTrustedServer') - ->with('url')->willReturn($isTrustedServer); - - if ($addServer) { - $this->trustedServers->expects($this->once())->method('addServer') - ->with('url'); - } else { - $this->trustedServers->expects($this->never())->method('addServer'); - } - - $this->hooks->addServerHook(['server' => 'url']); - - } - - public function dataTestAddServerHook() { - return [ - [true, true, false], - [false, true, false], - [true, false, true], - [false, false, false], - ]; - } -} diff --git a/apps/federation/tests/Lib/SyncFederationAddressbooksTest.php b/apps/federation/tests/Lib/SyncFederationAddressbooksTest.php deleted file mode 100644 index 2d7945b9690..00000000000 --- a/apps/federation/tests/Lib/SyncFederationAddressbooksTest.php +++ /dev/null @@ -1,88 +0,0 @@ - - * @author Lukas Reschke - * @author Thomas Müller - * - * @copyright Copyright (c) 2016, 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 - * - */ -namespace OCA\Federation\Tests\Lib; - -use OCA\Federation\DbHandler; -use OCA\Federation\SyncFederationAddressBooks; - -class SyncFederationAddressbooksTest extends \Test\TestCase { - - /** @var array */ - private $callBacks = []; - - function testSync() { - /** @var DbHandler | \PHPUnit_Framework_MockObject_MockObject $dbHandler */ - $dbHandler = $this->getMockBuilder('OCA\Federation\DbHandler')-> - disableOriginalConstructor()-> - getMock(); - $dbHandler->method('getAllServer')-> - willReturn([ - [ - 'url' => 'https://cloud.drop.box', - 'url_hash' => 'sha1', - 'shared_secret' => 'iloveowncloud', - 'sync_token' => '0' - ] - ]); - $dbHandler->expects($this->once())->method('setServerStatus')-> - with('https://cloud.drop.box', 1, '1'); - $syncService = $this->getMockBuilder('OCA\DAV\CardDAV\SyncService') - ->disableOriginalConstructor() - ->getMock(); - $syncService->expects($this->once())->method('syncRemoteAddressBook') - ->willReturn(1); - - $s = new SyncFederationAddressBooks($dbHandler, $syncService); - $s->syncThemAll(function($url, $ex) { - $this->callBacks[] = [$url, $ex]; - }); - $this->assertEquals(1, count($this->callBacks)); - } - - function testException() { - /** @var DbHandler | \PHPUnit_Framework_MockObject_MockObject $dbHandler */ - $dbHandler = $this->getMockBuilder('OCA\Federation\DbHandler')-> - disableOriginalConstructor()-> - getMock(); - $dbHandler->method('getAllServer')-> - willReturn([ - [ - 'url' => 'https://cloud.drop.box', - 'url_hash' => 'sha1', - 'shared_secret' => 'iloveowncloud', - 'sync_token' => '0' - ] - ]); - $syncService = $this->getMockBuilder('OCA\DAV\CardDAV\SyncService') - ->disableOriginalConstructor() - ->getMock(); - $syncService->expects($this->once())->method('syncRemoteAddressBook') - ->willThrowException(new \Exception('something did not work out')); - - $s = new SyncFederationAddressBooks($dbHandler, $syncService); - $s->syncThemAll(function($url, $ex) { - $this->callBacks[] = [$url, $ex]; - }); - $this->assertEquals(2, count($this->callBacks)); - } -} diff --git a/apps/federation/tests/Lib/TrustedServersTest.php b/apps/federation/tests/Lib/TrustedServersTest.php deleted file mode 100644 index 167734b2f61..00000000000 --- a/apps/federation/tests/Lib/TrustedServersTest.php +++ /dev/null @@ -1,372 +0,0 @@ - - * @author Thomas Müller - * - * @copyright Copyright (c) 2016, 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 - * - */ - - -namespace OCA\Federation\Tests\Lib; - - -use OCA\Federation\DbHandler; -use OCA\Federation\TrustedServers; -use OCP\BackgroundJob\IJobList; -use OCP\Http\Client\IClient; -use OCP\Http\Client\IClientService; -use OCP\Http\Client\IResponse; -use OCP\IConfig; -use OCP\ILogger; -use OCP\Security\ISecureRandom; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use Test\TestCase; - -class TrustedServersTest extends TestCase { - - /** @var TrustedServers */ - private $trustedServers; - - /** @var \PHPUnit_Framework_MockObject_MockObject | DbHandler */ - private $dbHandler; - - /** @var \PHPUnit_Framework_MockObject_MockObject | IClientService */ - private $httpClientService; - - /** @var \PHPUnit_Framework_MockObject_MockObject | IClient */ - private $httpClient; - - /** @var \PHPUnit_Framework_MockObject_MockObject | IResponse */ - private $response; - - /** @var \PHPUnit_Framework_MockObject_MockObject | ILogger */ - private $logger; - - /** @var \PHPUnit_Framework_MockObject_MockObject | IJobList */ - private $jobList; - - /** @var \PHPUnit_Framework_MockObject_MockObject | ISecureRandom */ - private $secureRandom; - - /** @var \PHPUnit_Framework_MockObject_MockObject | IConfig */ - private $config; - - /** @var \PHPUnit_Framework_MockObject_MockObject | EventDispatcherInterface */ - private $dispatcher; - - public function setUp() { - parent::setUp(); - - $this->dbHandler = $this->getMockBuilder('\OCA\Federation\DbHandler') - ->disableOriginalConstructor()->getMock(); - $this->dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface') - ->disableOriginalConstructor()->getMock(); - $this->httpClientService = $this->getMock('OCP\Http\Client\IClientService'); - $this->httpClient = $this->getMock('OCP\Http\Client\IClient'); - $this->response = $this->getMock('OCP\Http\Client\IResponse'); - $this->logger = $this->getMock('OCP\ILogger'); - $this->jobList = $this->getMock('OCP\BackgroundJob\IJobList'); - $this->secureRandom = $this->getMock('OCP\Security\ISecureRandom'); - $this->config = $this->getMock('OCP\IConfig'); - - $this->trustedServers = new TrustedServers( - $this->dbHandler, - $this->httpClientService, - $this->logger, - $this->jobList, - $this->secureRandom, - $this->config, - $this->dispatcher - ); - - } - - /** - * @dataProvider dataTrueFalse - * - * @param bool $success - */ - public function testAddServer($success) { - /** @var \PHPUnit_Framework_MockObject_MockObject | TrustedServers $trustedServer */ - $trustedServers = $this->getMockBuilder('OCA\Federation\TrustedServers') - ->setConstructorArgs( - [ - $this->dbHandler, - $this->httpClientService, - $this->logger, - $this->jobList, - $this->secureRandom, - $this->config, - $this->dispatcher - ] - ) - ->setMethods(['normalizeUrl', 'updateProtocol']) - ->getMock(); - $trustedServers->expects($this->once())->method('updateProtocol') - ->with('url')->willReturn('https://url'); - $this->dbHandler->expects($this->once())->method('addServer')->with('https://url') - ->willReturn($success); - - if ($success) { - $this->secureRandom->expects($this->once())->method('generate') - ->willReturn('token'); - $this->dbHandler->expects($this->once())->method('addToken')->with('https://url', 'token'); - $this->jobList->expects($this->once())->method('add') - ->with('OCA\Federation\BackgroundJob\RequestSharedSecret', - ['url' => 'https://url', 'token' => 'token']); - } else { - $this->jobList->expects($this->never())->method('add'); - } - - $this->assertSame($success, - $trustedServers->addServer('url') - ); - } - - 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; - $server = ['url_hash' => 'url_hash']; - $this->dbHandler->expects($this->once())->method('removeServer')->with($id); - $this->dbHandler->expects($this->once())->method('getServerById')->with($id) - ->willReturn($server); - $this->dispatcher->expects($this->once())->method('dispatch') - ->willReturnCallback( - function($eventId, $event) { - $this->assertSame($eventId, 'OCP\Federation\TrustedServerEvent::remove'); - $this->assertInstanceOf('Symfony\Component\EventDispatcher\GenericEvent', $event); - $this->assertSame('url_hash', $event->getSubject()); - } - ); - $this->trustedServers->removeServer($id); - } - - public function testGetServers() { - $this->dbHandler->expects($this->once())->method('getAllServer')->willReturn(true); - - $this->assertTrue( - $this->trustedServers->getServers() - ); - } - - - public function testIsTrustedServer() { - $this->dbHandler->expects($this->once())->method('serverExists')->with('url') - ->willReturn(true); - - $this->assertTrue( - $this->trustedServers->isTrustedServer('url') - ); - } - - 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 - * - * @param int $statusCode - * @param bool $isValidOwnCloudVersion - * @param bool $expected - */ - public function testIsOwnCloudServer($statusCode, $isValidOwnCloudVersion, $expected) { - - $server = 'server1'; - - /** @var \PHPUnit_Framework_MockObject_MockObject | TrustedServers $trustedServer */ - $trustedServers = $this->getMockBuilder('OCA\Federation\TrustedServers') - ->setConstructorArgs( - [ - $this->dbHandler, - $this->httpClientService, - $this->logger, - $this->jobList, - $this->secureRandom, - $this->config, - $this->dispatcher - ] - ) - ->setMethods(['checkOwnCloudVersion']) - ->getMock(); - - $this->httpClientService->expects($this->once())->method('newClient') - ->willReturn($this->httpClient); - - $this->httpClient->expects($this->once())->method('get')->with($server . '/status.php') - ->willReturn($this->response); - - $this->response->expects($this->once())->method('getStatusCode') - ->willReturn($statusCode); - - if ($statusCode === 200) { - $trustedServers->expects($this->once())->method('checkOwnCloudVersion') - ->willReturn($isValidOwnCloudVersion); - } else { - $trustedServers->expects($this->never())->method('checkOwnCloudVersion'); - } - - $this->assertSame($expected, - $trustedServers->isOwnCloudServer($server) - ); - - } - - public function dataTestIsOwnCloudServer() { - return [ - [200, true, true], - [200, false, false], - [404, true, false], - ]; - } - - /** - * @expectedException \Exception - * @expectedExceptionMessage simulated exception - */ - public function testIsOwnCloudServerFail() { - $server = 'server1'; - - $this->httpClientService->expects($this->once())->method('newClient') - ->willReturn($this->httpClient); - - $this->httpClient->expects($this->once())->method('get')->with($server . '/status.php') - ->willReturnCallback(function () { - throw new \Exception('simulated exception'); - }); - - $this->trustedServers->isOwnCloudServer($server); - } - - /** - * @dataProvider dataTestCheckOwnCloudVersion - */ - public function testCheckOwnCloudVersion($status) { - $this->assertTrue($this->invokePrivate($this->trustedServers, 'checkOwnCloudVersion', [$status])); - } - - public function dataTestCheckOwnCloudVersion() { - return [ - ['{"version":"9.0.0"}'], - ['{"version":"9.1.0"}'] - ]; - } - - /** - * @dataProvider dataTestCheckOwnCloudVersionTooLow - * @expectedException \OC\HintException - * @expectedExceptionMessage Remote server version is too low. ownCloud 9.0 is required. - */ - public function testCheckOwnCloudVersionTooLow($status) { - $this->invokePrivate($this->trustedServers, 'checkOwnCloudVersion', [$status]); - } - - public function dataTestCheckOwnCloudVersionTooLow() { - return [ - ['{"version":"8.2.3"}'], - ]; - } - - /** - * @dataProvider dataTestUpdateProtocol - * @param string $url - * @param string $expected - */ - public function testUpdateProtocol($url, $expected) { - $this->assertSame($expected, - $this->invokePrivate($this->trustedServers, 'updateProtocol', [$url]) - ); - } - - public function dataTestUpdateProtocol() { - return [ - ['http://owncloud.org', 'http://owncloud.org'], - ['https://owncloud.org', 'https://owncloud.org'], - ['owncloud.org', 'https://owncloud.org'], - ['httpserver', 'https://httpserver'], - ]; - } -} diff --git a/apps/federation/tests/SyncFederationAddressbooksTest.php b/apps/federation/tests/SyncFederationAddressbooksTest.php new file mode 100644 index 00000000000..4c266fae3a0 --- /dev/null +++ b/apps/federation/tests/SyncFederationAddressbooksTest.php @@ -0,0 +1,90 @@ + + * @author Lukas Reschke + * @author Thomas Müller + * + * @copyright Copyright (c) 2016, 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 + * + */ +namespace OCA\Federation\Tests; + +use OCA\Federation\DbHandler; +use OCA\Federation\SyncFederationAddressBooks; + +class SyncFederationAddressbooksTest extends \Test\TestCase { + + /** @var array */ + private $callBacks = []; + + function testSync() { + /** @var DbHandler | \PHPUnit_Framework_MockObject_MockObject $dbHandler */ + $dbHandler = $this->getMockBuilder('OCA\Federation\DbHandler')-> + disableOriginalConstructor()-> + getMock(); + $dbHandler->method('getAllServer')-> + willReturn([ + [ + 'url' => 'https://cloud.drop.box', + 'url_hash' => 'sha1', + 'shared_secret' => 'iloveowncloud', + 'sync_token' => '0' + ] + ]); + $dbHandler->expects($this->once())->method('setServerStatus')-> + with('https://cloud.drop.box', 1, '1'); + $syncService = $this->getMockBuilder('OCA\DAV\CardDAV\SyncService') + ->disableOriginalConstructor() + ->getMock(); + $syncService->expects($this->once())->method('syncRemoteAddressBook') + ->willReturn(1); + + /** @var \OCA\DAV\CardDAV\SyncService $syncService */ + $s = new SyncFederationAddressBooks($dbHandler, $syncService); + $s->syncThemAll(function($url, $ex) { + $this->callBacks[] = [$url, $ex]; + }); + $this->assertEquals(1, count($this->callBacks)); + } + + function testException() { + /** @var DbHandler | \PHPUnit_Framework_MockObject_MockObject $dbHandler */ + $dbHandler = $this->getMockBuilder('OCA\Federation\DbHandler')-> + disableOriginalConstructor()-> + getMock(); + $dbHandler->method('getAllServer')-> + willReturn([ + [ + 'url' => 'https://cloud.drop.box', + 'url_hash' => 'sha1', + 'shared_secret' => 'iloveowncloud', + 'sync_token' => '0' + ] + ]); + $syncService = $this->getMockBuilder('OCA\DAV\CardDAV\SyncService') + ->disableOriginalConstructor() + ->getMock(); + $syncService->expects($this->once())->method('syncRemoteAddressBook') + ->willThrowException(new \Exception('something did not work out')); + + /** @var \OCA\DAV\CardDAV\SyncService $syncService */ + $s = new SyncFederationAddressBooks($dbHandler, $syncService); + $s->syncThemAll(function($url, $ex) { + $this->callBacks[] = [$url, $ex]; + }); + $this->assertEquals(2, count($this->callBacks)); + } +} diff --git a/apps/federation/tests/TrustedServersTest.php b/apps/federation/tests/TrustedServersTest.php new file mode 100644 index 00000000000..e49db2556be --- /dev/null +++ b/apps/federation/tests/TrustedServersTest.php @@ -0,0 +1,374 @@ + + * @author Thomas Müller + * + * @copyright Copyright (c) 2016, 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 + * + */ + + +namespace OCA\Federation\Tests; + + +use OCA\Federation\DbHandler; +use OCA\Federation\TrustedServers; +use OCP\BackgroundJob\IJobList; +use OCP\Http\Client\IClient; +use OCP\Http\Client\IClientService; +use OCP\Http\Client\IResponse; +use OCP\IConfig; +use OCP\ILogger; +use OCP\Security\ISecureRandom; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Test\TestCase; + +class TrustedServersTest extends TestCase { + + /** @var \PHPUnit_Framework_MockObject_MockObject | TrustedServers */ + private $trustedServers; + + /** @var \PHPUnit_Framework_MockObject_MockObject | DbHandler */ + private $dbHandler; + + /** @var \PHPUnit_Framework_MockObject_MockObject | IClientService */ + private $httpClientService; + + /** @var \PHPUnit_Framework_MockObject_MockObject | IClient */ + private $httpClient; + + /** @var \PHPUnit_Framework_MockObject_MockObject | IResponse */ + private $response; + + /** @var \PHPUnit_Framework_MockObject_MockObject | ILogger */ + private $logger; + + /** @var \PHPUnit_Framework_MockObject_MockObject | IJobList */ + private $jobList; + + /** @var \PHPUnit_Framework_MockObject_MockObject | ISecureRandom */ + private $secureRandom; + + /** @var \PHPUnit_Framework_MockObject_MockObject | IConfig */ + private $config; + + /** @var \PHPUnit_Framework_MockObject_MockObject | EventDispatcherInterface */ + private $dispatcher; + + public function setUp() { + parent::setUp(); + + $this->dbHandler = $this->getMockBuilder('\OCA\Federation\DbHandler') + ->disableOriginalConstructor()->getMock(); + $this->dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface') + ->disableOriginalConstructor()->getMock(); + $this->httpClientService = $this->getMock('OCP\Http\Client\IClientService'); + $this->httpClient = $this->getMock('OCP\Http\Client\IClient'); + $this->response = $this->getMock('OCP\Http\Client\IResponse'); + $this->logger = $this->getMock('OCP\ILogger'); + $this->jobList = $this->getMock('OCP\BackgroundJob\IJobList'); + $this->secureRandom = $this->getMock('OCP\Security\ISecureRandom'); + $this->config = $this->getMock('OCP\IConfig'); + + $this->trustedServers = new TrustedServers( + $this->dbHandler, + $this->httpClientService, + $this->logger, + $this->jobList, + $this->secureRandom, + $this->config, + $this->dispatcher + ); + + } + + /** + * @dataProvider dataTrueFalse + * + * @param bool $success + */ + public function testAddServer($success) { + /** @var \PHPUnit_Framework_MockObject_MockObject|TrustedServers $trustedServers */ + $trustedServers = $this->getMockBuilder('OCA\Federation\TrustedServers') + ->setConstructorArgs( + [ + $this->dbHandler, + $this->httpClientService, + $this->logger, + $this->jobList, + $this->secureRandom, + $this->config, + $this->dispatcher + ] + ) + ->setMethods(['normalizeUrl', 'updateProtocol']) + ->getMock(); + $trustedServers->expects($this->once())->method('updateProtocol') + ->with('url')->willReturn('https://url'); + $this->dbHandler->expects($this->once())->method('addServer')->with('https://url') + ->willReturn($success); + + if ($success) { + $this->secureRandom->expects($this->once())->method('generate') + ->willReturn('token'); + $this->dbHandler->expects($this->once())->method('addToken')->with('https://url', 'token'); + $this->jobList->expects($this->once())->method('add') + ->with('OCA\Federation\BackgroundJob\RequestSharedSecret', + ['url' => 'https://url', 'token' => 'token']); + } else { + $this->jobList->expects($this->never())->method('add'); + } + + $this->assertSame($success, + $trustedServers->addServer('url') + ); + } + + 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() + ); + } + + 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; + $server = ['url_hash' => 'url_hash']; + $this->dbHandler->expects($this->once())->method('removeServer')->with($id); + $this->dbHandler->expects($this->once())->method('getServerById')->with($id) + ->willReturn($server); + $this->dispatcher->expects($this->once())->method('dispatch') + ->willReturnCallback( + function($eventId, $event) { + $this->assertSame($eventId, 'OCP\Federation\TrustedServerEvent::remove'); + $this->assertInstanceOf('Symfony\Component\EventDispatcher\GenericEvent', $event); + /** @var \Symfony\Component\EventDispatcher\GenericEvent $event */ + $this->assertSame('url_hash', $event->getSubject()); + } + ); + $this->trustedServers->removeServer($id); + } + + public function testGetServers() { + $this->dbHandler->expects($this->once())->method('getAllServer')->willReturn(['servers']); + + $this->assertEquals( + ['servers'], + $this->trustedServers->getServers() + ); + } + + + public function testIsTrustedServer() { + $this->dbHandler->expects($this->once())->method('serverExists')->with('url') + ->willReturn(true); + + $this->assertTrue( + $this->trustedServers->isTrustedServer('url') + ); + } + + 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 + * + * @param int $statusCode + * @param bool $isValidOwnCloudVersion + * @param bool $expected + */ + public function testIsOwnCloudServer($statusCode, $isValidOwnCloudVersion, $expected) { + + $server = 'server1'; + + /** @var \PHPUnit_Framework_MockObject_MockObject | TrustedServers $trustedServers */ + $trustedServers = $this->getMockBuilder('OCA\Federation\TrustedServers') + ->setConstructorArgs( + [ + $this->dbHandler, + $this->httpClientService, + $this->logger, + $this->jobList, + $this->secureRandom, + $this->config, + $this->dispatcher + ] + ) + ->setMethods(['checkOwnCloudVersion']) + ->getMock(); + + $this->httpClientService->expects($this->once())->method('newClient') + ->willReturn($this->httpClient); + + $this->httpClient->expects($this->once())->method('get')->with($server . '/status.php') + ->willReturn($this->response); + + $this->response->expects($this->once())->method('getStatusCode') + ->willReturn($statusCode); + + if ($statusCode === 200) { + $trustedServers->expects($this->once())->method('checkOwnCloudVersion') + ->willReturn($isValidOwnCloudVersion); + } else { + $trustedServers->expects($this->never())->method('checkOwnCloudVersion'); + } + + $this->assertSame($expected, + $trustedServers->isOwnCloudServer($server) + ); + + } + + public function dataTestIsOwnCloudServer() { + return [ + [200, true, true], + [200, false, false], + [404, true, false], + ]; + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage simulated exception + */ + public function testIsOwnCloudServerFail() { + $server = 'server1'; + + $this->httpClientService->expects($this->once())->method('newClient') + ->willReturn($this->httpClient); + + $this->httpClient->expects($this->once())->method('get')->with($server . '/status.php') + ->willReturnCallback(function () { + throw new \Exception('simulated exception'); + }); + + $this->trustedServers->isOwnCloudServer($server); + } + + /** + * @dataProvider dataTestCheckOwnCloudVersion + */ + public function testCheckOwnCloudVersion($status) { + $this->assertTrue($this->invokePrivate($this->trustedServers, 'checkOwnCloudVersion', [$status])); + } + + public function dataTestCheckOwnCloudVersion() { + return [ + ['{"version":"9.0.0"}'], + ['{"version":"9.1.0"}'] + ]; + } + + /** + * @dataProvider dataTestCheckOwnCloudVersionTooLow + * @expectedException \OC\HintException + * @expectedExceptionMessage Remote server version is too low. ownCloud 9.0 is required. + */ + public function testCheckOwnCloudVersionTooLow($status) { + $this->invokePrivate($this->trustedServers, 'checkOwnCloudVersion', [$status]); + } + + public function dataTestCheckOwnCloudVersionTooLow() { + return [ + ['{"version":"8.2.3"}'], + ]; + } + + /** + * @dataProvider dataTestUpdateProtocol + * @param string $url + * @param string $expected + */ + public function testUpdateProtocol($url, $expected) { + $this->assertSame($expected, + $this->invokePrivate($this->trustedServers, 'updateProtocol', [$url]) + ); + } + + public function dataTestUpdateProtocol() { + return [ + ['http://owncloud.org', 'http://owncloud.org'], + ['https://owncloud.org', 'https://owncloud.org'], + ['owncloud.org', 'https://owncloud.org'], + ['httpserver', 'https://httpserver'], + ]; + } +} -- cgit v1.2.3