Skip to content

Commit

Permalink
Rethink test config loading
Browse files Browse the repository at this point in the history
  • Loading branch information
chalasr committed Dec 8, 2016
1 parent f5901ff commit 781173a
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 70 deletions.
14 changes: 8 additions & 6 deletions DependencyInjection/Security/Factory/JWTUserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
use Symfony\Component\DependencyInjection\DefinitionDecorator;

/**
* JWTUserFactory.
* Creates the `lexik_jwt` user provider.
*
* @internal
*
* @author Robin Chalas <[email protected]>
*/
class JWTUserFactory implements UserProviderFactoryInterface
final class JWTUserFactory implements UserProviderFactoryInterface
{
public function create(ContainerBuilder $container, $id, $config)
{
Expand All @@ -35,10 +37,10 @@ public function addConfiguration(NodeDefinition $node)
->cannotBeEmpty()
->defaultValue(JWTUser::class)
->validate()
->ifTrue(function ($class) {
return !is_subclass_of($class, JWTUserInterface::class);
})
->thenInvalid('The %s class must implement JWTUserInterface.')
->ifTrue(function ($class) {
return !(new \ReflectionClass($class))->implementsInterface(JWTUserInterface::class);
})
->thenInvalid('The %s class must implement '.JWTUserInterface::class.' for using the "lexik_jwt" user provider.')
->end()
->end()
->end()
Expand Down
4 changes: 2 additions & 2 deletions Security/Guard/JWTTokenAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public function getUser($preAuthToken, UserProviderInterface $userProvider)
}

$payload = $preAuthToken->getPayload();
$user = $this->getUserFromPayload($userProvider, $payload);
$user = $this->doGetUser($userProvider, $payload);

$this->preAuthenticationTokenStorage->setToken($preAuthToken);

Expand Down Expand Up @@ -236,7 +236,7 @@ protected function getTokenExtractor()
return $this->tokenExtractor;
}

protected function getUserFromPayload(UserProviderInterface $userProvider, array $payload)
private function doGetUser(UserProviderInterface $userProvider, array $payload)
{
$identityField = $this->jwtManager->getUserIdentityField();

Expand Down
7 changes: 1 addition & 6 deletions Security/User/JWTUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@ public function __construct($username, array $roles = [])
}

/**
* Creates a new instance from a given JWT payload.
*
* @param string $username
* @param array $payload
*
* @return static
* {@inheritdoc}
*/
public static function createFromPayload($username, array $payload)
{
Expand Down
2 changes: 1 addition & 1 deletion Security/User/JWTUserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function loadUserByUsername($username, array $payload = null)
*/
public function supportsClass($class)
{
return $class === $this->class || is_subclass_of($class, $this->class);
return $class === $this->class || (new \ReflectionClass($class))->implementsInterface(JWTUserInterface::class);
}

public function refreshUser(UserInterface $user)
Expand Down
2 changes: 1 addition & 1 deletion Tests/Functional/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ protected static function createKernel(array $options = [])
{
require_once __DIR__.'/app/AppKernel.php';

return new AppKernel(getenv('SYMFONY__JWT__ENCODER') ?: 'default', true);
return new AppKernel('test', true);
}

protected static function createAuthenticatedClient($token = null)
Expand Down
3 changes: 2 additions & 1 deletion Tests/Functional/app/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function getLogDir()
*/
public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load(__DIR__.sprintf('/config/config_%s.yml', $this->getEnvironment()));
$loader->load(__DIR__.sprintf('/config/config_%s.yml', getenv('ENCODER') ?: 'default'));
$loader->load(__DIR__.sprintf('/config/security_%s.yml', getenv('PROVIDER') ?: 'in_memory'));
}
}
38 changes: 0 additions & 38 deletions Tests/Functional/app/config/base_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,6 @@ framework:
session:
storage_id: session.storage.mock_file

security:
encoders:
Symfony\Component\Security\Core\User\User: plaintext

providers:
in_memory:
memory:
users:
lexik:
password: dummy
roles: ROLE_USER
jwt:
lexik_jwt: ~

firewalls:
login:
pattern: ^/login
stateless: true
anonymous: true
provider: in_memory
form_login:
check_path: /login_check
require_previous_session: false
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure

api:
pattern: ^/api
stateless: true
anonymous: false
provider: jwt
guard:
authenticators:
- lexik_jwt_authentication.jwt_token_authenticator
access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/api, roles: IS_AUTHENTICATED_FULLY }

services:
lexik_jwt_authentication.test.jwt_event_subscriber:
class: Lexik\Bundle\JWTAuthenticationBundle\Tests\Functional\Utils\CallableEventSubscriber
Expand Down
18 changes: 18 additions & 0 deletions Tests/Functional/app/config/base_security.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
security:
encoders:
Symfony\Component\Security\Core\User\User: plaintext

providers:
in_memory:
memory:
users:
lexik:
password: dummy
roles: ROLE_USER
jwt:
lexik_jwt:
class: Lexik\Bundle\JWTAuthenticationBundle\Tests\Stubs\JWTUser

access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/api, roles: IS_AUTHENTICATED_FULLY }
24 changes: 24 additions & 0 deletions Tests/Functional/app/config/security_in_memory.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
imports:
- { resource: base_security.yml }

security:
firewalls:
login:
pattern: ^/login
stateless: true
anonymous: true
provider: in_memory
form_login:
check_path: /login_check
require_previous_session: false
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure

api:
pattern: ^/api
stateless: true
anonymous: false
provider: in_memory
guard:
authenticators:
- lexik_jwt_authentication.jwt_token_authenticator
24 changes: 24 additions & 0 deletions Tests/Functional/app/config/security_lexik_jwt.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
imports:
- { resource: base_security.yml }

security:
firewalls:
login:
pattern: ^/login
stateless: true
anonymous: true
provider: in_memory
form_login:
check_path: /login_check
require_previous_session: false
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure

api:
pattern: ^/api
stateless: true
anonymous: false
provider: jwt
guard:
authenticators:
- lexik_jwt_authentication.jwt_token_authenticator
13 changes: 0 additions & 13 deletions Tests/Stubs/JWTUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,4 @@

final class JWTUser extends BaseUser
{
private $email;

public function __construct($username, $email = null)
{
parent::__construct($username);

$this->email = $email;
}

public static function createFromPayload($username, array $payload)
{
return new self($username, isset($payload['email'] ? $payload['email'] : null);
}
}
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@
"scripts": {
"test": [
"vendor/bin/phpunit --testsuite unit",
"SYMFONY__JWT__ENCODER=default vendor/bin/phpunit --testsuite functional",
"SYMFONY__JWT__ENCODER=lcobucci vendor/bin/phpunit --testsuite functional"
"vendor/bin/phpunit --testsuite functional",
"ENCODER=lcobucci vendor/bin/phpunit --testsuite functional",
"PROVIDER=lexik_jwt vendor/bin/phpunit --testsuite functional"
]
}
}

0 comments on commit 781173a

Please sign in to comment.