From 68706de45bda77d61327c3288ec6b4210de51532 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Mon, 6 Jul 2015 23:07:33 +0200 Subject: [PATCH] refactor integration tests to make it easier to add new ones --- .../integration/abstractintegrationtest.php | 123 +++++++++++++++++ .../tests/integration/fakemanager.php | 33 +++++ ...IntegrationTestAccessGroupsMatchFilter.php | 126 +++++------------- apps/user_ldap/tests/integration/run-test.sh | 2 +- 4 files changed, 189 insertions(+), 95 deletions(-) create mode 100644 apps/user_ldap/tests/integration/abstractintegrationtest.php create mode 100644 apps/user_ldap/tests/integration/fakemanager.php diff --git a/apps/user_ldap/tests/integration/abstractintegrationtest.php b/apps/user_ldap/tests/integration/abstractintegrationtest.php new file mode 100644 index 0000000000..f24106fcbf --- /dev/null +++ b/apps/user_ldap/tests/integration/abstractintegrationtest.php @@ -0,0 +1,123 @@ + + * + * @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 + * + */ + +namespace OCA\user_ldap\tests\integration; + +use OCA\user_ldap\lib\Access; +use OCA\user_ldap\lib\Connection; +use OCA\user_ldap\lib\LDAP; + +abstract class AbstractIntegrationTest { + /** @var LDAP */ + protected $ldap; + + /** @var Connection */ + protected $connection; + + /** @var Access */ + protected $access; + + /** @var string */ + protected $base; + + /** @var string[] */ + protected $server; + + public function __construct($host, $port, $bind, $pwd, $base) { + $this->base = $base; + $this->server = [ + 'host' => $host, + 'port' => $port, + 'dn' => $bind, + 'pwd' => $pwd + ]; + } + + /** + * prepares the LDAP environment and sets up a test configuration for + * the LDAP backend. + */ + public function init() { + $this->initLDAPWrapper(); + $this->initConnection(); + $this->initAccess(); + } + + /** + * initializes the test LDAP wrapper + */ + protected function initLDAPWrapper() { + $this->ldap = new LDAP(); + } + + /** + * sets up the LDAP configuration to be used for the test + */ + protected function initConnection() { + $this->connection = new Connection($this->ldap, '', null); + $this->connection->setConfiguration([ + 'ldapHost' => $this->server['host'], + 'ldapPort' => $this->server['port'], + 'ldapBase' => $this->base, + 'ldapAgentName' => $this->server['dn'], + 'ldapAgentPassword' => $this->server['pwd'], + 'ldapUserFilter' => 'objectclass=inetOrgPerson', + 'ldapUserDisplayName' => 'cn', + 'ldapGroupDisplayName' => 'cn', + 'ldapLoginFilter' => '(|(uid=%uid)(samaccountname=%uid))', + 'ldapCacheTTL' => 0, + 'ldapConfigurationActive' => 1, + ]); + } + + /** + * initializes the Access test instance + */ + protected function initAccess() { + $this->access = new Access($this->connection, $this->ldap, new FakeManager()); + } + + /** + * runs the test cases while outputting progress and result information + * + * If a test failed, the script is exited with return code 1. + */ + public function run() { + $methods = get_class_methods($this); + $atLeastOneCaseRan = false; + foreach($methods as $method) { + if(strpos($method, 'case') === 0) { + print("running $method " . PHP_EOL); + if(!$this->$method()) { + print(PHP_EOL . '>>> !!! Test ' . $method . ' FAILED !!! <<<' . PHP_EOL . PHP_EOL); + exit(1); + } + $atLeastOneCaseRan = true; + } + } + if($atLeastOneCaseRan) { + print('Tests succeeded' . PHP_EOL); + } else { + print('No Test was available.' . PHP_EOL); + exit(1); + } + } +} diff --git a/apps/user_ldap/tests/integration/fakemanager.php b/apps/user_ldap/tests/integration/fakemanager.php new file mode 100644 index 0000000000..afc9c552a9 --- /dev/null +++ b/apps/user_ldap/tests/integration/fakemanager.php @@ -0,0 +1,33 @@ + + * + * @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 + * + */ + +namespace OCA\user_ldap\tests\integration; + +/** + * Class FakeManager + * + * this is a mock of \OCA\user_ldap\lib\user\Manager which is a dependency of + * Access, that pulls plenty more things in. Because it is not needed in the + * scope of these tests, we replace it with a mock. + */ +class FakeManager extends \OCA\user_ldap\lib\user\Manager { + public function __construct() {} +} diff --git a/apps/user_ldap/tests/integration/lib/IntegrationTestAccessGroupsMatchFilter.php b/apps/user_ldap/tests/integration/lib/IntegrationTestAccessGroupsMatchFilter.php index 92035d94b4..4be9ec829b 100644 --- a/apps/user_ldap/tests/integration/lib/IntegrationTestAccessGroupsMatchFilter.php +++ b/apps/user_ldap/tests/integration/lib/IntegrationTestAccessGroupsMatchFilter.php @@ -1,72 +1,42 @@ + * + * @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 + * */ -use OCA\user_ldap\lib\LDAP; +namespace OCA\user_ldap\tests\integration\lib; + +use OCA\user_ldap\lib\Connection; +use OCA\user_ldap\tests\integration\AbstractIntegrationTest; require_once __DIR__ . '/../../../../../lib/base.php'; -class IntegrationTestAccessGroupsMatchFilter { - /** @var LDAP */ - protected $ldap; - - /** @var \OCA\user_ldap\lib\Connection */ - protected $connection; - - /** @var \OCA\user_ldap\lib\Access */ - protected $access; - - /** @var string */ - protected $base; - - /** @var string[] */ - protected $server; - - public function __construct($host, $port, $bind, $pwd, $base) { - $this->base = $base; - $this->server = [ - 'host' => $host, - 'port' => $port, - 'dn' => $bind, - 'pwd' => $pwd - ]; - } +class IntegrationTestAccessGroupsMatchFilter extends AbstractIntegrationTest { /** - * prepares the LDAP environement and sets up a test configuration for + * prepares the LDAP environment and sets up a test configuration for * the LDAP backend. */ public function init() { - require('setup-scripts/createExplicitUsers.php'); - require('setup-scripts/createExplicitGroups.php'); - require('setup-scripts/createExplicitGroupsDifferentOU.php'); - - $this->initLDAPWrapper(); - $this->initConnection(); - $this->initAccess(); - } - - /** - * runs the test cases while outputting progress and result information - * - * If a test failed, the script is exited with return code 1. - */ - public function run() { - $cases = ['case1', 'case2', 'case3']; - - foreach ($cases as $case) { - print("running $case " . PHP_EOL); - if (!$this->$case()) { - print(PHP_EOL . '>>> !!! Test ' . $case . ' FAILED !!! <<<' . PHP_EOL . PHP_EOL); - exit(1); - } - } - - print('Tests succeeded' . PHP_EOL); + require(__DIR__ . '/../setup-scripts/createExplicitUsers.php'); + require(__DIR__ . '/../setup-scripts/createExplicitGroups.php'); + require(__DIR__ . '/../setup-scripts/createExplicitGroupsDifferentOU.php'); + parent::init(); } /** @@ -75,7 +45,7 @@ class IntegrationTestAccessGroupsMatchFilter { * * @return bool */ - private function case1() { + protected function case1() { $this->connection->setConfiguration(['ldapGroupFilter' => 'cn=RedGroup']); $dns = ['cn=RedGroup,ou=Groups,' . $this->base]; @@ -89,7 +59,7 @@ class IntegrationTestAccessGroupsMatchFilter { * * @return bool */ - private function case2() { + protected function case2() { $this->connection->setConfiguration(['ldapGroupFilter' => '(|(cn=RedGroup)(cn=PurpleGroup))']); $dns = [ @@ -131,54 +101,22 @@ class IntegrationTestAccessGroupsMatchFilter { return $status; } - /** - * initializes the Access test instance - */ - private function initAccess() { - $this->access = new \OCA\user_ldap\lib\Access($this->connection, $this->ldap, new FakeManager()); - } - - /** - * initializes the test LDAP wrapper - */ - private function initLDAPWrapper() { - $this->ldap = new LDAP(); - } - /** * sets up the LDAP configuration to be used for the test */ - private function initConnection() { - $this->connection = new \OCA\user_ldap\lib\Connection($this->ldap, '', null); + protected function initConnection() { + parent::initConnection(); $this->connection->setConfiguration([ - 'ldapHost' => $this->server['host'], - 'ldapPort' => $this->server['port'], - 'ldapBase' => $this->base, 'ldapBaseGroups' => 'ou=Groups,' . $this->base, - 'ldapAgentName' => $this->server['dn'], - 'ldapAgentPassword' => $this->server['pwd'], 'ldapUserFilter' => 'objectclass=inetOrgPerson', 'ldapUserDisplayName' => 'displayName', 'ldapGroupDisplayName' => 'cn', 'ldapLoginFilter' => 'uid=%uid', - 'ldapCacheTTL' => 0, - 'ldapConfigurationActive' => 1, ]); } } -/** - * Class FakeManager - * - * this is a mock of \OCA\user_ldap\lib\user\Manager which is a dependency of - * Access, that pulls plenty more things in. Because it is not needed in the - * scope of these tests, we replace it with a mock. - */ -class FakeManager extends \OCA\user_ldap\lib\user\Manager { - public function __construct() {} -} - -require_once('setup-scripts/config.php'); +require_once(__DIR__ . '/../setup-scripts/config.php'); $test = new IntegrationTestAccessGroupsMatchFilter($host, $port, $adn, $apwd, $bdn); $test->init(); $test->run(); diff --git a/apps/user_ldap/tests/integration/run-test.sh b/apps/user_ldap/tests/integration/run-test.sh index e07e9b4340..7a29db2567 100755 --- a/apps/user_ldap/tests/integration/run-test.sh +++ b/apps/user_ldap/tests/integration/run-test.sh @@ -13,5 +13,5 @@ fi # sleep is necessary, otherwise the LDAP server cannot be connected to, yet. -setup-scripts/start.sh && sleep 2 && php -f "$TESTSCRIPT" +setup-scripts/start.sh && sleep 5 && php -f "$TESTSCRIPT" setup-scripts/stop.sh