Skip to content

Commit 827cdf1

Browse files
javiereguiluzwouterj
authored andcommitted
[DX][Testing] Added a loginUser() method to test protected resources
1 parent b177450 commit 827cdf1

File tree

6 files changed

+98
-0
lines changed

6 files changed

+98
-0
lines changed

KernelBrowser.php

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

1212
namespace Symfony\Bundle\FrameworkBundle;
1313

14+
use Symfony\Component\BrowserKit\Cookie;
1415
use Symfony\Component\BrowserKit\CookieJar;
1516
use Symfony\Component\BrowserKit\History;
1617
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -19,6 +20,8 @@
1920
use Symfony\Component\HttpKernel\HttpKernelBrowser;
2021
use Symfony\Component\HttpKernel\KernelInterface;
2122
use Symfony\Component\HttpKernel\Profiler\Profile as HttpProfile;
23+
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
24+
use Symfony\Component\Security\Core\User\UserInterface;
2225

2326
/**
2427
* Simulates a browser and makes requests to a Kernel object.
@@ -203,4 +206,17 @@ protected function getScript($request)
203206

204207
return $code.$this->getHandleScript();
205208
}
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+
}
206222
}

Tests/Functional/SecurityTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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;
13+
14+
use Symfony\Component\HttpFoundation\Session\SessionInterface;
15+
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
16+
use Symfony\Component\Security\Core\User\User;
17+
18+
class SecurityTest extends AbstractWebTestCase
19+
{
20+
/**
21+
* @dataProvider getUsers
22+
*/
23+
public function testLoginUser(string $username, ?string $password, array $roles, ?string $firewallContext, string $expectedProviderKey)
24+
{
25+
$user = new User($username, $password, $roles);
26+
$client = $this->createClient(['test_case' => 'Security', 'root_config' => 'config.yml']);
27+
28+
if (null === $firewallContext) {
29+
$client->loginUser($user);
30+
} else {
31+
$client->loginUser($user, $firewallContext);
32+
}
33+
34+
/** @var SessionInterface $session */
35+
$session = $client->getContainer()->get('session');
36+
/** @var UsernamePasswordToken $userToken */
37+
$userToken = unserialize($session->get('_security_'.$expectedProviderKey));
38+
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());
44+
45+
$this->assertNotNull($client->getCookieJar()->get('MOCKSESSID'));
46+
}
47+
48+
public function getUsers()
49+
{
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'];
53+
54+
yield ['the-username', null, ['ROLE_FOO'], null, 'main'];
55+
yield ['the-username', 'the-password', [], null, 'main'];
56+
}
57+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
13+
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestBundle;
14+
use Symfony\Bundle\SecurityBundle\SecurityBundle;
15+
16+
return [
17+
new FrameworkBundle(),
18+
new SecurityBundle(),
19+
new TestBundle(),
20+
];
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
imports:
2+
- { resource: ./../config/default.yml }
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
_sessiontest_bundle:
2+
resource: '@TestBundle/Resources/config/routing.yml'

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +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",
5051
"symfony/security-csrf": "^4.4|^5.0",
5152
"symfony/security-http": "^4.4|^5.0",
5253
"symfony/serializer": "^4.4|^5.0",

0 commit comments

Comments
 (0)