mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Fix some tests
This commit is contained in:
@@ -25,7 +25,7 @@ class ComponentTest extends TestCase {
|
||||
|
||||
public function _before() {
|
||||
parent::_before();
|
||||
$this->component = new Component($this->getComponentConfig());
|
||||
$this->component = new Component();
|
||||
}
|
||||
|
||||
public function _fixtures(): array {
|
||||
@@ -72,7 +72,6 @@ class ComponentTest extends TestCase {
|
||||
/** @var Component|\PHPUnit\Framework\MockObject\MockObject $component */
|
||||
$component = $this->getMockBuilder(Component::class)
|
||||
->setMethods(['getIsGuest'])
|
||||
->setConstructorArgs([$this->getComponentConfig()])
|
||||
->getMock();
|
||||
|
||||
$component
|
||||
@@ -91,7 +90,7 @@ class ComponentTest extends TestCase {
|
||||
$session = $this->tester->grabFixture('sessions', 'admin2');
|
||||
|
||||
/** @var Component|\Mockery\MockInterface $component */
|
||||
$component = mock(Component::class . '[getActiveSession]', [$this->getComponentConfig()])->makePartial();
|
||||
$component = mock(Component::class . '[getActiveSession]')->makePartial();
|
||||
$component->shouldReceive('getActiveSession')->times(1)->andReturn($session);
|
||||
|
||||
/** @var Account $account */
|
||||
@@ -140,15 +139,4 @@ class ComponentTest extends TestCase {
|
||||
Yii::$app->request->headers->set('Authorization', $bearerToken);
|
||||
}
|
||||
|
||||
private function getComponentConfig() {
|
||||
return [
|
||||
'identityClass' => IdentityFactory::class,
|
||||
'enableSession' => false,
|
||||
'loginUrl' => null,
|
||||
'secret' => 'secret',
|
||||
'publicKeyPath' => 'data/certs/public.crt',
|
||||
'privateKeyPath' => 'data/certs/private.key',
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
|
57
api/tests/unit/components/User/JwtIdentityTest.php
Normal file
57
api/tests/unit/components/User/JwtIdentityTest.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace codeception\api\unit\components\User;
|
||||
|
||||
use api\components\User\JwtIdentity;
|
||||
use api\tests\unit\TestCase;
|
||||
use common\tests\fixtures\AccountFixture;
|
||||
use Emarref\Jwt\Claim\Expiration as ExpirationClaim;
|
||||
use Yii;
|
||||
|
||||
class JwtIdentityTest extends TestCase {
|
||||
|
||||
public function _fixtures(): array {
|
||||
return [
|
||||
'accounts' => AccountFixture::class,
|
||||
];
|
||||
}
|
||||
|
||||
public function testFindIdentityByAccessToken() {
|
||||
$token = $this->generateToken();
|
||||
$identity = JwtIdentity::findIdentityByAccessToken($token);
|
||||
$this->assertSame($token, $identity->getId());
|
||||
$this->assertSame($this->tester->grabFixture('accounts', 'admin')['id'], $identity->getAccount()->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \yii\web\UnauthorizedHttpException
|
||||
* @expectedExceptionMessage Token expired
|
||||
*/
|
||||
public function testFindIdentityByAccessTokenWithExpiredToken() {
|
||||
$expiredToken = $this->generateToken(time() - 3600);
|
||||
JwtIdentity::findIdentityByAccessToken($expiredToken);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \yii\web\UnauthorizedHttpException
|
||||
* @expectedExceptionMessage Incorrect token
|
||||
*/
|
||||
public function testFindIdentityByAccessTokenWithEmptyToken() {
|
||||
JwtIdentity::findIdentityByAccessToken('');
|
||||
}
|
||||
|
||||
private function generateToken(int $expiresAt = null): string {
|
||||
/** @var \api\components\User\Component $component */
|
||||
$component = Yii::$app->user;
|
||||
/** @var \common\models\Account $account */
|
||||
$account = $this->tester->grabFixture('accounts', 'admin');
|
||||
$token = $component->createJwtAuthenticationToken($account);
|
||||
if ($expiresAt !== null) {
|
||||
$token->addClaim(new ExpirationClaim($expiresAt));
|
||||
}
|
||||
|
||||
return $component->serializeToken($token);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user