Добавлена форма смены пароля и её тесты

This commit is contained in:
ErickSkrauch
2016-02-27 01:22:09 +03:00
parent 86c63f8724
commit 93a94c656b
7 changed files with 259 additions and 24 deletions

View File

@ -9,8 +9,17 @@ use yii\codeception\BasePage;
class AccountsRoute extends BasePage {
public function current() {
$this->route = ['users/current'];
$this->route = ['accounts/current'];
$this->actor->sendGET($this->getUrl());
}
public function changePassword($currentPassword = null, $newPassword = null, $newRePassword = null) {
$this->route = ['accounts/change-password'];
$this->actor->sendPOST($this->getUrl(), [
'password' => $currentPassword,
'newPassword' => $newPassword,
'newRePassword' => $newRePassword,
]);
}
}

View File

@ -0,0 +1,44 @@
<?php
namespace tests\codeception\api\functional;
use Codeception\Scenario;
use Codeception\Specify;
use tests\codeception\api\_pages\AccountsRoute;
use tests\codeception\api\_pages\LoginRoute;
use tests\codeception\api\functional\_steps\AccountSteps;
use tests\codeception\api\FunctionalTester;
class AccountsCurrentCest {
/**
* @var AccountsRoute
*/
private $route;
public function _before(FunctionalTester $I) {
$this->route = new AccountsRoute($I);
}
public function testChangePassword(FunctionalTester $I, Scenario $scenario) {
$I->wantTo('change my password');
$I = new AccountSteps($scenario);
$I->loggedInAsActiveAccount();
$this->route->changePassword('password_0', 'new-password', 'new-password');
$I->canSeeResponseCodeIs(200);
$I->canSeeResponseIsJson();
$I->canSeeResponseContainsJson([
'success' => true,
]);
$I->notLoggedIn();
$loginRoute = new LoginRoute($I);
$loginRoute->login('Admin', 'new-password');
$I->canSeeResponseCodeIs(200);
$I->canSeeResponseContainsJson([
'success' => true,
]);
}
}

View File

@ -16,4 +16,8 @@ class AccountSteps extends FunctionalTester {
$I->amBearerAuthenticated($jwt);
}
public function notLoggedIn() {
$this->haveHttpHeader('Authorization', null);
}
}

View File

@ -0,0 +1,108 @@
<?php
namespace tests\codeception\api\models;
use api\models\ChangePasswordForm;
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 ChangePasswordFormTest extends DbTestCase {
use Specify;
public function fixtures() {
return [
'accounts' => [
'class' => AccountFixture::class,
'dataFile' => '@tests/codeception/common/fixtures/data/accounts.php',
],
];
}
public function testChangePasswordErrors() {
/** @var Account $account */
$account = Account::findOne($this->accounts['admin']['id']);
$model = new ChangePasswordForm($account);
$this->specify('expected error.{field}_required if we don\'t pass some fields', function() use ($model, $account) {
expect('form should return false', $model->changePassword())->false();
expect('form should contain errors', $model->getErrors())->equals([
'password' => ['error.password_required'],
'newPassword' => ['error.newPassword_required'],
'newRePassword' => ['error.newRePassword_required'],
]);
expect('password not changed', $account->validatePassword('password_0'))->true();
});
$model = new ChangePasswordForm($account, [
'password' => 'this-is-wrong-password',
'newPassword' => 'my-new-password',
'newRePassword' => 'my-new-password',
]);
$this->specify('expected error.password_incorrect if we pass invalid current account password', function() use ($model, $account) {
expect('form should return false', $model->changePassword())->false();
expect('form should contain errors', $model->getErrors())->equals([
'password' => ['error.password_incorrect'],
]);
expect('password not changed', $account->validatePassword('password_0'))->true();
});
$model = new ChangePasswordForm($account, [
'password' => 'password_0',
'newPassword' => 'short',
'newRePassword' => 'short',
]);
$this->specify('expected error.password_too_short if we pass short password', function() use ($model, $account) {
expect('form should return false', $model->changePassword())->false();
expect('form should contain errors', $model->getErrors())->equals([
'newPassword' => ['error.password_too_short'],
]);
expect('password not changed', $account->validatePassword('password_0'))->true();
});
$model = new ChangePasswordForm($account, [
'password' => 'password_0',
'newPassword' => 'first-valid-pass',
'newRePassword' => 'another-valid-pass',
]);
$this->specify('expected error.newRePassword_does_not_match if we passwords mismatch', function() use ($model, $account) {
expect('form should return false', $model->changePassword())->false();
expect('form should contain errors', $model->getErrors())->equals([
'newRePassword' => ['error.newRePassword_does_not_match'],
]);
expect('password not changed', $account->validatePassword('password_0'))->true();
});
}
public function testChangePassword() {
/** @var Account $account */
$account = Account::findOne($this->accounts['admin']['id']);
$model = new ChangePasswordForm($account, [
'password' => 'password_0',
'newPassword' => 'my-new-password',
'newRePassword' => 'my-new-password',
]);
$this->specify('successfully change password with modern hash strategy', function() use ($model, $account) {
expect('form should return true', $model->changePassword())->true();
expect('new password should be successfully stored into account', $account->validatePassword('my-new-password'))->true();
expect('always use new strategy', $account->password_hash_strategy)->equals(Account::PASS_HASH_STRATEGY_YII2);
});
/** @var Account $account */
$account = Account::findOne($this->accounts['user-with-old-password-type']['id']);
$model = new ChangePasswordForm($account, [
'password' => '12345678',
'newPassword' => 'my-new-password',
'newRePassword' => 'my-new-password',
]);
$this->specify('successfully change password with legacy hash strategy', function() use ($model, $account) {
expect('form should return true', $model->changePassword())->true();
expect('new password should be successfully stored into account', $account->validatePassword('my-new-password'))->true();
expect('strategy should be changed to modern', $account->password_hash_strategy)->equals(Account::PASS_HASH_STRATEGY_YII2);
});
}
}