2016-05-30 05:14:17 +05:30
|
|
|
<?php
|
|
|
|
namespace codeception\api\unit\models;
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
use api\components\User\IdentityInterface;
|
|
|
|
use api\components\User\Jwt;
|
|
|
|
use api\components\User\JwtIdentity;
|
2016-05-30 05:14:17 +05:30
|
|
|
use Codeception\Specify;
|
2016-09-19 13:48:52 +05:30
|
|
|
use Emarref\Jwt\Claim;
|
|
|
|
use Emarref\Jwt\Encryption\Factory as EncryptionFactory;
|
|
|
|
use Emarref\Jwt\Token;
|
2016-10-29 03:17:31 +05:30
|
|
|
use tests\codeception\api\unit\TestCase;
|
2016-06-05 19:31:35 +05:30
|
|
|
use tests\codeception\common\_support\ProtectedCaller;
|
2016-05-30 05:14:17 +05:30
|
|
|
use tests\codeception\common\fixtures\AccountFixture;
|
|
|
|
use Yii;
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
class JwtIdentityTest extends TestCase {
|
2016-05-30 05:14:17 +05:30
|
|
|
use Specify;
|
2016-06-05 19:31:35 +05:30
|
|
|
use ProtectedCaller;
|
2016-05-30 05:14:17 +05:30
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public function _fixtures(): array {
|
2016-05-30 05:14:17 +05:30
|
|
|
return [
|
2016-05-30 23:41:22 +05:30
|
|
|
'accounts' => AccountFixture::class,
|
2016-05-30 05:14:17 +05:30
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFindIdentityByAccessToken() {
|
2017-09-19 22:36:16 +05:30
|
|
|
$token = $this->generateToken();
|
|
|
|
$identity = JwtIdentity::findIdentityByAccessToken($token);
|
2016-07-27 18:47:41 +05:30
|
|
|
$this->assertInstanceOf(IdentityInterface::class, $identity);
|
2017-09-19 22:36:16 +05:30
|
|
|
$this->assertEquals($token, $identity->getId());
|
|
|
|
$this->assertEquals($this->tester->grabFixture('accounts', 'admin')['id'], $identity->getAccount()->id);
|
2016-07-27 18:47:41 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \yii\web\UnauthorizedHttpException
|
|
|
|
* @expectedExceptionMessage Token expired
|
|
|
|
*/
|
|
|
|
public function testFindIdentityByAccessTokenWithExpiredToken() {
|
2016-09-19 13:48:52 +05:30
|
|
|
$token = new Token();
|
|
|
|
$token->addClaim(new Claim\IssuedAt(1464593193));
|
|
|
|
$token->addClaim(new Claim\Expiration(1464596793));
|
2017-09-19 22:36:16 +05:30
|
|
|
$token->addClaim(new Claim\Subject('ely|' . $this->tester->grabFixture('accounts', 'admin')['id']));
|
2016-09-19 13:48:52 +05:30
|
|
|
$expiredToken = (new Jwt())->serialize($token, EncryptionFactory::create(Yii::$app->user->getAlgorithm()));
|
2016-07-27 18:47:41 +05:30
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
JwtIdentity::findIdentityByAccessToken($expiredToken);
|
2016-07-27 18:47:41 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \yii\web\UnauthorizedHttpException
|
|
|
|
* @expectedExceptionMessage Incorrect token
|
|
|
|
*/
|
|
|
|
public function testFindIdentityByAccessTokenWithEmptyToken() {
|
2017-09-19 22:36:16 +05:30
|
|
|
JwtIdentity::findIdentityByAccessToken('');
|
2016-05-30 05:14:17 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
protected function generateToken() {
|
|
|
|
/** @var \api\components\User\Component $component */
|
|
|
|
$component = Yii::$app->user;
|
2017-09-19 22:36:16 +05:30
|
|
|
/** @var \common\models\Account $account */
|
|
|
|
$account = $this->tester->grabFixture('accounts', 'admin');
|
2016-06-05 19:31:35 +05:30
|
|
|
$token = $this->callProtected($component, 'createToken', $account);
|
2016-05-30 05:14:17 +05:30
|
|
|
|
2016-06-05 19:31:35 +05:30
|
|
|
return $this->callProtected($component, 'serializeToken', $token);
|
2016-05-30 05:14:17 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|