diff options
Diffstat (limited to 'apps/user_ldap/tests')
-rw-r--r-- | apps/user_ldap/tests/AccessTest.php | 51 | ||||
-rw-r--r-- | apps/user_ldap/tests/Group_LDAPTest.php | 3 | ||||
-rw-r--r-- | apps/user_ldap/tests/Integration/AbstractIntegrationTest.php | 14 | ||||
-rw-r--r-- | apps/user_ldap/tests/Jobs/CleanUpTest.php | 2 | ||||
-rw-r--r-- | apps/user_ldap/tests/LDAPProviderTest.php | 337 | ||||
-rw-r--r-- | apps/user_ldap/tests/User/UserTest.php | 3 | ||||
-rw-r--r-- | apps/user_ldap/tests/User_LDAPTest.php | 4 | ||||
-rw-r--r-- | apps/user_ldap/tests/WizardTest.php | 3 |
8 files changed, 386 insertions, 31 deletions
diff --git a/apps/user_ldap/tests/AccessTest.php b/apps/user_ldap/tests/AccessTest.php index f96813ba711..2fddafa214b 100644 --- a/apps/user_ldap/tests/AccessTest.php +++ b/apps/user_ldap/tests/AccessTest.php @@ -60,21 +60,22 @@ class AccessTest extends \Test\TestCase { $this->getMock('\OCP\Image'), $this->getMock('\OCP\IDBConnection'), $this->getMock('\OCP\IUserManager'))); + $helper = new \OCA\User_LDAP\Helper(); - return array($lw, $connector, $um); + return array($lw, $connector, $um, $helper); } public function testEscapeFilterPartValidChars() { - list($lw, $con, $um) = $this->getConnectorAndLdapMock(); - $access = new Access($con, $lw, $um); + list($lw, $con, $um, $helper) = $this->getConnectorAndLdapMock(); + $access = new Access($con, $lw, $um, $helper); $input = 'okay'; $this->assertTrue($input === $access->escapeFilterPart($input)); } public function testEscapeFilterPartEscapeWildcard() { - list($lw, $con, $um) = $this->getConnectorAndLdapMock(); - $access = new Access($con, $lw, $um); + list($lw, $con, $um, $helper) = $this->getConnectorAndLdapMock(); + $access = new Access($con, $lw, $um, $helper); $input = '*'; $expected = '\\\\*'; @@ -82,8 +83,8 @@ class AccessTest extends \Test\TestCase { } public function testEscapeFilterPartEscapeWildcard2() { - list($lw, $con, $um) = $this->getConnectorAndLdapMock(); - $access = new Access($con, $lw, $um); + list($lw, $con, $um, $helper) = $this->getConnectorAndLdapMock(); + $access = new Access($con, $lw, $um, $helper); $input = 'foo*bar'; $expected = 'foo\\\\*bar'; @@ -92,8 +93,8 @@ class AccessTest extends \Test\TestCase { /** @dataProvider convertSID2StrSuccessData */ public function testConvertSID2StrSuccess(array $sidArray, $sidExpected) { - list($lw, $con, $um) = $this->getConnectorAndLdapMock(); - $access = new Access($con, $lw, $um); + list($lw, $con, $um, $helper) = $this->getConnectorAndLdapMock(); + $access = new Access($con, $lw, $um, $helper); $sidBinary = implode('', $sidArray); $this->assertSame($sidExpected, $access->convertSID2Str($sidBinary)); @@ -127,8 +128,8 @@ class AccessTest extends \Test\TestCase { } public function testConvertSID2StrInputError() { - list($lw, $con, $um) = $this->getConnectorAndLdapMock(); - $access = new Access($con, $lw, $um); + list($lw, $con, $um, $helper) = $this->getConnectorAndLdapMock(); + $access = new Access($con, $lw, $um, $helper); $sidIllegal = 'foobar'; $sidExpected = ''; @@ -137,8 +138,8 @@ class AccessTest extends \Test\TestCase { } public function testGetDomainDNFromDNSuccess() { - list($lw, $con, $um) = $this->getConnectorAndLdapMock(); - $access = new Access($con, $lw, $um); + list($lw, $con, $um, $helper) = $this->getConnectorAndLdapMock(); + $access = new Access($con, $lw, $um, $helper); $inputDN = 'uid=zaphod,cn=foobar,dc=my,dc=server,dc=com'; $domainDN = 'dc=my,dc=server,dc=com'; @@ -152,8 +153,8 @@ class AccessTest extends \Test\TestCase { } public function testGetDomainDNFromDNError() { - list($lw, $con, $um) = $this->getConnectorAndLdapMock(); - $access = new Access($con, $lw, $um); + list($lw, $con, $um, $helper) = $this->getConnectorAndLdapMock(); + $access = new Access($con, $lw, $um, $helper); $inputDN = 'foobar'; $expected = ''; @@ -187,8 +188,8 @@ class AccessTest extends \Test\TestCase { } public function testStringResemblesDN() { - list($lw, $con, $um) = $this->getConnectorAndLdapMock(); - $access = new Access($con, $lw, $um); + list($lw, $con, $um, $helper) = $this->getConnectorAndLdapMock(); + $access = new Access($con, $lw, $um, $helper); $cases = $this->getResemblesDNInputData(); @@ -208,9 +209,9 @@ class AccessTest extends \Test\TestCase { } public function testStringResemblesDNLDAPmod() { - list($lw, $con, $um) = $this->getConnectorAndLdapMock(); + list($lw, $con, $um, $helper) = $this->getConnectorAndLdapMock(); $lw = new \OCA\User_LDAP\LDAP(); - $access = new Access($con, $lw, $um); + $access = new Access($con, $lw, $um, $helper); if(!function_exists('ldap_explode_dn')) { $this->markTestSkipped('LDAP Module not available'); @@ -224,8 +225,8 @@ class AccessTest extends \Test\TestCase { } public function testCacheUserHome() { - list($lw, $con, $um) = $this->getConnectorAndLdapMock(); - $access = new Access($con, $lw, $um); + list($lw, $con, $um, $helper) = $this->getConnectorAndLdapMock(); + $access = new Access($con, $lw, $um, $helper); $con->expects($this->once()) ->method('writeToCache'); @@ -234,8 +235,8 @@ class AccessTest extends \Test\TestCase { } public function testBatchApplyUserAttributes() { - list($lw, $con, $um) = $this->getConnectorAndLdapMock(); - $access = new Access($con, $lw, $um); + list($lw, $con, $um, $helper) = $this->getConnectorAndLdapMock(); + $access = new Access($con, $lw, $um, $helper); $mapperMock = $this->getMockBuilder('\OCA\User_LDAP\Mapping\UserMapping') ->disableOriginalConstructor() ->getMock(); @@ -294,7 +295,7 @@ class AccessTest extends \Test\TestCase { * @dataProvider dNAttributeProvider */ public function testSanitizeDN($attribute) { - list($lw, $con, $um) = $this->getConnectorAndLdapMock(); + list($lw, $con, $um, $helper) = $this->getConnectorAndLdapMock(); $dnFromServer = 'cn=Mixed Cases,ou=Are Sufficient To,ou=Test,dc=example,dc=org'; @@ -309,7 +310,7 @@ class AccessTest extends \Test\TestCase { $attribute => array('count' => 1, $dnFromServer) ))); - $access = new Access($con, $lw, $um); + $access = new Access($con, $lw, $um, $helper); $values = $access->readAttribute('uid=whoever,dc=example,dc=org', $attribute); $this->assertSame($values[0], strtolower($dnFromServer)); } diff --git a/apps/user_ldap/tests/Group_LDAPTest.php b/apps/user_ldap/tests/Group_LDAPTest.php index 71120bdb838..83ec2dedf22 100644 --- a/apps/user_ldap/tests/Group_LDAPTest.php +++ b/apps/user_ldap/tests/Group_LDAPTest.php @@ -55,9 +55,10 @@ class Group_LDAPTest extends \Test\TestCase { $um = $this->getMockBuilder('\OCA\User_LDAP\User\Manager') ->disableOriginalConstructor() ->getMock(); + $helper = new \OCA\User_LDAP\Helper(); $access = $this->getMock('\OCA\User_LDAP\Access', $accMethods, - array($connector, $lw, $um)); + array($connector, $lw, $um, $helper)); $access->expects($this->any()) ->method('getConnection') diff --git a/apps/user_ldap/tests/Integration/AbstractIntegrationTest.php b/apps/user_ldap/tests/Integration/AbstractIntegrationTest.php index 4ec36617c14..bd56494eac0 100644 --- a/apps/user_ldap/tests/Integration/AbstractIntegrationTest.php +++ b/apps/user_ldap/tests/Integration/AbstractIntegrationTest.php @@ -26,6 +26,7 @@ namespace OCA\User_LDAP\Tests\Integration; use OCA\User_LDAP\Access; use OCA\User_LDAP\Connection; use OCA\User_LDAP\LDAP; +use OCA\User_LDAP\Helper; use OCA\User_LDAP\User\Manager; abstract class AbstractIntegrationTest { @@ -40,6 +41,9 @@ abstract class AbstractIntegrationTest { /** @var Manager */ protected $userManager; + + /** @var Helper */ + protected $helper; /** @var string */ protected $base; @@ -65,6 +69,7 @@ abstract class AbstractIntegrationTest { $this->initLDAPWrapper(); $this->initConnection(); $this->initUserManager(); + $this->initHelper(); $this->initAccess(); } @@ -103,12 +108,19 @@ abstract class AbstractIntegrationTest { protected function initUserManager() { $this->userManager = new FakeManager(); } + + /** + * initializes the test Helper + */ + protected function initHelper() { + $this->helper = new Helper(); + } /** * initializes the Access test instance */ protected function initAccess() { - $this->access = new Access($this->connection, $this->ldap, $this->userManager); + $this->access = new Access($this->connection, $this->ldap, $this->userManager, $this->helper); } /** diff --git a/apps/user_ldap/tests/Jobs/CleanUpTest.php b/apps/user_ldap/tests/Jobs/CleanUpTest.php index 45e7998da01..4785a227d26 100644 --- a/apps/user_ldap/tests/Jobs/CleanUpTest.php +++ b/apps/user_ldap/tests/Jobs/CleanUpTest.php @@ -37,7 +37,7 @@ class CleanUpTest extends \Test\TestCase { ->getMock(); $mocks['ocConfig'] = $this->getMock('\OCP\IConfig'); $mocks['db'] = $this->getMock('\OCP\IDBConnection'); - $mocks['helper'] = $this->getMock('\OCA\User_LDAP\Helper'); + $mocks['helper'] = new \OCA\User_LDAP\Helper(); return $mocks; } diff --git a/apps/user_ldap/tests/LDAPProviderTest.php b/apps/user_ldap/tests/LDAPProviderTest.php new file mode 100644 index 00000000000..071cc09fa63 --- /dev/null +++ b/apps/user_ldap/tests/LDAPProviderTest.php @@ -0,0 +1,337 @@ +<?php +/** + * @author Roger Szabo <roger.szabo@web.de> + * + * @copyright Copyright (c) 2016, 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\User_LDAP\Tests; + +use OCP\IServerContainer; +use OCA\User_LDAP\IUserLDAP; + +/** + * Class LDAPProviderTest + * + * @group DB + * + * @package OCA\User_LDAP\Tests + */ +class LDAPProviderTest extends \Test\TestCase { + + protected function setUp() { + parent::setUp(); + } + + private function getServerMock(IUserLDAP $backend) { + $server = $this->getMockBuilder('OC\Server') + ->setMethods(['getUserManager', 'getBackends']) + ->setConstructorArgs(['', new \OC\Config(\OC::$configDir)]) + ->getMock(); + $server->expects($this->at(1)) + ->method('getBackends') + ->willReturn([$backend]); + $server->expects($this->any()) + ->method($this->anything()) + ->willReturnSelf(); + + return $server; + } + + private function getLDAPProvider(IServerContainer $serverContainer) { + $factory = new \OCA\User_LDAP\LDAPProviderFactory($serverContainer); + return $factory->getLDAPProvider(); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage User id not found in LDAP + */ + public function testGetUserDNUserIDNotFound() { + $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') + ->setMethods(['userExists']) + ->disableOriginalConstructor() + ->getMock(); + $backend->expects($this->any())->method('userExists')->willReturn(false); + + $server = $this->getServerMock($backend); + + $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider->getUserDN('nonexisting_user'); + } + + public function testGetUserDN() { + $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') + ->setMethods(['userExists', 'getLDAPAccess', 'username2dn']) + ->disableOriginalConstructor() + ->getMock(); + $backend->expects($this->at(0)) + ->method('userExists') + ->willReturn(true); + $backend->expects($this->at(2)) + ->method('username2dn') + ->willReturn('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org'); + $backend->expects($this->any()) + ->method($this->anything()) + ->willReturnSelf(); + + $server = $this->getServerMock($backend); + + $ldapProvider = $this->getLDAPProvider($server); + $this->assertEquals('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org', + $ldapProvider->getUserDN('existing_user')); + } + + public function testGetUserName() { + $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') + ->setMethods(['dn2UserName']) + ->disableOriginalConstructor() + ->getMock(); + $backend->expects($this->any()) + ->method('dn2UserName') + ->willReturn('existing_user'); + + $server = $this->getServerMock($backend); + + $ldapProvider = $this->getLDAPProvider($server); + $this->assertEquals('existing_user', + $ldapProvider->getUserName('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org')); + } + + public function testDNasBaseParameter() { + $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') + ->setMethods([]) + ->disableOriginalConstructor() + ->getMock(); + + $server = $this->getServerMock($backend); + + $helper = new \OCA\User_LDAP\Helper(); + + $ldapProvider = $this->getLDAPProvider($server); + $this->assertEquals( + $helper->DNasBaseParameter('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org'), + $ldapProvider->DNasBaseParameter('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org')); + } + + public function testSanitizeDN() { + $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') + ->setMethods([]) + ->disableOriginalConstructor() + ->getMock(); + + $server = $this->getServerMock($backend); + + $helper = new \OCA\User_LDAP\Helper(); + + $ldapProvider = $this->getLDAPProvider($server); + $this->assertEquals( + $helper->sanitizeDN('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org'), + $ldapProvider->sanitizeDN('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org')); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage User id not found in LDAP + */ + public function testGetLDAPConnectionUserIDNotFound() { + $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') + ->setMethods(['userExists']) + ->disableOriginalConstructor() + ->getMock(); + $backend->expects($this->any())->method('userExists')->willReturn(false); + + $server = $this->getServerMock($backend); + + $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider->getLDAPConnection('nonexisting_user'); + } + + public function testGetLDAPConnection() { + $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') + ->setMethods(['userExists', 'getNewLDAPConnection']) + ->disableOriginalConstructor() + ->getMock(); + $backend->expects($this->any()) + ->method('userExists') + ->willReturn(true); + $backend->expects($this->any()) + ->method('getNewLDAPConnection') + ->willReturn(true); + + $server = $this->getServerMock($backend); + + $ldapProvider = $this->getLDAPProvider($server); + $this->assertTrue($ldapProvider->getLDAPConnection('existing_user')); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage User id not found in LDAP + */ + public function testGetLDAPBaseUsersUserIDNotFound() { + $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') + ->setMethods(['userExists']) + ->disableOriginalConstructor() + ->getMock(); + $backend->expects($this->any())->method('userExists')->willReturn(false); + + $server = $this->getServerMock($backend); + + $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider->getLDAPBaseUsers('nonexisting_user'); + } + + public function testGetLDAPBaseUsers() { + $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') + ->setMethods(['userExists', 'getLDAPAccess', 'getConnection', 'getConfiguration']) + ->disableOriginalConstructor() + ->getMock(); + $backend->expects($this->at(0)) + ->method('userExists') + ->willReturn(true); + $backend->expects($this->at(3)) + ->method('getConfiguration') + ->willReturn(array('ldap_base_users'=>'ou=users,dc=example,dc=org')); + $backend->expects($this->any()) + ->method($this->anything()) + ->willReturnSelf(); + + $server = $this->getServerMock($backend); + + $ldapProvider = $this->getLDAPProvider($server); + $this->assertEquals('ou=users,dc=example,dc=org', $ldapProvider->getLDAPBaseUsers('existing_user')); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage User id not found in LDAP + */ + public function testGetLDAPBaseGroupsUserIDNotFound() { + $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') + ->setMethods(['userExists']) + ->disableOriginalConstructor() + ->getMock(); + $backend->expects($this->any())->method('userExists')->willReturn(false); + + $server = $this->getServerMock($backend); + + $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider->getLDAPBaseGroups('nonexisting_user'); + } + + public function testGetLDAPBaseGroups() { + $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') + ->setMethods(['userExists', 'getLDAPAccess', 'getConnection', 'getConfiguration']) + ->disableOriginalConstructor() + ->getMock(); + $backend->expects($this->at(0)) + ->method('userExists') + ->willReturn(true); + $backend->expects($this->at(3)) + ->method('getConfiguration') + ->willReturn(array('ldap_base_groups'=>'ou=groups,dc=example,dc=org')); + $backend->expects($this->any()) + ->method($this->anything()) + ->willReturnSelf(); + + $server = $this->getServerMock($backend); + + $ldapProvider = $this->getLDAPProvider($server); + $this->assertEquals('ou=groups,dc=example,dc=org', $ldapProvider->getLDAPBaseGroups('existing_user')); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage User id not found in LDAP + */ + public function testClearCacheUserIDNotFound() { + $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') + ->setMethods(['userExists']) + ->disableOriginalConstructor() + ->getMock(); + $backend->expects($this->any())->method('userExists')->willReturn(false); + + $server = $this->getServerMock($backend); + + $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider->clearCache('nonexisting_user'); + } + + public function testClearCache() { + $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') + ->setMethods(['userExists', 'getLDAPAccess', 'getConnection', 'clearCache']) + ->disableOriginalConstructor() + ->getMock(); + $backend->expects($this->at(0)) + ->method('userExists') + ->willReturn(true); + $backend->expects($this->at(3)) + ->method('clearCache') + ->willReturn(true); + $backend->expects($this->any()) + ->method($this->anything()) + ->willReturnSelf(); + + $server = $this->getServerMock($backend); + + $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider->clearCache('existing_user'); + $this->assertTrue(TRUE); + } + + public function testDnExists() { + $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') + ->setMethods(['dn2UserName']) + ->disableOriginalConstructor() + ->getMock(); + $backend->expects($this->any()) + ->method('dn2UserName') + ->willReturn('existing_user'); + + $server = $this->getServerMock($backend); + + $ldapProvider = $this->getLDAPProvider($server); + $this->assertTrue($ldapProvider->dnExists('cn=existing_user,ou=Are Sufficient To,ou=Test,dc=example,dc=org')); + } + + public function testFlagRecord() { + $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') + ->setMethods([]) + ->disableOriginalConstructor() + ->getMock(); + + $server = $this->getServerMock($backend); + + $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider->flagRecord('existing_user'); + $this->assertTrue(TRUE); + } + + public function testUnflagRecord() { + $backend = $this->getMockBuilder('OCA\User_LDAP\User_LDAP') + ->setMethods([]) + ->disableOriginalConstructor() + ->getMock(); + + $server = $this->getServerMock($backend); + + $ldapProvider = $this->getLDAPProvider($server); + $ldapProvider->unflagRecord('existing_user'); + $this->assertTrue(TRUE); + } +} diff --git a/apps/user_ldap/tests/User/UserTest.php b/apps/user_ldap/tests/User/UserTest.php index cf1514009d8..d9e43dee047 100644 --- a/apps/user_ldap/tests/User/UserTest.php +++ b/apps/user_ldap/tests/User/UserTest.php @@ -71,8 +71,9 @@ class UserTest extends \Test\TestCase { $umMethods, array($cfMock, $fsMock, $logMock, $avaMgr, $im, $dbc, $userMgr)); $connector = $this->getMock('\OCA\User_LDAP\Connection', $conMethods, array($lw, null, null)); + $helper = new \OCA\User_LDAP\Helper(); $access = $this->getMock('\OCA\User_LDAP\Access', - $accMethods, array($connector, $lw, $um)); + $accMethods, array($connector, $lw, $um, $helper)); return array($access, $connector); } diff --git a/apps/user_ldap/tests/User_LDAPTest.php b/apps/user_ldap/tests/User_LDAPTest.php index ae86e3cf3a2..47e789142e5 100644 --- a/apps/user_ldap/tests/User_LDAPTest.php +++ b/apps/user_ldap/tests/User_LDAPTest.php @@ -93,9 +93,11 @@ class User_LDAPTest extends \Test\TestCase { ->method('getDeletedUser') ->will($this->returnValue($offlineUser)); + $helper = new \OCA\User_LDAP\Helper(); + $access = $this->getMock('\OCA\User_LDAP\Access', $accMethods, - array($connector, $lw, $um)); + array($connector, $lw, $um, $helper)); $um->setLdapAccess($access); diff --git a/apps/user_ldap/tests/WizardTest.php b/apps/user_ldap/tests/WizardTest.php index cc110c6ee0d..e82cbcfc82a 100644 --- a/apps/user_ldap/tests/WizardTest.php +++ b/apps/user_ldap/tests/WizardTest.php @@ -69,8 +69,9 @@ class WizardTest extends \Test\TestCase { $um = $this->getMockBuilder('\OCA\User_LDAP\User\Manager') ->disableOriginalConstructor() ->getMock(); + $helper = new \OCA\User_LDAP\Helper(); $access = $this->getMock('\OCA\User_LDAP\Access', - $accMethods, array($connector, $lw, $um)); + $accMethods, array($connector, $lw, $um, $helper)); return array(new Wizard($conf, $lw, $access), $conf, $lw, $access); } |