2016-01-15 14:51:27 +05:30
|
|
|
<?php
|
2016-05-14 05:17:17 +05:30
|
|
|
namespace tests\codeception\api\models\authentication;
|
2016-01-15 14:51:27 +05:30
|
|
|
|
2016-05-30 05:14:17 +05:30
|
|
|
use api\components\User\LoginResult;
|
|
|
|
use api\models\AccountIdentity;
|
2016-05-14 05:17:17 +05:30
|
|
|
use api\models\authentication\LoginForm;
|
2016-01-15 14:51:27 +05:30
|
|
|
use Codeception\Specify;
|
2016-03-21 02:03:09 +05:30
|
|
|
use common\models\Account;
|
2016-05-24 01:12:50 +05:30
|
|
|
use tests\codeception\api\unit\DbTestCase;
|
|
|
|
use tests\codeception\common\fixtures\AccountFixture;
|
2016-01-15 14:51:27 +05:30
|
|
|
use Yii;
|
|
|
|
|
2016-05-24 01:12:50 +05:30
|
|
|
/**
|
|
|
|
* @property AccountFixture $accounts
|
|
|
|
*/
|
|
|
|
class LoginFormTest extends DbTestCase {
|
2016-01-15 14:51:27 +05:30
|
|
|
use Specify;
|
|
|
|
|
2016-05-30 05:14:17 +05:30
|
|
|
private $originalRemoteAddr;
|
|
|
|
|
|
|
|
public function setUp() {
|
|
|
|
$this->originalRemoteAddr = $_SERVER['REMOTE_ADDR'] ?? null;
|
|
|
|
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
|
|
|
|
parent::setUp();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown() {
|
|
|
|
parent::tearDown();
|
|
|
|
$_SERVER['REMOTE_ADDR'] = $this->originalRemoteAddr;
|
|
|
|
}
|
|
|
|
|
2016-05-24 01:12:50 +05:30
|
|
|
public function fixtures() {
|
|
|
|
return [
|
2016-05-30 05:14:17 +05:30
|
|
|
'accounts' => AccountFixture::class,
|
2016-05-24 01:12:50 +05:30
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-03-21 02:03:09 +05:30
|
|
|
public function testValidateLogin() {
|
2016-05-14 05:17:17 +05:30
|
|
|
$this->specify('error.login_not_exist if login not exists', function () {
|
|
|
|
$model = $this->createModel([
|
2016-03-21 02:03:09 +05:30
|
|
|
'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-05-14 05:17:17 +05:30
|
|
|
$this->specify('no errors if login exists', function () {
|
|
|
|
$model = $this->createModel([
|
2016-03-21 02:03:09 +05:30
|
|
|
'login' => 'mr-test',
|
2016-05-30 05:14:17 +05:30
|
|
|
'account' => new AccountIdentity(),
|
2016-03-21 02:03:09 +05:30
|
|
|
]);
|
|
|
|
$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() {
|
2016-05-14 05:17:17 +05:30
|
|
|
$this->specify('error.password_incorrect if password invalid', function () {
|
|
|
|
$model = $this->createModel([
|
2016-03-21 02:03:09 +05:30
|
|
|
'password' => '87654321',
|
2016-05-30 05:14:17 +05:30
|
|
|
'account' => new AccountIdentity(['password' => '12345678']),
|
2016-03-21 02:03:09 +05:30
|
|
|
]);
|
|
|
|
$model->validatePassword('password');
|
|
|
|
expect($model->getErrors('password'))->equals(['error.password_incorrect']);
|
|
|
|
});
|
|
|
|
|
2016-05-14 05:17:17 +05:30
|
|
|
$this->specify('no errors if password valid', function () {
|
|
|
|
$model = $this->createModel([
|
2016-03-21 02:03:09 +05:30
|
|
|
'password' => '12345678',
|
2016-05-30 05:14:17 +05:30
|
|
|
'account' => new AccountIdentity(['password' => '12345678']),
|
2016-03-21 02:03:09 +05:30
|
|
|
]);
|
|
|
|
$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() {
|
2016-05-14 05:17:17 +05:30
|
|
|
$this->specify('error.account_not_activated if account in not activated state', function () {
|
|
|
|
$model = $this->createModel([
|
2016-05-30 05:14:17 +05:30
|
|
|
'account' => new AccountIdentity(['status' => Account::STATUS_REGISTERED]),
|
2016-03-21 02:03:09 +05:30
|
|
|
]);
|
|
|
|
$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-05-14 05:17:17 +05:30
|
|
|
$this->specify('no errors if account active', function () {
|
|
|
|
$model = $this->createModel([
|
2016-05-30 05:14:17 +05:30
|
|
|
'account' => new AccountIdentity(['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 () {
|
2016-05-14 05:17:17 +05:30
|
|
|
$model = $this->createModel([
|
2016-03-21 02:03:09 +05:30
|
|
|
'login' => 'erickskrauch',
|
|
|
|
'password' => '12345678',
|
2016-05-30 05:14:17 +05:30
|
|
|
'account' => new AccountIdentity([
|
2016-03-21 02:03:09 +05:30
|
|
|
'username' => 'erickskrauch',
|
|
|
|
'password' => '12345678',
|
|
|
|
'status' => Account::STATUS_ACTIVE,
|
|
|
|
]),
|
|
|
|
]);
|
2016-05-30 05:14:17 +05:30
|
|
|
expect('model should login user', $model->login())->isInstanceOf(LoginResult::class);
|
2016-01-15 14:51:27 +05:30
|
|
|
expect('error message should not be set', $model->errors)->isEmpty();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-05-24 01:12:50 +05:30
|
|
|
public function testLoginWithRehashing() {
|
|
|
|
$this->specify('user, that login using account with old pass hash strategy should update it automatically', function () {
|
|
|
|
$model = new LoginForm([
|
|
|
|
'login' => $this->accounts['user-with-old-password-type']['username'],
|
|
|
|
'password' => '12345678',
|
|
|
|
]);
|
2016-05-30 05:14:17 +05:30
|
|
|
expect($model->login())->isInstanceOf(LoginResult::class);
|
2016-05-24 01:12:50 +05:30
|
|
|
expect($model->errors)->isEmpty();
|
|
|
|
expect($model->getAccount()->password_hash_strategy)->equals(Account::PASS_HASH_STRATEGY_YII2);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-05-14 05:17:17 +05:30
|
|
|
/**
|
|
|
|
* @param array $params
|
|
|
|
* @return LoginForm
|
|
|
|
*/
|
|
|
|
private function createModel(array $params = []) {
|
|
|
|
return new class($params) extends LoginForm {
|
|
|
|
private $_account;
|
2016-03-21 02:03:09 +05:30
|
|
|
|
2016-05-14 05:17:17 +05:30
|
|
|
public function setAccount($value) {
|
|
|
|
$this->_account = $value;
|
|
|
|
}
|
2016-03-21 02:03:09 +05:30
|
|
|
|
2016-05-14 05:17:17 +05:30
|
|
|
public function getAccount() {
|
|
|
|
return $this->_account;
|
|
|
|
}
|
|
|
|
};
|
2016-03-21 02:03:09 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|