Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 18c56b7

Browse files
ajgarlagnorberttech
authored andcommitted
Checks token authentication.
Checks user preauth. Improve functional test.
1 parent dbd1647 commit 18c56b7

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

src/Symfony/Bundle/SecurityBundle/Tests/Functional/Bundle/LoginManagerBundle/Controller/LoginController.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@
1313

1414
use Symfony\Component\HttpFoundation\Response;
1515
use Symfony\Component\DependencyInjection\ContainerAware;
16-
use Symfony\Component\Security\Core\User\User;
1716

1817
class LoginController extends ContainerAware
1918
{
2019
public function loginAction()
2120
{
22-
$user = new User('norzechowicz', 'password123');
21+
$user = $this->container->get('security.user.provider.concrete.in_memory')->loadUserByUsername('norzechowicz');
2322
$this->container->get('security.login_manager')->loginUser('secured_area', $user);
2423

2524
return new Response();

src/Symfony/Bundle/SecurityBundle/Tests/Functional/LoginManagerTestCase.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,14 @@ class LoginManagerTestCase extends WebTestCase
1919
public function testLoginUserInController()
2020
{
2121
$client = $this->createClient(array('test_case' => 'LoginManager'));
22-
$client->insulate();
22+
23+
// Avoid to follow redirects. If we follow this redirect, the user
24+
// will be logged in automatically
25+
$client->setMaxRedirects(-1);
26+
$client->request('GET', '/secured/index');
27+
$this->assertRedirect($client->getResponse(), '/login');
28+
29+
// Access to '/login' route to login the user automatically
2330
$client->request('GET', '/login');
2431
$client->request('GET', '/secured/index');
2532
$this->assertEquals('Secured area', $client->getResponse()->getContent());

src/Symfony/Component/Security/Http/Login/LoginManager.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\Security\Core\User\UserInterface;
2020
use Symfony\Component\Security\Http\RememberMe\RememberMeServicesResolverInterface;
2121
use Symfony\Component\Security\Http\Session\SessionAuthenticationStrategyInterface;
22+
use Symfony\Component\Security\Core\Exception\AuthenticationException;
2223

2324
class LoginManager
2425
{
@@ -70,9 +71,14 @@ public function __construct(SecurityContextInterface $securityContext, UserCheck
7071
*/
7172
public function loginUser($firewallName, UserInterface $user, Response $response = null)
7273
{
74+
$this->userChecker->checkPreAuth($user);
7375
$this->userChecker->checkPostAuth($user);
7476
$token = $this->createToken($firewallName, $user);
7577

78+
if (!$token->isAuthenticated()) {
79+
throw new AuthenticationException("Unauthenticated token");
80+
}
81+
7682
$request = $this->requestStack->getMasterRequest();
7783
if (null !== $request) {
7884
$this->sessionAuthenticationStrategy->onAuthentication($request, $token);

src/Symfony/Component/Security/Http/Tests/Login/LoginManagerTest.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function setUp()
6363
public function testLoginWithoutRequest()
6464
{
6565
$loginManager = $this->createLoginManager();
66-
$user = new User('norzechowicz', 'password123');
66+
$user = new User('norzechowicz', 'password123', array('ROLE_USER'));
6767

6868
$this->userChecker->expects($this->once())
6969
->method('checkPostAuth')
@@ -83,7 +83,7 @@ public function testLoginWithoutRequest()
8383
public function testLoginWithRequest()
8484
{
8585
$loginManager = $this->createLoginManager();
86-
$user = new User('norzechowicz', 'password123');
86+
$user = new User('norzechowicz', 'password123', array('ROLE_USER'));
8787

8888
$this->userChecker->expects($this->once())
8989
->method('checkPostAuth')
@@ -110,7 +110,7 @@ public function testLoginWithRequest()
110110
public function testLoginWithRequestResponseAndRememberMeServices()
111111
{
112112
$loginManager = $this->createLoginManager();
113-
$user = new User('norzechowicz', 'password123');
113+
$user = new User('norzechowicz', 'password123', array('ROLE_USER'));
114114

115115
$this->userChecker->expects($this->once())
116116
->method('checkPostAuth')
@@ -144,6 +144,17 @@ public function testLoginWithRequestResponseAndRememberMeServices()
144144
$loginManager->loginUser(self::FIREWALL_NAME, $user, Response::create());
145145
}
146146

147+
/**
148+
* @expectedException \Symfony\Component\Security\Core\Exception\AuthenticationException
149+
*/
150+
public function testLoginShouldFailWithoutAuthenticatedToken()
151+
{
152+
$loginManager = $this->createLoginManager();
153+
$user = new User('norzechowicz', 'password123');
154+
155+
$loginManager->loginUser(self::FIREWALL_NAME, $user);
156+
}
157+
147158
/**
148159
* @return LoginManager
149160
*/

0 commit comments

Comments
 (0)