From c6a6f35be6cc1f16461c9e68329cae103ea1395c Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Sun, 3 Jan 2016 20:37:55 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D1=80=D0=B0=D0=B1?= =?UTF-8?q?=D0=BE=D1=82=D0=B0=D0=BD=D0=B0=20=D1=81=D1=82=D1=80=D1=83=D0=BA?= =?UTF-8?q?=D1=82=D1=83=D1=80=D0=B0=20=D1=82=D0=B5=D1=81=D1=82=D0=BE=D0=B2?= =?UTF-8?q?=20=D0=B4=D0=BB=D1=8F=20=D1=84=D0=BE=D1=80=D0=BC=D1=8B=20=D0=B0?= =?UTF-8?q?=D1=83=D1=82=D0=B5=D0=BD=D1=82=D0=B8=D1=84=D0=B8=D0=BA=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../login/models/AuthenticationForm.php | 14 +- .../login/models/AuthenticationFormTest.php | 181 ++++++++---------- 2 files changed, 88 insertions(+), 107 deletions(-) diff --git a/api/modules/login/models/AuthenticationForm.php b/api/modules/login/models/AuthenticationForm.php index dd757f6..a658042 100644 --- a/api/modules/login/models/AuthenticationForm.php +++ b/api/modules/login/models/AuthenticationForm.php @@ -17,23 +17,17 @@ class AuthenticationForm extends Model { return [ ['email', 'required', 'message' => 'error.email_required'], ['email', 'email', 'message' => 'error.email_invalid'], - ['email', 'validateEmail'], + ['email', 'exist', 'targetClass' => Account::class, 'skipOnError' => true, 'message' => 'error.email_not_exist'], - ['password', 'required', 'message' => 'error.password_required'], + ['password', 'required', 'when' => function(self $model) { + return !$model->hasErrors(); + }, 'message' => 'error.password_required'], ['password', 'validatePassword'], ['rememberMe', 'boolean'], ]; } - public function validateEmail($attribute) { - if (!$this->hasErrors()) { - if ($this->getAccount() === NULL) { - $this->addError($attribute, 'error.email_not_exist'); - } - } - } - public function validatePassword($attribute) { if (!$this->hasErrors()) { $account = $this->getAccount(); diff --git a/tests/codeception/api/unit/modules/login/models/AuthenticationFormTest.php b/tests/codeception/api/unit/modules/login/models/AuthenticationFormTest.php index a183ab2..bebc6cd 100644 --- a/tests/codeception/api/unit/modules/login/models/AuthenticationFormTest.php +++ b/tests/codeception/api/unit/modules/login/models/AuthenticationFormTest.php @@ -16,109 +16,96 @@ class AuthenticationFormTest extends DbTestCase { parent::tearDown(); } - public function testValidateEmail() { - $model = new AuthenticationForm(); - $this->specify('error.email_required expected if email is not set', function() use ($model) { - $model->validate(['email']); - expect($model->getErrors('email'))->equals(['error.email_required']); - }); - - $this->specify('error.email_invalid expected if email not correct', function() use ($model) { - $model->email = 'wrong-email-string'; - $model->validate(['email']); - expect($model->getErrors('email'))->equals(['error.email_invalid']); - - $model->email = 'wrong@email'; - $model->validate(['email']); - expect($model->getErrors('email'))->equals(['error.email_invalid']); - }); - - $this->specify('error.email_not_exist expected if email not exists in database', function() use ($model) { - $model->email = 'not-exist@user.com'; - $model->validate(['email']); - expect($model->getErrors('email'))->equals(['error.email_not_exist']); - }); - - $this->specify('no errors if email is correct and exists in database', function() use ($model) { - $model->email = 'admin@ely.by'; - $model->validate(['email']); - expect($model->getErrors('email'))->isEmpty(); - }); - } - - public function testValidatePassword() { - $model = new AuthenticationForm(); - $this->specify('error.password_required expected if password is not set', function() use ($model) { - $model->validate(['password']); - expect($model->getErrors('password'))->equals(['error.password_required']); - }); - - $this->specify('error.password_incorrect expected if password not correct for passed email', function() use ($model) { - $model->email = 'non-exist@valid.mail'; - $model->password = 'wrong-password'; - $model->validate(['password']); - expect('if email incorrect, the error should be displayed in any case,', $model->getErrors('password')) - ->equals(['error.password_incorrect']); - - $model->email = 'admin@ely.by'; - $model->password = 'wrong-password'; - $model->validate(['password']); - expect($model->getErrors('password'))->equals(['error.password_incorrect']); - }); - - $this->specify('no errors if email and password is correct and exists in database', function() use ($model) { - $model->email = 'admin@ely.by'; - $model->password = 'password_0'; - $model->validate(['password']); - expect($model->getErrors('password'))->isEmpty(); - }); - } - - public function testLoginNoUser() { - $model = new AuthenticationForm([ - 'email' => 'non-exist@valid.mail', - 'password' => 'not_existing_password', - ]); - - $this->specify('user should not be able to login, when there is no identity', function () use ($model) { - expect('model should not login user', $model->login())->false(); - expect('user should not be logged in', Yii::$app->user->isGuest)->true(); - }); - } - - public function testLoginWrongPassword() { - $model = new AuthenticationForm([ - 'email' => 'admin@ely.by', - 'password' => 'wrong_password', - ]); - - $this->specify('user should not be able to login with wrong password', function () use ($model) { - expect('model should not login user', $model->login())->false(); - expect('error message should be set', $model->errors)->hasKey('password'); - expect('user should not be logged in', Yii::$app->user->isGuest)->true(); - }); - } - - public function testLoginCorrect() { - $model = new AuthenticationForm([ - 'email' => 'admin@ely.by', - 'password' => 'password_0', - ]); - - $this->specify('user should be able to login with correct credentials', function () use ($model) { - expect('model should login user', $model->login())->true(); - expect('error message should not be set', $model->errors)->hasntKey('password'); - expect('user should be logged in', Yii::$app->user->isGuest)->false(); - }); - } - public function fixtures() { return [ - 'user' => [ + 'account' => [ 'class' => AccountFixture::className(), 'dataFile' => '@tests/codeception/api/unit/fixtures/data/models/accounts.php' ], ]; } + protected function createModel($email = '', $password = '') { + return new AuthenticationForm([ + 'email' => $email, + 'password' => $password, + ]); + } + + public function testLoginEmail() { + $model = $this->createModel(); + $this->specify('error.email_required expected if email is not set', function() use ($model) { + expect($model->login())->false(); + expect($model->getErrors('email'))->equals(['error.email_required']); + expect(Yii::$app->user->isGuest)->true(); + }); + + $model = $this->createModel('wrong-email-string'); + $this->specify('error.email_invalid expected if email not correct', function() use ($model) { + expect($model->login())->false(); + expect(Yii::$app->user->isGuest)->true(); + expect($model->getErrors('email'))->equals(['error.email_invalid']); + }); + + $model = $this->createModel('wrong@email'); + $this->specify('error.email_invalid expected if email not correct', function() use ($model) { + expect($model->login())->false(); + expect(Yii::$app->user->isGuest)->true(); + expect($model->getErrors('email'))->equals(['error.email_invalid']); + }); + + $model = $this->createModel('not-exist@user.com'); + $this->specify('error.email_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('email'))->equals(['error.email_not_exist']); + }); + + $model = $this->createModel('admin@ely.by'); + $this->specify('no errors on email 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('email'))->isEmpty(); + }); + } + + public function testLoginPassword() { + $model = $this->createModel(); + $this->specify('password don\'t has errors if email not set', function() use ($model) { + expect($model->login())->false(); + expect(Yii::$app->user->isGuest)->true(); + expect($model->getErrors('password'))->isEmpty(); + }); + + $model = $this->createModel('wrong-email-string', 'random-password'); + $this->specify('password don\'t has errors if email invalid', 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']); + }); + } + + public function testLoginCorrect() { + $model = $this->createModel('admin@ely.by', 'password_0'); + $this->specify('user should be able to login with correct credentials', 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(); + }); + } + }