mirror of
				https://github.com/elyby/accounts.git
				synced 2025-05-31 14:11:46 +05:30 
			
		
		
		
	Заменил библиотеку для JWT токенов на свой форк с разнообразными исключениями, подрихтовал тесты для класса AccountIdentity
This commit is contained in:
		@@ -3,13 +3,11 @@ namespace codeception\api\unit\models;
 | 
			
		||||
 | 
			
		||||
use api\models\AccountIdentity;
 | 
			
		||||
use Codeception\Specify;
 | 
			
		||||
use Exception;
 | 
			
		||||
use tests\codeception\api\unit\DbTestCase;
 | 
			
		||||
use tests\codeception\common\_support\ProtectedCaller;
 | 
			
		||||
use tests\codeception\common\fixtures\AccountFixture;
 | 
			
		||||
use Yii;
 | 
			
		||||
use yii\web\IdentityInterface;
 | 
			
		||||
use yii\web\UnauthorizedHttpException;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @property AccountIdentity $accounts
 | 
			
		||||
@@ -25,27 +23,29 @@ class AccountIdentityTest extends DbTestCase {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testFindIdentityByAccessToken() {
 | 
			
		||||
        $this->specify('success validate passed jwt token', function() {
 | 
			
		||||
            $identity = AccountIdentity::findIdentityByAccessToken($this->generateToken());
 | 
			
		||||
            expect($identity)->isInstanceOf(IdentityInterface::class);
 | 
			
		||||
            expect($identity->getId())->equals($this->accounts['admin']['id']);
 | 
			
		||||
        });
 | 
			
		||||
        $identity = AccountIdentity::findIdentityByAccessToken($this->generateToken());
 | 
			
		||||
        $this->assertInstanceOf(IdentityInterface::class, $identity);
 | 
			
		||||
        $this->assertEquals($this->accounts['admin']['id'], $identity->getId());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
        $this->specify('get unauthorized exception with "Token expired" message if token valid, but expire', function() {
 | 
			
		||||
            $expiredToken = 'eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJodHRwOlwvXC9sb2NhbGhvc3Q6ODA4MCIsImlzcyI6Imh0d' .
 | 
			
		||||
                            'HA6XC9cL2xvY2FsaG9zdDo4MDgwIiwiaWF0IjoxNDY0NTkzMTkzLCJleHAiOjE0NjQ1OTY3OTN9.DV' .
 | 
			
		||||
                            '8uwh0OQhBYXkrNvxwJeO-kEjb9MQeLr3-6GoHM7RY';
 | 
			
		||||
    /**
 | 
			
		||||
     * @expectedException \yii\web\UnauthorizedHttpException
 | 
			
		||||
     * @expectedExceptionMessage Token expired
 | 
			
		||||
     */
 | 
			
		||||
    public function testFindIdentityByAccessTokenWithExpiredToken() {
 | 
			
		||||
        $expiredToken = 'eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJodHRwOlwvXC9sb2NhbGhvc3Q6ODA4MCIsImlzcyI6Imh0d' .
 | 
			
		||||
                        'HA6XC9cL2xvY2FsaG9zdDo4MDgwIiwiaWF0IjoxNDY0NTkzMTkzLCJleHAiOjE0NjQ1OTY3OTN9.DV' .
 | 
			
		||||
                        '8uwh0OQhBYXkrNvxwJeO-kEjb9MQeLr3-6GoHM7RY';
 | 
			
		||||
 | 
			
		||||
            try {
 | 
			
		||||
                AccountIdentity::findIdentityByAccessToken($expiredToken);
 | 
			
		||||
            } catch (Exception $e) {
 | 
			
		||||
                expect($e)->isInstanceOf(UnauthorizedHttpException::class);
 | 
			
		||||
                expect($e->getMessage())->equals('Token expired');
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
        AccountIdentity::findIdentityByAccessToken($expiredToken);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
            expect('if test valid, this should not happened', false)->true();
 | 
			
		||||
        });
 | 
			
		||||
    /**
 | 
			
		||||
     * @expectedException \yii\web\UnauthorizedHttpException
 | 
			
		||||
     * @expectedExceptionMessage Incorrect token
 | 
			
		||||
     */
 | 
			
		||||
    public function testFindIdentityByAccessTokenWithEmptyToken() {
 | 
			
		||||
        AccountIdentity::findIdentityByAccessToken('');
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected function generateToken() {
 | 
			
		||||
@@ -53,6 +53,7 @@ class AccountIdentityTest extends DbTestCase {
 | 
			
		||||
        $component = Yii::$app->user;
 | 
			
		||||
        /** @var AccountIdentity $account */
 | 
			
		||||
        $account = AccountIdentity::findOne($this->accounts['admin']['id']);
 | 
			
		||||
 | 
			
		||||
        $token = $this->callProtected($component, 'createToken', $account);
 | 
			
		||||
 | 
			
		||||
        return $this->callProtected($component, 'serializeToken', $token);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user