Skip to content

Commit

Permalink
WIP separate event tests
Browse files Browse the repository at this point in the history
  • Loading branch information
angel-dimitrov committed Sep 20, 2024
1 parent 3f7a440 commit 27dfbb8
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 60 deletions.
77 changes: 77 additions & 0 deletions tests/Integration/Event/PasswordChangeTest.php
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'));
}
}
81 changes: 81 additions & 0 deletions tests/Integration/Event/UserDeleteTest.php
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;
}
}
60 changes: 0 additions & 60 deletions tests/Integration/Infrastructure/TokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,66 +290,6 @@ public function testUserHasToken(): void
$this->assertFalse($this->tokenInfrastructure->userHasToken($otherUser, '_second'));
}

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'));
}

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'));
}

private function getTokenMock(
string $tokenId = self::TEST_TOKEN_ID,
string $userId = self::TEST_USER_ID
Expand Down

0 comments on commit 27dfbb8

Please sign in to comment.