SecurityRoutingIntegrationTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\SecurityBundle\Tests\Functional;
  11. class SecurityRoutingIntegrationTest extends WebTestCase
  12. {
  13. /**
  14. * @dataProvider getConfigs
  15. */
  16. public function testRoutingErrorIsNotExposedForProtectedResourceWhenAnonymous($config)
  17. {
  18. $client = $this->createClient(array('test_case' => 'StandardFormLogin', 'root_config' => $config));
  19. $client->request('GET', '/protected_resource');
  20. $this->assertRedirect($client->getResponse(), '/login');
  21. }
  22. /**
  23. * @dataProvider getConfigs
  24. */
  25. public function testRoutingErrorIsExposedWhenNotProtected($config)
  26. {
  27. $client = $this->createClient(array('test_case' => 'StandardFormLogin', 'root_config' => $config));
  28. $client->request('GET', '/unprotected_resource');
  29. $this->assertEquals(404, $client->getResponse()->getStatusCode(), (string) $client->getResponse());
  30. }
  31. /**
  32. * @dataProvider getConfigs
  33. */
  34. public function testRoutingErrorIsNotExposedForProtectedResourceWhenLoggedInWithInsufficientRights($config)
  35. {
  36. $client = $this->createClient(array('test_case' => 'StandardFormLogin', 'root_config' => $config));
  37. $form = $client->request('GET', '/login')->selectButton('login')->form();
  38. $form['_username'] = 'johannes';
  39. $form['_password'] = 'test';
  40. $client->submit($form);
  41. $client->request('GET', '/highly_protected_resource');
  42. $this->assertNotEquals(404, $client->getResponse()->getStatusCode());
  43. }
  44. /**
  45. * @dataProvider getConfigs
  46. */
  47. public function testSecurityConfigurationForSingleIPAddress($config)
  48. {
  49. $allowedClient = $this->createClient(array('test_case' => 'StandardFormLogin', 'root_config' => $config), array('REMOTE_ADDR' => '10.10.10.10'));
  50. $barredClient = $this->createClient(array('test_case' => 'StandardFormLogin', 'root_config' => $config), array('REMOTE_ADDR' => '10.10.20.10'));
  51. $this->assertAllowed($allowedClient, '/secured-by-one-ip');
  52. $this->assertRestricted($barredClient, '/secured-by-one-ip');
  53. }
  54. /**
  55. * @dataProvider getConfigs
  56. */
  57. public function testSecurityConfigurationForMultipleIPAddresses($config)
  58. {
  59. $allowedClientA = $this->createClient(array('test_case' => 'StandardFormLogin', 'root_config' => $config), array('REMOTE_ADDR' => '1.1.1.1'));
  60. $allowedClientB = $this->createClient(array('test_case' => 'StandardFormLogin', 'root_config' => $config), array('REMOTE_ADDR' => '2.2.2.2'));
  61. $barredClient = $this->createClient(array('test_case' => 'StandardFormLogin', 'root_config' => $config), array('REMOTE_ADDR' => '192.168.1.1'));
  62. $this->assertAllowed($allowedClientA, '/secured-by-two-ips');
  63. $this->assertAllowed($allowedClientB, '/secured-by-two-ips');
  64. $this->assertRestricted($barredClient, '/secured-by-two-ips');
  65. }
  66. /**
  67. * @dataProvider getConfigs
  68. */
  69. public function testSecurityConfigurationForExpression($config)
  70. {
  71. $allowedClient = $this->createClient(array('test_case' => 'StandardFormLogin', 'root_config' => $config), array('HTTP_USER_AGENT' => 'Firefox 1.0'));
  72. $this->assertAllowed($allowedClient, '/protected-via-expression');
  73. $barredClient = $this->createClient(array('test_case' => 'StandardFormLogin', 'root_config' => $config), array());
  74. $this->assertRestricted($barredClient, '/protected-via-expression');
  75. $allowedClient = $this->createClient(array('test_case' => 'StandardFormLogin', 'root_config' => $config), array());
  76. $allowedClient->request('GET', '/protected-via-expression');
  77. $form = $allowedClient->followRedirect()->selectButton('login')->form();
  78. $form['_username'] = 'johannes';
  79. $form['_password'] = 'test';
  80. $allowedClient->submit($form);
  81. $this->assertRedirect($allowedClient->getResponse(), '/protected-via-expression');
  82. $this->assertAllowed($allowedClient, '/protected-via-expression');
  83. }
  84. private function assertAllowed($client, $path)
  85. {
  86. $client->request('GET', $path);
  87. $this->assertEquals(404, $client->getResponse()->getStatusCode());
  88. }
  89. private function assertRestricted($client, $path)
  90. {
  91. $client->request('GET', $path);
  92. $this->assertEquals(302, $client->getResponse()->getStatusCode());
  93. }
  94. public function getConfigs()
  95. {
  96. return array(array('config.yml'), array('routes_as_path.yml'));
  97. }
  98. }