Отрефакторены тесты

Удалено тестовое окружение acceptance
Удалена часть потенциально ненужных тестов
Добавлена логика для формы регистрации
Добавлена таблица для хранения ключей активации по E-mail
Добавлены тесты для формы регистрации
Реорганизован роутинг
Добавлен компонент для ReCaptcha2
This commit is contained in:
ErickSkrauch
2016-01-15 12:21:27 +03:00
parent 45c31dfbbe
commit 44aaea2c08
56 changed files with 1075 additions and 972 deletions

View File

@@ -1,16 +0,0 @@
<?php
return [
[
'id' => 1,
'uuid' => 'df936908-b2e1-544d-96f8-2977ec213022',
'username' => 'Admin',
'email' => 'admin@ely.by',
'password_hash' => '$2y$13$CXT0Rkle1EMJ/c1l5bylL.EylfmQ39O5JlHJVFpNn618OUS1HwaIi', # password_0
'password_hash_strategy' => 1,
'password_reset_token' => NULL,
'auth_key' => 'iwTNae9t34OmnK6l4vT4IeaTk-YWI2Rv',
'status' => 10,
'created_at' => 1451775316,
'updated_at' => 1451775316,
],
];

View File

@@ -0,0 +1,61 @@
<?php
namespace tests\codeception\api\models;
use api\models\LoginForm;
use Codeception\Specify;
use tests\codeception\api\unit\DbTestCase;
use tests\codeception\common\fixtures\AccountFixture;
use Yii;
class LoginFormTest extends DbTestCase {
use Specify;
protected function tearDown() {
Yii::$app->user->logout();
parent::tearDown();
}
public function fixtures() {
return [
'account' => [
'class' => AccountFixture::className(),
'dataFile' => '@tests/codeception/common/fixtures/data/accounts.php',
],
];
}
protected function createModel($login = '', $password = '') {
return new LoginForm([
'login' => $login,
'password' => $password,
]);
}
public function testIncorrectLogin() {
$model = $this->createModel('not-esist-login', 'fully-invalid-password');
$this->specify('get errors and don\'t log in into account with wrong credentials', function () use ($model) {
expect('model should not login user', $model->login())->false();
expect('error messages should be set', $model->errors)->notEmpty();
expect('user should not be logged in', Yii::$app->user->isGuest)->true();
});
}
public function testLoginByUsernameCorrect() {
$model = $this->createModel('Admin', 'password_0');
$this->specify('user should be able to login with correct username and password', function () use ($model) {
expect('model should login user', $model->login())->true();
expect('error message should not be set', $model->errors)->isEmpty();
expect('user should be logged in', Yii::$app->user->isGuest)->false();
});
}
public function testLoginByEmailCorrect() {
$model = $this->createModel('admin@ely.by', 'password_0');
$this->specify('user should be able to login with correct email and password', function () use ($model) {
expect('model should login user', $model->login())->true();
expect('error message should not be set', $model->errors)->isEmpty();
expect('user should be logged in', Yii::$app->user->isGuest)->false();
});
}
}

View File

@@ -0,0 +1,111 @@
<?php
namespace tests\codeception\api\models;
use api\models\RegistrationForm;
use Codeception\Specify;
use common\models\Account;
use common\models\EmailActivation;
use tests\codeception\api\unit\DbTestCase;
use tests\codeception\common\fixtures\AccountFixture;
use Yii;
class RegistrationFormTest 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 [
'account' => [
'class' => AccountFixture::className(),
'dataFile' => '@tests/codeception/common/fixtures/data/accounts.php',
],
];
}
public function testNotCorrectRegistration() {
$model = new RegistrationForm([
'username' => 'valid_nickname',
'email' => 'correct-email@ely.by',
'password' => 'enough-length',
'rePassword' => 'password',
'rulesAgreement' => true,
]);
$this->specify('username and email in use, passwords not math - model is not created', function() use ($model) {
expect($model->signup())->null();
expect($model->getErrors())->notEmpty();
expect_file($this->getMessageFile())->notExists();
});
}
public function testUsernameValidators() {
$shouldBeValid = [
'русский_ник', 'русский_ник_на_грани!', 'numbers1132', '*__*-Stars-*__*', '1-_.!?#$%^&*()[]', '[ESP]Эрик',
'Свят_помидор;', роблена_ў_беларусі:)',
];
$shouldBeInvalid = [
'nick@name', 'spaced nick', ' ', 'sh', ' sh ',
];
foreach($shouldBeValid as $nickname) {
$model = new RegistrationForm([
'username' => $nickname,
]);
expect($nickname . ' passed validation', $model->validate(['username']))->true();
}
foreach($shouldBeInvalid as $nickname) {
$model = new RegistrationForm([
'username' => $nickname,
]);
expect($nickname . ' fail validation', $model->validate('username'))->false();
}
}
public function testCorrectSignup() {
$model = new RegistrationForm([
'username' => 'some_username',
'email' => 'some_email@example.com',
'password' => 'some_password',
'rePassword' => 'some_password',
'rulesAgreement' => true,
]);
$user = $model->signup();
expect('user should be valid', $user)->isInstanceOf(Account::class);
expect('password should be correct', $user->validatePassword('some_password'))->true();
expect('user model exists in database', Account::find()->andWhere([
'username' => 'some_username',
'email' => 'some_email@example.com',
])->exists())->true();
expect('email activation code exists in database', EmailActivation::find()->andWhere([
'account_id' => $user->id,
'type' => EmailActivation::TYPE_REGISTRATION_EMAIL_CONFIRMATION,
])->exists())->true();
expect_file('message file exists', $this->getMessageFile())->exists();
}
private function getMessageFile() {
/** @var \yii\swiftmailer\Mailer $mailer */
$mailer = Yii::$app->mailer;
return Yii::getAlias($mailer->fileTransportPath) . '/testing_message.eml';
}
}

View File

@@ -1,51 +0,0 @@
<?php
namespace tests\codeception\api\unit\models;
use tests\codeception\api\unit\DbTestCase;
use tests\codeception\common\fixtures\UserFixture;
use Codeception\Specify;
use api\models\SignupForm;
class SignupFormTest extends DbTestCase
{
use Specify;
public function testCorrectSignup()
{
$model = new SignupForm([
'username' => 'some_username',
'email' => 'some_email@example.com',
'password' => 'some_password',
]);
$user = $model->signup();
$this->assertInstanceOf('common\models\User', $user, 'user should be valid');
expect('email should be correct', $user->email)->equals('some_email@example.com');
expect('password should be correct', $user->validatePassword('some_password'))->true();
}
public function testNotCorrectSignup()
{
$model = new SignupForm([
'username' => 'troy.becker',
'email' => 'nicolas.dianna@hotmail.com',
'password' => 'some_password',
]);
expect('username and email are in use, user should not be created', $model->signup())->null();
}
public function fixtures()
{
return [
'user' => [
'class' => UserFixture::className(),
'dataFile' => '@tests/codeception/api/unit/fixtures/data/models/user.php',
],
];
}
}

View File

@@ -1,127 +0,0 @@
<?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 fixtures() {
return [
'account' => [
'class' => AccountFixture::className(),
'dataFile' => '@tests/codeception/api/unit/fixtures/data/models/accounts.php'
],
];
}
protected function createModel($login = '', $password = '') {
return new AuthenticationForm([
'login' => $login,
'password' => $password,
]);
}
public function testLoginEmailOrUsername() {
$model = $this->createModel();
$this->specify('error.login_required expected if login is not set', function() use ($model) {
expect($model->login())->false();
expect($model->getErrors('login'))->equals(['error.login_required']);
expect(Yii::$app->user->isGuest)->true();
});
$model = $this->createModel('non-exist-username');
$this->specify('error.login_not_exist expected if username not exists in database', function() use ($model) {
expect($model->login())->false();
expect(Yii::$app->user->isGuest)->true();
expect($model->getErrors('login'))->equals(['error.login_not_exist']);
});
$model = $this->createModel('not-exist@user.com');
$this->specify('error.login_not_exist expected if email not exists in database', function() use ($model) {
expect($model->login())->false();
expect(Yii::$app->user->isGuest)->true();
expect($model->getErrors('login'))->equals(['error.login_not_exist']);
});
$model = $this->createModel('Admin');
$this->specify('no errors on login field if username is correct and exists in database', function() use ($model) {
expect($model->login())->false();
expect(Yii::$app->user->isGuest)->true();
expect($model->getErrors('login'))->isEmpty();
});
$model = $this->createModel('admin@ely.by');
$this->specify('no errors on login field if email is correct and exists in database', function() use ($model) {
expect($model->login())->false();
expect(Yii::$app->user->isGuest)->true();
expect($model->getErrors('login'))->isEmpty();
});
}
public function testLoginPassword() {
$model = $this->createModel();
$this->specify('password don\'t has errors if email or username not set', function() use ($model) {
expect($model->login())->false();
expect(Yii::$app->user->isGuest)->true();
expect($model->getErrors('password'))->isEmpty();
});
$model = $this->createModel('non-exist-username', 'random-password');
$this->specify('password don\'t has errors if username not exists in database', function() use ($model) {
expect($model->login())->false();
expect(Yii::$app->user->isGuest)->true();
expect($model->getErrors('password'))->isEmpty();
});
$model = $this->createModel('not-exist@user.com', 'random-password');
$this->specify('password don\'t has errors if email not exists in database', function() use ($model) {
expect($model->login())->false();
expect(Yii::$app->user->isGuest)->true();
expect($model->getErrors('password'))->isEmpty();
});
$model = $this->createModel('admin@ely.by', 'wrong-password');
$this->specify('error.password_incorrect expected if email correct, but password wrong', function() use ($model) {
expect($model->login())->false();
expect(Yii::$app->user->isGuest)->true();
expect($model->getErrors('password'))->equals(['error.password_incorrect']);
});
$model = $this->createModel('Admin', 'wrong-password');
$this->specify('error.password_incorrect expected if username correct, but password wrong', function() use ($model) {
expect($model->login())->false();
expect(Yii::$app->user->isGuest)->true();
expect($model->getErrors('password'))->equals(['error.password_incorrect']);
});
}
public function testLoginByUsernameCorrect() {
$model = $this->createModel('Admin', 'password_0');
$this->specify('user should be able to login with correct username and password', function () use ($model) {
expect('model should login user', $model->login())->true();
expect('error message should not be set', $model->errors)->isEmpty();
expect('user should be logged in', Yii::$app->user->isGuest)->false();
});
}
public function testLoginByEmailCorrect() {
$model = $this->createModel('admin@ely.by', 'password_0');
$this->specify('user should be able to login with correct email and password', function () use ($model) {
expect('model should login user', $model->login())->true();
expect('error message should not be set', $model->errors)->isEmpty();
expect('user should be logged in', Yii::$app->user->isGuest)->false();
});
}
}

View File

@@ -0,0 +1,42 @@
<?php
namespace tests\codeception\api\traits;
use api\traits\ApiNormalize;
use Codeception\Specify;
use Codeception\TestCase\Test;
class ApiNormalizeTestClass {
use ApiNormalize;
}
/**
* @property \tests\codeception\api\UnitTester $actor
*/
class ApiNormalizerTest extends Test {
use Specify;
public function testNormalizeModelErrors() {
$object = new ApiNormalizeTestClass();
$this->specify('', function() use ($object) {
$normalized = $object->normalizeModelErrors([
'rulesAgreement' => [
'error.you_must_accept_rules',
],
'email' => [
'error.email_required',
],
'username' => [
'error.username_too_short',
'error.username_not_unique',
],
]);
expect($normalized)->equals([
'rulesAgreement' => 'error.you_must_accept_rules',
'email' => 'error.email_required',
'username' => 'error.username_too_short',
]);
});
}
}