mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Описана базовая миграция, добавлена модель аккаунта, добавлена модель авторизации, написаны первичные тесты для этой модели, добавлен модуль авторизации, настроен базовый контроллер. Короче много чего сделано
This commit is contained in:
15
tests/codeception/api/unit/fixtures/data/models/accounts.php
Normal file
15
tests/codeception/api/unit/fixtures/data/models/accounts.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
return [
|
||||
[
|
||||
'id' => 1,
|
||||
'uuid' => 'df936908-b2e1-544d-96f8-2977ec213022',
|
||||
'password_hash' => '$2y$13$CXT0Rkle1EMJ/c1l5bylL.EylfmQ39O5JlHJVFpNn618OUS1HwaIi', # password_0
|
||||
'password_hash_strategy' => 1,
|
||||
'password_reset_token' => NULL,
|
||||
'email' => 'admin@ely.by',
|
||||
'auth_key' => 'iwTNae9t34OmnK6l4vT4IeaTk-YWI2Rv',
|
||||
'status' => 10,
|
||||
'created_at' => 1451775316,
|
||||
'updated_at' => 1451775316,
|
||||
],
|
||||
];
|
||||
@@ -6,7 +6,7 @@ use Yii;
|
||||
use tests\codeception\api\unit\DbTestCase;
|
||||
use api\models\PasswordResetRequestForm;
|
||||
use tests\codeception\common\fixtures\UserFixture;
|
||||
use common\models\User;
|
||||
use common\models\Account;
|
||||
use Codeception\Specify;
|
||||
|
||||
class PasswordResetRequestFormTest extends DbTestCase
|
||||
@@ -54,7 +54,7 @@ class PasswordResetRequestFormTest extends DbTestCase
|
||||
{
|
||||
$model = new PasswordResetRequestForm();
|
||||
$model->email = $this->user[0]['email'];
|
||||
$user = User::findOne(['password_reset_token' => $this->user[0]['password_reset_token']]);
|
||||
$user = Account::findOne(['password_reset_token' => $this->user[0]['password_reset_token']]);
|
||||
|
||||
expect('email sent', $model->sendEmail())->true();
|
||||
expect('user has valid token', $user->password_reset_token)->notNull();
|
||||
|
||||
@@ -24,7 +24,6 @@ class SignupFormTest extends DbTestCase
|
||||
|
||||
$this->assertInstanceOf('common\models\User', $user, 'user should be valid');
|
||||
|
||||
expect('username should be correct', $user->username)->equals('some_username');
|
||||
expect('email should be correct', $user->email)->equals('some_email@example.com');
|
||||
expect('password should be correct', $user->validatePassword('some_password'))->true();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
namespace tests\codeception\api\modules\login\models;
|
||||
|
||||
use api\modules\login\models\AuthenticationForm;
|
||||
use Codeception\Specify;
|
||||
use tests\codeception\api\unit\DbTestCase;
|
||||
use tests\codeception\common\fixtures\AccountFixture;
|
||||
use Yii;
|
||||
|
||||
class AuthenticationFormTest extends DbTestCase {
|
||||
|
||||
use Specify;
|
||||
|
||||
protected function tearDown() {
|
||||
Yii::$app->user->logout();
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function testValidateEmail() {
|
||||
$model = new AuthenticationForm();
|
||||
$this->specify('error.email_required expected if email is not set', function() use ($model) {
|
||||
$model->validate(['email']);
|
||||
expect($model->getErrors('email'))->equals(['error.email_required']);
|
||||
});
|
||||
|
||||
$this->specify('error.email_invalid expected if email not correct', function() use ($model) {
|
||||
$model->email = 'wrong-email-string';
|
||||
$model->validate(['email']);
|
||||
expect($model->getErrors('email'))->equals(['error.email_invalid']);
|
||||
|
||||
$model->email = 'wrong@email';
|
||||
$model->validate(['email']);
|
||||
expect($model->getErrors('email'))->equals(['error.email_invalid']);
|
||||
});
|
||||
|
||||
$this->specify('error.email_not_exist expected if email not exists in database', function() use ($model) {
|
||||
$model->email = 'not-exist@user.com';
|
||||
$model->validate(['email']);
|
||||
expect($model->getErrors('email'))->equals(['error.email_not_exist']);
|
||||
});
|
||||
|
||||
$this->specify('no errors if email is correct and exists in database', function() use ($model) {
|
||||
$model->email = 'admin@ely.by';
|
||||
$model->validate(['email']);
|
||||
expect($model->getErrors('email'))->isEmpty();
|
||||
});
|
||||
}
|
||||
|
||||
public function testValidatePassword() {
|
||||
$model = new AuthenticationForm();
|
||||
$this->specify('error.password_required expected if password is not set', function() use ($model) {
|
||||
$model->validate(['password']);
|
||||
expect($model->getErrors('password'))->equals(['error.password_required']);
|
||||
});
|
||||
|
||||
$this->specify('error.password_incorrect expected if password not correct for passed email', function() use ($model) {
|
||||
$model->email = 'non-exist@valid.mail';
|
||||
$model->password = 'wrong-password';
|
||||
$model->validate(['password']);
|
||||
expect('if email incorrect, the error should be displayed in any case,', $model->getErrors('password'))
|
||||
->equals(['error.password_incorrect']);
|
||||
|
||||
$model->email = 'admin@ely.by';
|
||||
$model->password = 'wrong-password';
|
||||
$model->validate(['password']);
|
||||
expect($model->getErrors('password'))->equals(['error.password_incorrect']);
|
||||
});
|
||||
|
||||
$this->specify('no errors if email and password is correct and exists in database', function() use ($model) {
|
||||
$model->email = 'admin@ely.by';
|
||||
$model->password = 'password_0';
|
||||
$model->validate(['password']);
|
||||
expect($model->getErrors('password'))->isEmpty();
|
||||
});
|
||||
}
|
||||
|
||||
public function testLoginNoUser() {
|
||||
$model = new AuthenticationForm([
|
||||
'email' => 'non-exist@valid.mail',
|
||||
'password' => 'not_existing_password',
|
||||
]);
|
||||
|
||||
$this->specify('user should not be able to login, when there is no identity', function () use ($model) {
|
||||
expect('model should not login user', $model->login())->false();
|
||||
expect('user should not be logged in', Yii::$app->user->isGuest)->true();
|
||||
});
|
||||
}
|
||||
|
||||
public function testLoginWrongPassword() {
|
||||
$model = new AuthenticationForm([
|
||||
'email' => 'admin@ely.by',
|
||||
'password' => 'wrong_password',
|
||||
]);
|
||||
|
||||
$this->specify('user should not be able to login with wrong password', function () use ($model) {
|
||||
expect('model should not login user', $model->login())->false();
|
||||
expect('error message should be set', $model->errors)->hasKey('password');
|
||||
expect('user should not be logged in', Yii::$app->user->isGuest)->true();
|
||||
});
|
||||
}
|
||||
|
||||
public function testLoginCorrect() {
|
||||
$model = new AuthenticationForm([
|
||||
'email' => 'admin@ely.by',
|
||||
'password' => 'password_0',
|
||||
]);
|
||||
|
||||
$this->specify('user should be able to login with correct credentials', function () use ($model) {
|
||||
expect('model should login user', $model->login())->true();
|
||||
expect('error message should not be set', $model->errors)->hasntKey('password');
|
||||
expect('user should be logged in', Yii::$app->user->isGuest)->false();
|
||||
});
|
||||
}
|
||||
|
||||
public function fixtures() {
|
||||
return [
|
||||
'user' => [
|
||||
'class' => AccountFixture::className(),
|
||||
'dataFile' => '@tests/codeception/api/unit/fixtures/data/models/accounts.php'
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user