-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3f7a440
commit 27dfbb8
Showing
3 changed files
with
158 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright © OXID eSales AG. All rights reserved. | ||
* See LICENSE file for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OxidEsales\GraphQL\Base\Tests\Integration\Infrastructure; | ||
|
||
use DateTimeImmutable; | ||
use OxidEsales\Eshop\Application\Model\User; | ||
use OxidEsales\EshopCommunity\Internal\Framework\Database\ConnectionProviderInterface; | ||
use OxidEsales\EshopCommunity\Tests\Integration\IntegrationTestCase; | ||
use OxidEsales\EshopCommunity\Tests\TestContainerFactory; | ||
use OxidEsales\GraphQL\Base\DataType\User as UserDataType; | ||
use OxidEsales\GraphQL\Base\Infrastructure\Model\Token as TokenModel; | ||
use OxidEsales\GraphQL\Base\Infrastructure\Token as TokenInfrastructure; | ||
|
||
class PasswordChangeTest extends IntegrationTestCase | ||
{ | ||
/** @var TokenInfrastructure */ | ||
private $tokenInfrastructure; | ||
|
||
/** @var ConnectionProviderInterface */ | ||
private $connection; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$containerFactory = new TestContainerFactory(); | ||
$container = $containerFactory->create(); | ||
$container->compile(); | ||
$this->tokenInfrastructure = $container->get(TokenInfrastructure::class); | ||
$this->connection = $container->get(ConnectionProviderInterface::class)->get(); | ||
} | ||
|
||
public function testExpireTokenAfterUserPasswordChange(): void | ||
{ | ||
$userModel = oxNew(User::class); | ||
$userModel->load('e7af1c3b786fd02906ccd75698f4e6b9'); | ||
|
||
$issued = new DateTimeImmutable('now'); | ||
$expires = new DateTimeImmutable('+8 hours'); | ||
$tokenModel = oxNew(TokenModel::class); | ||
$tokenModel->setId('_changePwdUserToken'); | ||
$tokenModel->assign( | ||
[ | ||
'OXID' => '_changePwdUserToken', | ||
'OXSHOPID' => '1', | ||
'OXUSERID' => 'e7af1c3b786fd02906ccd75698f4e6b9', | ||
'ISSUED_AT' => $issued->format('Y-m-d H:i:s'), | ||
'EXPIRES_AT' => $expires->format('Y-m-d H:i:s'), | ||
'USERAGENT' => '', | ||
'TOKEN' => 'very_large_string', | ||
] | ||
); | ||
$tokenModel->save(); | ||
$tokenModel->load('_changePwdUserToken'); | ||
|
||
$user = new UserDataType($userModel); | ||
$this->assertTrue($this->tokenInfrastructure->userHasToken($user, '_changePwdUserToken')); | ||
$this->assertFalse(new DateTimeImmutable($tokenModel->getRawFieldData('expires_at')) <= new DateTimeImmutable('now')); | ||
|
||
$userModel->setPassword('_newPassword'); | ||
$userModel->save(); | ||
|
||
$result = $this->connection->executeQuery( | ||
"select expires_at from `oegraphqltoken` where oxid=:tokenId", | ||
['tokenId' => '_changePwdUserToken'] | ||
); | ||
$tokenDateAfterChange = $result->fetchOne(); | ||
|
||
$this->assertTrue(new DateTimeImmutable($tokenDateAfterChange) <= new DateTimeImmutable('now')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright © OXID eSales AG. All rights reserved. | ||
* See LICENSE file for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OxidEsales\GraphQL\Base\Tests\Integration\Infrastructure; | ||
|
||
use DateTimeImmutable; | ||
use Lcobucci\JWT\Token\DataSet; | ||
use Lcobucci\JWT\UnencryptedToken; | ||
use OxidEsales\Eshop\Application\Model\User; | ||
use OxidEsales\EshopCommunity\Tests\Integration\IntegrationTestCase; | ||
use OxidEsales\EshopCommunity\Tests\TestContainerFactory; | ||
use OxidEsales\GraphQL\Base\DataType\User as UserDataType; | ||
use OxidEsales\GraphQL\Base\Infrastructure\Token as TokenInfrastructure; | ||
use OxidEsales\GraphQL\Base\Service\Token as TokenService; | ||
|
||
class UserDeleteTest extends IntegrationTestCase | ||
{ | ||
private const TEST_TOKEN_ID = '_my_test_token'; | ||
|
||
private const TEST_USER_ID = '_testuser'; | ||
|
||
/** @var TokenInfrastructure */ | ||
private $tokenInfrastructure; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$containerFactory = new TestContainerFactory(); | ||
$container = $containerFactory->create(); | ||
$container->compile(); | ||
$this->tokenInfrastructure = $container->get(TokenInfrastructure::class); | ||
} | ||
|
||
public function testInvalidateTokenAfterDeleteUser(): void | ||
{ | ||
$userModel = oxNew(User::class); | ||
$userModel->setId('_testUser'); | ||
$userModel->setPassword('_testPassword'); | ||
$userModel->assign(['oxusername' => '_testUsername']); | ||
$userModel->save(); | ||
|
||
$this->tokenInfrastructure->registerToken( | ||
$this->getTokenMock('_deletedUser'), | ||
new DateTimeImmutable('now'), | ||
new DateTimeImmutable('+8 hours') | ||
); | ||
|
||
$user = new UserDataType($userModel); | ||
$this->assertTrue($this->tokenInfrastructure->userHasToken($user, '_deletedUser')); | ||
|
||
$userModel->delete(self::TEST_USER_ID); | ||
$this->assertFalse($this->tokenInfrastructure->isTokenRegistered('_deletedUser')); | ||
} | ||
|
||
private function getTokenMock( | ||
string $tokenId = self::TEST_TOKEN_ID, | ||
string $userId = self::TEST_USER_ID | ||
): UnencryptedToken { | ||
$claims = new DataSet( | ||
[ | ||
TokenService::CLAIM_TOKENID => $tokenId, | ||
TokenService::CLAIM_SHOPID => 1, | ||
TokenService::CLAIM_USERID => $userId, | ||
], | ||
'' | ||
); | ||
|
||
$token = $this->getMockBuilder(UnencryptedToken::class) | ||
->getMock(); | ||
$token->method('claims')->willReturn($claims); | ||
$token->method('toString')->willReturn('here_is_the_string_token'); | ||
|
||
return $token; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters