Commit a1240023 authored by SofHad's avatar SofHad Committed by Fabien Potencier

Improve Silex\Route\SecurityTrait coverage

parent 417deb44
......@@ -24,20 +24,9 @@ use Symfony\Component\HttpFoundation\Request;
*/
class SecurityTraitTest extends \PHPUnit_Framework_TestCase
{
public function testSecure()
public function testSecureWithNoAuthenticatedUser()
{
$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=='),
),
),
),
));
$app = $this->createApplication();
$app->get('/', function () { return 'foo'; })
->secure('ROLE_ADMIN')
......@@ -46,6 +35,15 @@ class SecurityTraitTest extends \PHPUnit_Framework_TestCase
$request = Request::create('/');
$response = $app->handle($request);
$this->assertEquals(401, $response->getStatusCode());
}
public function testSecureWithAuthorizedRoles()
{
$app = $this->createApplication();
$app->get('/', function () { return 'foo'; })
->secure('ROLE_ADMIN')
;
$request = Request::create('/');
$request->headers->set('PHP_AUTH_USER', 'fabien');
......@@ -53,4 +51,37 @@ class SecurityTraitTest extends \PHPUnit_Framework_TestCase
$response = $app->handle($request);
$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