2016-01-15 14:51:27 +05:30
|
|
|
<?php
|
|
|
|
namespace tests\codeception\api\models;
|
|
|
|
|
|
|
|
use api\models\LoginForm;
|
|
|
|
use Codeception\Specify;
|
2016-03-21 02:03:09 +05:30
|
|
|
use common\models\Account;
|
2016-01-15 14:51:27 +05:30
|
|
|
use tests\codeception\api\unit\DbTestCase;
|
|
|
|
use Yii;
|
|
|
|
|
|
|
|
class LoginFormTest extends DbTestCase {
|
|
|
|
use Specify;
|
|
|
|
|
2016-03-21 02:03:09 +05:30
|
|
|
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']);
|
|
|
|
});
|
2016-01-15 14:51:27 +05:30
|
|
|
|
2016-03-21 02:03:09 +05:30
|
|
|
$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();
|
|
|
|
});
|
2016-01-15 14:51:27 +05:30
|
|
|
}
|
|
|
|
|
2016-03-21 02:03:09 +05:30
|
|
|
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();
|
|
|
|
});
|
2016-01-15 14:51:27 +05:30
|
|
|
}
|
|
|
|
|
2016-03-21 02:03:09 +05:30
|
|
|
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']);
|
2016-01-15 14:51:27 +05:30
|
|
|
});
|
2016-02-28 03:20:49 +05:30
|
|
|
|
2016-03-21 02:03:09 +05:30
|
|
|
$this->specify('no errors if account active', function() {
|
|
|
|
$model = new DummyLoginForm([
|
|
|
|
'account' => new Account(['status' => Account::STATUS_ACTIVE]),
|
2016-02-28 03:20:49 +05:30
|
|
|
]);
|
2016-03-21 02:03:09 +05:30
|
|
|
$model->validateActivity('login');
|
|
|
|
expect($model->getErrors('login'))->isEmpty();
|
2016-02-28 03:20:49 +05:30
|
|
|
});
|
2016-01-15 14:51:27 +05:30
|
|
|
}
|
|
|
|
|
2016-03-21 02:03:09 +05:30
|
|
|
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,
|
|
|
|
]),
|
|
|
|
]);
|
2016-02-23 03:52:04 +05:30
|
|
|
expect('model should login user', $model->login())->notEquals(false);
|
2016-01-15 14:51:27 +05:30
|
|
|
expect('error message should not be set', $model->errors)->isEmpty();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2016-03-21 02:03:09 +05:30
|
|
|
|
|
|
|
class DummyLoginForm extends LoginForm {
|
|
|
|
|
|
|
|
private $_account;
|
|
|
|
|
|
|
|
public function setAccount($value) {
|
|
|
|
$this->_account = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAccount() {
|
|
|
|
return $this->_account;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|