From 3f8e1f19e053401f90c0b474845024fa5f91c879 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Fri, 20 Oct 2017 17:41:39 +0200 Subject: [PATCH] Add tests for AuthenticationPlugin::checkRules MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Michal Čihař --- .../Plugins/Auth/AuthenticationCookieTest.php | 219 ++++++++++++++++-- 1 file changed, 196 insertions(+), 23 deletions(-) diff --git a/test/classes/Plugins/Auth/AuthenticationCookieTest.php b/test/classes/Plugins/Auth/AuthenticationCookieTest.php index 1c410cfb71..3dd3b2c57a 100644 --- a/test/classes/Plugins/Auth/AuthenticationCookieTest.php +++ b/test/classes/Plugins/Auth/AuthenticationCookieTest.php @@ -91,13 +91,7 @@ class AuthenticationCookieTest extends PmaTestCase ); } - /** - * Test for PhpMyAdmin\Plugins\Auth\AuthenticationConfig::showLoginForm - * - * @return void - * @group medium - */ - public function testAuthError() + private function getAuthErrorMockResponse() { $mockResponse = $this->mockResponse(); @@ -106,12 +100,6 @@ class AuthenticationCookieTest extends PmaTestCase ->with() ->will($this->returnValue(false)); - $_REQUEST['old_usr'] = ''; - $GLOBALS['cfg']['LoginCookieRecall'] = true; - $GLOBALS['cfg']['blowfish_secret'] = 'secret'; - $this->object->user = 'pmauser'; - $GLOBALS['pma_auth_server'] = 'localhost'; - // mock footer $mockFooter = $this->getMockBuilder('PhpMyAdmin\Footer') ->disableOriginalConstructor() @@ -165,17 +153,7 @@ class AuthenticationCookieTest extends PmaTestCase ->will($this->returnValue($mockHeader)); $GLOBALS['pmaThemeImage'] = 'test'; - $GLOBALS['conn_error'] = true; - $GLOBALS['cfg']['Lang'] = 'en'; - $GLOBALS['cfg']['AllowArbitraryServer'] = true; $GLOBALS['cfg']['Servers'] = array(1, 2); - $GLOBALS['cfg']['CaptchaLoginPrivateKey'] = ''; - $GLOBALS['cfg']['CaptchaLoginPublicKey'] = ''; - $GLOBALS['target'] = 'testTarget'; - $GLOBALS['db'] = 'testDb'; - $GLOBALS['table'] = 'testTable'; - - file_put_contents('testlogo_right.png', ''); // mock error handler @@ -194,6 +172,35 @@ class AuthenticationCookieTest extends PmaTestCase ->with(); $GLOBALS['error_handler'] = $mockErrorHandler; + } + + /** + * Test for PhpMyAdmin\Plugins\Auth\AuthenticationConfig::showLoginForm + * + * @return void + * @group medium + */ + public function testAuthError() + { + $this->getAuthErrorMockResponse(); + + $_REQUEST['old_usr'] = ''; + $GLOBALS['cfg']['LoginCookieRecall'] = true; + $GLOBALS['cfg']['blowfish_secret'] = 'secret'; + $this->object->user = 'pmauser'; + $GLOBALS['pma_auth_server'] = 'localhost'; + + + $GLOBALS['conn_error'] = true; + $GLOBALS['cfg']['Lang'] = 'en'; + $GLOBALS['cfg']['AllowArbitraryServer'] = true; + $GLOBALS['cfg']['CaptchaLoginPrivateKey'] = ''; + $GLOBALS['cfg']['CaptchaLoginPublicKey'] = ''; + $GLOBALS['target'] = 'testTarget'; + $GLOBALS['db'] = 'testDb'; + $GLOBALS['table'] = 'testTable'; + + file_put_contents('testlogo_right.png', ''); ob_start(); $this->object->showLoginForm(); @@ -1156,4 +1163,170 @@ class AuthenticationCookieTest extends PmaTestCase $this->assertEquals('testUser', $GLOBALS['cfg']['Server']['user']); $this->assertEquals('testPassword', $GLOBALS['cfg']['Server']['password']); } + + /** + * Test for PhpMyAdmin\Plugins\Auth\AuthenticationCookie::checkRules + * + * @return void + * + * @dataProvider checkRulesProvider + */ + public function testCheckRules($user, $pass, $ip, $root, $nopass, $rules, $expected) + { + $this->object->user = $user; + $this->object->password = $pass; + $this->object->storeCredentials(); + + $_SERVER['REMOTE_ADDR'] = $ip; + + $GLOBALS['cfg']['Server']['AllowRoot'] = $root; + $GLOBALS['cfg']['Server']['AllowNoPassword'] = $nopass; + $GLOBALS['cfg']['Server']['AllowDeny'] = $rules; + + if (! empty($expected)) { + $this->getAuthErrorMockResponse(); + } + + ob_start(); + $this->object->checkRules(); + $result = ob_get_clean(); + + if (empty($expected)) { + $this->assertEquals($expected, $result); + } else { + $this->assertContains($expected, $result); + } + } + + public function checkRulesProvider() + { + return array( + 'nopass-ok' => array( + 'testUser', + '', + '1.2.3.4', + true, + true, + array(), + '', + ), + 'nopass' => array( + 'testUser', + '', + '1.2.3.4', + true, + false, + array(), + 'Login without a password is forbidden', + ), + 'root-ok' => array( + 'root', + 'root', + '1.2.3.4', + true, + true, + array(), + '', + ), + 'root' => array( + 'root', + 'root', + '1.2.3.4', + false, + true, + array(), + 'Access denied!', + ), + 'rules-deny-allow-ok' => array( + 'root', + 'root', + '1.2.3.4', + true, + true, + array( + 'order' => 'deny,allow', + 'rules' => array( + 'allow root 1.2.3.4', + 'deny % from all', + ), + ), + '', + ), + 'rules-deny-allow-reject' => array( + 'user', + 'root', + '1.2.3.4', + true, + true, + array( + 'order' => 'deny,allow', + 'rules' => array( + 'allow root 1.2.3.4', + 'deny % from all', + ), + ), + 'Access denied!', + ), + 'rules-allow-deny-ok' => array( + 'root', + 'root', + '1.2.3.4', + true, + true, + array( + 'order' => 'allow,deny', + 'rules' => array( + 'deny user from all', + 'allow root 1.2.3.4', + ), + ), + '', + ), + 'rules-allow-deny-reject' => array( + 'user', + 'root', + '1.2.3.4', + true, + true, + array( + 'order' => 'allow,deny', + 'rules' => array( + 'deny user from all', + 'allow root 1.2.3.4', + ), + ), + 'Access denied!', + ), + 'rules-explicit-ok' => array( + 'root', + 'root', + '1.2.3.4', + true, + true, + array( + 'order' => 'explicit', + 'rules' => array( + 'deny user from all', + 'allow root 1.2.3.4', + ), + ), + '', + ), + 'rules-explicit-reject' => array( + 'user', + 'root', + '1.2.3.4', + true, + true, + array( + 'order' => 'explicit', + 'rules' => array( + 'deny user from all', + 'allow root 1.2.3.4', + ), + ), + 'Access denied!', + ), + ); + } }