Skip to content

Fix JWT authentication test file with static container #1565

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions core/jwt.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ security:
# https://symfony.com/doc/current/security.html#c-hashing-passwords
password_hashers:
App\Entity\User: 'auto'

# https://symfony.com/doc/current/security/authenticator_manager.html
enable_authenticator_manager: true
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
Expand Down Expand Up @@ -280,11 +280,11 @@ And register this service in `config/services.yaml`:
```yaml
# api/config/services.yaml
services:
# ...
# ...

App\OpenApi\JwtDecorator:
decorates: 'api_platform.openapi.factory'
arguments: ['@.inner']
arguments: ['@.inner']
```

## Testing
Expand All @@ -308,14 +308,15 @@ class AuthenticationTest extends ApiTestCase
public function testLogin(): void
{
$client = self::createClient();
$container = self::getContainer();

$user = new User();
$user->setEmail('[email protected]');
$user->setPassword(
self::$container->get('security.user_password_hasher')->hashPassword($user, '$3CR3T')
$container->get('security.user_password_hasher')->hashPassword($user, '$3CR3T')
);

$manager = self::$container->get('doctrine')->getManager();
$manager = $container->get('doctrine')->getManager();
$manager->persist($user);
$manager->flush();

Expand Down Expand Up @@ -351,7 +352,7 @@ Since now we have a `JWT` authentication, functional tests require us to log in

Hashers are used for 2 reasons:

1. To generate a hash for a raw password (`self::$container->get('security.user_password_hasher')->hashPassword($user, '$3CR3T')`)
1. To generate a hash for a raw password (`$container->get('security.user_password_hasher')->hashPassword($user, '$3CR3T')`)
2. To verify a password during authentication

While hashing and verifying 1 password is quite a fast operation, doing it hundreds or even thousands of times in a tests suite becomes a bottleneck, because reliable hashing algorithms are slow by their nature.
Expand Down