diff options
author | Björn Schießle <bjoern@schiessle.org> | 2015-11-19 17:49:43 +0100 |
---|---|---|
committer | Björn Schießle <bjoern@schiessle.org> | 2015-11-20 15:48:20 +0100 |
commit | a63a1043b6a40193a44d2fb1b702a775bb761c8d (patch) | |
tree | 74aa72a8ec9e39bbfc4a3d427f0abf1e8bf4ed5d /apps/federation/tests/lib | |
parent | f6446a64b62e964e043995f6bfeb2118a7dc489a (diff) | |
download | nextcloud-server-a63a1043b6a40193a44d2fb1b702a775bb761c8d.tar.gz nextcloud-server-a63a1043b6a40193a44d2fb1b702a775bb761c8d.zip |
unit tests
Diffstat (limited to 'apps/federation/tests/lib')
-rw-r--r-- | apps/federation/tests/lib/dbhandlertest.php | 108 | ||||
-rw-r--r-- | apps/federation/tests/lib/trustedserverstest.php | 71 |
2 files changed, 167 insertions, 12 deletions
diff --git a/apps/federation/tests/lib/dbhandlertest.php b/apps/federation/tests/lib/dbhandlertest.php index 50bb9a73d92..e47df092f8c 100644 --- a/apps/federation/tests/lib/dbhandlertest.php +++ b/apps/federation/tests/lib/dbhandlertest.php @@ -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 * diff --git a/apps/federation/tests/lib/trustedserverstest.php b/apps/federation/tests/lib/trustedserverstest.php index dabf353ef21..d067cd1c185 100644 --- a/apps/federation/tests/lib/trustedserverstest.php +++ b/apps/federation/tests/lib/trustedserverstest.php @@ -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 * |