Skip to content

Commit fecfeb4

Browse files
committed
Added special test token and implemented 'real' functional tests
1 parent 827cdf1 commit fecfeb4

File tree

7 files changed

+156
-37
lines changed

7 files changed

+156
-37
lines changed

KernelBrowser.php

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle;
1313

14+
use Symfony\Bundle\FrameworkBundle\Test\TestBrowserToken;
1415
use Symfony\Component\BrowserKit\Cookie;
1516
use Symfony\Component\BrowserKit\CookieJar;
1617
use Symfony\Component\BrowserKit\History;
@@ -20,7 +21,6 @@
2021
use Symfony\Component\HttpKernel\HttpKernelBrowser;
2122
use Symfony\Component\HttpKernel\KernelInterface;
2223
use Symfony\Component\HttpKernel\Profiler\Profile as HttpProfile;
23-
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
2424
use Symfony\Component\Security\Core\User\UserInterface;
2525

2626
/**
@@ -107,6 +107,31 @@ public function enableReboot()
107107
$this->reboot = true;
108108
}
109109

110+
/**
111+
* @param UserInterface $user
112+
*/
113+
public function loginUser($user, string $firewallContext = 'main'): self
114+
{
115+
if (!interface_exists(UserInterface::class)) {
116+
throw new \LogicException(sprintf('"%s" requires symfony/security-core to be installed.', __METHOD__));
117+
}
118+
119+
if (!$user instanceof UserInterface) {
120+
throw new \LogicException(sprintf('The first argument of "%s" must be instance of "%s", "%s" provided.', __METHOD__, UserInterface::class, \is_object($user) ? \get_class($user) : \gettype($user)));
121+
}
122+
123+
$token = new TestBrowserToken($user->getRoles(), $user);
124+
$token->setAuthenticated(true);
125+
$session = $this->getContainer()->get('session');
126+
$session->set('_security_'.$firewallContext, serialize($token));
127+
$session->save();
128+
129+
$cookie = new Cookie($session->getName(), $session->getId());
130+
$this->getCookieJar()->set($cookie);
131+
132+
return $this;
133+
}
134+
110135
/**
111136
* {@inheritdoc}
112137
*
@@ -206,17 +231,4 @@ protected function getScript($request)
206231

207232
return $code.$this->getHandleScript();
208233
}
209-
210-
public function loginUser(UserInterface $user, string $firewallContext = 'main'): self
211-
{
212-
$token = new UsernamePasswordToken($user, null, $firewallContext, $user->getRoles());
213-
$session = $this->getContainer()->get('session');
214-
$session->set('_security_'.$firewallContext, serialize($token));
215-
$session->save();
216-
217-
$cookie = new Cookie($session->getName(), $session->getId());
218-
$this->getCookieJar()->set($cookie);
219-
220-
return $this;
221-
}
222234
}

Test/TestBrowserToken.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Test;
13+
14+
use Symfony\Component\Security\Core\Authentication\Token\AbstractToken;
15+
use Symfony\Component\Security\Core\User\UserInterface;
16+
17+
/**
18+
* A very limited token that is used to login in tests using the KernelBrowser.
19+
*
20+
* @author Wouter de Jong <[email protected]>
21+
*/
22+
class TestBrowserToken extends AbstractToken
23+
{
24+
public function __construct(array $roles = [], UserInterface $user = null)
25+
{
26+
parent::__construct($roles);
27+
28+
if (null !== $user) {
29+
$this->setUser($user);
30+
}
31+
}
32+
33+
public function getCredentials()
34+
{
35+
return null;
36+
}
37+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller;
13+
14+
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
15+
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
16+
use Symfony\Component\HttpFoundation\Response;
17+
18+
class SecurityController implements ContainerAwareInterface
19+
{
20+
use ContainerAwareTrait;
21+
22+
public function profileAction()
23+
{
24+
return new Response('Welcome '.$this->container->get('security.token_storage')->getToken()->getUsername().'!');
25+
}
26+
}

Tests/Functional/SecurityTest.php

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,16 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;
1313

14-
use Symfony\Component\HttpFoundation\Session\SessionInterface;
15-
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
1614
use Symfony\Component\Security\Core\User\User;
1715

1816
class SecurityTest extends AbstractWebTestCase
1917
{
2018
/**
2119
* @dataProvider getUsers
2220
*/
23-
public function testLoginUser(string $username, ?string $password, array $roles, ?string $firewallContext, string $expectedProviderKey)
21+
public function testLoginUser(string $username, array $roles, ?string $firewallContext)
2422
{
25-
$user = new User($username, $password, $roles);
23+
$user = new User($username, 'the-password', $roles);
2624
$client = $this->createClient(['test_case' => 'Security', 'root_config' => 'config.yml']);
2725

2826
if (null === $firewallContext) {
@@ -31,27 +29,44 @@ public function testLoginUser(string $username, ?string $password, array $roles,
3129
$client->loginUser($user, $firewallContext);
3230
}
3331

34-
/** @var SessionInterface $session */
35-
$session = $client->getContainer()->get('session');
36-
/** @var UsernamePasswordToken $userToken */
37-
$userToken = unserialize($session->get('_security_'.$expectedProviderKey));
32+
$client->request('GET', '/'.($firewallContext ?? 'main').'/user_profile');
33+
$this->assertEquals('Welcome '.$username.'!', $client->getResponse()->getContent());
34+
}
3835

39-
$this->assertSame('_security_'.$expectedProviderKey, array_keys($session->all())[0]);
40-
$this->assertSame($expectedProviderKey, $userToken->getProviderKey());
41-
$this->assertSame($username, $userToken->getUsername());
42-
$this->assertSame($password, $userToken->getUser()->getPassword());
43-
$this->assertSame($roles, $userToken->getUser()->getRoles());
36+
public function getUsers()
37+
{
38+
yield ['the-username', ['ROLE_FOO'], null];
39+
yield ['the-username', ['ROLE_FOO'], 'main'];
40+
yield ['other-username', ['ROLE_FOO'], 'custom'];
4441

45-
$this->assertNotNull($client->getCookieJar()->get('MOCKSESSID'));
42+
yield ['the-username', ['ROLE_FOO'], null];
43+
yield ['no-role-username', [], null];
4644
}
4745

48-
public function getUsers()
46+
public function testLoginUserMultipleRequests()
4947
{
50-
yield ['the-username', 'the-password', ['ROLE_FOO'], null, 'main'];
51-
yield ['the-username', 'the-password', ['ROLE_FOO'], 'main', 'main'];
52-
yield ['the-username', 'the-password', ['ROLE_FOO'], 'custom_firewall_context', 'custom_firewall_context'];
48+
$user = new User('the-username', 'the-password', ['ROLE_FOO']);
49+
$client = $this->createClient(['test_case' => 'Security', 'root_config' => 'config.yml']);
50+
$client->loginUser($user);
51+
52+
$client->request('GET', '/main/user_profile');
53+
$this->assertEquals('Welcome the-username!', $client->getResponse()->getContent());
54+
55+
$client->request('GET', '/main/user_profile');
56+
$this->assertEquals('Welcome the-username!', $client->getResponse()->getContent());
57+
}
58+
59+
public function testLoginInBetweenRequests()
60+
{
61+
$user = new User('the-username', 'the-password', ['ROLE_FOO']);
62+
$client = $this->createClient(['test_case' => 'Security', 'root_config' => 'config.yml']);
63+
64+
$client->request('GET', '/main/user_profile');
65+
$this->assertTrue($client->getResponse()->isRedirect('http://localhost/login'));
66+
67+
$client->loginUser($user);
5368

54-
yield ['the-username', null, ['ROLE_FOO'], null, 'main'];
55-
yield ['the-username', 'the-password', [], null, 'main'];
69+
$client->request('GET', '/main/user_profile');
70+
$this->assertEquals('Welcome the-username!', $client->getResponse()->getContent());
5671
}
5772
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,26 @@
11
imports:
22
- { resource: ./../config/default.yml }
3+
4+
security:
5+
providers:
6+
main:
7+
memory:
8+
users:
9+
the-username: { password: the-password, roles: ['ROLE_FOO'] }
10+
no-role-username: { password: the-password, roles: [] }
11+
custom:
12+
memory:
13+
users:
14+
other-username: { password: the-password, roles: ['ROLE_FOO'] }
15+
16+
firewalls:
17+
main:
18+
pattern: ^/main
19+
form_login:
20+
check_path: /main/login/check
21+
provider: main
22+
custom:
23+
pattern: ^/custom
24+
form_login:
25+
check_path: /custom/login/check
26+
provider: custom
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1-
_sessiontest_bundle:
2-
resource: '@TestBundle/Resources/config/routing.yml'
1+
security_profile:
2+
path: /main/user_profile
3+
defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\SecurityController::profileAction }
4+
5+
security_custom_profile:
6+
path: /custom/user_profile
7+
defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\SecurityController::profileAction }

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"symfony/messenger": "^4.4|^5.0",
4848
"symfony/mime": "^4.4|^5.0",
4949
"symfony/process": "^4.4|^5.0",
50-
"symfony/security-bundle": "^4.0|^5.0",
50+
"symfony/security-bundle": "^5.1",
5151
"symfony/security-csrf": "^4.4|^5.0",
5252
"symfony/security-http": "^4.4|^5.0",
5353
"symfony/serializer": "^4.4|^5.0",

0 commit comments

Comments
 (0)