123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- <?php
- namespace OCA\Federation\Tests;
- use OCA\Federation\DbHandler;
- use OCA\Federation\TrustedServers;
- use OCP\IDBConnection;
- use OCP\IL10N;
- use Test\TestCase;
- class DbHandlerTest extends TestCase {
-
- private $dbHandler;
-
- private $il10n;
-
- private $connection;
-
- private $dbTable = 'trusted_servers';
- protected function setUp(): void {
- parent::setUp();
- $this->connection = \OC::$server->getDatabaseConnection();
- $this->il10n = $this->getMockBuilder(IL10N::class)->getMock();
- $this->dbHandler = new DbHandler(
- $this->connection,
- $this->il10n
- );
- $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
- $qResult = $query->execute();
- $result = $qResult->fetchAll();
- $qResult->closeCursor();
- $this->assertEmpty($result, 'we need to start with a empty trusted_servers table');
- }
- protected function tearDown(): void {
- parent::tearDown();
- $query = $this->connection->getQueryBuilder()->delete($this->dbTable);
- $query->execute();
- }
-
- public function testAddServer($url, $expectedUrl, $expectedHash) {
- $id = $this->dbHandler->addServer($url);
- $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
- $qResult = $query->execute();
- $result = $qResult->fetchAll();
- $qResult->closeCursor();
- $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);
- $qResult = $query->execute();
- $result = $qResult->fetchAll();
- $qResult->closeCursor();
- $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);
- $qResult = $query->execute();
- $result = $qResult->fetchAll();
- $qResult->closeCursor();
- $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']);
- }
-
- 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 XtestAddToken() {
- $this->dbHandler->addServer('server1');
- $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
- $qResult = $query->execute();
- $result = $qResult->fetchAll();
- $qResult->closeCursor();
- $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);
- $qResult = $query->execute();
- $result = $qResult->fetchAll();
- $qResult->closeCursor();
- $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 XtestAddSharedSecret() {
- $this->dbHandler->addServer('server1');
- $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
- $qResult = $query->execute();
- $result = $qResult->fetchAll();
- $qResult->closeCursor();
- $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);
- $qResult = $query->execute();
- $result = $qResult->fetchAll();
- $qResult->closeCursor();
- $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);
- $qResult = $query->execute();
- $result = $qResult->fetchAll();
- $qResult->closeCursor();
- $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);
- $qResult = $query->execute();
- $result = $qResult->fetchAll();
- $qResult->closeCursor();
- $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')
- );
-
- $this->dbHandler->setServerStatus('http://server1', TrustedServers::STATUS_OK, 'token1234567890');
- $servers = $this->dbHandler->getAllServer();
- $this->assertSame('token1234567890', $servers[0]['sync_token']);
- }
-
- 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')],
- ];
- }
-
- 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'],
- ];
- }
-
- 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'],
- ];
- }
- }
|