From 28eab0544b3430582881725d815f0f3de6db07d3 Mon Sep 17 00:00:00 2001 From: yivi Date: Tue, 19 Dec 2023 12:10:10 +0100 Subject: [PATCH] MISC: Add Security configuration to tests, since it's required on 7. Stop using Safe/DateTime, since the deprecation warning was killing my tests --- .idea/YivoffJwtRefreshBundle.iml | 4 --- .../BundleInitializationTest.php | 1 + tests/Functional/PurgeCommandTest.php | 4 ++- .../PurgableInMemoryRefreshTokenProvider.php | 6 +++-- tests/Resource/config/security-config.yaml | 27 +++++++++++++++++++ 5 files changed, 35 insertions(+), 7 deletions(-) create mode 100644 tests/Resource/config/security-config.yaml diff --git a/.idea/YivoffJwtRefreshBundle.iml b/.idea/YivoffJwtRefreshBundle.iml index 45d602b..c572228 100644 --- a/.idea/YivoffJwtRefreshBundle.iml +++ b/.idea/YivoffJwtRefreshBundle.iml @@ -2,12 +2,8 @@ - - - - diff --git a/tests/DependencyInjection/BundleInitializationTest.php b/tests/DependencyInjection/BundleInitializationTest.php index 906e2c8..0492bc0 100644 --- a/tests/DependencyInjection/BundleInitializationTest.php +++ b/tests/DependencyInjection/BundleInitializationTest.php @@ -26,6 +26,7 @@ public function testServiceAndAliasesCreation(): void self::bootKernel([ 'config' => static function (TestKernel $kernel): void { $kernel->addTestConfig(__DIR__.'/../Resource/config/config.php'); + $kernel->addTestConfig(__DIR__.'/../Resource/config/security-config.yaml'); }, ]); $container = self::getContainer(); diff --git a/tests/Functional/PurgeCommandTest.php b/tests/Functional/PurgeCommandTest.php index 2ca15a8..9cf998b 100644 --- a/tests/Functional/PurgeCommandTest.php +++ b/tests/Functional/PurgeCommandTest.php @@ -29,6 +29,7 @@ public function testNonPurgableProviderFails(): void $kernel = self::bootKernel([ 'config' => static function (TestKernel $kernel): void { $kernel->addTestConfig(__DIR__.'/../Resource/config/config.php'); + $kernel->addTestConfig(__DIR__.'/../Resource/config/security-config.yaml'); }, ]); @@ -59,6 +60,7 @@ public function testPurgableProviderPurges(): void $kernel = self::bootKernel([ 'config' => static function (TestKernel $kernel): void { $kernel->addTestConfig(__DIR__.'/../Resource/config/config_purgable.php'); + $kernel->addTestConfig(__DIR__.'/../Resource/config/security-config.yaml'); }, ]); @@ -69,7 +71,7 @@ public function testPurgableProviderPurges(): void $provider->add(new RefreshToken('foo', '11111', 'baz', new DateTimeImmutable('-5 seconds'))); $provider->add(new RefreshToken('bar', '22222', 'baz', new DateTimeImmutable('-5 seconds'))); - $provider->add(new RefreshToken('baz', '33333', 'baz', new DateTimeImmutable('+5 seconds'))); + $provider->add(new RefreshToken('baz', '33333', 'baz', new DateTimeImmutable('+15 seconds'))); $application = new Application($kernel); $command = $application->find(PurgeExpiredTokensCommand::getDefaultName()); diff --git a/tests/Resource/PurgableInMemoryRefreshTokenProvider.php b/tests/Resource/PurgableInMemoryRefreshTokenProvider.php index 82e5fe4..52e7fab 100644 --- a/tests/Resource/PurgableInMemoryRefreshTokenProvider.php +++ b/tests/Resource/PurgableInMemoryRefreshTokenProvider.php @@ -4,7 +4,7 @@ namespace Yivoff\JwtRefreshBundle\Test\Resource; -use Safe\DateTimeImmutable; +use DateTimeImmutable; use Yivoff\JwtRefreshBundle\Contracts\PurgableRefreshTokenProviderInterface; class PurgableInMemoryRefreshTokenProvider extends InMemoryRefreshTokenProvider implements PurgableRefreshTokenProviderInterface @@ -12,7 +12,9 @@ class PurgableInMemoryRefreshTokenProvider extends InMemoryRefreshTokenProvider public function purgeExpiredTokens(): void { foreach ($this->tokens as $tokenId => $token) { - if ($token->getValidUntil() < (new DateTimeImmutable())->getTimestamp()) { + $now = (new DateTimeImmutable())->getTimestamp(); + $expiration = $token->getValidUntil(); + if ($expiration < $now) { unset($this->tokens[$tokenId]); } } diff --git a/tests/Resource/config/security-config.yaml b/tests/Resource/config/security-config.yaml new file mode 100644 index 0000000..c4b58fa --- /dev/null +++ b/tests/Resource/config/security-config.yaml @@ -0,0 +1,27 @@ +# config/packages/security.yaml +security: + # https://symfony.com/doc/current/security.html#registering-the-user-hashing-passwords + password_hashers: + Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto' + # https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers + providers: + users_in_memory: { memory: null } + firewalls: + dev: + pattern: ^/(_(profiler|wdt)|css|images|js)/ + security: false + main: + lazy: true + provider: users_in_memory + + # activate different ways to authenticate + # https://symfony.com/doc/current/security.html#firewalls-authentication + + # https://symfony.com/doc/current/security/impersonating_user.html + # switch_user: true + + # Easy way to control access for large sections of your site + # Note: Only the *first* access control that matches will be used + access_control: + # - { path: ^/admin, roles: ROLE_ADMIN } + # - { path: ^/profile, roles: ROLE_USER }