aboutsummaryrefslogtreecommitdiffstats
path: root/apps/federation/tests/lib
diff options
context:
space:
mode:
authorBjörn Schießle <bjoern@schiessle.org>2015-11-10 10:50:59 +0100
committerBjörn Schießle <bjoern@schiessle.org>2015-11-19 18:06:51 +0100
commit698100d279fdd8c8bc086e729833705d1a31c018 (patch)
tree3e38110373da82a5a89dbbe1a84708b6208b7d51 /apps/federation/tests/lib
parented039ee5ebdba6778b245f249fe206d2423a6a36 (diff)
downloadnextcloud-server-698100d279fdd8c8bc086e729833705d1a31c018.tar.gz
nextcloud-server-698100d279fdd8c8bc086e729833705d1a31c018.zip
exchange shared secret
Diffstat (limited to 'apps/federation/tests/lib')
-rw-r--r--apps/federation/tests/lib/dbhandlertest.php45
-rw-r--r--apps/federation/tests/lib/trustedserverstest.php113
2 files changed, 108 insertions, 50 deletions
diff --git a/apps/federation/tests/lib/dbhandlertest.php b/apps/federation/tests/lib/dbhandlertest.php
index 202199d2b5b..50bb9a73d92 100644
--- a/apps/federation/tests/lib/dbhandlertest.php
+++ b/apps/federation/tests/lib/dbhandlertest.php
@@ -27,6 +27,9 @@ use OCA\Federation\DbHandler;
use OCP\IDBConnection;
use Test\TestCase;
+/**
+ * @group DB
+ */
class DbHandlerTest extends TestCase {
/** @var DbHandler */
@@ -63,8 +66,8 @@ class DbHandlerTest extends TestCase {
$query->execute();
}
- public function testAdd() {
- $id = $this->dbHandler->add('server1');
+ public function testAddServer() {
+ $id = $this->dbHandler->addServer('server1');
$query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
$result = $query->execute()->fetchAll();
@@ -74,8 +77,8 @@ class DbHandlerTest extends TestCase {
}
public function testRemove() {
- $id1 = $this->dbHandler->add('server1');
- $id2 = $this->dbHandler->add('server2');
+ $id1 = $this->dbHandler->addServer('server1');
+ $id2 = $this->dbHandler->addServer('server2');
$query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
$result = $query->execute()->fetchAll();
@@ -85,7 +88,7 @@ class DbHandlerTest extends TestCase {
$this->assertSame($id1, $result[0]['id']);
$this->assertSame($id2, $result[1]['id']);
- $this->dbHandler->remove($id2);
+ $this->dbHandler->removeServer($id2);
$query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
$result = $query->execute()->fetchAll();
$this->assertSame(1, count($result));
@@ -94,10 +97,10 @@ class DbHandlerTest extends TestCase {
}
public function testGetAll() {
- $id1 = $this->dbHandler->add('server1');
- $id2 = $this->dbHandler->add('server2');
+ $id1 = $this->dbHandler->addServer('server1');
+ $id2 = $this->dbHandler->addServer('server2');
- $result = $this->dbHandler->getAll();
+ $result = $this->dbHandler->getAllServer();
$this->assertSame(2, count($result));
$this->assertSame('server1', $result[0]['url']);
$this->assertSame('server2', $result[1]['url']);
@@ -113,9 +116,9 @@ class DbHandlerTest extends TestCase {
* @param bool $expected
*/
public function testExists($serverInTable, $checkForServer, $expected) {
- $this->dbHandler->add($serverInTable);
+ $this->dbHandler->addServer($serverInTable);
$this->assertSame($expected,
- $this->dbHandler->exists($checkForServer)
+ $this->dbHandler->serverExists($checkForServer)
);
}
@@ -127,4 +130,26 @@ class DbHandlerTest extends TestCase {
];
}
+ /**
+ * @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'],
+ ];
+ }
+
}
diff --git a/apps/federation/tests/lib/trustedserverstest.php b/apps/federation/tests/lib/trustedserverstest.php
index 07aa7531274..dabf353ef21 100644
--- a/apps/federation/tests/lib/trustedserverstest.php
+++ b/apps/federation/tests/lib/trustedserverstest.php
@@ -25,11 +25,13 @@ 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\IDBConnection;
+use OCP\IConfig;
use OCP\ILogger;
+use OCP\Security\ISecureRandom;
use Test\TestCase;
class TrustedServersTest extends TestCase {
@@ -52,6 +54,15 @@ class TrustedServersTest extends TestCase {
/** @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;
+
public function setUp() {
parent::setUp();
@@ -61,45 +72,79 @@ class TrustedServersTest extends TestCase {
$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->logger,
+ $this->jobList,
+ $this->secureRandom,
+ $this->config
);
}
- public function testAddServer() {
+ /**
+ * @dataProvider dataTestAddServer
+ *
+ * @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->logger,
+ $this->jobList,
+ $this->secureRandom,
+ $this->config
]
)
- ->setMethods(['normalizeUrl'])
+ ->setMethods(['normalizeUrl', 'updateProtocol'])
->getMock();
- $trustedServers->expects($this->once())->method('normalizeUrl')
- ->with('url')->willReturn('normalized');
- $this->dbHandler->expects($this->once())->method('add')->with('normalized')
- ->willReturn(true);
+ $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('getMediumStrengthGenerator')
+ ->willReturn($this->secureRandom);
+ $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->assertTrue(
+ $this->assertSame($success,
$trustedServers->addServer('url')
);
}
+ public function dataTestAddServer() {
+ return [
+ [true],
+ [false]
+ ];
+ }
+
public function testRemoveServer() {
$id = 42;
- $this->dbHandler->expects($this->once())->method('remove')->with($id);
+ $this->dbHandler->expects($this->once())->method('removeServer')->with($id);
$this->trustedServers->removeServer($id);
}
public function testGetServers() {
- $this->dbHandler->expects($this->once())->method('getAll')->willReturn(true);
+ $this->dbHandler->expects($this->once())->method('getAllServer')->willReturn(true);
$this->assertTrue(
$this->trustedServers->getServers()
@@ -108,24 +153,11 @@ class TrustedServersTest extends TestCase {
public function testIsTrustedServer() {
- /** @var \PHPUnit_Framework_MockObject_MockObject | TrustedServers $trustedServer */
- $trustedServers = $this->getMockBuilder('OCA\Federation\TrustedServers')
- ->setConstructorArgs(
- [
- $this->dbHandler,
- $this->httpClientService,
- $this->logger
- ]
- )
- ->setMethods(['normalizeUrl'])
- ->getMock();
- $trustedServers->expects($this->once())->method('normalizeUrl')
- ->with('url')->willReturn('normalized');
- $this->dbHandler->expects($this->once())->method('exists')->with('normalized')
- ->willReturn(true);
+ $this->dbHandler->expects($this->once())->method('serverExists')->with('url')
+ ->willReturn(true);
$this->assertTrue(
- $trustedServers->isTrustedServer('url')
+ $this->trustedServers->isTrustedServer('url')
);
}
@@ -146,7 +178,10 @@ class TrustedServersTest extends TestCase {
[
$this->dbHandler,
$this->httpClientService,
- $this->logger
+ $this->logger,
+ $this->jobList,
+ $this->secureRandom,
+ $this->config
]
)
->setMethods(['checkOwnCloudVersion'])
@@ -192,7 +227,7 @@ class TrustedServersTest extends TestCase {
->with('simulated exception', ['app' => 'federation']);
$this->httpClient->expects($this->once())->method('get')->with($server . '/status.php')
- ->willReturnCallback(function() {
+ ->willReturnCallback(function () {
throw new \Exception('simulated exception');
});
@@ -221,24 +256,22 @@ class TrustedServersTest extends TestCase {
}
/**
- * @dataProvider dataTestNormalizeUrl
- *
+ * @dataProvider dataTestUpdateProtocol
* @param string $url
* @param string $expected
*/
- public function testNormalizeUrl($url, $expected) {
+ public function testUpdateProtocol($url, $expected) {
$this->assertSame($expected,
- $this->invokePrivate($this->trustedServers, 'normalizeUrl', [$url])
+ $this->invokePrivate($this->trustedServers, 'updateProtocol', [$url])
);
}
- public function dataTestNormalizeUrl() {
+ public function dataTestUpdateProtocol() {
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'],
+ ['http://owncloud.org', 'http://owncloud.org'],
+ ['https://owncloud.org', 'https://owncloud.org'],
+ ['owncloud.org', 'https://owncloud.org'],
+ ['httpserver', 'https://httpserver'],
];
}
}