Образован trait AccountFinder для поиска пользователя по его нику\мылу

Модель EmailActivation теперь умеет автоматически создавать своих правильных потомков по соответствующему типу
Добавлена форма восстановления пароля и её обработчик (без контроллера)
This commit is contained in:
ErickSkrauch
2016-05-10 22:40:06 +03:00
parent ce2e68faf6
commit a29cb76cbf
21 changed files with 664 additions and 62 deletions

View File

@@ -31,7 +31,7 @@ class ConfirmEmailFormTest extends DbTestCase {
}
public function testConfirm() {
$fixture = $this->emailActivations[0];
$fixture = $this->emailActivations['freshRegistrationConfirmation'];
$model = $this->createModel($fixture['key']);
$this->specify('expect true result', function() use ($model, $fixture) {
expect('model return successful result', $model->confirm())->notEquals(false);

View File

@@ -0,0 +1,149 @@
<?php
namespace codeception\api\unit\models;
use api\models\ForgotPasswordForm;
use Codeception\Specify;
use common\models\EmailActivation;
use tests\codeception\api\unit\DbTestCase;
use tests\codeception\common\fixtures\AccountFixture;
use tests\codeception\common\fixtures\EmailActivationFixture;
use Yii;
/**
* @property array $accounts
* @property array $emailActivations
*/
class ForgotPasswordFormTest extends DbTestCase {
use Specify;
protected function setUp() {
parent::setUp();
/** @var \yii\swiftmailer\Mailer $mailer */
$mailer = Yii::$app->mailer;
$mailer->fileTransportCallback = function () {
return 'testing_message.eml';
};
}
protected function tearDown() {
if (file_exists($this->getMessageFile())) {
unlink($this->getMessageFile());
}
parent::tearDown();
}
public function fixtures() {
return [
'accounts' => [
'class' => AccountFixture::class,
'dataFile' => '@tests/codeception/common/fixtures/data/accounts.php',
],
'emailActivations' => [
'class' => EmailActivationFixture::class,
'dataFile' => '@tests/codeception/common/fixtures/data/email-activations.php',
],
];
}
public function testValidateAccount() {
$this->specify('error.login_not_exist if login is invalid', function() {
$model = new ForgotPasswordForm(['login' => 'unexist']);
$model->validateLogin('login');
expect($model->getErrors('login'))->equals(['error.login_not_exist']);
});
$this->specify('empty errors if login is exists', function() {
$model = new ForgotPasswordForm(['login' => $this->accounts['admin']['username']]);
$model->validateLogin('login');
expect($model->getErrors('login'))->isEmpty();
});
}
public function testValidateActivity() {
$this->specify('error.account_not_activated if account is not confirmed', function() {
$model = new ForgotPasswordForm(['login' => $this->accounts['not-activated-account']['username']]);
$model->validateActivity('login');
expect($model->getErrors('login'))->equals(['error.account_not_activated']);
});
$this->specify('empty errors if login is exists', function() {
$model = new ForgotPasswordForm(['login' => $this->accounts['admin']['username']]);
$model->validateLogin('login');
expect($model->getErrors('login'))->isEmpty();
});
}
public function testValidateFrequency() {
$this->specify('error.account_not_activated if recently was message', function() {
$model = new DummyForgotPasswordForm([
'login' => $this->accounts['admin']['username'],
'key' => $this->emailActivations['freshPasswordRecovery']['key'],
]);
$model->validateFrequency('login');
expect($model->getErrors('login'))->equals(['error.email_frequency']);
});
$this->specify('empty errors if email was sent a long time ago', function() {
$model = new DummyForgotPasswordForm([
'login' => $this->accounts['admin']['username'],
'key' => $this->emailActivations['oldPasswordRecovery']['key'],
]);
$model->validateFrequency('login');
expect($model->getErrors('login'))->isEmpty();
});
$this->specify('empty errors if previous confirmation model not founded', function() {
$model = new DummyForgotPasswordForm([
'login' => $this->accounts['admin']['username'],
'key' => 'invalid-key',
]);
$model->validateFrequency('login');
expect($model->getErrors('login'))->isEmpty();
});
}
public function testForgotPassword() {
$this->specify('successfully send message with restore password key', function() {
$model = new ForgotPasswordForm(['login' => $this->accounts['admin']['username']]);
expect($model->forgotPassword())->true();
expect($model->getEmailActivation())->notNull();
expect_file($this->getMessageFile())->exists();
});
}
public function testForgotPasswordResend() {
$this->specify('successfully renew and send message with restore password key', function() {
$model = new ForgotPasswordForm([
'login' => $this->accounts['account-with-expired-forgot-password-message']['username'],
]);
$callTime = time();
expect($model->forgotPassword())->true();
$emailActivation = $model->getEmailActivation();
expect($emailActivation)->notNull();
expect($emailActivation->created_at)->greaterOrEquals($callTime);
expect_file($this->getMessageFile())->exists();
});
}
private function getMessageFile() {
/** @var \yii\swiftmailer\Mailer $mailer */
$mailer = Yii::$app->mailer;
return Yii::getAlias($mailer->fileTransportPath) . '/testing_message.eml';
}
}
class DummyForgotPasswordForm extends ForgotPasswordForm {
public $key;
public function getEmailActivation() {
return EmailActivation::findOne($this->key);
}
}

View File

@@ -84,18 +84,6 @@ class LoginFormTest extends DbTestCase {
});
}
public function testGetLoginAttribute() {
$this->specify('email if login look like email value', function() {
$model = new DummyLoginForm(['login' => 'erickskrauch@ely.by']);
expect($model->getLoginAttribute())->equals('email');
});
$this->specify('username in any other case', function() {
$model = new DummyLoginForm(['login' => 'erickskrauch']);
expect($model->getLoginAttribute())->equals('username');
});
}
}
class DummyLoginForm extends LoginForm {

View File

@@ -3,6 +3,7 @@ namespace tests\codeception\api\models;
use api\models\RepeatAccountActivationForm;
use Codeception\Specify;
use common\models\EmailActivation;
use tests\codeception\api\unit\DbTestCase;
use tests\codeception\common\fixtures\AccountFixture;
use tests\codeception\common\fixtures\EmailActivationFixture;
@@ -67,14 +68,17 @@ class RepeatAccountActivationFormTest extends DbTestCase {
public function testValidateExistsActivation() {
$this->specify('error.recently_sent_message if passed email has recently sent message', function() {
$model = new RepeatAccountActivationForm(['email' => $this->accounts['not-activated-account']['email']]);
$model = new DummyRepeatAccountActivationForm([
'emailKey' => $this->activations['freshRegistrationConfirmation']['key'],
]);
$model->validateExistsActivation('email');
expect($model->getErrors('email'))->equals(['error.recently_sent_message']);
});
$this->specify('no errors if passed email has expired activation message', function() {
$email = $this->accounts['not-activated-account-with-expired-message']['email'];
$model = new RepeatAccountActivationForm(['email' => $email]);
$model = new DummyRepeatAccountActivationForm([
'emailKey' => $this->activations['oldRegistrationConfirmation']['key'],
]);
$model->validateExistsActivation('email');
expect($model->getErrors('email'))->isEmpty();
});
@@ -91,7 +95,7 @@ class RepeatAccountActivationFormTest extends DbTestCase {
$email = $this->accounts['not-activated-account-with-expired-message']['email'];
$model = new RepeatAccountActivationForm(['email' => $email]);
expect($model->sendRepeatMessage())->true();
expect($model->getActiveActivation())->notNull();
expect($model->getActivation())->notNull();
expect_file($this->getMessageFile())->exists();
});
}
@@ -104,3 +108,13 @@ class RepeatAccountActivationFormTest extends DbTestCase {
}
}
class DummyRepeatAccountActivationForm extends RepeatAccountActivationForm {
public $emailKey;
public function getActivation() {
return EmailActivation::findOne($this->emailKey);
}
}

View File

@@ -53,7 +53,7 @@ class KeyConfirmationFormTest extends DbTestCase {
}
public function testCorrectKey() {
$model = $this->createModel($this->emailActivations[0]['key']);
$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();
});

View File

@@ -0,0 +1,67 @@
<?php
namespace tests\codeception\api\traits;
use api\traits\AccountFinder;
use Codeception\Specify;
use common\models\Account;
use tests\codeception\api\unit\DbTestCase;
use tests\codeception\common\fixtures\AccountFixture;
/**
* @property \tests\codeception\api\UnitTester $actor
* @property array $accounts
*/
class AccountFinderTest extends DbTestCase {
use Specify;
public function fixtures() {
return [
'accounts' => [
'class' => AccountFixture::class,
'dataFile' => '@tests/codeception/common/fixtures/data/accounts.php',
],
];
}
public function testGetAccount() {
$this->specify('founded account for passed login data', function() {
$model = new AccountFinderTestTestClass();
$model->login = $this->accounts['admin']['email'];
$account = $model->getAccount();
expect($account)->isInstanceOf(Account::class);
expect($account->id)->equals($this->accounts['admin']['id']);
});
$this->specify('null, if account not founded', function() {
$model = new AccountFinderTestTestClass();
$model->login = 'unexpected';
expect($account = $model->getAccount())->null();
});
}
public function testGetLoginAttribute() {
$this->specify('if login look like email value, then \'email\'', function() {
$model = new AccountFinderTestTestClass();
$model->login = 'erickskrauch@ely.by';
expect($model->getLoginAttribute())->equals('email');
});
$this->specify('username in any other case', function() {
$model = new AccountFinderTestTestClass();
$model->login = 'erickskrauch';
expect($model->getLoginAttribute())->equals('username');
});
}
}
class AccountFinderTestTestClass {
use AccountFinder;
public $login;
public function getLogin() {
return $this->login;
}
}

View File

@@ -51,4 +51,28 @@ return [
'created_at' => 1457890086,
'updated_at' => 1457890086,
],
'account-with-fresh-forgot-password-message' => [
'id' => 5,
'uuid' => '4aaf4f00-3b5b-4d36-9252-9e8ee0c86679',
'username' => 'Notch',
'email' => 'notch@mojang.com',
'password_hash' => '$2y$13$2rYkap5T6jG8z/mMK8a3Ou6aZxJcmAaTha6FEuujvHEmybSHRzW5e', # password_0
'password_hash_strategy' => \common\models\Account::PASS_HASH_STRATEGY_YII2,
'password_reset_token' => null,
'status' => \common\models\Account::STATUS_ACTIVE,
'created_at' => 1462891432,
'updated_at' => 1462891432,
],
'account-with-expired-forgot-password-message' => [
'id' => 6,
'uuid' => '26187ae7-bc96-421f-9766-6517f8ee52b7',
'username' => '23derevo',
'email' => '23derevo@gmail.com',
'password_hash' => '$2y$13$2rYkap5T6jG8z/mMK8a3Ou6aZxJcmAaTha6FEuujvHEmybSHRzW5e', # password_0
'password_hash_strategy' => \common\models\Account::PASS_HASH_STRATEGY_YII2,
'password_reset_token' => null,
'status' => \common\models\Account::STATUS_ACTIVE,
'created_at' => 1462891612,
'updated_at' => 1462891612,
],
];

View File

@@ -1,15 +1,27 @@
<?php
return [
[
'freshRegistrationConfirmation' => [
'key' => 'HABGCABHJ1234HBHVD',
'account_id' => 3,
'type' => \common\models\EmailActivation::TYPE_REGISTRATION_EMAIL_CONFIRMATION,
'created_at' => time(),
],
[
'oldRegistrationConfirmation' => [
'key' => 'H23HBDCHHAG2HGHGHS',
'account_id' => 4,
'type' => \common\models\EmailActivation::TYPE_REGISTRATION_EMAIL_CONFIRMATION,
'created_at' => time() - \api\models\RepeatAccountActivationForm::REPEAT_FREQUENCY - 10,
'created_at' => time() - (new \common\models\confirmations\RegistrationConfirmation())->repeatTimeout - 10,
],
'freshPasswordRecovery' => [
'key' => 'H24HBDCHHAG2HGHGHS',
'account_id' => 5,
'type' => \common\models\EmailActivation::TYPE_FORGOT_PASSWORD_KEY,
'created_at' => time(),
],
'oldPasswordRecovery' => [
'key' => 'H25HBDCHHAG2HGHGHS',
'account_id' => 6,
'type' => \common\models\EmailActivation::TYPE_FORGOT_PASSWORD_KEY,
'created_at' => time() - (new \common\models\confirmations\RecoverPassword())->repeatTimeout - 10,
],
];

View File

@@ -0,0 +1,34 @@
<?php
namespace codeception\common\unit\models;
use Codeception\Specify;
use common\models\confirmations\RecoverPassword;
use common\models\confirmations\RegistrationConfirmation;
use common\models\EmailActivation;
use tests\codeception\common\fixtures\EmailActivationFixture;
use tests\codeception\console\unit\DbTestCase;
class EmailActivationTest extends DbTestCase {
use Specify;
public function fixtures() {
return [
'emailActivations' => [
'class' => EmailActivationFixture::class,
'dataFile' => '@tests/codeception/common/fixtures/data/email-activations.php',
],
];
}
public function testInstantiate() {
$this->specify('return valid model type', function() {
expect(EmailActivation::findOne([
'type' => EmailActivation::TYPE_REGISTRATION_EMAIL_CONFIRMATION,
]))->isInstanceOf(RegistrationConfirmation::class);
expect(EmailActivation::findOne([
'type' => EmailActivation::TYPE_FORGOT_PASSWORD_KEY,
]))->isInstanceOf(RecoverPassword::class);
});
}
}