mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Реализованы формы для шагов смены E-mail адреса, покрыты unit-тестами
У EmailActivation добавлено поле $_data и дописано поведение для работы с ним Упрощено подключение фикстур для EmailActivations
This commit is contained in:
@@ -17,10 +17,7 @@ class ConfirmEmailFormTest extends DbTestCase {
|
||||
|
||||
public function fixtures() {
|
||||
return [
|
||||
'emailActivations' => [
|
||||
'class' => EmailActivationFixture::class,
|
||||
'dataFile' => '@tests/codeception/common/fixtures/data/email-activations.php',
|
||||
],
|
||||
'emailActivations' => EmailActivationFixture::class,
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -39,10 +39,7 @@ class ForgotPasswordFormTest extends DbTestCase {
|
||||
'class' => AccountFixture::class,
|
||||
'dataFile' => '@tests/codeception/common/fixtures/data/accounts.php',
|
||||
],
|
||||
'emailActivations' => [
|
||||
'class' => EmailActivationFixture::class,
|
||||
'dataFile' => '@tests/codeception/common/fixtures/data/email-activations.php',
|
||||
],
|
||||
'emailActivations' => EmailActivationFixture::class,
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -17,10 +17,7 @@ class RecoverPasswordFormTest extends DbTestCase {
|
||||
|
||||
public function fixtures() {
|
||||
return [
|
||||
'emailActivations' => [
|
||||
'class' => EmailActivationFixture::class,
|
||||
'dataFile' => '@tests/codeception/common/fixtures/data/email-activations.php',
|
||||
],
|
||||
'emailActivations' => EmailActivationFixture::class,
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -39,10 +39,7 @@ class RepeatAccountActivationFormTest extends DbTestCase {
|
||||
'class' => AccountFixture::class,
|
||||
'dataFile' => '@tests/codeception/common/fixtures/data/accounts.php',
|
||||
],
|
||||
'activations' => [
|
||||
'class' => EmailActivationFixture::class,
|
||||
'dataFile' => '@tests/codeception/common/fixtures/data/email-activations.php',
|
||||
],
|
||||
'activations' => EmailActivationFixture::class,
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
namespace codeception\api\unit\models\profile\ChangeEmail;
|
||||
|
||||
use api\models\profile\ChangeEmail\ConfirmNewEmailForm;
|
||||
use Codeception\Specify;
|
||||
use common\models\Account;
|
||||
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 AccountFixture $accounts
|
||||
* @property EmailActivationFixture $emailActivations
|
||||
*/
|
||||
class ConfirmNewEmailFormTest extends DbTestCase {
|
||||
use Specify;
|
||||
|
||||
public function fixtures() {
|
||||
return [
|
||||
'accounts' => [
|
||||
'class' => AccountFixture::class,
|
||||
'dataFile' => '@tests/codeception/common/fixtures/data/accounts.php',
|
||||
],
|
||||
'emailActivations' => EmailActivationFixture::class,
|
||||
];
|
||||
}
|
||||
|
||||
public function testChangeEmail() {
|
||||
$this->specify('successfully change account email', function() {
|
||||
/** @var Account $account */
|
||||
$account = Account::findOne($this->accounts['account-with-change-email-finish-state']['id']);
|
||||
$model = new ConfirmNewEmailForm($account, [
|
||||
'key' => $this->emailActivations['newEmailConfirmation']['key'],
|
||||
]);
|
||||
expect($model->changeEmail())->true();
|
||||
expect(EmailActivation::findOne([
|
||||
'account_id' => $account->id,
|
||||
'type' => EmailActivation::TYPE_NEW_EMAIL_CONFIRMATION,
|
||||
]))->null();
|
||||
$data = unserialize($this->emailActivations['newEmailConfirmation']['_data']);
|
||||
expect($account->email)->equals($data['newEmail']);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
namespace codeception\api\unit\models\profile\ChangeEmail;
|
||||
|
||||
use api\models\profile\ChangeEmail\InitStateForm;
|
||||
use Codeception\Specify;
|
||||
use common\models\Account;
|
||||
use common\models\confirmations\CurrentEmailConfirmation;
|
||||
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 AccountFixture $accounts
|
||||
* @property EmailActivationFixture $emailActivations
|
||||
*/
|
||||
class InitStateFormTest extends DbTestCase {
|
||||
use Specify;
|
||||
|
||||
public 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' => EmailActivationFixture::class,
|
||||
];
|
||||
}
|
||||
|
||||
public function testValidateAccountPasswordHashStrategy() {
|
||||
$this->specify('we cannot change password on old password hash strategy', function() {
|
||||
$account = new Account();
|
||||
$account->password_hash_strategy = Account::PASS_HASH_STRATEGY_OLD_ELY;
|
||||
$model = new InitStateForm($account);
|
||||
$model->validateAccountPasswordHashStrategy('email');
|
||||
expect($model->getErrors('email'))->equals(['error.old_hash_strategy']);
|
||||
});
|
||||
|
||||
$this->specify('no errors on modern password hash strategy', function() {
|
||||
$account = new Account();
|
||||
$account->password_hash_strategy = Account::PASS_HASH_STRATEGY_YII2;
|
||||
$model = new InitStateForm($account);
|
||||
$model->validateAccountPasswordHashStrategy('email');
|
||||
expect($model->getErrors('email'))->isEmpty();
|
||||
});
|
||||
}
|
||||
|
||||
public function testCreateCode() {
|
||||
$this->specify('create valid code and store it to database', function() {
|
||||
/** @var Account $account */
|
||||
$account = Account::findOne($this->accounts['admin']['id']);
|
||||
$model = new InitStateForm($account);
|
||||
$activationModel = $model->createCode();
|
||||
expect($activationModel)->isInstanceOf(CurrentEmailConfirmation::class);
|
||||
expect($activationModel->account_id)->equals($account->id);
|
||||
expect(EmailActivation::findOne($activationModel->key))->notNull();
|
||||
});
|
||||
}
|
||||
|
||||
public function testSendCurrentEmailConfirmation() {
|
||||
$this->specify('send email', function() {
|
||||
/** @var Account $account */
|
||||
$account = Account::findOne($this->accounts['admin']['id']);
|
||||
$model = new InitStateForm($account);
|
||||
expect($model->sendCurrentEmailConfirmation())->true();
|
||||
expect(EmailActivation::find()->andWhere([
|
||||
'account_id' => $account->id,
|
||||
'type' => EmailActivation::TYPE_CURRENT_EMAIL_CONFIRMATION,
|
||||
])->exists())->true();
|
||||
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';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
namespace codeception\api\unit\models\profile\ChangeEmail;
|
||||
|
||||
use api\models\profile\ChangeEmail\NewEmailForm;
|
||||
use Codeception\Specify;
|
||||
use common\models\Account;
|
||||
use common\models\confirmations\NewEmailConfirmation;
|
||||
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 AccountFixture $accounts
|
||||
* @property EmailActivationFixture $emailActivations
|
||||
*/
|
||||
class NewEmailFormTest extends DbTestCase {
|
||||
use Specify;
|
||||
|
||||
public 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' => EmailActivationFixture::class,
|
||||
];
|
||||
}
|
||||
|
||||
public function testCreateCode() {
|
||||
$this->specify('create valid code and store it to database', function() {
|
||||
/** @var Account $account */
|
||||
$account = Account::findOne($this->accounts['admin']['id']);
|
||||
$model = new NewEmailForm($account);
|
||||
$model->email = 'my-new-email@ely.by';
|
||||
$activationModel = $model->createCode();
|
||||
expect($activationModel)->isInstanceOf(NewEmailConfirmation::class);
|
||||
expect($activationModel->account_id)->equals($account->id);
|
||||
expect($activationModel->newEmail)->equals($model->email);
|
||||
expect(EmailActivation::findOne($activationModel->key))->notNull();
|
||||
});
|
||||
}
|
||||
|
||||
public function testSendNewEmailConfirmation() {
|
||||
$this->specify('send email', function() {
|
||||
/** @var Account $account */
|
||||
$account = Account::findOne($this->accounts['admin']['id']);
|
||||
/** @var NewEmailForm $model */
|
||||
$model = new NewEmailForm($account, [
|
||||
'key' => $this->emailActivations['currentEmailConfirmation']['key'],
|
||||
'email' => 'my-new-email@ely.by',
|
||||
]);
|
||||
expect($model->sendNewEmailConfirmation())->true();
|
||||
expect(EmailActivation::findOne($this->emailActivations['currentEmailConfirmation']['key']))->null();
|
||||
expect(EmailActivation::findOne([
|
||||
'account_id' => $account->id,
|
||||
'type' => EmailActivation::TYPE_NEW_EMAIL_CONFIRMATION,
|
||||
]))->notNull();
|
||||
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';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -9,7 +9,7 @@ use tests\codeception\common\fixtures\AccountFixture;
|
||||
use Yii;
|
||||
|
||||
/**
|
||||
* @property array $accounts
|
||||
* @property AccountFixture $accounts
|
||||
*/
|
||||
class ChangePasswordFormTest extends DbTestCase {
|
||||
use Specify;
|
||||
|
||||
Reference in New Issue
Block a user