]> source.dussan.org Git - nextcloud-server.git/commitdiff
Adding tests and adjust PHPDoc
authorThomas Müller <thomas.mueller@tmit.eu>
Mon, 7 Dec 2015 14:28:06 +0000 (15:28 +0100)
committerThomas Müller <thomas.mueller@tmit.eu>
Tue, 12 Jan 2016 13:24:47 +0000 (14:24 +0100)
apps/dav/lib/carddav/syncservice.php
apps/federation/appinfo/application.php
apps/federation/dav/fedauth.php
apps/federation/lib/dbhandler.php
apps/federation/tests/dav/fedauthtest.php [new file with mode: 0644]
apps/federation/tests/lib/dbhandlertest.php

index 8442f7cf52bac9cd6bb8fe07bba36874355e8069..bfaf09f4c0e7333177063543933d41e4a2d9fef6 100644 (file)
@@ -34,6 +34,16 @@ class SyncService {
                $this->backend = $backend;
        }
 
+       /**
+        * @param string $url
+        * @param string $userName
+        * @param string $sharedSecret
+        * @param string $syncToken
+        * @param int $targetBookId
+        * @param string $targetPrincipal
+        * @param array $targetProperties
+        * @return string
+        */
        public function syncRemoteAddressBook($url, $userName, $sharedSecret, $syncToken, $targetBookId, $targetPrincipal, $targetProperties) {
                // 1. create addressbook
                $book = $this->ensureSystemAddressBookExists($targetPrincipal, $targetBookId, $targetProperties);
@@ -62,6 +72,13 @@ class SyncService {
                return $response['token'];
        }
 
+       /**
+        * @param string $principal
+        * @param string $id
+        * @param array $properties
+        * @return array|null
+        * @throws \Sabre\DAV\Exception\BadRequest
+        */
        protected function ensureSystemAddressBookExists($principal, $id, $properties) {
                $book = $this->backend->getAddressBooksByUri($id);
                if (!is_null($book)) {
@@ -104,7 +121,13 @@ class SyncService {
                return $result;
        }
 
-       private function download($url, $sharedSecret, $changeSet) {
+       /**
+        * @param string $url
+        * @param string $sharedSecret
+        * @param string $resourcePath
+        * @return array
+        */
+       private function download($url, $sharedSecret, $resourcePath) {
                $settings = [
                        'baseUri' => $url,
                        'userName' => 'system',
@@ -113,11 +136,15 @@ class SyncService {
                $client = new Client($settings);
                $client->setThrowExceptions(true);
 
-               $response = $client->request('GET', $changeSet);
+               $response = $client->request('GET', $resourcePath);
                return $response;
        }
 
-       function buildSyncCollectionRequestBody($synToken) {
+       /**
+        * @param string|null $synToken
+        * @return string
+        */
+       private function buildSyncCollectionRequestBody($synToken) {
 
                $dom = new \DOMDocument('1.0', 'UTF-8');
                $dom->formatOutput = true;
@@ -137,6 +164,11 @@ class SyncService {
                return $body;
        }
 
+       /**
+        * @param string $body
+        * @return array
+        * @throws \Sabre\Xml\ParseException
+        */
        private function parseMultiStatus($body) {
                $xml = new Service();
 
index f0fefb948af9ba0e70fed43edabf5ef9881a1926..0214d73d9009462db086b4cc0db0a956c3d1f7df 100644 (file)
@@ -153,8 +153,10 @@ class Application extends \OCP\AppFramework\App {
                        if ($event instanceof SabrePluginEvent) {
                                $authPlugin = $event->getServer()->getPlugin('auth');
                                if ($authPlugin instanceof Plugin) {
-                                       $db = $container->getServer()->getDatabaseConnection();
-                                       $authPlugin->addBackend(new FedAuth($db));
+                                       $h = new DbHandler($container->getServer()->getDatabaseConnection(),
+                                                       $container->getServer()->getL10N('federation')
+                                       );
+                                       $authPlugin->addBackend(new FedAuth($h));
                                }
                        }
                });
index ade5448d1bcd8604e1d92fc5127bf4a2319dab58..56173233bf343e2bf675f82cfcd1257932cbe4ce 100644 (file)
@@ -21,7 +21,6 @@
 namespace OCA\Federation\DAV;
 
 use OCA\Federation\DbHandler;
-use OCP\IDBConnection;
 use Sabre\DAV\Auth\Backend\AbstractBasic;
 
 class FedAuth extends AbstractBasic {
@@ -29,9 +28,9 @@ class FedAuth extends AbstractBasic {
        /**
         * FedAuth constructor.
         *
-        * @param IDBConnection $db
+        * @param DbHandler $db
         */
-       public function __construct(IDBConnection $db) {
+       public function __construct(DbHandler $db) {
                $this->db = $db;
                $this->principalPrefix = 'principals/system/';
        }
@@ -47,9 +46,6 @@ class FedAuth extends AbstractBasic {
         * @return bool
         */
        protected function validateUserPass($username, $password) {
-               $h = new DbHandler($this->db,
-                               \OC::$server->getL10N('federation')
-               );
-               return $h->auth($username, $password);
+               return $this->db->auth($username, $password);
        }
 }
index 0677430f799aee6bc0b97554ca724ebcd08f3698..f50854fefb3ff9f490ac9fa1f1ab36114bd15b86 100644 (file)
@@ -206,6 +206,7 @@ class DbHandler {
         *
         * @param string $url
         * @param int $status
+        * @param string|null $token
         */
        public function setServerStatus($url, $status, $token = null) {
                $hash = $this->hash($url);
diff --git a/apps/federation/tests/dav/fedauthtest.php b/apps/federation/tests/dav/fedauthtest.php
new file mode 100644 (file)
index 0000000..845cfc6
--- /dev/null
@@ -0,0 +1,52 @@
+<?php
+/**
+ * @author Thomas Müller <thomas.mueller@tmit.eu>
+ *
+ * @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\DAV;
+
+use OCA\Federation\DAV\FedAuth;
+use OCA\Federation\DbHandler;
+use Test\TestCase;
+
+class FedAuthTest extends TestCase {
+
+       /**
+        * @dataProvider providesUser
+        *
+        * @param array $expected
+        * @param string $user
+        * @param string $password
+        */
+       public function testFedAuth($expected, $user, $password) {
+               /** @var DbHandler | \PHPUnit_Framework_MockObject_MockObject $db */
+               $db = $this->getMockBuilder('OCA\Federation\DbHandler')->disableOriginalConstructor()->getMock();
+               $db->method('auth')->willReturn(true);
+               $auth = new FedAuth($db);
+               $result = $this->invokePrivate($auth, 'validateUserPass', [$user, $password]);
+               $this->assertEquals($expected, $result);
+       }
+
+       public function providesUser() {
+               return [
+                       [true, 'system', '123456']
+               ];
+       }
+}
index 123eaaee45039a1ec8bcb33da7c091f64d0e41a6..65126e39f72fd638fafd146257aa5a53fe5d438e 100644 (file)
@@ -26,6 +26,7 @@ namespace OCA\Federation\Tests\lib;
 use OCA\Federation\DbHandler;
 use OCA\Federation\TrustedServers;
 use OCP\IDBConnection;
+use OCP\IL10N;
 use Test\TestCase;
 
 /**
@@ -36,7 +37,7 @@ class DbHandlerTest extends TestCase {
        /** @var  DbHandler */
        private $dbHandler;
 
-       /** @var  \PHPUnit_Framework_MockObject_MockObject */
+       /** @var IL10N | \PHPUnit_Framework_MockObject_MockObject */
        private $il10n;
 
        /** @var  IDBConnection */
@@ -209,6 +210,11 @@ class DbHandlerTest extends TestCase {
                $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']);
        }
 
        /**
@@ -256,4 +262,22 @@ class DbHandlerTest extends TestCase {
                ];
        }
 
+       /**
+        * @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'],
+               ];
+       }
 }