mirror of
				https://github.com/elyby/accounts.git
				synced 2025-05-31 14:11:46 +05:30 
			
		
		
		
	Обновлена версия Codeception (там поправили баг с анонимными классами)
Переписаны тесты для базовой формы с кодовым доступом В базовую форму кодового доступа добавлена проверка на истечение кода
This commit is contained in:
		| @@ -14,6 +14,7 @@ class KeyConfirmationForm extends ApiForm { | |||||||
|             // TODO: нужно провалидировать количество попыток ввода кода для определённого IP адреса и в случае чего запросить капчу |             // TODO: нужно провалидировать количество попыток ввода кода для определённого IP адреса и в случае чего запросить капчу | ||||||
|             ['key', 'required', 'message' => 'error.key_is_required'], |             ['key', 'required', 'message' => 'error.key_is_required'], | ||||||
|             ['key', 'validateKey'], |             ['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 |      * @return EmailActivation|null | ||||||
|      */ |      */ | ||||||
|   | |||||||
| @@ -32,7 +32,7 @@ | |||||||
|         "yiisoft/yii2-gii": "*", |         "yiisoft/yii2-gii": "*", | ||||||
|         "yiisoft/yii2-faker": "*", |         "yiisoft/yii2-faker": "*", | ||||||
|         "flow/jsonpath": "^0.3.1", |         "flow/jsonpath": "^0.3.1", | ||||||
|         "codeception/codeception": "2.1.*", |         "codeception/codeception": "dev-master#5584c75a85b8217bbf6b0a77f2349aef82e94a15", | ||||||
|         "codeception/specify": "*", |         "codeception/specify": "*", | ||||||
|         "codeception/verify": "*" |         "codeception/verify": "*" | ||||||
|     }, |     }, | ||||||
|   | |||||||
| @@ -3,12 +3,14 @@ namespace tests\codeception\api\models\base; | |||||||
|  |  | ||||||
| use api\models\base\KeyConfirmationForm; | use api\models\base\KeyConfirmationForm; | ||||||
| use Codeception\Specify; | use Codeception\Specify; | ||||||
|  | use common\models\confirmations\ForgotPassword; | ||||||
|  | use common\models\EmailActivation; | ||||||
| use tests\codeception\api\unit\DbTestCase; | use tests\codeception\api\unit\DbTestCase; | ||||||
| use tests\codeception\common\fixtures\EmailActivationFixture; | use tests\codeception\common\fixtures\EmailActivationFixture; | ||||||
| use Yii; | use Yii; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * @property array $emailActivations |  * @property EmailActivationFixture $emailActivations | ||||||
|  */ |  */ | ||||||
| class KeyConfirmationFormTest extends DbTestCase { | class KeyConfirmationFormTest extends DbTestCase { | ||||||
|     use Specify; |     use Specify; | ||||||
| @@ -22,40 +24,79 @@ class KeyConfirmationFormTest extends DbTestCase { | |||||||
|         ]; |         ]; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     protected function createModel($key = null) { |     public function testValidateKey() { | ||||||
|         return new KeyConfirmationForm([ |         $this->specify('get error.key_not_exists with validation wrong key', function () { | ||||||
|             'key' => $key, |             /** @var KeyConfirmationForm $model */ | ||||||
|         ]); |             $model = new class extends KeyConfirmationForm { | ||||||
|     } |                 public function getActivationCodeModel() { | ||||||
|  |                     return null; | ||||||
|     public function testEmptyKey() { |                 } | ||||||
|         $model = $this->createModel(); |             }; | ||||||
|         $this->specify('get error.key_is_required with validating empty key field', function () use ($model) { |             $model->validateKey('key'); | ||||||
|             expect('model should don\'t pass validation', $model->validate())->false(); |             expect($model->errors)->equals([ | ||||||
|             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([ |  | ||||||
|                 'key' => [ |                 'key' => [ | ||||||
|                     'error.key_not_exists', |                     '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() { |     public function testValidateKeyExpiration() { | ||||||
|         $model = $this->createModel($this->emailActivations['freshRegistrationConfirmation']['key']); |         $this->specify('get error.key_expire if we use old key', function () { | ||||||
|         $this->specify('no errors if key exists', function () use ($model) { |             /** @var KeyConfirmationForm $model */ | ||||||
|             expect('model should pass validation', $model->validate())->true(); |             $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(); | ||||||
|         }); |         }); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user