Skip to content

Commit aab0bf7

Browse files
committed
merged branch schmittjoh/httpUtilFixes (PR #1739)
Commits ------- eae6a77 fixed wrong case d0a175b fixes #1659 f300ede fixes several bugs a4f05ac added some tests Discussion ---------- Http util fixes Fixes several bugs in the http utils. Please don't add anymore features without sufficient tests. Especially for the Security\Http namespace, regressions are very likely otherwise. --------------------------------------------------------------------------- by fabpot at 2011/07/19 22:37:26 -0700 Tests do not pass for me: There were 2 errors: 1) Symfony\Bundle\SecurityBundle\Tests\Functional\LocalizedRoutesAsPathTest::testLoginLogoutProcedure with data set #0 ('en') InvalidArgumentException: The current node list is empty. .../src/Symfony/Component/DomCrawler/Crawler.php:604 .../src/Symfony/Bundle/SecurityBundle/Tests/Functional/LocalizedRoutesAsPathTest.php:16 2) Symfony\Bundle\SecurityBundle\Tests\Functional\LocalizedRoutesAsPathTest::testLoginLogoutProcedure with data set #1 ('de') InvalidArgumentException: The current node list is empty. .../src/Symfony/Component/DomCrawler/Crawler.php:604 .../src/Symfony/Bundle/SecurityBundle/Tests/Functional/LocalizedRoutesAsPathTest.php:16 -- There were 4 failures: 1) Symfony\Bundle\SecurityBundle\Tests\Functional\LocalizedRoutesAsPathTest::testAccessRestrictedResource with data set #0 ('en') Failed asserting that two strings are equal. --- Expected +++ Actual @@ @@ -http://localhost/en/login +http://localhost/login .../src/Symfony/Bundle/Securitybundle/Tests/Functional/WebTestCase.php:22 .../src/Symfony/Bundle/SecurityBundle/Tests/Functional/LocalizedRoutesAsPathTest.php:38 2) Symfony\Bundle\SecurityBundle\Tests\Functional\LocalizedRoutesAsPathTest::testAccessRestrictedResource with data set #1 ('de') Failed asserting that two strings are equal. --- Expected +++ Actual @@ @@ -http://localhost/de/login +http://localhost/login .../src/Symfony/Bundle/Securitybundle/Tests/Functional/WebTestCase.php:22 .../src/Symfony/Bundle/SecurityBundle/Tests/Functional/LocalizedRoutesAsPathTest.php:38 3) Symfony\Bundle\SecurityBundle\Tests\Functional\LocalizedRoutesAsPathTest::testAccessRestrictedResourceWithForward with data set #0 ('en') HTTP/1.0 302 Found Cache-Control: no-cache Content-Length: 299 Content-Type: text/html; charset=UTF-8 Date: Wed, 20 Jul 2011 05:36:27 GMT Location: http://localhost/login Set-Cookie: PHPSESSID=11c9c6a7e7620e13bddef223a5ba46d9; path=/; domain= <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="refresh" content="1;url=http://localhost/login" /> </head> <body> Redirecting to <a href="http://localhost/login">http://localhost/login</a>. </body> </html> Failed asserting that <integer:0> matches expected <integer:1>. .../src/Symfony/Bundle/SecurityBundle/Tests/Functional/LocalizedRoutesAsPathTest.php:50 4) Symfony\Bundle\SecurityBundle\Tests\Functional\LocalizedRoutesAsPathTest::testAccessRestrictedResourceWithForward with data set #1 ('de') HTTP/1.0 302 Found Cache-Control: no-cache Content-Length: 299 Content-Type: text/html; charset=UTF-8 Date: Wed, 20 Jul 2011 05:36:28 GMT Location: http://localhost/login Set-Cookie: PHPSESSID=2bbe63786a088471ade3717917f4ba4f; path=/; domain= <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="refresh" content="1;url=http://localhost/login" /> </head> <body> Redirecting to <a href="http://localhost/login">http://localhost/login</a>. </body> </html> Failed asserting that <integer:0> matches expected <integer:1>. .../src/Symfony/Bundle/SecurityBundle/Tests/Functional/LocalizedRoutesAsPathTest.php:50 --------------------------------------------------------------------------- by schmittjoh at 2011/07/19 23:47:29 -0700 I fixed a wrong case, but I couldn't reproduce the other errors (tested on Ubuntu). My guess is that the temporary directory on your machine couldn't be deleted for some reason, and the test runs with the configuration of some of the previous tests. --------------------------------------------------------------------------- by fabpot at 2011/07/20 00:28:41 -0700 That does not make any difference for me. For instance, in `LocalizedRoutesAsPathTest::testLoginLogoutProcedure()`, the first request to `'/'.$locale.'/login'` returns the following Response: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="refresh" content="1;url=http://localhost/login" /> </head> <body> Redirecting to <a href="http://localhost/login">http://localhost/login</a>. </body> </html> --------------------------------------------------------------------------- by schmittjoh at 2011/07/20 00:31:34 -0700 That's weird, did you make sure that the temporary directory does not exist? ``rm -Rf /tmp/StandardFormLogin/`` On Wed, Jul 20, 2011 at 9:28 AM, fabpot < [email protected]>wrote: > That does not make any difference for me. For instance, in > `LocalizedRoutesAsPathTest::testLoginLogoutProcedure()`, the first request > to `'/'.$locale.'/login'` returns the following Response: > > <html> > <head> > <meta http-equiv="Content-Type" content="text/html; > charset=utf-8" /> > <meta http-equiv="refresh" content="1;url= > http://localhost/login" /> > </head> > <body> > Redirecting to <a href="http://localhost/login"> > http://localhost/login</a>. > </body> > </html> > > -- > Reply to this email directly or view it on GitHub: > symfony/symfony#1739 (comment) > --------------------------------------------------------------------------- by fabpot at 2011/07/20 00:33:40 -0700 Yes, I've just checked and the directory does not exist. --------------------------------------------------------------------------- by schmittjoh at 2011/07/20 00:39:55 -0700 Sorry, I can't reproduce it on Ubuntu and unless someone wants to sponsor me a Mac, there is not much I can do.
2 parents cc07af2 + eae6a77 commit aab0bf7

File tree

23 files changed

+403
-46
lines changed

23 files changed

+403
-46
lines changed

UPDATE.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ RC4 to RC5
8484
Session::getAttributes() -> Session::all()
8585
Session::setAttributes() -> Session::replace()
8686

87+
* {_locale} is not supported in paths in the access_control section anymore. You can
88+
rewrite the paths using a regular expression such as "(?:[a-z]{2})".
89+
8790
RC3 to RC4
8891
----------
8992

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\SecurityBundle\Tests\Functional;
4+
5+
class AuthenticationCommencingTest extends WebTestCase
6+
{
7+
public function testAuthenticationIsCommencingIfAccessDeniedExceptionIsWrapped()
8+
{
9+
$client = $this->createClient(array('test_case' => 'StandardFormLogin', 'root_config' => 'config.yml'));
10+
$client->insulate();
11+
12+
$client->request('GET', '/secure-but-not-covered-by-access-control');
13+
$this->assertRedirect($client->getResponse(), '/login');
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\FormLoginBundle\Controller;
4+
5+
use Symfony\Component\Security\Core\SecurityContext;
6+
use Symfony\Component\HttpFoundation\Response;
7+
use Symfony\Component\DependencyInjection\ContainerAware;
8+
9+
class LocalizedController extends ContainerAware
10+
{
11+
public function loginAction()
12+
{
13+
// get the login error if there is one
14+
if ($this->container->get('request')->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
15+
$error = $this->container->get('request')->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
16+
} else {
17+
$error = $this->container->get('request')->getSession()->get(SecurityContext::AUTHENTICATION_ERROR);
18+
}
19+
20+
return $this->container->get('templating')->renderResponse('FormLoginBundle:Localized:login.html.twig', array(
21+
// last username entered by the user
22+
'last_username' => $this->container->get('request')->getSession()->get(SecurityContext::LAST_USERNAME),
23+
'error' => $error,
24+
));
25+
}
26+
27+
public function loginCheckAction()
28+
{
29+
throw new \RuntimeException('loginCheckAction() should never be called.');
30+
}
31+
32+
public function logoutAction()
33+
{
34+
throw new \RuntimeException('logoutAction() should never be called.');
35+
}
36+
37+
public function secureAction()
38+
{
39+
throw new \RuntimeException('secureAction() should never be called.');
40+
}
41+
42+
public function profileAction()
43+
{
44+
return new Response('Profile');
45+
}
46+
47+
public function homepageAction()
48+
{
49+
return new Response('Homepage');
50+
}
51+
}

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

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

1212
namespace Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\FormLoginBundle\Controller;
1313

14+
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
1415
use Symfony\Component\HttpFoundation\Response;
1516
use Symfony\Component\Security\Core\SecurityContext;
1617
use Symfony\Component\DependencyInjection\ContainerAware;
@@ -42,4 +43,9 @@ public function loginCheckAction()
4243
{
4344
return new Response('', 400);
4445
}
46+
47+
public function secureAction()
48+
{
49+
throw new \Exception('Wrapper', 0, new \Exception('Another Wrapper', 0, new AccessDeniedException()));
50+
}
4551
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
localized_login_path:
2+
pattern: /{_locale}/login
3+
defaults: { _controller: FormLoginBundle:Localized:login }
4+
requirements: { _locale: "^[a-z]{2}$" }
5+
6+
localized_check_path:
7+
pattern: /{_locale}/login_check
8+
defaults: { _controller: FormLoginBundle:Localized:loginCheck }
9+
requirements: { _locale: "^[a-z]{2}$" }
10+
11+
localized_default_target_path:
12+
pattern: /{_locale}/profile
13+
defaults: { _controller: FormLoginBundle:Localized:profile }
14+
requirements: { _locale: "^[a-z]{2}$" }
15+
16+
localized_logout_path:
17+
pattern: /{_locale}/logout
18+
defaults: { _controller: FormLoginBundle:Localized:logout }
19+
requirements: { _locale: "^[a-z]{2}$" }
20+
21+
localized_logout_target_path:
22+
pattern: /{_locale}/
23+
defaults: { _controller: FormLoginBundle:Localized:homepage }
24+
requirements: { _locale: "^[a-z]{2}$" }
25+
26+
localized_secure_path:
27+
pattern: /{_locale}/secure/
28+
defaults: { _controller: FormLoginBundle:Localized:secure }
29+
requirements: { _locale: "^[a-z]{2}$" }
30+

src/Symfony/Bundle/SecurityBundle/Tests/Functional/Bundle/FormLoginBundle/Resources/config/routing.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ form_login_custom_target_path:
1414
pattern: /foo
1515
defaults: { _controller: FormLoginBundle:Login:afterLogin }
1616

17+
form_login_default_target_path:
18+
pattern: /profile
19+
defaults: { _controller: FormLoginBundle:Login:afterLogin }
20+
1721
form_login_redirect_to_protected_resource_after_login:
1822
pattern: /protected-resource
1923
defaults: { _controller: FormLoginBundle:Login:afterLogin }
24+
25+
form_logout:
26+
pattern: /logout_path
27+
28+
form_secure_action:
29+
pattern: /secure-but-not-covered-by-access-control
30+
defaults: { _controller: FormLoginBundle:Login:secure }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{% extends "::base.html.twig" %}
2+
3+
{% block body %}
4+
5+
{% if error %}
6+
<div>{{ error.message }}</div>
7+
{% endif %}
8+
9+
<form action="{{ path('localized_check_path') }}" method="post">
10+
<label for="username">Username:</label>
11+
<input type="text" id="username" name="_username" value="{{ last_username }}" />
12+
13+
<label for="password">Password:</label>
14+
<input type="password" id="password" name="_password" />
15+
16+
<input type="hidden" name="_target_path" value="" />
17+
18+
<input type="submit" name="login" />
19+
</form>
20+
21+
{% endblock %}

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

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,33 @@
1616
*/
1717
class FormLoginTest extends WebTestCase
1818
{
19-
public function testFormLogin()
19+
/**
20+
* @dataProvider getConfigs
21+
*/
22+
public function testFormLogin($config)
2023
{
21-
$client = $this->createClient(array('test_case' => 'StandardFormLogin'));
24+
$client = $this->createClient(array('test_case' => 'StandardFormLogin', 'root_config' => $config));
25+
$client->insulate();
2226

2327
$form = $client->request('GET', '/login')->selectButton('login')->form();
2428
$form['_username'] = 'johannes';
2529
$form['_password'] = 'test';
2630
$client->submit($form);
2731

28-
$this->assertRedirect($client->getResponse(), '/');
32+
$this->assertRedirect($client->getResponse(), '/profile');
2933

3034
$text = $client->followRedirect()->text();
3135
$this->assertContains('Hello johannes!', $text);
32-
$this->assertContains('You\'re browsing to path "/".', $text);
36+
$this->assertContains('You\'re browsing to path "/profile".', $text);
3337
}
3438

35-
public function testFormLoginWithCustomTargetPath()
39+
/**
40+
* @dataProvider getConfigs
41+
*/
42+
public function testFormLoginWithCustomTargetPath($config)
3643
{
37-
$client = $this->createClient(array('test_case' => 'StandardFormLogin'));
44+
$client = $this->createClient(array('test_case' => 'StandardFormLogin', 'root_config' => $config));
45+
$client->insulate();
3846

3947
$form = $client->request('GET', '/login')->selectButton('login')->form();
4048
$form['_username'] = 'johannes';
@@ -49,9 +57,13 @@ public function testFormLoginWithCustomTargetPath()
4957
$this->assertContains('You\'re browsing to path "/foo".', $text);
5058
}
5159

52-
public function testFormLoginRedirectsToProtectedResourceAfterLogin()
60+
/**
61+
* @dataProvider getConfigs
62+
*/
63+
public function testFormLoginRedirectsToProtectedResourceAfterLogin($config)
5364
{
54-
$client = $this->createClient(array('test_case' => 'StandardFormLogin'));
65+
$client = $this->createClient(array('test_case' => 'StandardFormLogin', 'root_config' => $config));
66+
$client->insulate();
5567

5668
$client->request('GET', '/protected-resource');
5769
$this->assertRedirect($client->getResponse(), '/login');
@@ -67,6 +79,14 @@ public function testFormLoginRedirectsToProtectedResourceAfterLogin()
6779
$this->assertContains('You\'re browsing to path "/protected-resource".', $text);
6880
}
6981

82+
public function getConfigs()
83+
{
84+
return array(
85+
array('config.yml'),
86+
array('routes_as_path.yml'),
87+
);
88+
}
89+
7090
protected function setUp()
7191
{
7292
parent::setUp();
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\SecurityBundle\Tests\Functional;
4+
5+
class LocalizedRoutesAsPathTest extends WebTestCase
6+
{
7+
/**
8+
* @dataProvider getLocales
9+
*/
10+
public function testLoginLogoutProcedure($locale)
11+
{
12+
$client = $this->createClient(array('test_case' => 'StandardFormLogin', 'root_config' => 'localized_routes.yml'));
13+
$client->insulate();
14+
15+
$crawler = $client->request('GET', '/'.$locale.'/login');
16+
$form = $crawler->selectButton('login')->form();
17+
$form['_username'] = 'johannes';
18+
$form['_password'] = 'test';
19+
$client->submit($form);
20+
21+
$this->assertRedirect($client->getResponse(), '/'.$locale.'/profile');
22+
$this->assertEquals('Profile', $client->followRedirect()->text());
23+
24+
$client->request('GET', '/'.$locale.'/logout');
25+
$this->assertRedirect($client->getResponse(), '/'.$locale.'/');
26+
$this->assertEquals('Homepage', $client->followRedirect()->text());
27+
}
28+
29+
/**
30+
* @dataProvider getLocales
31+
*/
32+
public function testAccessRestrictedResource($locale)
33+
{
34+
$client = $this->createClient(array('test_case' => 'StandardFormLogin', 'root_config' => 'localized_routes.yml'));
35+
$client->insulate();
36+
37+
$client->request('GET', '/'.$locale.'/secure/');
38+
$this->assertRedirect($client->getResponse(), '/'.$locale.'/login');
39+
}
40+
41+
/**
42+
* @dataProvider getLocales
43+
*/
44+
public function testAccessRestrictedResourceWithForward($locale)
45+
{
46+
$client = $this->createClient(array('test_case' => 'StandardFormLogin', 'root_config' => 'localized_routes_with_forward.yml'));
47+
$client->insulate();
48+
49+
$crawler = $client->request('GET', '/'.$locale.'/secure/');
50+
$this->assertEquals(1, count($crawler->selectButton('login')), (string) $client->getResponse());
51+
}
52+
53+
public function getLocales()
54+
{
55+
return array(array('en'), array('de'));
56+
}
57+
58+
protected function setUp()
59+
{
60+
parent::setUp();
61+
62+
$this->deleteTmpDir('StandardFormLogin');
63+
}
64+
65+
protected function tearDown()
66+
{
67+
parent::setUp();
68+
69+
$this->deleteTmpDir('StandardFormLogin');
70+
}
71+
}

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

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,37 @@
1313

1414
class SecurityRoutingIntegrationTest extends WebTestCase
1515
{
16-
public function testRoutingErrorIsNotExposedForProtectedResourceWhenAnonymous()
16+
/**
17+
* @dataProvider getConfigs
18+
*/
19+
public function testRoutingErrorIsNotExposedForProtectedResourceWhenAnonymous($config)
1720
{
18-
$client = $this->createClient(array('test_case' => 'StandardFormLogin'));
21+
$client = $this->createClient(array('test_case' => 'StandardFormLogin', 'root_config' => $config));
22+
$client->insulate();
1923
$client->request('GET', '/protected_resource');
2024

2125
$this->assertRedirect($client->getResponse(), '/login');
2226
}
2327

24-
public function testRoutingErrorIsExposedWhenNotProtected()
28+
/**
29+
* @dataProvider getConfigs
30+
*/
31+
public function testRoutingErrorIsExposedWhenNotProtected($config)
2532
{
26-
$client = $this->createClient(array('test_case' => 'StandardFormLogin'));
33+
$client = $this->createClient(array('test_case' => 'StandardFormLogin', 'root_config' => $config));
34+
$client->insulate();
2735
$client->request('GET', '/unprotected_resource');
2836

29-
$this->assertEquals(404, $client->getResponse()->getStatusCode());
37+
$this->assertEquals(404, $client->getResponse()->getStatusCode(), (string) $client->getResponse());
3038
}
3139

32-
public function testRoutingErrorIsNotExposedForProtectedResourceWhenLoggedInWithInsufficientRights()
40+
/**
41+
* @dataProvider getConfigs
42+
*/
43+
public function testRoutingErrorIsNotExposedForProtectedResourceWhenLoggedInWithInsufficientRights($config)
3344
{
34-
$client = $this->createClient(array('test_case' => 'StandardFormLogin'));
45+
$client = $this->createClient(array('test_case' => 'StandardFormLogin', 'root_config' => $config));
46+
$client->insulate();
3547

3648
$form = $client->request('GET', '/login')->selectButton('login')->form();
3749
$form['_username'] = 'johannes';
@@ -43,6 +55,11 @@ public function testRoutingErrorIsNotExposedForProtectedResourceWhenLoggedInWith
4355
$this->assertNotEquals(404, $client->getResponse()->getStatusCode());
4456
}
4557

58+
public function getConfigs()
59+
{
60+
return array(array('config.yml'), array('routes_as_path.yml'));
61+
}
62+
4663
protected function setUp()
4764
{
4865
parent::setUp();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class WebTestCase extends BaseWebTestCase
1818
{
1919
static public function assertRedirect($response, $location)
2020
{
21-
self::assertTrue($response->isRedirect());
21+
self::assertTrue($response->isRedirect(), 'Response is not a redirect, got status code: '.$response->getStatusCode());
2222
self::assertEquals('http://localhost'.$location, $response->headers->get('Location'));
2323
}
2424

0 commit comments

Comments
 (0)