From c851ff12ff6ac14f4639d05ab492b9f8e25ba44e Mon Sep 17 00:00:00 2001 From: Damien <damien.burel@maarch.org> Date: Thu, 12 Jul 2018 11:11:10 +0200 Subject: [PATCH] FEAT #7659 Password Unit Tests --- phpunit.xml | 3 +- sql/develop.sql | 2 +- sql/structure.sql | 2 +- src/core/controllers/PasswordController.php | 13 +- src/core/models/PasswordModel.php | 8 +- test/PasswordControllerTest.php | 151 ++++++++++++++++---- 6 files changed, 137 insertions(+), 42 deletions(-) diff --git a/phpunit.xml b/phpunit.xml index 7113fbff13b..e21e6ff2d3d 100755 --- a/phpunit.xml +++ b/phpunit.xml @@ -9,6 +9,7 @@ <file>test/ContactControllerTest.php</file> <file>test/ContactGroupControllerTest.php</file> <file>test/ContactTypeControllerTest.php</file> + <file>test/CoreControllerTest.php</file> <file>test/DocserverControllerTest.php</file> <file>test/DoctypeControllerTest.php</file> <file>test/EntityControllerTest.php</file> @@ -17,13 +18,13 @@ <file>test/NotificationControllerTest.php</file> <file>test/NotificationScheduleControllerTest.php</file> <file>test/ParameterControllerTest.php</file> + <file>test/PasswordControllerTest.php</file> <file>test/PriorityControllerTest.php</file> <file>test/ReportControllerTest.php</file> <file>test/ResControllerTest.php</file> <file>test/StatusControllerTest.php</file> <file>test/UserControllerTest.php</file> <file>test/VersionUpdateControllerTest.php</file> - <file>test/CoreControllerTest.php</file> <file>test/TemplateControllerTest.php</file> <!-- The last one should be history --> <file>test/HistoryControllerTest.php</file> diff --git a/sql/develop.sql b/sql/develop.sql index 41ce64997fe..5bef39b205c 100644 --- a/sql/develop.sql +++ b/sql/develop.sql @@ -78,7 +78,7 @@ CREATE TABLE password_rules id serial, label character varying(64) NOT NULL, "value" INTEGER NOT NULL, - enabled boolean DEFAULT FALSE, + enabled boolean DEFAULT FALSE NOT NULL, CONSTRAINT password_rules_pkey PRIMARY KEY (id), CONSTRAINT password_rules_label_key UNIQUE (label) ) diff --git a/sql/structure.sql b/sql/structure.sql index 644231b085b..bc4583b6043 100755 --- a/sql/structure.sql +++ b/sql/structure.sql @@ -2201,7 +2201,7 @@ CREATE TABLE password_rules id serial, label character varying(64) NOT NULL, "value" integer NOT NULL, - enabled boolean DEFAULT FALSE, + enabled boolean DEFAULT FALSE NOT NULL, CONSTRAINT password_rules_pkey PRIMARY KEY (id), CONSTRAINT password_rules_label_key UNIQUE (label) ) diff --git a/src/core/controllers/PasswordController.php b/src/core/controllers/PasswordController.php index bfb98844a75..6f713c34417 100644 --- a/src/core/controllers/PasswordController.php +++ b/src/core/controllers/PasswordController.php @@ -42,18 +42,19 @@ class PasswordController } foreach ($data['rules'] as $rule) { - $existingRule = PasswordModel::getRuleById(['id' => $rule['id'], 'select' => [1]]); - if (empty($existingRule)) { - continue; - } - $check = Validator::intVal()->validate($rule['value']); + $check = $check && Validator::stringType()->validate($rule['label']); $check = $check && Validator::boolType()->validate($rule['enabled']); if (!$check) { continue; } - $rule['enabled'] = $rule['enabled'] ? 'true' : 'false'; + $existingRule = PasswordModel::getRuleById(['id' => $rule['id'], 'select' => ['label']]); + if (empty($existingRule) || $existingRule['label'] != $rule['label']) { + continue; + } + + $rule['enabled'] = empty($rule['enabled']) ? 'false' : 'true'; PasswordModel::updateRuleById($rule); } diff --git a/src/core/models/PasswordModel.php b/src/core/models/PasswordModel.php index 5ca66d85f65..3c302a92de3 100644 --- a/src/core/models/PasswordModel.php +++ b/src/core/models/PasswordModel.php @@ -57,14 +57,18 @@ class PasswordModel ValidatorModel::intVal($aArgs, ['id']); ValidatorModel::arrayType($aArgs, ['select']); - $aRules = DatabaseModel::select([ + $rules = DatabaseModel::select([ 'select' => empty($aArgs['select']) ? ['*'] : $aArgs['select'], 'table' => ['password_rules'], 'where' => ['id = ?'], 'data' => [$aArgs['id']], ]); - return $aRules; + if (empty($rules[0])) { + return []; + } + + return $rules[0]; } public static function updateRuleById(array $aArgs) diff --git a/test/PasswordControllerTest.php b/test/PasswordControllerTest.php index 17d81e34f80..49765f23762 100644 --- a/test/PasswordControllerTest.php +++ b/test/PasswordControllerTest.php @@ -11,7 +11,8 @@ use PHPUnit\Framework\TestCase; class PasswordControllerTest extends TestCase { - public function testGetRules(){ + public function testGetRules() + { $passwordController = new \SrcCore\controllers\PasswordController(); $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']); @@ -19,50 +20,138 @@ class PasswordControllerTest extends TestCase $response = $passwordController->getRules($request, new \Slim\Http\Response()); $responseBody = json_decode((string)$response->getBody()); + $this->assertInternalType('array', $responseBody->rules); - $this->assertNotNull($responseBody->rules); + $this->assertNotEmpty($responseBody->rules); } - public function testUpdateRules(){ + public function testUpdateRules() + { $passwordController = new \SrcCore\controllers\PasswordController(); $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']); $request = \Slim\Http\Request::createFromEnvironment($environment); - $aArgs = [ - 'rules' => [ - [ - 'id' => 1, - 'value' => 5, - 'enabled' => true, - ] - ] - ]; - $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); - $response = $passwordController->updateRules($fullRequest, new \Slim\Http\Response()); + + $response = $passwordController->getRules($request, new \Slim\Http\Response()); $responseBody = json_decode((string)$response->getBody()); - + + // reset + $rules = (array)$responseBody->rules; + foreach ($rules as $key => $rule) { + $rules[$key] = (array)$rule; + $rule = (array)$rule; + if ($rule['label'] == 'complexitySpecial' || $rule['label'] == 'complexityNumber' || $rule['label'] == 'complexityUpper') { + $rules[$key]['enabled'] = false; + } + if ($rule['label'] == 'minLength') { + $rules[$key]['value'] = 6; + $rules[$key]['enabled'] = true; + } + } + + $fullRequest = \httpRequestCustom::addContentInBody(['rules' => $rules], $request); + $response = $passwordController->updateRules($fullRequest, new \Slim\Http\Response()); + $responseBody = json_decode((string)$response->getBody()); + $this->assertSame($responseBody->success, 'success'); - } - public function testIsPasswordValid(){ - $passwordController = new \SrcCore\controllers\PasswordController(); + $isPasswordValid = $passwordController->isPasswordValid(['password' => 'maarch']); + $this->assertSame($isPasswordValid, true); - $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']); + // minLength + foreach ($rules as $key => $rule) { + if ($rule['label'] == 'minLength') { + $rules[$key]['value'] = 7; + $rules[$key]['enabled'] = true; + } + } + + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']); $request = \Slim\Http\Request::createFromEnvironment($environment); - $aArgs = [ - 'password' => 'notValidPassword', - ]; + $fullRequest = \httpRequestCustom::addContentInBody(['rules' => $rules], $request); + $response = $passwordController->updateRules($fullRequest, new \Slim\Http\Response()); + $responseBody = json_decode((string)$response->getBody()); + + $this->assertSame($responseBody->success, 'success'); + + $isPasswordValid = $passwordController->isPasswordValid(['password' => 'maarch']); + $this->assertSame($isPasswordValid, false); + $isPasswordValid = $passwordController->isPasswordValid(['password' => 'maaarch']); + $this->assertSame($isPasswordValid, true); + + // complexityUpper + foreach ($rules as $key => $rule) { + if ($rule['label'] == 'complexityUpper') { + $rules[$key]['enabled'] = true; + } + } + + $fullRequest = \httpRequestCustom::addContentInBody(['rules' => $rules], $request); + $response = $passwordController->updateRules($fullRequest, new \Slim\Http\Response()); + $responseBody = json_decode((string)$response->getBody()); - $response = $passwordController->isPasswordValid($aArgs); + $this->assertSame($responseBody->success, 'success'); + + $isPasswordValid = $passwordController->isPasswordValid(['password' => 'maaarch']); + $this->assertSame($isPasswordValid, false); + $isPasswordValid = $passwordController->isPasswordValid(['password' => 'Maaarch']); + $this->assertSame($isPasswordValid, true); + + // complexityNumber + foreach ($rules as $key => $rule) { + if ($rule['label'] == 'complexityNumber') { + $rules[$key]['enabled'] = true; + } + } + + $fullRequest = \httpRequestCustom::addContentInBody(['rules' => $rules], $request); + $response = $passwordController->updateRules($fullRequest, new \Slim\Http\Response()); + $responseBody = json_decode((string)$response->getBody()); + + $this->assertSame($responseBody->success, 'success'); + + $isPasswordValid = $passwordController->isPasswordValid(['password' => 'Maaarch']); + $this->assertSame($isPasswordValid, false); + $isPasswordValid = $passwordController->isPasswordValid(['password' => 'Maaarch1']); + $this->assertSame($isPasswordValid, true); + + // complexitySpecial + foreach ($rules as $key => $rule) { + if ($rule['label'] == 'complexitySpecial') { + $rules[$key]['enabled'] = true; + } + } - $this->assertSame($response,false); + $fullRequest = \httpRequestCustom::addContentInBody(['rules' => $rules], $request); + $response = $passwordController->updateRules($fullRequest, new \Slim\Http\Response()); + $responseBody = json_decode((string)$response->getBody()); + + $this->assertSame($responseBody->success, 'success'); + + $isPasswordValid = $passwordController->isPasswordValid(['password' => 'Maaarch1']); + $this->assertSame($isPasswordValid, false); + $isPasswordValid = $passwordController->isPasswordValid(['password' => 'Maaarch1!']); + $this->assertSame($isPasswordValid, true); + + // reset + foreach ($rules as $key => $rule) { + if ($rule['label'] == 'complexitySpecial' || $rule['label'] == 'complexityNumber' || $rule['label'] == 'complexityUpper') { + $rules[$key]['enabled'] = false; + } + if ($rule['label'] == 'minLength') { + $rules[$key]['value'] = 6; + $rules[$key]['enabled'] = true; + } + } + + $fullRequest = \httpRequestCustom::addContentInBody(['rules' => $rules], $request); + $response = $passwordController->updateRules($fullRequest, new \Slim\Http\Response()); + $responseBody = json_decode((string)$response->getBody()); + + $this->assertSame($responseBody->success, 'success'); - $aArgs = [ - 'password' => 'validPassword123&', - ]; - - $response = $passwordController->isPasswordValid($aArgs); - $this->assertSame($response,true); + $isPasswordValid = $passwordController->isPasswordValid(['password' => 'maarch']); + $this->assertSame($isPasswordValid, true); } -} \ No newline at end of file +} -- GitLab