mirror of
https://github.com/elyby/accounts.git
synced 2024-12-27 15:40:21 +05:30
Логика проверки ключа из KeyConfirmationForm вынесена в отдельный валидатор
У EmailActivationFixture зафиксирован стандартный путь к файлу данных
This commit is contained in:
parent
0ba1be27e8
commit
e2e31c3720
@ -1,6 +1,7 @@
|
||||
<?php
|
||||
namespace api\models\base;
|
||||
|
||||
use api\validators\EmailActivationKeyValidator;
|
||||
use common\models\EmailActivation;
|
||||
|
||||
class KeyConfirmationForm extends ApiForm {
|
||||
@ -13,27 +14,10 @@ class KeyConfirmationForm extends ApiForm {
|
||||
return [
|
||||
// TODO: нужно провалидировать количество попыток ввода кода для определённого IP адреса и в случае чего запросить капчу
|
||||
['key', 'required', 'message' => 'error.key_is_required'],
|
||||
['key', 'validateKey'],
|
||||
['key', 'validateKeyExpiration'],
|
||||
['key', EmailActivationKeyValidator::class],
|
||||
];
|
||||
}
|
||||
|
||||
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
|
||||
*/
|
||||
|
33
api/validators/EmailActivationKeyValidator.php
Normal file
33
api/validators/EmailActivationKeyValidator.php
Normal 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);
|
||||
}
|
||||
|
||||
}
|
@ -4,10 +4,10 @@ modules:
|
||||
- Filesystem
|
||||
- Yii2
|
||||
- tests\codeception\common\_support\FixtureHelper
|
||||
- REST:
|
||||
depends: Yii2
|
||||
- Redis
|
||||
- AMQP
|
||||
- REST:
|
||||
depends: Yii2
|
||||
config:
|
||||
Yii2:
|
||||
configFile: '../config/api/functional.php'
|
||||
|
@ -1,6 +1 @@
|
||||
# Codeception Test Suite Configuration
|
||||
|
||||
# suite for unit (internal) tests.
|
||||
# RUN `build` COMMAND AFTER ADDING/REMOVING MODULES.
|
||||
|
||||
class_name: UnitTester
|
||||
|
@ -3,7 +3,6 @@ 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;
|
||||
@ -17,75 +16,10 @@ class KeyConfirmationFormTest extends DbTestCase {
|
||||
|
||||
public function fixtures() {
|
||||
return [
|
||||
'emailActivations' => [
|
||||
'class' => EmailActivationFixture::class,
|
||||
'dataFile' => '@tests/codeception/common/fixtures/data/email-activations.php',
|
||||
],
|
||||
'emailActivations' => EmailActivationFixture::class,
|
||||
];
|
||||
}
|
||||
|
||||
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() {
|
||||
$this->specify('should return model, based on passed key', function() {
|
||||
$model = new KeyConfirmationForm();
|
||||
|
@ -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();
|
||||
});
|
||||
}
|
||||
|
||||
}
|
17
tests/codeception/common/_support/ProtectedCaller.php
Normal file
17
tests/codeception/common/_support/ProtectedCaller.php
Normal 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);
|
||||
}
|
||||
|
||||
}
|
@ -8,6 +8,8 @@ class EmailActivationFixture extends ActiveFixture {
|
||||
|
||||
public $modelClass = EmailActivation::class;
|
||||
|
||||
public $dataFile = '@tests/codeception/common/fixtures/data/email-activations.php';
|
||||
|
||||
public $depends = [
|
||||
AccountFixture::class,
|
||||
];
|
||||
|
Loading…
Reference in New Issue
Block a user