]> source.dussan.org Git - nextcloud-server.git/commitdiff
fix_tests
authorJörn Friedrich Dreyer <jfd@butonic.de>
Tue, 31 Mar 2015 13:33:44 +0000 (15:33 +0200)
committerMorris Jobke <hey@morrisjobke.de>
Wed, 22 Apr 2015 09:33:05 +0000 (11:33 +0200)
apps/user_ldap/lib/user/manager.php
apps/user_ldap/tests/user/manager.php
apps/user_ldap/tests/user_ldap.php

index accbc14cffb87f8ba38001694e89c1eb54c055fe..4a979a8fd61efe778bc922af28959c3315a42054 100644 (file)
@@ -157,6 +157,11 @@ class Manager {
                        $this->access);
        }
 
+       /**
+        * @brief returns a User object by it's ownCloud username
+        * @param string the DN or username of the user
+        * @return \OCA\user_ldap\lib\user\User|\OCA\user_ldap\lib\user\OfflineUser|null
+        */
        protected function createInstancyByUserName($id) {
                //most likely a uid. Check whether it is a deleted user
                if($this->isDeletedUser($id)) {
@@ -166,12 +171,12 @@ class Manager {
                if($dn !== false) {
                        return $this->createAndCache($dn, $id);
                }
-               throw new \Exception('Could not create User instance');
+               return null;
        }
 
        /**
         * @brief returns a User object by it's DN or ownCloud username
-        * @param string the DN or username of the user
+        * @param string the username of the user
         * @return \OCA\user_ldap\lib\user\User|\OCA\user_ldap\lib\user\OfflineUser|null
         * @throws \Exception when connection could not be established
         */
index 5f55448fc09960dd75a483aada40ca3b6b457ae0..1d98ec4c0de4ef7e6c4f3e36343648482b684363 100644 (file)
@@ -142,8 +142,6 @@ class Test_User_Manager extends \PHPUnit_Framework_TestCase {
         $manager = new Manager($config, $filesys, $log, $avaMgr, $image);
         $manager->setLdapAccess($access);
         $user = $manager->get($inputDN);
-
-        $this->assertNull($user);
     }
 
     public function testGetByUidExisting() {
@@ -191,8 +189,6 @@ class Test_User_Manager extends \PHPUnit_Framework_TestCase {
         $manager = new Manager($config, $filesys, $log, $avaMgr, $image);
         $manager->setLdapAccess($access);
         $user = $manager->get($uid);
-
-        $this->assertNull($user);
     }
 
 }
index 6afa9d79e3e2a308d9d98ff3bbd933354c888363..0309bc6108e94a27a99955c34d7150f24f47765f 100644 (file)
@@ -396,21 +396,53 @@ class Test_User_Ldap_Direct extends \PHPUnit_Framework_TestCase {
                $this->prepareMockForUserExists($access);
 
                $access->expects($this->any())
-                          ->method('readAttribute')
-                          ->will($this->returnCallback(function($dn) {
-                                       if($dn === 'dnOfRoland,dc=test') {
-                                               return array();
-                                       }
-                                       return false;
-                          }));
+                       ->method('readAttribute')
+                       ->will($this->returnCallback(function($dn) {
+                               if($dn === 'dnOfRoland,dc=test') {
+                                       return array();
+                               }
+                               return false;
+                       }));
 
                //test for existing user
                $result = $backend->userExists('gunslinger');
                $this->assertTrue($result);
+       }
+
+       /**
+        * @expectedException \Exception
+        */
+       public function testUserExistsForDeleted() {
+               $access = $this->getAccessMock();
+               $backend = new UserLDAP($access);
+               $this->prepareMockForUserExists($access);
+
+               $access->expects($this->any())
+                       ->method('readAttribute')
+                       ->will($this->returnCallback(function($dn) {
+                               if($dn === 'dnOfRoland,dc=test') {
+                                       return array();
+                               }
+                               return false;
+                       }));
 
                //test for deleted user
                $result = $backend->userExists('formerUser');
-               $this->assertFalse($result);
+       }
+
+       public function testUserExistsForNeverExisting() {
+               $access = $this->getAccessMock();
+               $backend = new UserLDAP($access);
+               $this->prepareMockForUserExists($access);
+
+               $access->expects($this->any())
+                       ->method('readAttribute')
+                       ->will($this->returnCallback(function($dn) {
+                               if($dn === 'dnOfRoland,dc=test') {
+                                       return array();
+                               }
+                               return false;
+                       }));
 
                //test for never-existing user
                $result = $backend->userExists('mallory');
@@ -424,21 +456,55 @@ class Test_User_Ldap_Direct extends \PHPUnit_Framework_TestCase {
                \OC_User::useBackend($backend);
 
                $access->expects($this->any())
-                          ->method('readAttribute')
-                          ->will($this->returnCallback(function($dn) {
-                                       if($dn === 'dnOfRoland,dc=test') {
-                                               return array();
-                                       }
-                                       return false;
-                          }));
+                       ->method('readAttribute')
+                       ->will($this->returnCallback(function($dn) {
+                               if($dn === 'dnOfRoland,dc=test') {
+                                       return array();
+                               }
+                               return false;
+                       }));
 
                //test for existing user
                $result = \OCP\User::userExists('gunslinger');
                $this->assertTrue($result);
+       }
+
+       /**
+        * @expectedException \Exception
+        */
+       public function testUserExistsPublicAPIForDeleted() {
+               $access = $this->getAccessMock();
+               $backend = new UserLDAP($access);
+               $this->prepareMockForUserExists($access);
+               \OC_User::useBackend($backend);
+
+               $access->expects($this->any())
+                       ->method('readAttribute')
+                       ->will($this->returnCallback(function($dn) {
+                               if($dn === 'dnOfRoland,dc=test') {
+                                       return array();
+                               }
+                               return false;
+                       }));
 
                //test for deleted user
                $result = \OCP\User::userExists('formerUser');
-               $this->assertFalse($result);
+       }
+
+       public function testUserExistsPublicAPIForNeverExisting() {
+               $access = $this->getAccessMock();
+               $backend = new UserLDAP($access);
+               $this->prepareMockForUserExists($access);
+               \OC_User::useBackend($backend);
+
+               $access->expects($this->any())
+                       ->method('readAttribute')
+                       ->will($this->returnCallback(function($dn) {
+                               if($dn === 'dnOfRoland,dc=test') {
+                                       return array();
+                               }
+                               return false;
+                       }));
 
                //test for never-existing user
                $result = \OCP\User::userExists('mallory');
@@ -454,50 +520,100 @@ class Test_User_Ldap_Direct extends \PHPUnit_Framework_TestCase {
                $this->assertFalse($result);
        }
 
-       public function testGetHome() {
+       public function testGetHomeAbsolutePath() {
                $access = $this->getAccessMock();
                $backend = new UserLDAP($access);
                $this->prepareMockForUserExists($access);
 
                $access->connection->expects($this->any())
-                          ->method('__get')
-                          ->will($this->returnCallback(function($name) {
-                                       if($name === 'homeFolderNamingRule') {
-                                               return 'attr:testAttribute';
-                                       }
-                                       return null;
-                          }));
+                       ->method('__get')
+                       ->will($this->returnCallback(function($name) {
+                               if($name === 'homeFolderNamingRule') {
+                                       return 'attr:testAttribute';
+                               }
+                               return null;
+                       }));
 
                $access->expects($this->any())
-                          ->method('readAttribute')
-                          ->will($this->returnCallback(function($dn, $attr) {
-                                       switch ($dn) {
-                                               case 'dnOfRoland,dc=test':
-                                                       if($attr === 'testAttribute') {
-                                                               return array('/tmp/rolandshome/');
-                                                       }
-                                                       return array();
-                                                       break;
-                                               case 'dnOfLadyOfShadows,dc=test':
-                                                       if($attr === 'testAttribute') {
-                                                               return array('susannah/');
-                                                       }
-                                                       return array();
-                                                       break;
-                                               default:
-                                                       return false;
-                                  }
-                          }));
+                       ->method('readAttribute')
+                       ->will($this->returnCallback(function($dn, $attr) {
+                               switch ($dn) {
+                                       case 'dnOfRoland,dc=test':
+                                               if($attr === 'testAttribute') {
+                                                       return array('/tmp/rolandshome/');
+                                               }
+                                               return array();
+                                               break;
+                                       default:
+                                               return false;
+                               }
+                       }));
 
                //absolut path
                $result = $backend->getHome('gunslinger');
                $this->assertEquals('/tmp/rolandshome/', $result);
+       }
+
+       public function testGetHomeDatadirRelative() {
+               $access = $this->getAccessMock();
+               $backend = new UserLDAP($access);
+               $this->prepareMockForUserExists($access);
+
+               $access->connection->expects($this->any())
+                       ->method('__get')
+                       ->will($this->returnCallback(function($name) {
+                               if($name === 'homeFolderNamingRule') {
+                                       return 'attr:testAttribute';
+                               }
+                               return null;
+                       }));
 
+               $access->expects($this->any())
+                       ->method('readAttribute')
+                       ->will($this->returnCallback(function($dn, $attr) {
+                               switch ($dn) {
+                                       case 'dnOfLadyOfShadows,dc=test':
+                                               if($attr === 'testAttribute') {
+                                                       return array('susannah/');
+                                               }
+                                               return array();
+                                               break;
+                                       default:
+                                               return false;
+                               }
+                       }));
                //datadir-relativ path
                $result = $backend->getHome('ladyofshadows');
                $datadir = \OCP\Config::getSystemValue('datadirectory',
-                                                                                          \OC::$SERVERROOT.'/data');
+                       \OC::$SERVERROOT.'/data');
                $this->assertEquals($datadir.'/susannah/', $result);
+       }
+
+       /**
+        * @expectedException \Exception
+        */
+       public function testGetHomeNoPath() {
+               $access = $this->getAccessMock();
+               $backend = new UserLDAP($access);
+               $this->prepareMockForUserExists($access);
+
+               $access->connection->expects($this->any())
+                       ->method('__get')
+                       ->will($this->returnCallback(function($name) {
+                               if($name === 'homeFolderNamingRule') {
+                                       return 'attr:testAttribute';
+                               }
+                               return null;
+                       }));
+
+               $access->expects($this->any())
+                       ->method('readAttribute')
+                       ->will($this->returnCallback(function($dn, $attr) {
+                               switch ($dn) {
+                                       default:
+                                               return false;
+                               }
+                       }));
 
                //no path at all – triggers OC default behaviour
                $result = $backend->getHome('newyorker');
@@ -537,6 +653,12 @@ class Test_User_Ldap_Direct extends \PHPUnit_Framework_TestCase {
                $backend = new UserLDAP($access);
                $this->prepareMockForUserExists($access);
 
+               $access->connection->expects($this->any())
+                       ->method('getConnectionResource')
+                       ->will($this->returnCallback(function() {
+                               return true;
+                       }));
+
                //with displayName
                $result = $backend->getDisplayName('gunslinger');
                $this->assertEquals('Roland Deschain', $result);
@@ -548,9 +670,36 @@ class Test_User_Ldap_Direct extends \PHPUnit_Framework_TestCase {
 
        public function testGetDisplayNamePublicAPI() {
                $access = $this->getAccessMock();
+               $access->expects($this->any())
+                       ->method('username2dn')
+                       ->will($this->returnCallback(function($uid) {
+                               switch ($uid) {
+                                       case 'gunslinger':
+                                               return 'dnOfRoland,dc=test';
+                                               break;
+                                       case 'formerUser':
+                                               return 'dnOfFormerUser,dc=test';
+                                               break;
+                                       case 'newyorker':
+                                               return 'dnOfNewYorker,dc=test';
+                                               break;
+                                       case 'ladyofshadows':
+                                               return 'dnOfLadyOfShadows,dc=test';
+                                               break;
+                                       default:
+                                               return false;
+                               }
+                       }));
                $this->prepareAccessForGetDisplayName($access);
                $backend = new UserLDAP($access);
                $this->prepareMockForUserExists($access);
+
+               $access->connection->expects($this->any())
+                       ->method('getConnectionResource')
+                       ->will($this->returnCallback(function() {
+                               return true;
+                       }));
+
                \OC_User::useBackend($backend);
 
                //with displayName