Skip to content
This repository has been archived by the owner on Jul 4, 2018. It is now read-only.

Commit

Permalink
Improve Silex\Route\SecurityTrait coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
SofHad authored and fabpot committed Apr 11, 2015
1 parent 417deb4 commit a124002
Showing 1 changed file with 44 additions and 13 deletions.
57 changes: 44 additions & 13 deletions tests/Silex/Tests/Route/SecurityTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,9 @@
*/
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')
Expand All @@ -46,11 +35,53 @@ public function testSecure()
$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');
$request->headers->set('PHP_AUTH_PW', 'foo');
$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;
}
}

0 comments on commit a124002

Please sign in to comment.