From ebf4947c37b85d3d3551bfd7f09a1f3c8f8996a2 Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Thu, 12 May 2016 01:08:40 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=B2=D0=B5=D1=80=D1=81=D0=B8=D1=8F=20Codecepti?= =?UTF-8?q?on=20(=D1=82=D0=B0=D0=BC=20=D0=BF=D0=BE=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=B8=D0=BB=D0=B8=20=D0=B1=D0=B0=D0=B3=20=D1=81=20=D0=B0=D0=BD?= =?UTF-8?q?=D0=BE=D0=BD=D0=B8=D0=BC=D0=BD=D1=8B=D0=BC=D0=B8=20=D0=BA=D0=BB?= =?UTF-8?q?=D0=B0=D1=81=D1=81=D0=B0=D0=BC=D0=B8)=20=D0=9F=D0=B5=D1=80?= =?UTF-8?q?=D0=B5=D0=BF=D0=B8=D1=81=D0=B0=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81?= =?UTF-8?q?=D1=82=D1=8B=20=D0=B4=D0=BB=D1=8F=20=D0=B1=D0=B0=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=BE=D0=B9=20=D1=84=D0=BE=D1=80=D0=BC=D1=8B=20=D1=81=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=B4=D0=BE=D0=B2=D1=8B=D0=BC=20=D0=B4=D0=BE=D1=81?= =?UTF-8?q?=D1=82=D1=83=D0=BF=D0=BE=D0=BC=20=D0=92=20=D0=B1=D0=B0=D0=B7?= =?UTF-8?q?=D0=BE=D0=B2=D1=83=D1=8E=20=D1=84=D0=BE=D1=80=D0=BC=D1=83=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=B4=D0=BE=D0=B2=D0=BE=D0=B3=D0=BE=20=D0=B4=D0=BE?= =?UTF-8?q?=D1=81=D1=82=D1=83=D0=BF=D0=B0=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B0=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80?= =?UTF-8?q?=D0=BA=D0=B0=20=D0=BD=D0=B0=20=D0=B8=D1=81=D1=82=D0=B5=D1=87?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=BE=D0=B4=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/models/base/KeyConfirmationForm.php | 9 ++ composer.json | 2 +- .../models/base/KeyConfirmationFormTest.php | 97 +++++++++++++------ 3 files changed, 79 insertions(+), 29 deletions(-) diff --git a/api/models/base/KeyConfirmationForm.php b/api/models/base/KeyConfirmationForm.php index efdcd53..1ad128a 100644 --- a/api/models/base/KeyConfirmationForm.php +++ b/api/models/base/KeyConfirmationForm.php @@ -14,6 +14,7 @@ class KeyConfirmationForm extends ApiForm { // TODO: нужно провалидировать количество попыток ввода кода для определённого IP адреса и в случае чего запросить капчу ['key', 'required', 'message' => 'error.key_is_required'], ['key', 'validateKey'], + ['key', 'validateKeyExpiration'], ]; } @@ -25,6 +26,14 @@ class KeyConfirmationForm extends ApiForm { } } + public function validateKeyExpiration($attribute) { + if (!$this->hasErrors()) { + if ($this->getActivationCodeModel()->isExpired()) { + $this->addError($attribute, "error.{$attribute}_expire"); + } + } + } + /** * @return EmailActivation|null */ diff --git a/composer.json b/composer.json index 4abc32e..22f82e3 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,7 @@ "yiisoft/yii2-gii": "*", "yiisoft/yii2-faker": "*", "flow/jsonpath": "^0.3.1", - "codeception/codeception": "2.1.*", + "codeception/codeception": "dev-master#5584c75a85b8217bbf6b0a77f2349aef82e94a15", "codeception/specify": "*", "codeception/verify": "*" }, diff --git a/tests/codeception/api/unit/models/base/KeyConfirmationFormTest.php b/tests/codeception/api/unit/models/base/KeyConfirmationFormTest.php index 8be5f9c..e3a0799 100644 --- a/tests/codeception/api/unit/models/base/KeyConfirmationFormTest.php +++ b/tests/codeception/api/unit/models/base/KeyConfirmationFormTest.php @@ -3,12 +3,14 @@ namespace tests\codeception\api\models\base; use api\models\base\KeyConfirmationForm; use Codeception\Specify; +use common\models\confirmations\ForgotPassword; +use common\models\EmailActivation; use tests\codeception\api\unit\DbTestCase; use tests\codeception\common\fixtures\EmailActivationFixture; use Yii; /** - * @property array $emailActivations + * @property EmailActivationFixture $emailActivations */ class KeyConfirmationFormTest extends DbTestCase { use Specify; @@ -22,40 +24,79 @@ class KeyConfirmationFormTest extends DbTestCase { ]; } - protected function createModel($key = null) { - return new KeyConfirmationForm([ - 'key' => $key, - ]); - } - - public function testEmptyKey() { - $model = $this->createModel(); - $this->specify('get error.key_is_required with validating empty key field', function () use ($model) { - expect('model should don\'t pass validation', $model->validate())->false(); - expect('error messages should be set', $model->errors)->equals([ - 'key' => [ - 'error.key_is_required', - ], - ]); - }); - } - - public function testIncorrectKey() { - $model = $this->createModel('not-exists-key'); - $this->specify('get error.key_not_exists with validation wrong key', function () use ($model) { - expect('model should don\'t pass validation', $model->validate())->false(); - expect('error messages should be set', $model->errors)->equals([ + public function testValidateKey() { + $this->specify('get error.key_not_exists with validation wrong key', function () { + /** @var KeyConfirmationForm $model */ + $model = new class extends KeyConfirmationForm { + public function getActivationCodeModel() { + return null; + } + }; + $model->validateKey('key'); + expect($model->errors)->equals([ 'key' => [ 'error.key_not_exists', ], ]); }); + + $this->specify('no errors, if model exists', function () { + /** @var KeyConfirmationForm $model */ + $model = new class extends KeyConfirmationForm { + public function getActivationCodeModel() { + return new EmailActivation(); + } + }; + $model->validateKey('key'); + expect($model->errors)->isEmpty(); + }); } - public function testCorrectKey() { - $model = $this->createModel($this->emailActivations['freshRegistrationConfirmation']['key']); - $this->specify('no errors if key exists', function () use ($model) { - expect('model should pass validation', $model->validate())->true(); + public function testValidateKeyExpiration() { + $this->specify('get error.key_expire if we use old key', function () { + /** @var KeyConfirmationForm $model */ + $model = new class extends KeyConfirmationForm { + public function getActivationCodeModel() { + $codeModel = new ForgotPassword(); + $codeModel->created_at = time() - $codeModel->expirationTimeout - 10; + + return $codeModel; + } + }; + $model->validateKeyExpiration('key'); + expect($model->errors)->equals([ + 'key' => [ + 'error.key_expire', + ], + ]); + }); + + $this->specify('no errors if key is not yet expired', function () { + /** @var KeyConfirmationForm $model */ + $model = new class extends KeyConfirmationForm { + public function getActivationCodeModel() { + $codeModel = new ForgotPassword(); + $codeModel->created_at = time() - $codeModel->expirationTimeout + 10; + + return $codeModel; + } + }; + $model->validateKeyExpiration('key'); + expect($model->errors)->isEmpty(); + }); + } + + public function testGetActivationCodeModel() { + $this->specify('should return model, based on passed key', function() { + $model = new KeyConfirmationForm(); + $model->key = array_values($this->emailActivations->data)[0]['key']; + expect($model->getActivationCodeModel())->isInstanceOf(EmailActivation::class); + }); + + $this->specify('should return null, if passed key is invalid', function() { + $model = new KeyConfirmationForm(); + $model->key = 'this-is-invalid-key'; + expect($model->getActivationCodeModel())->null(); }); }