mirror of
				https://github.com/elyby/accounts.git
				synced 2025-05-31 14:11:46 +05:30 
			
		
		
		
	Упразднён контроллер PasswordProtectedController и заменён на валидатор PasswordRequiredValidator
Реорганизованы тесты для ChangeUsernameFormTest
This commit is contained in:
		@@ -1,38 +0,0 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace tests\codeception\api\models\base;
 | 
			
		||||
 | 
			
		||||
use api\models\base\PasswordProtectedForm;
 | 
			
		||||
use Codeception\Specify;
 | 
			
		||||
use common\models\Account;
 | 
			
		||||
use tests\codeception\api\unit\TestCase;
 | 
			
		||||
 | 
			
		||||
class PasswordProtectedFormTest extends TestCase {
 | 
			
		||||
    use Specify;
 | 
			
		||||
 | 
			
		||||
    public function testValidatePassword() {
 | 
			
		||||
        $this->specify('error.password_invalid on passing invalid account password', function() {
 | 
			
		||||
            $model = new DummyBasePasswordProtectedForm();
 | 
			
		||||
            $model->password = 'some-invalid-password';
 | 
			
		||||
            $model->validatePassword();
 | 
			
		||||
            expect($model->getErrors('password'))->equals(['error.password_invalid']);
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        $this->specify('no errors on passing valid account password', function() {
 | 
			
		||||
            $model = new DummyBasePasswordProtectedForm();
 | 
			
		||||
            $model->password = 'password_0';
 | 
			
		||||
            $model->validatePassword();
 | 
			
		||||
            expect($model->getErrors('password'))->isEmpty();
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
class DummyBasePasswordProtectedForm extends PasswordProtectedForm {
 | 
			
		||||
 | 
			
		||||
    protected function getAccount() {
 | 
			
		||||
        return new Account([
 | 
			
		||||
            'password' => 'password_0',
 | 
			
		||||
        ]);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -1,6 +1,7 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace tests\codeception\api\models\profile;
 | 
			
		||||
 | 
			
		||||
use api\models\AccountIdentity;
 | 
			
		||||
use api\models\profile\ChangeUsernameForm;
 | 
			
		||||
use Codeception\Specify;
 | 
			
		||||
use common\models\Account;
 | 
			
		||||
@@ -8,6 +9,7 @@ use common\models\UsernameHistory;
 | 
			
		||||
use tests\codeception\api\unit\DbTestCase;
 | 
			
		||||
use tests\codeception\common\fixtures\AccountFixture;
 | 
			
		||||
use tests\codeception\common\fixtures\UsernameHistoryFixture;
 | 
			
		||||
use Yii;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @property AccountFixture $accounts
 | 
			
		||||
@@ -22,9 +24,15 @@ class ChangeUsernameFormTest extends DbTestCase {
 | 
			
		||||
        ];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function setUp() {
 | 
			
		||||
        parent::setUp();
 | 
			
		||||
        $account = AccountIdentity::findOne($this->getAccountId());
 | 
			
		||||
        Yii::$app->user->setIdentity($account);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testChange() {
 | 
			
		||||
        $this->specify('successfully change username to new one', function() {
 | 
			
		||||
            $model = $this->createModel([
 | 
			
		||||
            $model = new ChangeUsernameForm([
 | 
			
		||||
                'password' => 'password_0',
 | 
			
		||||
                'username' => 'my_new_nickname',
 | 
			
		||||
            ]);
 | 
			
		||||
@@ -36,7 +44,7 @@ class ChangeUsernameFormTest extends DbTestCase {
 | 
			
		||||
 | 
			
		||||
    public function testChangeWithoutChange() {
 | 
			
		||||
        $this->specify('no new UsernameHistory record, if we don\'t change nickname', function() {
 | 
			
		||||
            $model = $this->createModel([
 | 
			
		||||
            $model = new ChangeUsernameForm([
 | 
			
		||||
                'password' => 'password_0',
 | 
			
		||||
                'username' => $this->accounts['admin']['username'],
 | 
			
		||||
            ]);
 | 
			
		||||
@@ -53,7 +61,7 @@ class ChangeUsernameFormTest extends DbTestCase {
 | 
			
		||||
    public function testChangeCase() {
 | 
			
		||||
        $this->specify('username should change, if we change case of some letters', function() {
 | 
			
		||||
            $newUsername = mb_strtoupper($this->accounts['admin']['username']);
 | 
			
		||||
            $model = $this->createModel([
 | 
			
		||||
            $model = new ChangeUsernameForm([
 | 
			
		||||
                'password' => 'password_0',
 | 
			
		||||
                'username' => $newUsername,
 | 
			
		||||
            ]);
 | 
			
		||||
@@ -65,7 +73,7 @@ class ChangeUsernameFormTest extends DbTestCase {
 | 
			
		||||
 | 
			
		||||
    public function testValidateUsername() {
 | 
			
		||||
        $this->specify('error.username_not_available expected if username is already taken', function() {
 | 
			
		||||
            $model = $this->createModel([
 | 
			
		||||
            $model = new ChangeUsernameForm([
 | 
			
		||||
                'password' => 'password_0',
 | 
			
		||||
                'username' => 'Jon',
 | 
			
		||||
            ]);
 | 
			
		||||
@@ -74,7 +82,7 @@ class ChangeUsernameFormTest extends DbTestCase {
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        $this->specify('error.username_not_available is NOT expected if username is already taken by CURRENT user', function() {
 | 
			
		||||
            $model = $this->createModel([
 | 
			
		||||
            $model = new ChangeUsernameForm([
 | 
			
		||||
                'password' => 'password_0',
 | 
			
		||||
                'username' => $this->accounts['admin']['username'],
 | 
			
		||||
            ]);
 | 
			
		||||
@@ -84,27 +92,12 @@ class ChangeUsernameFormTest extends DbTestCase {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testCreateTask() {
 | 
			
		||||
        $model = $this->createModel();
 | 
			
		||||
        $model = new ChangeUsernameForm();
 | 
			
		||||
        $model->createEventTask('1', 'test1', 'test');
 | 
			
		||||
        // TODO: у меня пока нет идей о том, чтобы это как-то успешно протестировать, увы
 | 
			
		||||
        // но по крайней мере можно убедиться, что оно не падает где-то на этом шаге
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private function createModel(array $params = []) : ChangeUsernameForm {
 | 
			
		||||
        /** @noinspection PhpUnusedLocalVariableInspection */
 | 
			
		||||
        $params = array_merge($params, [
 | 
			
		||||
            'accountId' => $this->getAccountId(),
 | 
			
		||||
        ]);
 | 
			
		||||
 | 
			
		||||
        return new class($params) extends ChangeUsernameForm {
 | 
			
		||||
            public $accountId;
 | 
			
		||||
 | 
			
		||||
            protected function getAccount() {
 | 
			
		||||
                return Account::findOne($this->accountId);
 | 
			
		||||
            }
 | 
			
		||||
        };
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private function getAccountId() {
 | 
			
		||||
        return $this->accounts['admin']['id'];
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,32 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace codeception\api\unit\validators;
 | 
			
		||||
 | 
			
		||||
use api\validators\PasswordRequiredValidator;
 | 
			
		||||
use Codeception\Specify;
 | 
			
		||||
use common\models\Account;
 | 
			
		||||
use tests\codeception\api\unit\DbTestCase;
 | 
			
		||||
use tests\codeception\common\_support\ProtectedCaller;
 | 
			
		||||
 | 
			
		||||
class PasswordRequiredValidatorTest extends DbTestCase {
 | 
			
		||||
    use Specify;
 | 
			
		||||
    use ProtectedCaller;
 | 
			
		||||
 | 
			
		||||
    public function testValidateValue() {
 | 
			
		||||
        $account = new Account(['password' => '12345678']);
 | 
			
		||||
        $this->specify('get error.password_required if password is empty', function () use ($account) {
 | 
			
		||||
            $model = new PasswordRequiredValidator(['account' => $account]);
 | 
			
		||||
            expect($this->callProtected($model, 'validateValue', ''))->equals(['error.password_required', []]);
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        $this->specify('get error.password_invalid if password is incorrect', function () use ($account) {
 | 
			
		||||
            $model = new PasswordRequiredValidator(['account' => $account]);
 | 
			
		||||
            expect($this->callProtected($model, 'validateValue', '87654321'))->equals(['error.password_invalid', []]);
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        $this->specify('no errors, if password is correct for provided account', function () use ($account) {
 | 
			
		||||
            $model = new PasswordRequiredValidator(['account' => $account]);
 | 
			
		||||
            expect($this->callProtected($model, 'validateValue', '12345678'))->null();
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user