Логика проверки ключа из KeyConfirmationForm вынесена в отдельный валидатор

У EmailActivationFixture зафиксирован стандартный путь к файлу данных
This commit is contained in:
ErickSkrauch 2016-05-15 01:33:21 +03:00
parent 0ba1be27e8
commit e2e31c3720
8 changed files with 133 additions and 92 deletions

View File

@ -1,6 +1,7 @@
<?php <?php
namespace api\models\base; namespace api\models\base;
use api\validators\EmailActivationKeyValidator;
use common\models\EmailActivation; use common\models\EmailActivation;
class KeyConfirmationForm extends ApiForm { class KeyConfirmationForm extends ApiForm {
@ -13,27 +14,10 @@ class KeyConfirmationForm extends ApiForm {
return [ return [
// TODO: нужно провалидировать количество попыток ввода кода для определённого IP адреса и в случае чего запросить капчу // TODO: нужно провалидировать количество попыток ввода кода для определённого IP адреса и в случае чего запросить капчу
['key', 'required', 'message' => 'error.key_is_required'], ['key', 'required', 'message' => 'error.key_is_required'],
['key', 'validateKey'], ['key', EmailActivationKeyValidator::class],
['key', 'validateKeyExpiration'],
]; ];
} }
public function validateKey($attribute) {
if (!$this->hasErrors()) {
if ($this->getActivationCodeModel() === null) {
$this->addError($attribute, "error.{$attribute}_not_exists");
}
}
}
public function validateKeyExpiration($attribute) {
if (!$this->hasErrors()) {
if ($this->getActivationCodeModel()->isExpired()) {
$this->addError($attribute, "error.{$attribute}_expire");
}
}
}
/** /**
* @return EmailActivation|null * @return EmailActivation|null
*/ */

View File

@ -0,0 +1,33 @@
<?php
namespace api\validators;
use common\models\EmailActivation;
use yii\validators\Validator;
class EmailActivationKeyValidator extends Validator {
public $notExist = 'error.key_not_exists';
public $expired = 'error.key_expire';
public function validateValue($value) {
if (($model = $this->findEmailActivationModel($value)) === null) {
return [$this->notExist, []];
}
if ($model->isExpired()) {
return [$this->expired, []];
}
return null;
}
/**
* @param string $key
* @return null|EmailActivation
*/
protected function findEmailActivationModel($key) {
return EmailActivation::findOne($key);
}
}

View File

@ -4,10 +4,10 @@ modules:
- Filesystem - Filesystem
- Yii2 - Yii2
- tests\codeception\common\_support\FixtureHelper - tests\codeception\common\_support\FixtureHelper
- REST:
depends: Yii2
- Redis - Redis
- AMQP - AMQP
- REST:
depends: Yii2
config: config:
Yii2: Yii2:
configFile: '../config/api/functional.php' configFile: '../config/api/functional.php'

View File

@ -1,6 +1 @@
# Codeception Test Suite Configuration
# suite for unit (internal) tests.
# RUN `build` COMMAND AFTER ADDING/REMOVING MODULES.
class_name: UnitTester class_name: UnitTester

View File

@ -3,7 +3,6 @@ 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 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;
@ -17,75 +16,10 @@ class KeyConfirmationFormTest extends DbTestCase {
public function fixtures() { public function fixtures() {
return [ return [
'emailActivations' => [ 'emailActivations' => EmailActivationFixture::class,
'class' => EmailActivationFixture::class,
'dataFile' => '@tests/codeception/common/fixtures/data/email-activations.php',
],
]; ];
} }
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 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() { public function testGetActivationCodeModel() {
$this->specify('should return model, based on passed key', function() { $this->specify('should return model, based on passed key', function() {
$model = new KeyConfirmationForm(); $model = new KeyConfirmationForm();

View File

@ -0,0 +1,76 @@
<?php
namespace codeception\api\unit\validators;
use api\validators\EmailActivationKeyValidator;
use Codeception\Specify;
use common\models\confirmations\ForgotPassword;
use common\models\EmailActivation;
use tests\codeception\api\unit\DbTestCase;
use tests\codeception\common\_support\ProtectedCaller;
use tests\codeception\common\fixtures\EmailActivationFixture;
/**
* @property EmailActivationFixture $emailActivations
*/
class EmailActivationKeyValidatorTest extends DbTestCase {
use Specify;
use ProtectedCaller;
public function fixtures() {
return [
'emailActivations' => EmailActivationFixture::class,
];
}
public function testFindEmailActivationModel() {
$this->specify('get EmailActivation model for exists key', function() {
$key = array_values($this->emailActivations->data)[0]['key'];
$model = new EmailActivationKeyValidator();
/** @var EmailActivation $result */
$result = $this->callProtected($model, 'findEmailActivationModel', $key);
expect($result)->isInstanceOf(EmailActivation::class);
expect($result->key)->equals($key);
});
$this->specify('get null model for exists key', function() {
$model = new EmailActivationKeyValidator();
expect($this->callProtected($model, 'findEmailActivationModel', 'invalid-key'))->null();
});
}
public function testValidateValue() {
$this->specify('get error.key_not_exists with validation wrong key', function () {
/** @var EmailActivationKeyValidator $model */
$model = new class extends EmailActivationKeyValidator {
public function findEmailActivationModel($key) {
return null;
}
};
expect($this->callProtected($model, 'validateValue', null))->equals([$model->notExist, []]);
});
$this->specify('get error.key_expire if we use old key', function () {
/** @var EmailActivationKeyValidator $model */
$model = new class extends EmailActivationKeyValidator {
public function findEmailActivationModel($key) {
$codeModel = new ForgotPassword();
$codeModel->created_at = time() - $codeModel->expirationTimeout - 10;
return $codeModel;
}
};
expect($this->callProtected($model, 'validateValue', null))->equals([$model->expired, []]);
});
$this->specify('no errors, if model exists and not expired', function () {
/** @var EmailActivationKeyValidator $model */
$model = new class extends EmailActivationKeyValidator {
public function findEmailActivationModel($key) {
return new EmailActivation();
}
};
expect($this->callProtected($model, 'validateValue', null))->null();
});
}
}

View File

@ -0,0 +1,17 @@
<?php
namespace tests\codeception\common\_support;
use Codeception\Module;
use ReflectionClass;
trait ProtectedCaller {
protected function callProtected($object, string $function, ...$args) {
$class = new ReflectionClass($object);
$method = $class->getMethod($function);
$method->setAccessible(true);
return $method->invokeArgs($object, $args);
}
}

View File

@ -8,6 +8,8 @@ class EmailActivationFixture extends ActiveFixture {
public $modelClass = EmailActivation::class; public $modelClass = EmailActivation::class;
public $dataFile = '@tests/codeception/common/fixtures/data/email-activations.php';
public $depends = [ public $depends = [
AccountFixture::class, AccountFixture::class,
]; ];