diff --git a/api/models/LoginForm.php b/api/models/LoginForm.php index 18ebed8..3f9db42 100644 --- a/api/models/LoginForm.php +++ b/api/models/LoginForm.php @@ -22,6 +22,8 @@ class LoginForm extends BaseApiForm { }, 'message' => 'error.password_required'], ['password', 'validatePassword'], + ['login', 'validateActivity'], + ['rememberMe', 'boolean'], ]; } @@ -43,6 +45,15 @@ class LoginForm extends BaseApiForm { } } + public function validateActivity($attribute) { + if (!$this->hasErrors()) { + $account = $this->getAccount(); + if ($account->status !== Account::STATUS_ACTIVE) { + $this->addError($attribute, 'error.account_not_activated'); + } + } + } + /** * @return bool|string JWT с информацией об аккаунте */ diff --git a/tests/codeception/api/functional/LoginCest.php b/tests/codeception/api/functional/LoginCest.php index 5b86f2d..adff510 100644 --- a/tests/codeception/api/functional/LoginCest.php +++ b/tests/codeception/api/functional/LoginCest.php @@ -35,6 +35,15 @@ class LoginCest { ], ]); + $I->wantTo('see error.account_not_activated expected if credentials are valid, but account is not activated'); + $route->login('howe.garnett', 'password_0'); + $I->canSeeResponseContainsJson([ + 'success' => false, + 'errors' => [ + 'login' => 'error.account_not_activated', + ], + ]); + $I->wantTo('don\'t see errors on login field if username is correct and exists in database'); $route->login('Admin'); $I->canSeeResponseContainsJson([ diff --git a/tests/codeception/api/unit/models/LoginFormTest.php b/tests/codeception/api/unit/models/LoginFormTest.php index 0f138fb..bc9dc97 100644 --- a/tests/codeception/api/unit/models/LoginFormTest.php +++ b/tests/codeception/api/unit/models/LoginFormTest.php @@ -40,10 +40,20 @@ class LoginFormTest extends DbTestCase { 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 testLoginByUsernameCorrect() { - $model = $this->createModel('Admin', 'password_0'); + $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) { expect('model should login user', $model->login())->notEquals(false); expect('error message should not be set', $model->errors)->isEmpty(); @@ -51,7 +61,7 @@ class LoginFormTest extends DbTestCase { } public function testLoginByEmailCorrect() { - $model = $this->createModel('admin@ely.by', 'password_0'); + $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();