summaryrefslogtreecommitdiffstats
path: root/apps/federation/tests
diff options
context:
space:
mode:
Diffstat (limited to 'apps/federation/tests')
-rw-r--r--apps/federation/tests/api/ocsauthapitest.php184
-rw-r--r--apps/federation/tests/lib/dbhandlertest.php45
-rw-r--r--apps/federation/tests/lib/trustedserverstest.php113
3 files changed, 292 insertions, 50 deletions
diff --git a/apps/federation/tests/api/ocsauthapitest.php b/apps/federation/tests/api/ocsauthapitest.php
new file mode 100644
index 00000000000..a334686c24e
--- /dev/null
+++ b/apps/federation/tests/api/ocsauthapitest.php
@@ -0,0 +1,184 @@
+<?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\API;
+
+
+use OC\BackgroundJob\JobList;
+use OCA\Federation\API\OCSAuthAPI;
+use OCA\Federation\DbHandler;
+use OCA\Federation\TrustedServers;
+use OCP\AppFramework\Http;
+use OCP\IRequest;
+use OCP\Security\ISecureRandom;
+use Test\TestCase;
+
+class OCSAuthAPITest extends TestCase {
+
+ /** @var \PHPUnit_Framework_MockObject_MockObject | IRequest */
+ private $request;
+
+ /** @var \PHPUnit_Framework_MockObject_MockObject | ISecureRandom */
+ private $secureRandom;
+
+ /** @var \PHPUnit_Framework_MockObject_MockObject | JobList */
+ private $jobList;
+
+ /** @var \PHPUnit_Framework_MockObject_MockObject | TrustedServers */
+ private $trustedServers;
+
+ /** @var \PHPUnit_Framework_MockObject_MockObject | DbHandler */
+ private $dbHandler;
+
+ /** @var OCSAuthApi */
+ private $ocsAuthApi;
+
+ public function setUp() {
+ parent::setUp();
+
+ $this->request = $this->getMock('OCP\IRequest');
+ $this->secureRandom = $this->getMock('OCP\Security\ISecureRandom');
+ $this->trustedServers = $this->getMockBuilder('OCA\Federation\TrustedServers')
+ ->disableOriginalConstructor()->getMock();
+ $this->dbHandler = $this->getMockBuilder('OCA\Federation\DbHandler')
+ ->disableOriginalConstructor()->getMock();
+ $this->jobList = $this->getMockBuilder('OC\BackgroundJob\JobList')
+ ->disableOriginalConstructor()->getMock();
+
+ $this->ocsAuthApi = new OCSAuthAPI(
+ $this->request,
+ $this->secureRandom,
+ $this->jobList,
+ $this->trustedServers,
+ $this->dbHandler
+ );
+
+ }
+
+ /**
+ * @dataProvider dataTestRequestSharedSecret
+ *
+ * @param string $token
+ * @param string $localToken
+ * @param bool $isTrustedServer
+ * @param int $expected
+ */
+ public function testRequestSharedSecret($token, $localToken, $isTrustedServer, $expected) {
+
+ $url = 'url';
+
+ $this->request->expects($this->at(0))->method('getParam')->with('url')->willReturn($url);
+ $this->request->expects($this->at(1))->method('getParam')->with('token')->willReturn($token);
+ $this->trustedServers
+ ->expects($this->once())
+ ->method('isTrustedServer')->with($url)->willReturn($isTrustedServer);
+ $this->dbHandler->expects($this->any())
+ ->method('getToken')->with($url)->willReturn($localToken);
+
+ if ($expected === Http::STATUS_OK) {
+ $this->jobList->expects($this->once())->method('add')
+ ->with('OCA\Federation\BackgroundJob\GetSharedSecret', ['url' => $url, 'token' => $token]);
+ } else {
+ $this->jobList->expects($this->never())->method('add');
+ }
+
+ $result = $this->ocsAuthApi->requestSharedSecret();
+ $this->assertSame($expected, $result->getStatusCode());
+ }
+
+ public function dataTestRequestSharedSecret() {
+ return [
+ ['token2', 'token1', true, Http::STATUS_OK],
+ ['token1', 'token2', false, Http::STATUS_FORBIDDEN],
+ ['token1', 'token2', true, Http::STATUS_FORBIDDEN],
+ ];
+ }
+
+ /**
+ * @dataProvider dataTestGetSharedSecret
+ *
+ * @param bool $isTrustedServer
+ * @param bool $isValidToken
+ * @param int $expected
+ */
+ public function testGetSharedSecret($isTrustedServer, $isValidToken, $expected) {
+
+ $url = 'url';
+ $token = 'token';
+
+ $this->request->expects($this->at(0))->method('getParam')->with('url')->willReturn($url);
+ $this->request->expects($this->at(1))->method('getParam')->with('token')->willReturn($token);
+
+ /** @var OCSAuthAPI | \PHPUnit_Framework_MockObject_MockObject $ocsAuthApi */
+ $ocsAuthApi = $this->getMockBuilder('OCA\Federation\API\OCSAuthAPI')
+ ->setConstructorArgs(
+ [
+ $this->request,
+ $this->secureRandom,
+ $this->jobList,
+ $this->trustedServers,
+ $this->dbHandler
+ ]
+ )->setMethods(['isValidToken'])->getMock();
+
+ $this->trustedServers
+ ->expects($this->any())
+ ->method('isTrustedServer')->with($url)->willReturn($isTrustedServer);
+ $ocsAuthApi->expects($this->any())
+ ->method('isValidToken')->with($url, $token)->willReturn($isValidToken);
+
+ if($expected === Http::STATUS_OK) {
+ $this->secureRandom->expects($this->once())->method('getMediumStrengthGenerator')
+ ->willReturn($this->secureRandom);
+ $this->secureRandom->expects($this->once())->method('generate')->with(32)
+ ->willReturn('secret');
+ $this->trustedServers->expects($this->once())
+ ->method('addSharedSecret')->willReturn($url, 'secret');
+ $this->dbHandler->expects($this->once())
+ ->method('addToken')->with($url, '');
+ } else {
+ $this->secureRandom->expects($this->never())->method('getMediumStrengthGenerator');
+ $this->secureRandom->expects($this->never())->method('generate');
+ $this->trustedServers->expects($this->never())->method('addSharedSecret');
+ $this->dbHandler->expects($this->never())->method('addToken');
+ }
+
+ $result = $ocsAuthApi->getSharedSecret();
+
+ $this->assertSame($expected, $result->getStatusCode());
+
+ if ($expected === Http::STATUS_OK) {
+ $data = $result->getData();
+ $this->assertSame('secret', $data['sharedSecret']);
+ }
+ }
+
+ public function dataTestGetSharedSecret() {
+ return [
+ [true, true, Http::STATUS_OK],
+ [false, true, Http::STATUS_FORBIDDEN],
+ [true, false, Http::STATUS_FORBIDDEN],
+ [false, false, Http::STATUS_FORBIDDEN],
+ ];
+ }
+
+}
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'],
];
}
}