From: Gunnar Wrobel Date: Wed, 4 Nov 2009 14:41:17 +0000 (+0100) Subject: Added testing for the Kolab auth driver. X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=17e82bea3c1755bf3905ab1888c1ebd982baf09d;p=horde.git Added testing for the Kolab auth driver. --- diff --git a/framework/Auth/lib/Horde/Auth/Kolab.php b/framework/Auth/lib/Horde/Auth/Kolab.php index 66b430c28..a2280f9fb 100644 --- a/framework/Auth/lib/Horde/Auth/Kolab.php +++ b/framework/Auth/lib/Horde/Auth/Kolab.php @@ -17,6 +17,20 @@ class Horde_Auth_Kolab extends Horde_Auth_Base { /** + * The session handler. + * + * @var Horde_Kolab_Session + */ + private $_session; + + /** + * The session factory. + * + * @var Horde_Kolab_Session_Factory + */ + private $_factory; + + /** * An array of capabilities, so that the driver can report which * operations it supports and which it doesn't. * @@ -32,6 +46,52 @@ class Horde_Auth_Kolab extends Horde_Auth_Base ); /** + * Set the session handler. + * + * @param Horde_Kolab_Session $session The session handler. + * + * @return NULL + */ + public function setSession(Horde_Kolab_Session $session) + { + $this->_session = $session; + } + + /** + * Set the session factory. + * + * @param Horde_Kolab_Session_Factory $factory The session factory. + * + * @return NULL + */ + public function setSessionFactory(Horde_Kolab_Session_Factory $factory) + { + $this->_factory = $factory; + } + + /** + * Retrieve a connected kolab session. + * + * @return Horde_Kolab_Session The connected session. + * + * @throws Horde_Kolab_Session_Exception + */ + public function getSession($userId = null, $credentials = null) + { + if (!isset($this->_session)) { + if (!isset($this->_factory)) { + $this->_session = Horde_Kolab_Session_Singleton::singleton( + $userId, $credentials + ); + } else { + $this->_session = $this->_factory->getSession($userId); + $this->_session->connect($credentials); + } + } + return $this->_session; + } + + /** * Find out if a set of login credentials are valid. * * For Kolab this requires to identify the IMAP server the user should @@ -48,12 +108,10 @@ class Horde_Auth_Kolab extends Horde_Auth_Base protected function _authenticate($userId, $credentials) { try { - $session = Horde_Kolab_Session_Singleton::singleton( - $userId, $credentials - ); + $session = $this->getSession($userId, $credentials); } catch (Horde_Kolab_Session_Exception_Badlogin $e) { throw new Horde_Auth_Exception('', Horde_Auth::REASON_BADLOGIN); - } catch (Exception $e) { + } catch (Horde_Kolab_Session_Exception $e) { Horde::logMessage($e, __FILE__, __LINE__, PEAR_LOG_ERR); throw new Horde_Auth_Exception('', Horde_Auth::REASON_FAILED); } diff --git a/framework/Auth/test/Horde/Auth/Kolab/Autoload.php b/framework/Auth/test/Horde/Auth/Kolab/Autoload.php index fe20b625f..28b9fbd81 100644 --- a/framework/Auth/test/Horde/Auth/Kolab/Autoload.php +++ b/framework/Auth/test/Horde/Auth/Kolab/Autoload.php @@ -4,46 +4,25 @@ * * PHP version 5 * - * @category Kolab - * @package Kolab_Server + * @category Horde + * @package Auth * @author Gunnar Wrobel * @license http://www.fsf.org/copyleft/lgpl.html LGPL - * @link http://pear.horde.org/index.php?package=Kolab_Server + * @link http://pear.horde.org/index.php?package=Auth */ -/** - * The Autoloader allows us to omit "require/include" statements. - */ -require_once 'Horde/Autoloader.php'; - -if (!defined('HORE_KOLAB_SERVER_TESTS')) { - $test_dir = '@test_dir@/Kolab_Server'; - - if (substr($test_dir, 0, 1) == '@') { - /** - * Assume we are working in development mode and this package resides in - * 'framework'. - */ - define('HORE_KOLAB_SERVER_TESTS', dirname(__FILE__) . '/../../../../../Kolab_Server/test'); - } else { - define('HORE_KOLAB_SERVER_TESTS', $test_dir); - } - - Horde_Autoloader::addClassPath(HORE_KOLAB_SERVER_TESTS); +if (!spl_autoload_functions()) { + spl_autoload_register( + create_function( + '$class', + '$filename = str_replace(array(\'::\', \'_\'), \'/\', $class);' + . '$err_mask = E_ALL ^ E_WARNING;' + . '$oldErrorReporting = error_reporting($err_mask);' + . 'include "$filename.php";' + . 'error_reporting($oldErrorReporting);' + ) + ); } -if (!defined('HORE_KOLAB_AUTH_TESTS')) { - $test_dir = '@test_dir@/Auth'; - - if (substr($test_dir, 0, 1) == '@') { - /** - * Assume we are working in development mode and this package resides in - * 'framework'. - */ - define('HORE_KOLAB_AUTH_TESTS', dirname(__FILE__) . '/../../..'); - } else { - define('HORE_KOLAB_AUTH_TESTS', $test_dir); - } - - Horde_Autoloader::addClassPath(HORE_KOLAB_AUTH_TESTS); -} \ No newline at end of file +/** Catch strict standards */ +error_reporting(E_ALL | E_STRICT); diff --git a/framework/Auth/test/Horde/Auth/Kolab/Class/KolabTest.php b/framework/Auth/test/Horde/Auth/Kolab/Class/KolabTest.php new file mode 100644 index 000000000..8e1e20f6c --- /dev/null +++ b/framework/Auth/test/Horde/Auth/Kolab/Class/KolabTest.php @@ -0,0 +1,144 @@ + + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @link http://pear.horde.org/index.php?package=Kolab_Session + */ + +/** + * Prepare the test setup. + */ +require_once dirname(__FILE__) . '/../Autoload.php'; + +/** + * Test the Kolab auth handler. + * + * Copyright 2009 The Horde Project (http://www.horde.org/) + * + * See the enclosed file COPYING for license information (LGPL). If you + * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. + * + * @category Kolab + * @package Kolab_Session + * @author Gunnar Wrobel + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @link http://pear.horde.org/index.php?package=Kolab_Session + */ +class Horde_Auth_Kolab_Class_KolabTest extends PHPUnit_Framework_TestCase +{ + public function setUp() + { + @include_once 'Log.php'; + if (!defined('PEAR_LOG_DEBUG')) { + $this->markTestSkipped('The PEAR_LOG_DEBUG constant is not available!'); + } + + $this->session = $this->getMock('Horde_Kolab_Session'); + $this->factory = $this->getMock('Horde_Kolab_Session_Factory'); + } + + public function testMethodSetsessionHasParameterSession() + { + $auth = new Horde_Auth_Kolab(); + $auth->setSession($this->session); + } + + public function testMethodGetsessionHasResultSession() + { + $auth = new Horde_Auth_Kolab(); + $auth->setSession($this->session); + $this->assertType( + 'Horde_Kolab_Session', + $auth->getSession('user', array('password' => 'test')) + ); + } + + public function testMethodGetsessionHasResultSessionFromTheFactoryIfTheSessionWasUnset() + { + $auth = new Horde_Auth_Kolab(); + $auth->setSessionFactory($this->factory); + $this->factory->expects($this->once()) + ->method('getSession') + ->with('user') + ->will($this->returnValue($this->session)); + $this->session->expects($this->once()) + ->method('connect') + ->with(array('password' => 'test')); + $this->assertType( + 'Horde_Kolab_Session', + $auth->getSession('user', array('password' => 'test')) + ); + } + + public function testMethodAuthenticateHasResultBooleanTrueIfTheConnectionWasSuccessful() + { + $auth = new Horde_Auth_Kolab(); + $auth->setSessionFactory($this->factory); + $this->factory->expects($this->once()) + ->method('getSession') + ->with('user') + ->will($this->returnValue($this->session)); + $this->session->expects($this->once()) + ->method('connect') + ->with(array('password' => 'test')); + $this->assertTrue( + $auth->authenticate('user', array('password' => 'test'), false) + ); + } + + public function testMethodAuthenticateHasPostconditionThatTheUserIdIsBeingRewrittenIfRequired() + { + $auth = new Horde_Auth_Kolab(); + $auth->setSessionFactory($this->factory); + $this->factory->expects($this->once()) + ->method('getSession') + ->with('user') + ->will($this->returnValue($this->session)); + $this->session->expects($this->once()) + ->method('connect') + ->with(array('password' => 'test')); + $this->session->expects($this->once()) + ->method('getMail') + ->will($this->returnValue('mail@example.org')); + /* Untestable with the way the Auth driver is currently structured */ + $auth->authenticate('user', array('password' => 'test'), false); + } + + public function testMethodAuthenticateThrowsExceptionIfTheLoginFailed() + { + $auth = new Horde_Auth_Kolab(); + $auth->setSessionFactory($this->factory); + $this->factory->expects($this->once()) + ->method('getSession') + ->with('user') + ->will($this->returnValue($this->session)); + $this->session->expects($this->once()) + ->method('connect') + ->with(array('password' => 'test')) + ->will($this->throwException(new Horde_Kolab_Session_Exception('Error'))); + $auth->authenticate('user', array('password' => 'test'), false); + $this->assertEquals(Horde_Auth::REASON_FAILED, Horde_Auth::getAuthError()); + } + + public function testMethodAuthenticateThrowsExceptionIfTheCredentialsWereInvalid() + { + $auth = new Horde_Auth_Kolab(); + $auth->setSessionFactory($this->factory); + $this->factory->expects($this->once()) + ->method('getSession') + ->with('user') + ->will($this->returnValue($this->session)); + $this->session->expects($this->once()) + ->method('connect') + ->with(array('password' => 'test')) + ->will($this->throwException(new Horde_Kolab_Session_Exception_Badlogin('Error'))); + $auth->authenticate('user', array('password' => 'test'), false); + $this->assertEquals(Horde_Auth::REASON_BADLOGIN, Horde_Auth::getAuthError()); + } +} \ No newline at end of file diff --git a/framework/Auth/test/Horde/Auth/Kolab/Integration/AuthTest.php b/framework/Auth/test/Horde/Auth/Kolab/Integration/AuthTest.php new file mode 100644 index 000000000..c536679b7 --- /dev/null +++ b/framework/Auth/test/Horde/Auth/Kolab/Integration/AuthTest.php @@ -0,0 +1,152 @@ + + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @link http://pear.horde.org/index.php?package=Auth + */ + +/** + * Prepare the test setup. + */ +require_once dirname(__FILE__) . '/../Autoload.php'; + +/** + * Kolab authentication tests. + * + * Copyright 2008-2009 The Horde Project (http://www.horde.org/) + * + * See the enclosed file COPYING for license information (LGPL). If you + * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. + * + * @category Horde + * @package Auth + * @subpackage UnitTests + * @author Gunnar Wrobel + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @link http://pear.horde.org/index.php?package=Auth + */ +class Horde_Auth_Kolab_Integration_AuthTest extends PHPUnit_Framework_TestCase +{ + public function setUp() + { + @include_once 'Log.php'; + if (!defined('PEAR_LOG_DEBUG')) { + $this->markTestSkipped('The PEAR_LOG_DEBUG constant is not available!'); + } + + /** Provide the mock configuration for the server */ + $config = array(); + $config['ldap']['basedn'] = 'dc=test'; + $config['ldap']['mock'] = true; + $config['ldap']['data'] = array( + 'dn=user,dc=test' => array( + 'dn' => 'dn=user,dc=test', + 'data' => array( + 'uid' => array('user'), + 'mail' => array('user@example.org'), + 'userPassword' => array('pass'), + 'objectClass' => array('top', 'kolabInetOrgPerson'), + ) + ) + ); + $this->factory = new Horde_Kolab_Session_Factory_Configuration($config); + } + + public function testKolabLoginViaUid() + { + $auth = new Horde_Auth_Kolab(); + $auth->setSessionFactory($this->factory); + $this->assertTrue( + $auth->authenticate('user', array('password' => 'pass'), false) + ); + } + + public function testKolabLoginViaMail() + { + $auth = new Horde_Auth_Kolab(); + $auth->setSessionFactory($this->factory); + $this->assertTrue( + $auth->authenticate( + 'user@example.org', array('password' => 'pass'), false + ) + ); + } + + public function testKolabLoginFailureViaUid() + { + $auth = new Horde_Auth_Kolab(); + $auth->setSessionFactory($this->factory); + $auth->authenticate('user', array('password' => 'invalid'), false); + $this->assertEquals( + Horde_Auth::REASON_BADLOGIN, + Horde_Auth::getAuthError() + ); + } + + public function testKolabLoginFailureViaMail() + { + $auth = new Horde_Auth_Kolab(); + $auth->setSessionFactory($this->factory); + $auth->authenticate( + 'user@example.org', array('password' => 'invalid'), false + ); + $this->assertEquals( + Horde_Auth::REASON_BADLOGIN, + Horde_Auth::getAuthError() + ); + } + + public function testKolabLoginIdRewrite() + { + $auth = new Horde_Auth_Kolab_Dummy(); + $auth->setSessionFactory($this->factory); + $this->assertTrue( + $auth->authenticate('user', array('password' => 'pass'), false) + ); + $this->assertEquals('user@example.org', $auth->getId()); + } + + /** + * !!! This test has side effects !!! + * + * The Horde_Kolab_Session_Singleton will be set. + */ + public function testKolabLoginViaSessionSingleton() + { + global $conf; + + $conf['kolab']['ldap']['basedn'] = 'dc=test'; + $conf['kolab']['ldap']['mock'] = true; + $conf['kolab']['ldap']['data'] = array( + 'dn=user,dc=test' => array( + 'dn' => 'dn=user,dc=test', + 'data' => array( + 'uid' => array('user'), + 'mail' => array('user@example.org'), + 'userPassword' => array('pass'), + 'objectClass' => array('top', 'kolabInetOrgPerson'), + ) + ) + ); + + $auth = new Horde_Auth_Kolab(); + $this->assertTrue( + $auth->authenticate('user', array('password' => 'pass'), false) + ); + } +} + +class Horde_Auth_Kolab_Dummy extends Horde_Auth_Kolab +{ + public function getId() + { + return $this->_credentials['userId']; + } +} \ No newline at end of file diff --git a/framework/Auth/test/Horde/Auth/Kolab/KolabScenarioTest.php b/framework/Auth/test/Horde/Auth/Kolab/KolabScenarioTest.php deleted file mode 100644 index 56d334af6..000000000 --- a/framework/Auth/test/Horde/Auth/Kolab/KolabScenarioTest.php +++ /dev/null @@ -1,58 +0,0 @@ - - * @license http://www.fsf.org/copyleft/lgpl.html LGPL - * @link http://pear.horde.org/index.php?package=Auth - */ - -/** - * Prepare the test setup. - */ -require_once 'Autoload.php'; - -/** - * Kolab authentication scenarios. - * - * $Horde: framework/Auth/tests/Horde/Auth/KolabScenarioTest.php,v 1.3 2009/03/20 23:38:13 wrobel Exp $ - * - * Copyright 2008-2009 The Horde Project (http://www.horde.org/) - * - * See the enclosed file COPYING for license information (LGPL). If you - * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. - * - * @category Kolab - * @package Kolab_Server - * @author Gunnar Wrobel - * @license http://www.fsf.org/copyleft/lgpl.html LGPL - * @link http://pear.horde.org/index.php?package=Kolab_Server - */ - -class Horde_Auth_Kolab_KolabScenarioTest extends Horde_Kolab_Server_Integration_Scenario -{ - /** - * Test loggin in after a user has been added. - * - * @scenario - * - * @return NULL - */ - public function login() - { - $test_user = $this->provideBasicUserOne(); - - $this->given('a populated Kolab setup') - ->and('the Kolab auth driver has been selected') - ->when('logging in as a user with a password', $test_user['mail'], - $test_user['userPassword']) - ->then('the login was successful'); - } -} \ No newline at end of file diff --git a/framework/Auth/test/Horde/Auth/Kolab/KolabTest.php b/framework/Auth/test/Horde/Auth/Kolab/KolabTest.php deleted file mode 100644 index e5c3a72c2..000000000 --- a/framework/Auth/test/Horde/Auth/Kolab/KolabTest.php +++ /dev/null @@ -1,92 +0,0 @@ - - * @license http://www.fsf.org/copyleft/lgpl.html LGPL - * @link http://pear.horde.org/index.php?package=Auth - */ - -/** - * Prepare the test setup. - */ -require_once 'Autoload.php'; - -/** - * Kolab authentication tests. - * - * $Horde: framework/Auth/tests/Horde/Auth/KolabTest.php,v 1.4 2009/04/01 07:59:47 wrobel Exp $ - * - * Copyright 2008-2009 The Horde Project (http://www.horde.org/) - * - * See the enclosed file COPYING for license information (LGPL). If you - * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. - * - * @category Kolab - * @package Kolab_Server - * @author Gunnar Wrobel - * @license http://www.fsf.org/copyleft/lgpl.html LGPL - * @link http://pear.horde.org/index.php?package=Kolab_Server - */ -class Horde_Auth_Kolab_KolabTest extends Horde_Kolab_Server_Integration_Scenario -{ - /** - * Test loggin in after a user has been added. - * - * @return NULL - */ - public function testLogin() - { - /** Create the test base */ - $world = &$this->prepareBasicSetup(); - $server = $world['server']; - $auth = $world['auth']; - - /** Ensure we always use the test server */ - $GLOBALS['conf']['kolab']['server']['driver'] = 'test'; - - $uid = $server->uidForIdOrMail('wrobel@example.org'); - $this->assertEquals('cn=Gunnar Wrobel,dc=example,dc=org', $uid); - - $result = $auth->authenticate('wrobel@example.org', - array('password' => 'none')); - $this->assertNoError($result); - $this->assertTrue($result); - - $session = Horde_Kolab_Session::singleton(); - $this->assertNoError($session->user_mail); - $this->assertEquals('wrobel@example.org', $session->user_mail); - - $result = $auth->authenticate('wrobel@example.org', - array('password' => 'invalid')); - $this->assertFalse($result); - - /** Ensure we don't use a connection from older tests */ - $server->unbind(); - - $result = $auth->authenticate('wrobel', - array('password' => 'invalid')); - $this->assertNoError($result); - $this->assertFalse($result); - - /** Ensure we don't use a connection from older tests */ - $server->unbind(); - $result = $auth->authenticate('wrobel', - array('password' => 'none')); - $this->assertNoError($result); - $this->assertTrue($result); - - $session = Horde_Kolab_Session::singleton(); - $this->assertNoError($session->user_mail); - $this->assertEquals('wrobel@example.org', $session->user_mail); - - $this->assertEquals('wrobel@example.org', Horde_Auth::getAuth()); - } -} \ No newline at end of file diff --git a/framework/Auth/test/Horde/Auth/Kolab/Scenario.php b/framework/Auth/test/Horde/Auth/Kolab/Scenario.php deleted file mode 100644 index a5be24496..000000000 --- a/framework/Auth/test/Horde/Auth/Kolab/Scenario.php +++ /dev/null @@ -1,121 +0,0 @@ - - * @license http://www.fsf.org/copyleft/lgpl.html LGPL - * @link http://pear.horde.org/index.php?package=Auth - */ - -/** - * The Autoloader allows us to omit "require/include" statements. - */ -require_once 'Horde/Autoloader.php'; - -/** - * Supports scenario based testing of the Kolab auth driver. - * - * Copyright 2009 The Horde Project (http://www.horde.org/) - * - * See the enclosed file COPYING for license information (LGPL). If you - * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. - * - - * @category Horde - * @package Auth - * @subpackage UnitTests - * @author Gunnar Wrobel - * @license http://www.fsf.org/copyleft/lgpl.html LGPL - * @link http://pear.horde.org/index.php?package=Auth - */ -class Horde_Auth_Kolab_Scenario extends Horde_Kolab_Server_Integration_Scenario -{ - /** - * Handle a "given" step. - * - * @param array &$world Joined "world" of variables. - * @param string $action The description of the step. - * @param array $arguments Additional arguments to the step. - * - * @return mixed The outcome of the step. - */ - public function runGiven(&$world, $action, $arguments) - { - switch($action) { - case 'a provider': - $world['provider'] = new Horde_Provider_Base(); - break; - case 'a registered element': - $world['provider']->{$arguments[0]} = $arguments[1]; - break; - default: - return $this->notImplemented($action); - } - } - - /** - * Handle a "when" step. - * - * @param array &$world Joined "world" of variables. - * @param string $action The description of the step. - * @param array $arguments Additional arguments to the step. - * - * @return mixed The outcome of the step. - */ - public function runWhen(&$world, $action, $arguments) - { - switch($action) { - case 'retrieving the element': - try { - $world['result'] = $world['provider']->{$arguments[0]}; - } catch (Exception $e) { - $world['result'] = $e; - } - break; - case 'deleting the element': - try { - unset($world['provider']->{$arguments[0]}); - } catch (Exception $e) { - $world['result'] = $e; - } - break; - default: - return $this->notImplemented($action); - } - } - - /** - * Handle a "then" step. - * - * @param array &$world Joined "world" of variables. - * @param string $action The description of the step. - * @param array $arguments Additional arguments to the step. - * - * @return mixed The outcome of the step. - */ - public function runThen(&$world, $action, $arguments) - { - switch($action) { - case 'the result is': - $this->assertEquals($arguments[0], $world['result']); - break; - case 'the result is an error': - $this->assertTrue($world['result'] instanceOf Exception); - break; - case 'the result is an error with the message': - $this->assertTrue($world['result'] instanceOf Exception); - $this->assertEquals($arguments[0], $world['result']->getMessage()); - break; - case 'the element exists': - $this->assertTrue(isset($world['provider']->{$arguments[0]})); - break; - default: - return $this->notImplemented($action); - } - } -} diff --git a/framework/Auth/test/Horde/Auth/phpunit.xml b/framework/Auth/test/Horde/Auth/phpunit.xml new file mode 100644 index 000000000..502d3c9b8 --- /dev/null +++ b/framework/Auth/test/Horde/Auth/phpunit.xml @@ -0,0 +1,8 @@ + + + + + ../../../lib + + +