From 467983c4a87a7848ac62705cb6a1e924fdb1a357 Mon Sep 17 00:00:00 2001 From: hschoenenberger Date: Wed, 7 Aug 2024 11:31:21 +0200 Subject: [PATCH 01/12] fix: bump version number --- src/Account/LinkShop.php | 8 ++++++++ src/Account/Session/ShopSession.php | 11 +++++++++++ src/Adapter/Configuration.php | 23 ++++++++++++++++++++++ src/Repository/ConfigurationRepository.php | 13 ++++++++++++ 4 files changed, 55 insertions(+) diff --git a/src/Account/LinkShop.php b/src/Account/LinkShop.php index 40e3699ec..962e351b8 100644 --- a/src/Account/LinkShop.php +++ b/src/Account/LinkShop.php @@ -78,6 +78,14 @@ public function exists() return (bool) $this->getShopUuid(); } + /** + * @return string|null + */ + public function linkedAt() + { + return $this->configuration->getShopUuidDateUpd(); + } + /** * @return bool * diff --git a/src/Account/Session/ShopSession.php b/src/Account/Session/ShopSession.php index 5fc592439..d31335aac 100644 --- a/src/Account/Session/ShopSession.php +++ b/src/Account/Session/ShopSession.php @@ -183,7 +183,18 @@ private function getShopUuid() */ public function assertAssociationState() { + // TODO: try to reproduce disconnect with a delay before receiving oauth2client + // TODO: send oauth2 client with link shop payload + // TODO: check linkshop exists for [timeout] before unlinking + + $waitForOAuth2ClientSeconds = 60; + $linkedAtTs = $currentTs = time(); + if ($this->linkShop->linkedAt()) { + $linkedAtTs = (new \DateTime($this->linkShop->linkedAt()))->getTimestamp(); + } + if ($this->linkShop->exists() && + $currentTs - $linkedAtTs > $waitForOAuth2ClientSeconds && !$this->oauth2ClientProvider->getOauth2Client()->exists()) { throw new InconsistentAssociationStateException('Invalid OAuth2 client'); } diff --git a/src/Adapter/Configuration.php b/src/Adapter/Configuration.php index ab5bdc6c4..245f1c6c0 100644 --- a/src/Adapter/Configuration.php +++ b/src/Adapter/Configuration.php @@ -199,4 +199,27 @@ public function getUncached($key, $idShopGroup = null, $idShop = null, $default return $default; } + + /** + * @param string $key + * @param int|null $idShopGroup + * @param int|null $idShop + * @param mixed $default + * + * @return \Configuration + * + * @throw \Exception + */ + public function getUncachedConfiguration($key, $idShopGroup = null, $idShop = null, $default = false) + { + $id = \Configuration::getIdByName($key, $idShopGroup, $idShop); + if ($id > 0) { + $found = (new \Configuration($id)); + $found->clearCache(); + + return $found; + } + + throw new \Exception('Configuration entry not found'); + } } diff --git a/src/Repository/ConfigurationRepository.php b/src/Repository/ConfigurationRepository.php index 7677b176e..6474aa0b5 100644 --- a/src/Repository/ConfigurationRepository.php +++ b/src/Repository/ConfigurationRepository.php @@ -164,6 +164,19 @@ public function getShopUuid() return $this->configuration->get(ConfigurationKeys::PSX_UUID_V4); } + /** + * @return string|null + */ + public function getShopUuidDateUpd() + { + try { + $entry = $this->configuration->getUncachedConfiguration(ConfigurationKeys::PSX_UUID_V4, $this->idShopGroup, $this->idShop); + return $entry->date_upd; + } catch (\Exception $e) { + return null; + } + } + /** * @param string $uuid Firebase User UUID * From 45e7681661de9b1db22a24cc6f7d2b53e3f7810b Mon Sep 17 00:00:00 2001 From: hschoenenberger Date: Wed, 7 Aug 2024 14:05:36 +0200 Subject: [PATCH 02/12] fix: cleanup comments --- src/Account/Session/ShopSession.php | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/Account/Session/ShopSession.php b/src/Account/Session/ShopSession.php index d31335aac..0d8c5c969 100644 --- a/src/Account/Session/ShopSession.php +++ b/src/Account/Session/ShopSession.php @@ -177,17 +177,14 @@ private function getShopUuid() } /** - * @throws InconsistentAssociationStateException + * @param int $waitForOAuth2ClientSeconds * * @return void + * + * @throws InconsistentAssociationStateException */ - public function assertAssociationState() + public function assertAssociationState($waitForOAuth2ClientSeconds = 60) { - // TODO: try to reproduce disconnect with a delay before receiving oauth2client - // TODO: send oauth2 client with link shop payload - // TODO: check linkshop exists for [timeout] before unlinking - - $waitForOAuth2ClientSeconds = 60; $linkedAtTs = $currentTs = time(); if ($this->linkShop->linkedAt()) { $linkedAtTs = (new \DateTime($this->linkShop->linkedAt()))->getTimestamp(); From 14d58bc200b61204a951d425b248ea88d9e15dd3 Mon Sep 17 00:00:00 2001 From: hschoenenberger Date: Wed, 7 Aug 2024 14:11:48 +0200 Subject: [PATCH 03/12] fix: phpstan & php-cs-fixer --- src/Repository/ConfigurationRepository.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Repository/ConfigurationRepository.php b/src/Repository/ConfigurationRepository.php index 6474aa0b5..0f4835b65 100644 --- a/src/Repository/ConfigurationRepository.php +++ b/src/Repository/ConfigurationRepository.php @@ -170,7 +170,11 @@ public function getShopUuid() public function getShopUuidDateUpd() { try { - $entry = $this->configuration->getUncachedConfiguration(ConfigurationKeys::PSX_UUID_V4, $this->idShopGroup, $this->idShop); + $entry = $this->configuration->getUncachedConfiguration( + ConfigurationKeys::PSX_UUID_V4, + $this->configuration->getIdShopGroup(), + $this->configuration->getIdShop()); + return $entry->date_upd; } catch (\Exception $e) { return null; From 18ff1253598d178f695fe23b059dff271c4cee58 Mon Sep 17 00:00:00 2001 From: hschoenenberger Date: Wed, 7 Aug 2024 16:49:49 +0200 Subject: [PATCH 04/12] fix: unit tests --- src/Account/Session/ShopSession.php | 33 +++- .../Session/ShopSession/RefreshTokenTest.php | 183 ++++++++++++------ 2 files changed, 152 insertions(+), 64 deletions(-) diff --git a/src/Account/Session/ShopSession.php b/src/Account/Session/ShopSession.php index 0d8c5c969..6c8d3ad28 100644 --- a/src/Account/Session/ShopSession.php +++ b/src/Account/Session/ShopSession.php @@ -57,6 +57,11 @@ class ShopSession extends Session implements SessionInterface */ protected $linkShop; + /** + * @var int + */ + protected $waitForOAuth2ClientSeconds = 60; + /** * @param ConfigurationRepository $configurationRepository * @param ShopProvider $oauth2ClientProvider @@ -96,7 +101,7 @@ public function getOrRefreshToken($forceRefresh = false) public function refreshToken($refreshToken = null) { try { - $this->assertAssociationState(); + $this->assertAssociationState($this->waitForOAuth2ClientSeconds); $shopUuid = $this->getShopUuid(); $accessToken = $this->getAccessToken($shopUuid); @@ -146,6 +151,14 @@ public function cleanup() $this->configurationRepository->updateAccessToken(''); } + /** + * @param int $waitForOAuth2ClientSeconds + */ + public function setWaitForOAuth2ClientSeconds($waitForOAuth2ClientSeconds) + { + $this->waitForOAuth2ClientSeconds = $waitForOAuth2ClientSeconds; + } + /** * @param string $shopUid * @@ -168,14 +181,6 @@ protected function getAccessToken($shopUid) return $token; } - /** - * @return string - */ - private function getShopUuid() - { - return $this->linkShop->getShopUuid(); - } - /** * @param int $waitForOAuth2ClientSeconds * @@ -183,7 +188,7 @@ private function getShopUuid() * * @throws InconsistentAssociationStateException */ - public function assertAssociationState($waitForOAuth2ClientSeconds = 60) + protected function assertAssociationState($waitForOAuth2ClientSeconds = 60) { $linkedAtTs = $currentTs = time(); if ($this->linkShop->linkedAt()) { @@ -196,4 +201,12 @@ public function assertAssociationState($waitForOAuth2ClientSeconds = 60) throw new InconsistentAssociationStateException('Invalid OAuth2 client'); } } + + /** + * @return string + */ + private function getShopUuid() + { + return $this->linkShop->getShopUuid(); + } } diff --git a/tests/Unit/Account/Session/ShopSession/RefreshTokenTest.php b/tests/Unit/Account/Session/ShopSession/RefreshTokenTest.php index 5f4512972..be2f5e39a 100644 --- a/tests/Unit/Account/Session/ShopSession/RefreshTokenTest.php +++ b/tests/Unit/Account/Session/ShopSession/RefreshTokenTest.php @@ -4,6 +4,7 @@ use PrestaShop\Module\PsAccounts\Account\Session\ShopSession; use PrestaShop\Module\PsAccounts\Account\Token\Token; +use PrestaShop\Module\PsAccounts\Cqrs\CommandBus; use PrestaShop\Module\PsAccounts\Exception\RefreshTokenException; use PrestaShop\Module\PsAccounts\Provider\OAuth2\Oauth2Client; use PrestaShop\Module\PsAccounts\Provider\OAuth2\ShopProvider; @@ -21,12 +22,55 @@ class RefreshTokenTest extends TestCase */ protected $shopSession; + /** + * @inject + * + * @var Oauth2Client + */ + protected $oauth2Client; + + /** + * @inject + * + * @var ShopProvider + */ + protected $shopProvider; + + /** + * @var \PrestaShop\Module\PsAccounts\Vendor\Lcobucci\JWT\Token + */ + protected $validAccessToken; + + function setUp(): void + { + parent::setUp(); + + $this->validAccessToken = $this->makeJwtToken(new \DateTimeImmutable('tomorrow')); + $shopProvider = $this->createMock(ShopProvider::class); + $shopProvider->method('getAccessToken') + ->willReturn(new AccessToken([ + 'access_token' => (string)$this->validAccessToken + ])); + $shopProvider->method('getOauth2Client') + ->willReturn($this->oauth2Client); + + $commandBus = $this->createMock(CommandBus::class); + + $this->shopSession = new ShopSession( + $this->configurationRepository, + $shopProvider, + $this->linkShop, + $commandBus + ); + } + /** * @return void */ function tearDown(): void { parent::tearDown(); + $this->shopSession->cleanup(); } @@ -35,77 +79,108 @@ function tearDown(): void */ public function itShouldClearConfigurationAndThrowIfNotOauth() { - $shopSession = $this->createMockedSession(false, null); - $shopSession->cleanup(); - - $idToken = $this->makeJwtToken(new \DateTimeImmutable('yesterday'), [ - 'sub' => $this->faker->uuid, - ]); - $refreshToken = $this->makeJwtToken(new \DateTimeImmutable('+1 year')); - $shopSession->setToken((string) $idToken, (string) $refreshToken); + $e = null; try { - $shopSession->refreshToken(); - } catch (\Exception $e) { - $this->assertInstanceOf(RefreshTokenException::class, $e); - $token = $shopSession->getOrRefreshToken(); - $this->assertEquals("", $token->getJwt()); - $this->assertEquals("", $token->getRefreshToken()); - return; + // Fix single shop context + $this->configuration->setIdShop(null); + $this->configuration->setIdShopGroup(null); + + // Shop is linked + $this->linkShop->update(new \PrestaShop\Module\PsAccounts\Account\Dto\LinkShop([ + 'shopId' => 1, + 'uid' => $this->faker->uuid, + ])); + + // OAuth2Client has been cleared + $this->oauth2Client->delete(); + + $this->shopSession->setWaitForOAuth2ClientSeconds(1); + + sleep(2); + + $this->shopSession->refreshToken(); + } catch (RefreshTokenException $e) { + //$this->module->getLogger()->info($e->getMessage()); } - $this->fail('Test should have throw'); + + $this->assertInstanceOf(RefreshTokenException::class, $e); + $this->assertMatchesRegularExpression('/Invalid OAuth2 client/', $e->getMessage()); + $token = $this->shopSession->getOrRefreshToken(); + $this->assertEquals("", (string) $token->getJwt()); + $this->assertEquals("", (string) $token->getRefreshToken()); } /** * @test */ - public function itShouldRefreshToken() + public function itShouldNotClearConfigurationAndThrowIfNotOauth() { - $newAccessToken = $this->makeJwtToken(new \DateTimeImmutable('tomorrow')); - $shopSession = $this->createMockedSession(true, new AccessToken([ - 'access_token' => $newAccessToken->toString() - ])); - $shopSession->cleanup(); + $e = null; + try { + // Fix single shop context + $this->configuration->setIdShop(null); + $this->configuration->setIdShopGroup(null); - $clientId = $this->faker->uuid; - $clientSecret = $this->faker->uuid; + // Shop is linked + $this->linkShop->update(new \PrestaShop\Module\PsAccounts\Account\Dto\LinkShop([ + 'shopId' => 1, + 'uid' => $this->faker->uuid, + ])); - $this->configurationRepository->updateOauth2ClientId($clientId); - $this->configurationRepository->updateOauth2ClientSecret($clientSecret); + // OAuth2Client has been cleared + $this->oauth2Client->delete(); - try { - $token = $shopSession->refreshToken(); - } catch (\Exception $e) { - $this->fail('Test shouldn\'t throw'); + $this->shopSession->setWaitForOAuth2ClientSeconds(1); + + //sleep(2); + + $this->shopSession->refreshToken(); + } catch (RefreshTokenException $e) { + //$this->module->getLogger()->info($e->getMessage()); } - $this->assertEquals($this->configurationRepository->getAccessToken(), $newAccessToken->toString()); - $this->assertEquals($token, new Token($newAccessToken)); + $this->assertNull($e); + $token = $this->shopSession->getToken(); + $this->assertEquals((string) $this->validAccessToken, (string) $token->getJwt()); + $this->assertEquals("", (string) $token->getRefreshToken()); } /** - * @param $existResponse boolean - * @param $tokenResponse AccessToken - * @return ShopSession + * @test */ - private function createMockedSession($existResponse, $tokenResponse) { - $shopProvider = $this->getMockBuilder(ShopProvider::class) - ->disableOriginalConstructor() - ->getMock(); - $oauth2Client = $this->getMockBuilder(Oauth2Client::class) - ->disableOriginalConstructor() - ->getMock(); - $oauth2Client->method('exists')->willReturn($existResponse); - if ($tokenResponse) { - $shopProvider->method('getAccessToken')->willReturn($tokenResponse); - } else { - $shopProvider->method('getAccessToken')->willThrowException(new \Exception('Fail !!')); + public function itShouldRefreshToken() + { + $e = null; + try { + // Fix single shop context + //$this->configuration->setIdShop(null); + //$this->configuration->setIdShopGroup(null); + $this->shopSession->cleanup(); + + // Shop is linked + $this->linkShop->update(new \PrestaShop\Module\PsAccounts\Account\Dto\LinkShop([ + 'shopId' => 1, + 'uid' => $this->faker->uuid, + ])); + + // OAuth2Client exists + $this->oauth2Client->update( + $this->faker->uuid, + $this->faker->password + ); + + $this->shopSession->setWaitForOAuth2ClientSeconds(1); + + //sleep(2); + + $token = $this->shopSession->refreshToken(); + } catch (RefreshTokenException $e) { + //$this->module->getLogger()->info($e->getMessage()); } - $shopProvider->method('getOauth2Client')->willReturn($oauth2Client); - return new ShopSession( - $this->configurationRepository, - $shopProvider, - $this->linkShop, - $this->commandBus - ); + + $this->assertNull($e); + $this->assertEquals((string) $this->validAccessToken, (string) $token->getJwt()); + $this->assertEquals("", (string) $token->getRefreshToken()); + $this->assertEquals(new Token($this->validAccessToken), $token); } } From 3971ec8103395fcabe7ee1a177e2f84538f9494c Mon Sep 17 00:00:00 2001 From: hschoenenberger Date: Wed, 7 Aug 2024 16:52:12 +0200 Subject: [PATCH 05/12] fix: unit tests --- src/Account/Session/ShopSession.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Account/Session/ShopSession.php b/src/Account/Session/ShopSession.php index 6c8d3ad28..ca9f74342 100644 --- a/src/Account/Session/ShopSession.php +++ b/src/Account/Session/ShopSession.php @@ -153,6 +153,8 @@ public function cleanup() /** * @param int $waitForOAuth2ClientSeconds + * + * @return void */ public function setWaitForOAuth2ClientSeconds($waitForOAuth2ClientSeconds) { From 5462850d83ddbbb64d83dabc5b473dba301645e8 Mon Sep 17 00:00:00 2001 From: hschoenenberger Date: Wed, 7 Aug 2024 17:18:35 +0200 Subject: [PATCH 06/12] fix: unit tests --- tests/Unit/Account/Session/ShopSession/RefreshTokenTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/Account/Session/ShopSession/RefreshTokenTest.php b/tests/Unit/Account/Session/ShopSession/RefreshTokenTest.php index be2f5e39a..1c5762e26 100644 --- a/tests/Unit/Account/Session/ShopSession/RefreshTokenTest.php +++ b/tests/Unit/Account/Session/ShopSession/RefreshTokenTest.php @@ -104,7 +104,7 @@ public function itShouldClearConfigurationAndThrowIfNotOauth() } $this->assertInstanceOf(RefreshTokenException::class, $e); - $this->assertMatchesRegularExpression('/Invalid OAuth2 client/', $e->getMessage()); + $this->assertEquals(1, preg_match('/Invalid OAuth2 client/', $e->getMessage())); $token = $this->shopSession->getOrRefreshToken(); $this->assertEquals("", (string) $token->getJwt()); $this->assertEquals("", (string) $token->getRefreshToken()); From c0d9ae66f587d7d8e0d7020a2a0b1db2b67df875 Mon Sep 17 00:00:00 2001 From: hschoenenberger Date: Wed, 7 Aug 2024 18:15:50 +0200 Subject: [PATCH 07/12] fix: unit tests --- .../Session/ShopSession/RefreshTokenTest.php | 26 +++++++++---------- tests/phpunit.xml | 1 + 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/tests/Unit/Account/Session/ShopSession/RefreshTokenTest.php b/tests/Unit/Account/Session/ShopSession/RefreshTokenTest.php index 1c5762e26..9b62de851 100644 --- a/tests/Unit/Account/Session/ShopSession/RefreshTokenTest.php +++ b/tests/Unit/Account/Session/ShopSession/RefreshTokenTest.php @@ -62,6 +62,11 @@ function setUp(): void $this->linkShop, $commandBus ); + + // Fix single shop context + $this->configuration->setIdShop(null); + $this->configuration->setIdShopGroup(null); + $this->shopSession->cleanup(); } /** @@ -81,16 +86,15 @@ public function itShouldClearConfigurationAndThrowIfNotOauth() { $e = null; try { - // Fix single shop context - $this->configuration->setIdShop(null); - $this->configuration->setIdShopGroup(null); - // Shop is linked $this->linkShop->update(new \PrestaShop\Module\PsAccounts\Account\Dto\LinkShop([ 'shopId' => 1, 'uid' => $this->faker->uuid, ])); + echo "now : " . (new \DateTime())->getTimestamp() . "\n"; + echo "At : " . (new \DateTime($this->linkShop->linkedAt()))->getTimestamp() . "\n"; + // OAuth2Client has been cleared $this->oauth2Client->delete(); @@ -98,6 +102,9 @@ public function itShouldClearConfigurationAndThrowIfNotOauth() sleep(2); + echo "now : " . (new \DateTime())->getTimestamp() . "\n"; + echo "At : " . (new \DateTime($this->linkShop->linkedAt()))->getTimestamp() . "\n"; + $this->shopSession->refreshToken(); } catch (RefreshTokenException $e) { //$this->module->getLogger()->info($e->getMessage()); @@ -105,7 +112,7 @@ public function itShouldClearConfigurationAndThrowIfNotOauth() $this->assertInstanceOf(RefreshTokenException::class, $e); $this->assertEquals(1, preg_match('/Invalid OAuth2 client/', $e->getMessage())); - $token = $this->shopSession->getOrRefreshToken(); + $token = $this->shopSession->getToken(); $this->assertEquals("", (string) $token->getJwt()); $this->assertEquals("", (string) $token->getRefreshToken()); } @@ -117,10 +124,6 @@ public function itShouldNotClearConfigurationAndThrowIfNotOauth() { $e = null; try { - // Fix single shop context - $this->configuration->setIdShop(null); - $this->configuration->setIdShopGroup(null); - // Shop is linked $this->linkShop->update(new \PrestaShop\Module\PsAccounts\Account\Dto\LinkShop([ 'shopId' => 1, @@ -152,11 +155,6 @@ public function itShouldRefreshToken() { $e = null; try { - // Fix single shop context - //$this->configuration->setIdShop(null); - //$this->configuration->setIdShopGroup(null); - $this->shopSession->cleanup(); - // Shop is linked $this->linkShop->update(new \PrestaShop\Module\PsAccounts\Account\Dto\LinkShop([ 'shopId' => 1, diff --git a/tests/phpunit.xml b/tests/phpunit.xml index 6d00a8ec9..000b282a4 100644 --- a/tests/phpunit.xml +++ b/tests/phpunit.xml @@ -7,6 +7,7 @@ convertNoticesToExceptions="true" convertWarningsToExceptions="true" defaultTestSuite="all" + processIsolation="true" > From ebcd0a5e9186cde33a9e3c3a38383dc78ec12835 Mon Sep 17 00:00:00 2001 From: hschoenenberger Date: Wed, 7 Aug 2024 18:19:08 +0200 Subject: [PATCH 08/12] fix: unit tests --- tests/phpunit.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit.xml b/tests/phpunit.xml index 000b282a4..1ed9e085f 100644 --- a/tests/phpunit.xml +++ b/tests/phpunit.xml @@ -7,7 +7,7 @@ convertNoticesToExceptions="true" convertWarningsToExceptions="true" defaultTestSuite="all" - processIsolation="true" + processIsolation="false" > From f7a85b6f29a41014724f01df336600b391157c36 Mon Sep 17 00:00:00 2001 From: hschoenenberger Date: Thu, 8 Aug 2024 09:39:55 +0200 Subject: [PATCH 09/12] fix: unit tests --- .../Account/Session/ShopSession/RefreshTokenTest.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/tests/Unit/Account/Session/ShopSession/RefreshTokenTest.php b/tests/Unit/Account/Session/ShopSession/RefreshTokenTest.php index 9b62de851..84da65ff2 100644 --- a/tests/Unit/Account/Session/ShopSession/RefreshTokenTest.php +++ b/tests/Unit/Account/Session/ShopSession/RefreshTokenTest.php @@ -81,11 +81,18 @@ function tearDown(): void /** * @test + * + * @runInSeparateProcess */ public function itShouldClearConfigurationAndThrowIfNotOauth() { $e = null; try { + $this->shopSession->setWaitForOAuth2ClientSeconds(1); + + // OAuth2Client has been cleared + $this->oauth2Client->delete(); + // Shop is linked $this->linkShop->update(new \PrestaShop\Module\PsAccounts\Account\Dto\LinkShop([ 'shopId' => 1, @@ -95,11 +102,6 @@ public function itShouldClearConfigurationAndThrowIfNotOauth() echo "now : " . (new \DateTime())->getTimestamp() . "\n"; echo "At : " . (new \DateTime($this->linkShop->linkedAt()))->getTimestamp() . "\n"; - // OAuth2Client has been cleared - $this->oauth2Client->delete(); - - $this->shopSession->setWaitForOAuth2ClientSeconds(1); - sleep(2); echo "now : " . (new \DateTime())->getTimestamp() . "\n"; From 0dead7087e55311d85e9668d78c9c05d9bfb6558 Mon Sep 17 00:00:00 2001 From: hschoenenberger Date: Thu, 8 Aug 2024 12:27:15 +0200 Subject: [PATCH 10/12] fix: unit tests --- tests/TestCase.php | 3 +++ .../Session/ShopSession/RefreshTokenTest.php | 13 ++++--------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/tests/TestCase.php b/tests/TestCase.php index 3244c1a4b..e99460146 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -63,6 +63,9 @@ class TestCase extends \PHPUnit\Framework\TestCase */ protected function setUp(): void { + // Don't remove this line + \Configuration::clearConfigurationCacheForTesting(); + parent::setUp(); if (true === $this->enableTransactions) { diff --git a/tests/Unit/Account/Session/ShopSession/RefreshTokenTest.php b/tests/Unit/Account/Session/ShopSession/RefreshTokenTest.php index 84da65ff2..d5b2b6b5c 100644 --- a/tests/Unit/Account/Session/ShopSession/RefreshTokenTest.php +++ b/tests/Unit/Account/Session/ShopSession/RefreshTokenTest.php @@ -81,15 +81,11 @@ function tearDown(): void /** * @test - * - * @runInSeparateProcess */ public function itShouldClearConfigurationAndThrowIfNotOauth() { $e = null; try { - $this->shopSession->setWaitForOAuth2ClientSeconds(1); - // OAuth2Client has been cleared $this->oauth2Client->delete(); @@ -97,16 +93,15 @@ public function itShouldClearConfigurationAndThrowIfNotOauth() $this->linkShop->update(new \PrestaShop\Module\PsAccounts\Account\Dto\LinkShop([ 'shopId' => 1, 'uid' => $this->faker->uuid, + 'employeeId' => 5, + 'ownerUid' => $this->faker->uuid, + 'ownerEmail' => $this->faker->safeEmail, ])); - echo "now : " . (new \DateTime())->getTimestamp() . "\n"; - echo "At : " . (new \DateTime($this->linkShop->linkedAt()))->getTimestamp() . "\n"; + $this->shopSession->setWaitForOAuth2ClientSeconds(1); sleep(2); - echo "now : " . (new \DateTime())->getTimestamp() . "\n"; - echo "At : " . (new \DateTime($this->linkShop->linkedAt()))->getTimestamp() . "\n"; - $this->shopSession->refreshToken(); } catch (RefreshTokenException $e) { //$this->module->getLogger()->info($e->getMessage()); From 658cdca4f2d7113769e13649eab88d6cb241a207 Mon Sep 17 00:00:00 2001 From: hschoenenberger Date: Tue, 1 Oct 2024 15:01:54 +0200 Subject: [PATCH 11/12] refactor: renaming for clarity --- src/Account/Session/ShopSession.php | 10 +++++----- .../Account/Session/ShopSession/RefreshTokenTest.php | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Account/Session/ShopSession.php b/src/Account/Session/ShopSession.php index ca9f74342..c382295f7 100644 --- a/src/Account/Session/ShopSession.php +++ b/src/Account/Session/ShopSession.php @@ -60,7 +60,7 @@ class ShopSession extends Session implements SessionInterface /** * @var int */ - protected $waitForOAuth2ClientSeconds = 60; + protected $oauth2ClientReceiptTimeout = 60; /** * @param ConfigurationRepository $configurationRepository @@ -101,7 +101,7 @@ public function getOrRefreshToken($forceRefresh = false) public function refreshToken($refreshToken = null) { try { - $this->assertAssociationState($this->waitForOAuth2ClientSeconds); + $this->assertAssociationState($this->oauth2ClientReceiptTimeout); $shopUuid = $this->getShopUuid(); $accessToken = $this->getAccessToken($shopUuid); @@ -152,13 +152,13 @@ public function cleanup() } /** - * @param int $waitForOAuth2ClientSeconds + * @param int $oauth2ClientReceiptTimeout * * @return void */ - public function setWaitForOAuth2ClientSeconds($waitForOAuth2ClientSeconds) + public function setOauth2ClientReceiptTimeout($oauth2ClientReceiptTimeout) { - $this->waitForOAuth2ClientSeconds = $waitForOAuth2ClientSeconds; + $this->oauth2ClientReceiptTimeout = $oauth2ClientReceiptTimeout; } /** diff --git a/tests/Unit/Account/Session/ShopSession/RefreshTokenTest.php b/tests/Unit/Account/Session/ShopSession/RefreshTokenTest.php index d5b2b6b5c..12cf4678a 100644 --- a/tests/Unit/Account/Session/ShopSession/RefreshTokenTest.php +++ b/tests/Unit/Account/Session/ShopSession/RefreshTokenTest.php @@ -98,7 +98,7 @@ public function itShouldClearConfigurationAndThrowIfNotOauth() 'ownerEmail' => $this->faker->safeEmail, ])); - $this->shopSession->setWaitForOAuth2ClientSeconds(1); + $this->shopSession->setOauth2ClientReceiptTimeout(1); sleep(2); @@ -130,7 +130,7 @@ public function itShouldNotClearConfigurationAndThrowIfNotOauth() // OAuth2Client has been cleared $this->oauth2Client->delete(); - $this->shopSession->setWaitForOAuth2ClientSeconds(1); + $this->shopSession->setOauth2ClientReceiptTimeout(1); //sleep(2); @@ -164,7 +164,7 @@ public function itShouldRefreshToken() $this->faker->password ); - $this->shopSession->setWaitForOAuth2ClientSeconds(1); + $this->shopSession->setOauth2ClientReceiptTimeout(1); //sleep(2); From ba557d64bee045e0e4856640834c5e2bba457a55 Mon Sep 17 00:00:00 2001 From: hschoenenberger Date: Tue, 1 Oct 2024 15:44:57 +0200 Subject: [PATCH 12/12] fix: typo --- tests/Unit/Account/Session/ShopSession/RefreshTokenTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/Account/Session/ShopSession/RefreshTokenTest.php b/tests/Unit/Account/Session/ShopSession/RefreshTokenTest.php index 308969eb4..4c1e24225 100644 --- a/tests/Unit/Account/Session/ShopSession/RefreshTokenTest.php +++ b/tests/Unit/Account/Session/ShopSession/RefreshTokenTest.php @@ -118,7 +118,7 @@ public function itShouldClearConfigurationAndThrowIfNoOauth() /** * @test */ - public function itShouldNotClearConfigurationAndThrowIfNotOauth() + public function itShouldNotClearConfigurationAndThrowIfNoOauth() { $e = null; try {