Commit bd18b1fc authored by Fabien Potencier's avatar Fabien Potencier

minor #1101 Improve Silex\Route\SecurityTrait coverage (SofHad)

This PR was submitted for the 1.3 branch but it was merged into the 1.2 branch instead (closes #1101).

Discussion
----------

Improve Silex\Route\SecurityTrait coverage

Commits
-------

a1240023 Improve Silex\Route\SecurityTrait coverage
parents 417deb44 a1240023
...@@ -24,20 +24,9 @@ use Symfony\Component\HttpFoundation\Request; ...@@ -24,20 +24,9 @@ use Symfony\Component\HttpFoundation\Request;
*/ */
class SecurityTraitTest extends \PHPUnit_Framework_TestCase class SecurityTraitTest extends \PHPUnit_Framework_TestCase
{ {
public function testSecure() public function testSecureWithNoAuthenticatedUser()
{ {
$app = new Application(); $app = $this->createApplication();
$app['route_class'] = 'Silex\Tests\Route\SecurityRoute';
$app->register(new SecurityServiceProvider(), array(
'security.firewalls' => array(
'default' => array(
'http' => true,
'users' => array(
'fabien' => array('ROLE_ADMIN', '5FZ2Z8QIkA7UTZ4BYkoC+GsReLf569mSKDsfods6LYQ8t+a8EW9oaircfMpmaLbPBh4FOBiiFyLfuZmTSUwzZg=='),
),
),
),
));
$app->get('/', function () { return 'foo'; }) $app->get('/', function () { return 'foo'; })
->secure('ROLE_ADMIN') ->secure('ROLE_ADMIN')
...@@ -46,6 +35,15 @@ class SecurityTraitTest extends \PHPUnit_Framework_TestCase ...@@ -46,6 +35,15 @@ class SecurityTraitTest extends \PHPUnit_Framework_TestCase
$request = Request::create('/'); $request = Request::create('/');
$response = $app->handle($request); $response = $app->handle($request);
$this->assertEquals(401, $response->getStatusCode()); $this->assertEquals(401, $response->getStatusCode());
}
public function testSecureWithAuthorizedRoles()
{
$app = $this->createApplication();
$app->get('/', function () { return 'foo'; })
->secure('ROLE_ADMIN')
;
$request = Request::create('/'); $request = Request::create('/');
$request->headers->set('PHP_AUTH_USER', 'fabien'); $request->headers->set('PHP_AUTH_USER', 'fabien');
...@@ -53,4 +51,37 @@ class SecurityTraitTest extends \PHPUnit_Framework_TestCase ...@@ -53,4 +51,37 @@ class SecurityTraitTest extends \PHPUnit_Framework_TestCase
$response = $app->handle($request); $response = $app->handle($request);
$this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(200, $response->getStatusCode());
} }
public function testSecureWithUnauthorizedRoles()
{
$app = $this->createApplication();
$app->get('/', function () { return 'foo'; })
->secure('ROLE_SUPER_ADMIN')
;
$request = Request::create('/');
$request->headers->set('PHP_AUTH_USER', 'fabien');
$request->headers->set('PHP_AUTH_PW', 'foo');
$response = $app->handle($request);
$this->assertEquals(403, $response->getStatusCode());
}
private function createApplication()
{
$app = new Application();
$app['route_class'] = 'Silex\Tests\Route\SecurityRoute';
$app->register(new SecurityServiceProvider(), array(
'security.firewalls' => array(
'default' => array(
'http' => true,
'users' => array(
'fabien' => array('ROLE_ADMIN', '5FZ2Z8QIkA7UTZ4BYkoC+GsReLf569mSKDsfods6LYQ8t+a8EW9oaircfMpmaLbPBh4FOBiiFyLfuZmTSUwzZg=='),
),
),
),
));
return $app;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment