Skip to content

Commit

Permalink
MISC: Add Security configuration to tests, since it's required on 7. …
Browse files Browse the repository at this point in the history
…Stop using Safe/DateTime, since the deprecation warning was killing my tests
  • Loading branch information
yivi committed Dec 19, 2023
1 parent c712de3 commit 28eab05
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 7 deletions.
4 changes: 0 additions & 4 deletions .idea/YivoffJwtRefreshBundle.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/DependencyInjection/BundleInitializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 3 additions & 1 deletion tests/Functional/PurgeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
},
]);

Expand Down Expand Up @@ -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');
},
]);

Expand All @@ -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());
Expand Down
6 changes: 4 additions & 2 deletions tests/Resource/PurgableInMemoryRefreshTokenProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@

namespace Yivoff\JwtRefreshBundle\Test\Resource;

use Safe\DateTimeImmutable;
use DateTimeImmutable;
use Yivoff\JwtRefreshBundle\Contracts\PurgableRefreshTokenProviderInterface;

class PurgableInMemoryRefreshTokenProvider extends InMemoryRefreshTokenProvider implements PurgableRefreshTokenProviderInterface
{
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]);
}
}
Expand Down
27 changes: 27 additions & 0 deletions tests/Resource/config/security-config.yaml
Original file line number Diff line number Diff line change
@@ -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 }

0 comments on commit 28eab05

Please sign in to comment.