Описана базовая миграция, добавлена модель аккаунта, добавлена модель авторизации, написаны первичные тесты для этой модели, добавлен модуль авторизации, настроен базовый контроллер. Короче много чего сделано

This commit is contained in:
ErickSkrauch
2016-01-03 03:18:37 +03:00
parent 841303b8ab
commit 7b650e2654
40 changed files with 694 additions and 292 deletions

View File

@@ -0,0 +1,21 @@
<?php
namespace tests\codeception\api\_pages;
use yii\codeception\BasePage;
/**
* Represents loging page
* @property \tests\codeception\api\FunctionalTester $actor
*/
class LoginRoute extends BasePage {
public $route = 'login/authentication/login-info';
public function login($email, $password) {
$this->actor->sendPOST($this->getUrl(), [
'email' => $email,
'password' => $password,
]);
}
}

View File

@@ -1,13 +1,13 @@
<?php
use tests\codeception\api\_pages\LoginRoute;
use tests\codeception\api\AcceptanceTester;
use tests\codeception\common\_pages\LoginPage;
/* @var $scenario Codeception\Scenario */
$I = new AcceptanceTester($scenario);
$I->wantTo('ensure login page works');
$loginPage = LoginPage::openBy($I);
$loginPage = LoginRoute::openBy($I);
$I->amGoingTo('submit login form with no data');
$loginPage->login('', '');

View File

@@ -3,7 +3,7 @@
namespace tests\codeception\api\acceptance;
use tests\codeception\api\_pages\SignupPage;
use common\models\User;
use common\models\Account;
class SignupCest
{
@@ -22,7 +22,7 @@ class SignupCest
*/
public function _after($event)
{
User::deleteAll([
Account::deleteAll([
'email' => 'tester.email@example.com',
'username' => 'tester',
]);

View File

@@ -12,6 +12,7 @@ modules:
- Filesystem
- Yii2
- tests\codeception\common\_support\FixtureHelper
- REST
config:
Yii2:
configFile: '../config/api/functional.php'

View File

@@ -1,23 +1,33 @@
<?php
use tests\codeception\api\_pages\LoginRoute;
use tests\codeception\api\FunctionalTester;
use tests\codeception\common\_pages\LoginPage;
/* @var $scenario Codeception\Scenario */
$I = new FunctionalTester($scenario);
$I->wantTo('ensure login page works');
$loginPage = LoginPage::openBy($I);
$loginPage = LoginRoute::openBy($I);
$I->amGoingTo('submit login form with no data');
$loginPage->login('', '');
$I->expectTo('see validations errors');
$I->see('Username cannot be blank.', '.help-block');
$I->see('Password cannot be blank.', '.help-block');
$I->canSeeResponseContainsJson([
'success' => false,
'errors' => [
'email' => [
'error.email_required',
],
'password' => [
'error.password_required',
],
],
]);
/*
$I->amGoingTo('try to login with wrong credentials');
$I->expectTo('see validations errors');
$loginPage->login('admin', 'wrong');
$loginPage->login('', 'wrong');
$I->expectTo('see validations errors');
$I->see('Incorrect username or password.', '.help-block');
@@ -27,3 +37,4 @@ $I->expectTo('see that user is logged');
$I->seeLink('Logout (erau)');
$I->dontSeeLink('Login');
$I->dontSeeLink('Signup');
*/

View File

@@ -3,7 +3,7 @@
namespace tests\codeception\api\functional;
use tests\codeception\api\_pages\SignupPage;
use common\models\User;
use common\models\Account;
class SignupCest
{
@@ -22,7 +22,7 @@ class SignupCest
*/
public function _after($event)
{
User::deleteAll([
Account::deleteAll([
'email' => 'tester.email@example.com',
'username' => 'tester',
]);

View 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,
],
];

View File

@@ -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();

View File

@@ -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();
}

View File

@@ -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'
],
];
}
}