-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[SecurityBundle] Add tests for debug:firewall
command
#61334
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
197 changes: 197 additions & 0 deletions
197
src/Symfony/Bundle/SecurityBundle/Tests/Command/DebugFirewallCommandTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\SecurityBundle\Tests\Command; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Bundle\SecurityBundle\Command\DebugFirewallCommand; | ||
use Symfony\Bundle\SecurityBundle\Security\FirewallConfig; | ||
use Symfony\Bundle\SecurityBundle\Security\FirewallContext; | ||
use Symfony\Bundle\SecurityBundle\Tests\Fixtures\DummyAuthenticator; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
use Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface; | ||
|
||
class DebugFirewallCommandTest extends TestCase | ||
{ | ||
public function testFirewallListOutputMatchesFixture() | ||
{ | ||
$firewallNames = ['main', 'api']; | ||
$contexts = $this->createMock(ContainerInterface::class); | ||
$eventDispatchers = $this->createMock(ContainerInterface::class); | ||
|
||
$command = new DebugFirewallCommand($firewallNames, $contexts, $eventDispatchers, []); | ||
$tester = new CommandTester($command); | ||
|
||
$this->assertSame(0, $tester->execute([])); | ||
$this->assertStringContainsString('Firewalls', $tester->getDisplay()); | ||
$this->assertStringContainsString('The following firewalls are defined:', $tester->getDisplay()); | ||
$this->assertStringContainsString('* main', $tester->getDisplay()); | ||
$this->assertStringContainsString('* api', $tester->getDisplay()); | ||
$this->assertStringContainsString('To view details of a specific firewall', $tester->getDisplay()); | ||
} | ||
|
||
public function testFirewallNotFoundDisplaysError() | ||
{ | ||
$firewallNames = ['main', 'api']; | ||
|
||
$contexts = $this->createMock(ContainerInterface::class); | ||
$contexts->method('has')->willReturn(false); | ||
|
||
$eventDispatchers = $this->createMock(ContainerInterface::class); | ||
$authenticators = []; | ||
|
||
$command = new DebugFirewallCommand( | ||
$firewallNames, | ||
$contexts, | ||
$eventDispatchers, | ||
$authenticators | ||
); | ||
|
||
$tester = new CommandTester($command); | ||
|
||
$this->assertSame(1, $tester->execute(['name' => 'admin'])); | ||
$this->assertStringContainsString('Firewall admin was not found.', $tester->getDisplay()); | ||
$this->assertStringContainsString('Available firewalls are: main, api', $tester->getDisplay()); | ||
} | ||
|
||
public function testFirewallMainOutputMatchesFixture() | ||
{ | ||
$firewallNames = ['main']; | ||
|
||
$config = new FirewallConfig( | ||
name: 'main', | ||
userChecker: 'user_checker_service', | ||
requestMatcher: null, | ||
securityEnabled: true, | ||
stateless: false, | ||
provider: 'user_provider_service', | ||
context: 'main', | ||
entryPoint: 'entry_point_service', | ||
accessDeniedHandler: 'access_denied_handler_service', | ||
accessDeniedUrl: '/access-denied', | ||
authenticators: [], | ||
switchUser: null | ||
); | ||
|
||
$context = new FirewallContext([], config: $config); | ||
|
||
$contexts = $this->createMock(ContainerInterface::class); | ||
$contexts->method('has')->willReturn(true); | ||
$contexts->method('get')->willReturn($context); | ||
|
||
$eventDispatchers = $this->createMock(ContainerInterface::class); | ||
$authenticator = new DummyAuthenticator(); | ||
$authenticators = ['main' => [$authenticator]]; | ||
|
||
$command = new DebugFirewallCommand($firewallNames, $contexts, $eventDispatchers, $authenticators); | ||
$tester = new CommandTester($command); | ||
|
||
$this->assertSame(0, $tester->execute(['name' => 'main', '--events' => true])); | ||
$this->assertEquals($this->getFixtureOutput('firewall_main_output.txt'), trim(str_replace(\PHP_EOL, "\n", $tester->getDisplay()))); | ||
} | ||
|
||
public function testFirewallWithEventsOutputMatchesFixture() | ||
{ | ||
$firewallNames = ['main']; | ||
|
||
$config = new FirewallConfig( | ||
name: 'main', | ||
userChecker: 'user_checker_service', | ||
context: 'main', | ||
stateless: false, | ||
provider: 'user_provider_service', | ||
entryPoint: 'entry_point_service', | ||
accessDeniedHandler: 'access_denied_handler_service', | ||
accessDeniedUrl: '/access-denied', | ||
); | ||
|
||
$context = new FirewallContext([], config: $config); | ||
|
||
$contexts = $this->createMock(ContainerInterface::class); | ||
$contexts->method('has')->willReturn(true); | ||
$contexts->method('get')->willReturn($context); | ||
|
||
$dispatcher = $this->createMock(EventDispatcherInterface::class); | ||
$listener = fn () => null; | ||
$listenerTwo = fn (int $number) => $number * 2; | ||
$dispatcher->method('getListeners')->willReturn([ | ||
'security.event' => [$listener, $listenerTwo], | ||
]); | ||
$dispatcher->method('getListenerPriority')->willReturn(42); | ||
|
||
$eventDispatchers = $this->createMock(ContainerInterface::class); | ||
$eventDispatchers->method('has')->willReturn(true); | ||
$eventDispatchers->method('get')->willReturn($dispatcher); | ||
|
||
$authenticator = new DummyAuthenticator(); | ||
$authenticatorTwo = new DummyAuthenticator(); | ||
$authenticatorThree = new DummyAuthenticator(); | ||
$authenticators = ['main' => [$authenticator, $authenticatorTwo], 'api' => [$authenticatorThree]]; | ||
|
||
$command = new DebugFirewallCommand($firewallNames, $contexts, $eventDispatchers, $authenticators); | ||
$tester = new CommandTester($command); | ||
|
||
$this->assertSame(0, $tester->execute(['name' => 'main', '--events' => true])); | ||
$this->assertEquals($this->getFixtureOutput('firewall_main_with_events_output.txt'), trim(str_replace(\PHP_EOL, "\n", $tester->getDisplay()))); | ||
} | ||
|
||
public function testFirewallWithSwitchUserDisplaysSection() | ||
{ | ||
$firewallNames = ['main']; | ||
|
||
$switchUserConfig = [ | ||
'parameter' => '_switch_user_test', | ||
'provider' => 'custom_provider_test', | ||
'role' => 'ROLE_ALLOWED_TO_SWITCH', | ||
]; | ||
|
||
$config = new FirewallConfig( | ||
name: 'main', | ||
userChecker: 'user_checker_service_test', | ||
context: 'main', | ||
stateless: false, | ||
provider: 'user_provider_service_test', | ||
entryPoint: 'entry_point_service_test', | ||
accessDeniedHandler: 'access_denied_handler_service_test', | ||
accessDeniedUrl: '/access-denied-test', | ||
switchUser: $switchUserConfig, | ||
); | ||
|
||
$context = new FirewallContext([], config: $config); | ||
|
||
$contexts = $this->createMock(ContainerInterface::class); | ||
$contexts->method('has')->willReturn(true); | ||
$contexts->method('get')->willReturn($context); | ||
|
||
$eventDispatchers = $this->createMock(ContainerInterface::class); | ||
$authenticator = new DummyAuthenticator(); | ||
$authenticatorTwo = $this->createMock(AuthenticatorInterface::class); | ||
$authenticators = ['main' => [$authenticator], 'api' => [$authenticatorTwo]]; | ||
|
||
$command = new DebugFirewallCommand( | ||
$firewallNames, | ||
$contexts, | ||
$eventDispatchers, | ||
$authenticators | ||
); | ||
$tester = new CommandTester($command); | ||
|
||
$this->assertSame(0, $tester->execute(['name' => 'main'])); | ||
$this->assertEquals($this->getFixtureOutput('firewall_main_with_switch_user.txt'), trim(str_replace(\PHP_EOL, "\n", $tester->getDisplay()))); | ||
} | ||
|
||
private function getFixtureOutput(string $file): string | ||
{ | ||
return trim(file_get_contents(__DIR__.'/../Fixtures/Descriptor/'.$file)); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/Symfony/Bundle/SecurityBundle/Tests/Fixtures/Descriptor/firewall_main_output.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
Firewall "main" | ||
=============== | ||
|
||
----------------------- ------------------------------- | ||
Option Value | ||
----------------------- ------------------------------- | ||
Name main | ||
Context main | ||
Lazy No | ||
Stateless No | ||
User Checker user_checker_service | ||
Provider user_provider_service | ||
Entry Point entry_point_service | ||
Access Denied URL /access-denied | ||
Access Denied Handler access_denied_handler_service | ||
----------------------- ------------------------------- | ||
|
||
Event listeners for firewall "main" | ||
=================================== | ||
|
||
No event dispatcher has been registered for this firewall. | ||
|
||
Authenticators for firewall "main" | ||
================================== | ||
|
||
----------------------------------------------------------------- | ||
Classname | ||
----------------------------------------------------------------- | ||
Symfony\Bundle\SecurityBundle\Tests\Fixtures\DummyAuthenticator | ||
----------------------------------------------------------------- |
39 changes: 39 additions & 0 deletions
39
...fony/Bundle/SecurityBundle/Tests/Fixtures/Descriptor/firewall_main_with_events_output.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
Firewall "main" | ||
=============== | ||
|
||
----------------------- ------------------------------- | ||
Option Value | ||
----------------------- ------------------------------- | ||
Name main | ||
Context main | ||
Lazy No | ||
Stateless No | ||
User Checker user_checker_service | ||
Provider user_provider_service | ||
Entry Point entry_point_service | ||
Access Denied URL /access-denied | ||
Access Denied Handler access_denied_handler_service | ||
----------------------- ------------------------------- | ||
|
||
Event listeners for firewall "main" | ||
=================================== | ||
|
||
"security.event" event | ||
---------------------- | ||
|
||
------- ----------- ---------- | ||
Order Callable Priority | ||
------- ----------- ---------- | ||
#1 Closure() 42 | ||
#2 Closure() 42 | ||
------- ----------- ---------- | ||
|
||
Authenticators for firewall "main" | ||
================================== | ||
|
||
----------------------------------------------------------------- | ||
Classname | ||
----------------------------------------------------------------- | ||
Symfony\Bundle\SecurityBundle\Tests\Fixtures\DummyAuthenticator | ||
Symfony\Bundle\SecurityBundle\Tests\Fixtures\DummyAuthenticator | ||
----------------------------------------------------------------- |
36 changes: 36 additions & 0 deletions
36
...ymfony/Bundle/SecurityBundle/Tests/Fixtures/Descriptor/firewall_main_with_switch_user.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
Firewall "main" | ||
=============== | ||
|
||
----------------------- ------------------------------------ | ||
Option Value | ||
----------------------- ------------------------------------ | ||
Name main | ||
Context main | ||
Lazy No | ||
Stateless No | ||
User Checker user_checker_service_test | ||
Provider user_provider_service_test | ||
Entry Point entry_point_service_test | ||
Access Denied URL /access-denied-test | ||
Access Denied Handler access_denied_handler_service_test | ||
----------------------- ------------------------------------ | ||
|
||
User switching | ||
-------------- | ||
|
||
----------- ------------------------ | ||
Option Value | ||
----------- ------------------------ | ||
Parameter _switch_user_test | ||
Provider custom_provider_test | ||
User Role ROLE_ALLOWED_TO_SWITCH | ||
----------- ------------------------ | ||
|
||
Authenticators for firewall "main" | ||
================================== | ||
|
||
----------------------------------------------------------------- | ||
Classname | ||
----------------------------------------------------------------- | ||
Symfony\Bundle\SecurityBundle\Tests\Fixtures\DummyAuthenticator | ||
----------------------------------------------------------------- |
50 changes: 50 additions & 0 deletions
50
src/Symfony/Bundle/SecurityBundle/Tests/Fixtures/DummyAuthenticator.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\SecurityBundle\Tests\Fixtures; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
use Symfony\Component\Security\Core\Exception\AuthenticationException; | ||
use Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface; | ||
use Symfony\Component\Security\Http\Authenticator\Passport\Passport; | ||
use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface; | ||
|
||
class DummyAuthenticator implements AuthenticatorInterface | ||
{ | ||
public function supports(Request $request): ?bool | ||
{ | ||
return null; | ||
} | ||
|
||
public function authenticate(Request $request): Passport | ||
{ | ||
} | ||
|
||
public function createToken(Passport $passport, string $firewallName): TokenInterface | ||
{ | ||
} | ||
|
||
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response | ||
{ | ||
return null; | ||
} | ||
|
||
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response | ||
{ | ||
return null; | ||
} | ||
|
||
public function createAuthenticatedToken(PassportInterface $passport, string $firewallName): TokenInterface | ||
{ | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would inline those outputs and newline at end of file missing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, thanks so much for your suggestion! 😊
I believe the change has been made, please let me know if you'd like me to adjust anything else ❤️