From 446da85a881d4acb8cf73ce713892cf7456f5b23 Mon Sep 17 00:00:00 2001 From: Marius Hein Date: Fri, 30 Aug 2013 15:31:21 +0200 Subject: [PATCH] UserBackend: Fix interface in test refs #4593 --- .../Authentication/Backend/DbUserBackend.php | 28 ++---------------- .../Backend/LdapUserBackend.php | 6 ++++ .../Authentication/DbUserBackendTest.php | 29 ++++++++++++++++++- .../Authentication/ErrorProneBackendMock.php | 13 +++++++++ 4 files changed, 49 insertions(+), 27 deletions(-) diff --git a/library/Icinga/Authentication/Backend/DbUserBackend.php b/library/Icinga/Authentication/Backend/DbUserBackend.php index 8f506010a..39382ee63 100644 --- a/library/Icinga/Authentication/Backend/DbUserBackend.php +++ b/library/Icinga/Authentication/Backend/DbUserBackend.php @@ -131,7 +131,6 @@ class DbUserBackend implements UserBackend return $this->name; } - /** * Check if the user identified by the given credentials is available * @@ -154,8 +153,6 @@ class DbUserBackend implements UserBackend */ public function authenticate(Credential $credential) { - $this->assertDbConnection(); - try { $salt = $this->getUserSalt($credential->getUsername()); } catch (Exception $e) { @@ -196,8 +193,6 @@ class DbUserBackend implements UserBackend */ private function getUserSalt($username) { - $this->assertDbConnection(); - $res = $this->db->select() ->from($this->userTable, self::SALT_COLUMN) ->where(self::USER_NAME_COLUMN.' = ?', $username) @@ -218,9 +213,6 @@ class DbUserBackend implements UserBackend */ private function getUserByName($username) { - - $this->assertDbConnection(); - $this->db->getConnection(); $res = $this->db-> select()->from($this->userTable) @@ -249,32 +241,16 @@ class DbUserBackend implements UserBackend return $usr; } - /** - * Assert a valid database connection - * - * @throws ConfigurationError - */ - private function assertDbConnection() - { - if ($this->db === null) { - $msg = 'DbUserBackend ' . $this->getName() . ' has no valid database connection.'; - Logger::fatal($msg); - throw new ConfigurationError($msg); - } - } - /** * Return the number of users in this database connection * * This class is mainly used for determining whether the authentication backend is valid or not * - * @return int The number of users set in this backend - * @see UserBackend::getUserCount + * @return int The number of users set in this backend + * @see UserBackend::getUserCount */ public function getUserCount() { - - $this->db->getConnection(); $query = $this->db->select()->from($this->userTable, 'COUNT(*) as count')->query(); return $query->fetch()->count; } diff --git a/library/Icinga/Authentication/Backend/LdapUserBackend.php b/library/Icinga/Authentication/Backend/LdapUserBackend.php index 8be1520ec..643624f0d 100644 --- a/library/Icinga/Authentication/Backend/LdapUserBackend.php +++ b/library/Icinga/Authentication/Backend/LdapUserBackend.php @@ -153,6 +153,12 @@ class LdapUserBackend implements UserBackend return $user; } + /** + * Return number of users in this backend + * + * @return int The number of users set in this backend + * @see UserBackend::getUserCount + */ public function getUserCount() { return $this->connection->count( diff --git a/test/php/library/Icinga/Authentication/DbUserBackendTest.php b/test/php/library/Icinga/Authentication/DbUserBackendTest.php index 8e96d22a5..25d73c656 100644 --- a/test/php/library/Icinga/Authentication/DbUserBackendTest.php +++ b/test/php/library/Icinga/Authentication/DbUserBackendTest.php @@ -40,6 +40,7 @@ require_once 'Zend/Config/Ini.php'; require_once 'Zend/Db/Adapter/Abstract.php'; require_once 'Zend/Db.php'; require_once 'Zend/Log.php'; +require_once BaseTestCase::$libDir . '/Exception/ProgrammingError.php'; require_once BaseTestCase::$libDir . '/Util/ConfigAwareFactory.php'; require_once BaseTestCase::$libDir . '/Authentication/UserBackend.php'; require_once BaseTestCase::$libDir . '/Protocol/Ldap/Exception.php'; @@ -172,7 +173,7 @@ class DbUserBackendTest extends BaseTestCase $usr[self::SALT_COLUMN], $usr[self::PASSWORD_COLUMN] ), - self::ACTIVE_COLUMN => $usr[self::ACTIVE_COLUMN], + self::ACTIVE_COLUMN => $usr[self::ACTIVE_COLUMN], self::SALT_COLUMN => $usr[self::SALT_COLUMN] ); $resource->insert($this->testTable, $data); @@ -284,4 +285,30 @@ class DbUserBackendTest extends BaseTestCase $this->assertSame($testName, $backend->getName()); } + + /** + * @dataProvider mysqlDb + */ + public function testCountUsersMySql($db) + { + $this->setupDbProvider($db); + $testName = 'test-name-123123'; + $backend = new DbUserBackend($this->createDbBackendConfig($db, $testName)); + + $this->assertGreaterThan(0, $backend->getUserCount()); + } + + /** + * @dataProvider pgsqlDb + */ + public function testCountUsersPgSql($db) + { + $this->setupDbProvider($db); + $testName = 'test-name-123123'; + $backend = new DbUserBackend($this->createDbBackendConfig($db, $testName)); + + $this->assertGreaterThan(0, $backend->getUserCount()); + } + + } diff --git a/test/php/library/Icinga/Authentication/ErrorProneBackendMock.php b/test/php/library/Icinga/Authentication/ErrorProneBackendMock.php index 077d92666..b8172c707 100644 --- a/test/php/library/Icinga/Authentication/ErrorProneBackendMock.php +++ b/test/php/library/Icinga/Authentication/ErrorProneBackendMock.php @@ -88,4 +88,17 @@ class ErrorProneBackendMock implements UserBackend { return $this->name; } + + /** + * Get the number of users available through this backend + * + * @return int + * @throws Exception + */ + public function getUserCount() + { + throw new Exception('getUserCount error: No users in this error prone backend'); + } + + }