Skip to content

Commit f3eb22c

Browse files
committed
OXDEV-8407 Add tests for isTokenExpired
1 parent 4e4cbdf commit f3eb22c

2 files changed

Lines changed: 26 additions & 19 deletions

File tree

CHANGELOG-v10.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3030
- New methods:
3131
- `OxidEsales\GraphQL\Base\Infrastructure\RefreshTokenRepositoryInterface::invalidateUserTokens`
3232
- `OxidEsales\GraphQL\Base\Infrastructure\Token::invalidateUserTokens`
33+
- `OxidEsales\GraphQL\Base\Infrastructure\Token::isTokenExpired`
3334
- New event subscriber:
3435
- `OxidEsales\GraphQL\Base\Event\Subscriber\PasswordChangeSubscriber`
3536

tests/Integration/Infrastructure/TokenTest.php

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
use Lcobucci\JWT\Token\DataSet;
1414
use Lcobucci\JWT\UnencryptedToken;
1515
use OxidEsales\Eshop\Application\Model\User;
16-
use OxidEsales\EshopCommunity\Internal\Framework\Database\ConnectionProviderInterface;
1716
use OxidEsales\EshopCommunity\Tests\Integration\IntegrationTestCase;
1817
use OxidEsales\EshopCommunity\Tests\TestContainerFactory;
1918
use OxidEsales\GraphQL\Base\DataType\Token as TokenDataType;
2019
use OxidEsales\GraphQL\Base\DataType\User as UserDataType;
2120
use OxidEsales\GraphQL\Base\Infrastructure\Model\Token as TokenModel;
2221
use OxidEsales\GraphQL\Base\Infrastructure\Token as TokenInfrastructure;
22+
use OxidEsales\GraphQL\Base\Service\Token;
2323
use OxidEsales\GraphQL\Base\Service\Token as TokenService;
2424

2525
class TokenTest extends IntegrationTestCase
@@ -31,17 +31,13 @@ class TokenTest extends IntegrationTestCase
3131
/** @var TokenInfrastructure */
3232
private $tokenInfrastructure;
3333

34-
/** @var ConnectionProviderInterface */
35-
private $connection;
36-
3734
public function setUp(): void
3835
{
3936
parent::setUp();
4037
$containerFactory = new TestContainerFactory();
4138
$container = $containerFactory->create();
4239
$container->compile();
4340
$this->tokenInfrastructure = $container->get(TokenInfrastructure::class);
44-
$this->connection = $container->get(ConnectionProviderInterface::class)->get();
4541
}
4642

4743
public function testRegisterToken(): void
@@ -74,6 +70,28 @@ public function testIsTokenRegisteredNo(): void
7470
$this->assertFalse($this->tokenInfrastructure->isTokenRegistered('not_registered_token'));
7571
}
7672

73+
public function testIsTokenExpired(): void
74+
{
75+
$this->tokenInfrastructure->registerToken(
76+
$this->getTokenMock('valid_token'),
77+
new DateTimeImmutable('now'),
78+
new DateTimeImmutable('+8 hours')
79+
);
80+
81+
$this->assertFalse($this->tokenInfrastructure->isTokenExpired('valid_token'));
82+
}
83+
84+
public function testIsTokenExpiredNo(): void
85+
{
86+
$this->tokenInfrastructure->registerToken(
87+
$this->getTokenMock('expired_token'),
88+
new DateTimeImmutable('now'),
89+
new DateTimeImmutable('-8 hours')
90+
);
91+
92+
$this->assertTrue($this->tokenInfrastructure->isTokenExpired('expired_token'));
93+
}
94+
7795
public function testRemoveExpiredTokens(): void
7896
{
7997
$this->tokenInfrastructure->registerToken(
@@ -323,13 +341,7 @@ public function testInvalidateAccessTokens(): void
323341
);
324342

325343
$this->tokenInfrastructure->invalidateUserTokens($userId);
326-
$result = $this->connection->executeQuery(
327-
"select expires_at from `oegraphqltoken` where oxid=:tokenId",
328-
['tokenId' => $token]
329-
);
330-
$expiresAtAfterChange = $result->fetchOne();
331-
332-
$this->assertTrue(new DateTimeImmutable($expiresAtAfterChange) <= new DateTimeImmutable('now'));
344+
$this->assertTrue($this->tokenInfrastructure->isTokenExpired($token));
333345
}
334346

335347
public function testInvalidateAccessTokensWrongUserId(): void
@@ -344,13 +356,7 @@ public function testInvalidateAccessTokensWrongUserId(): void
344356
);
345357

346358
$this->tokenInfrastructure->invalidateUserTokens('wrong_user_id');
347-
$result = $this->connection->executeQuery(
348-
"select expires_at from `oegraphqltoken` where oxid=:tokenId",
349-
['tokenId' => $token]
350-
);
351-
$expiresAtAfterChange = $result->fetchOne();
352-
353-
$this->assertFalse(new DateTimeImmutable($expiresAtAfterChange) <= new DateTimeImmutable('now'));
359+
$this->assertFalse($this->tokenInfrastructure->isTokenExpired($token));
354360
}
355361

356362
private function getTokenMock(

0 commit comments

Comments
 (0)