mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Отрефакторены тесты для форм
This commit is contained in:
@ -3,69 +3,111 @@ namespace tests\codeception\api\models;
|
||||
|
||||
use api\models\LoginForm;
|
||||
use Codeception\Specify;
|
||||
use common\models\Account;
|
||||
use tests\codeception\api\unit\DbTestCase;
|
||||
use tests\codeception\common\fixtures\AccountFixture;
|
||||
use Yii;
|
||||
|
||||
/**
|
||||
* @property array $accounts
|
||||
*/
|
||||
class LoginFormTest extends DbTestCase {
|
||||
use Specify;
|
||||
|
||||
protected function tearDown() {
|
||||
Yii::$app->user->logout();
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function fixtures() {
|
||||
return [
|
||||
'accounts' => [
|
||||
'class' => AccountFixture::class,
|
||||
'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();
|
||||
});
|
||||
|
||||
$model = $this->createModel($this->accounts['not-activated-account']['username'], 'password_0');
|
||||
$this->specify('get error if account data valid, but account is not activated', function () use ($model) {
|
||||
expect('model should not login user', $model->login())->false();
|
||||
expect('error messages should be set', $model->errors)->equals([
|
||||
'login' => [
|
||||
'error.account_not_activated',
|
||||
],
|
||||
public function testValidateLogin() {
|
||||
$this->specify('error.login_not_exist if login not exists', function() {
|
||||
$model = new DummyLoginForm([
|
||||
'login' => 'mr-test',
|
||||
'account' => null,
|
||||
]);
|
||||
$model->validateLogin('login');
|
||||
expect($model->getErrors('login'))->equals(['error.login_not_exist']);
|
||||
});
|
||||
|
||||
$this->specify('no errors if login exists', function() {
|
||||
$model = new DummyLoginForm([
|
||||
'login' => 'mr-test',
|
||||
'account' => new Account(),
|
||||
]);
|
||||
$model->validateLogin('login');
|
||||
expect($model->getErrors('login'))->isEmpty();
|
||||
});
|
||||
}
|
||||
|
||||
public function testLoginByUsernameCorrect() {
|
||||
$model = $this->createModel($this->accounts['admin']['username'], 'password_0');
|
||||
$this->specify('user should be able to login with correct username and password', function () use ($model) {
|
||||
public function testValidatePassword() {
|
||||
$this->specify('error.password_incorrect if password invalid', function() {
|
||||
$model = new DummyLoginForm([
|
||||
'password' => '87654321',
|
||||
'account' => new Account(['password' => '12345678']),
|
||||
]);
|
||||
$model->validatePassword('password');
|
||||
expect($model->getErrors('password'))->equals(['error.password_incorrect']);
|
||||
});
|
||||
|
||||
$this->specify('no errors if password valid', function() {
|
||||
$model = new DummyLoginForm([
|
||||
'password' => '12345678',
|
||||
'account' => new Account(['password' => '12345678']),
|
||||
]);
|
||||
$model->validatePassword('password');
|
||||
expect($model->getErrors('password'))->isEmpty();
|
||||
});
|
||||
}
|
||||
|
||||
public function testValidateActivity() {
|
||||
$this->specify('error.account_not_activated if account in not activated state', function() {
|
||||
$model = new DummyLoginForm([
|
||||
'account' => new Account(['status' => Account::STATUS_REGISTERED]),
|
||||
]);
|
||||
$model->validateActivity('login');
|
||||
expect($model->getErrors('login'))->equals(['error.account_not_activated']);
|
||||
});
|
||||
|
||||
$this->specify('no errors if account active', function() {
|
||||
$model = new DummyLoginForm([
|
||||
'account' => new Account(['status' => Account::STATUS_ACTIVE]),
|
||||
]);
|
||||
$model->validateActivity('login');
|
||||
expect($model->getErrors('login'))->isEmpty();
|
||||
});
|
||||
}
|
||||
|
||||
public function testLogin() {
|
||||
$this->specify('user should be able to login with correct username and password', function () {
|
||||
$model = new DummyLoginForm([
|
||||
'login' => 'erickskrauch',
|
||||
'password' => '12345678',
|
||||
'account' => new Account([
|
||||
'username' => 'erickskrauch',
|
||||
'password' => '12345678',
|
||||
'status' => Account::STATUS_ACTIVE,
|
||||
]),
|
||||
]);
|
||||
expect('model should login user', $model->login())->notEquals(false);
|
||||
expect('error message should not be set', $model->errors)->isEmpty();
|
||||
});
|
||||
}
|
||||
|
||||
public function testLoginByEmailCorrect() {
|
||||
$model = $this->createModel($this->accounts['admin']['email'], '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())->notEquals(false);
|
||||
expect('error message should not be set', $model->errors)->isEmpty();
|
||||
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 {
|
||||
|
||||
private $_account;
|
||||
|
||||
public function setAccount($value) {
|
||||
$this->_account = $value;
|
||||
}
|
||||
|
||||
public function getAccount() {
|
||||
return $this->_account;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user