Skip to content

Commit e5d1474

Browse files
committed
minor #2184 add access denied listener tests (lsmith77)
This PR was merged into the 2.x branch. Discussion ---------- add access denied listener tests Commits ------- 969529e add access denied listener tests
2 parents 3676156 + 969529e commit e5d1474

File tree

7 files changed

+226
-0
lines changed

7 files changed

+226
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSRestBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\RestBundle\Tests\Functional;
13+
14+
use Symfony\Component\ErrorHandler\ErrorRenderer\ErrorRendererInterface;
15+
16+
class AccessDeniedListenerTest extends WebTestCase
17+
{
18+
private static $client;
19+
20+
public static function setUpBeforeClass()
21+
{
22+
parent::setUpBeforeClass();
23+
static::$client = static::createClient(['test_case' => 'AccessDeniedListener']);
24+
}
25+
26+
public static function tearDownAfterClass()
27+
{
28+
self::deleteTmpDir('AccessDeniedListener');
29+
parent::tearDownAfterClass();
30+
}
31+
32+
protected function setUp()
33+
{
34+
if (!interface_exists(ErrorRendererInterface::class)) {
35+
$this->markTestSkipped();
36+
}
37+
}
38+
39+
public function testBundleListenerHandlesExceptionsInRestZonesWithoutLogin()
40+
{
41+
static::$client->request('GET', '/api/comments');
42+
43+
$this->assertEquals(401, static::$client->getResponse()->getStatusCode());
44+
$this->assertEquals('application/json', static::$client->getResponse()->headers->get('Content-Type'));
45+
}
46+
47+
public function testBundleListenerHandlesExceptionsInRestZonesWithLogin()
48+
{
49+
$credentials = [
50+
'PHP_AUTH_USER' => 'restapi',
51+
'PHP_AUTH_PW' => 'secretpw',
52+
];
53+
54+
static::$client->request('GET', '/api/comments', [], [], $credentials);
55+
56+
$this->assertEquals(200, static::$client->getResponse()->getStatusCode());
57+
$this->assertEquals('application/json', static::$client->getResponse()->headers->get('Content-Type'));
58+
}
59+
60+
public function testBundleListenerHandlesExceptionsInRestZonesWrongLogin()
61+
{
62+
$credentials = [
63+
'PHP_AUTH_USER' => 'admin',
64+
'PHP_AUTH_PW' => 'secretpw',
65+
];
66+
67+
static::$client->request('GET', '/api/comments', [], [], $credentials);
68+
69+
$this->assertEquals(403, static::$client->getResponse()->getStatusCode());
70+
$this->assertEquals('application/json', static::$client->getResponse()->headers->get('Content-Type'));
71+
}
72+
73+
public function testBundleListenerHandlesExceptionsInRestZonesWithIncorrectLogin()
74+
{
75+
$credentials = [
76+
'PHP_AUTH_USER' => 'restapi',
77+
'PHP_AUTH_PW' => 'foobar',
78+
];
79+
80+
static::$client->request('GET', '/api/comments', [], [], $credentials);
81+
82+
$this->assertEquals(401, static::$client->getResponse()->getStatusCode());
83+
$this->assertEquals('application/json', static::$client->getResponse()->headers->get('Content-Type'));
84+
}
85+
86+
public function testSymfonyListenerHandlesExceptionsOutsideRestZones()
87+
{
88+
static::$client->request('GET', '/admin/comments');
89+
90+
$this->assertEquals(302, static::$client->getResponse()->getStatusCode());
91+
$this->assertEquals('text/html; charset=UTF-8', static::$client->getResponse()->headers->get('Content-Type'));
92+
}
93+
}

Tests/Functional/Bundle/TestBundle/Controller/Api/CommentController.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,9 @@ public function getCommentAction($id)
1919
{
2020
return new JsonResponse(array('id' => (int) $id));
2121
}
22+
23+
public function getComments()
24+
{
25+
return new JsonResponse(array());
26+
}
2227
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSRestBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\RestBundle\Tests\Functional\Bundle\TestBundle\Controller;
13+
14+
use Symfony\Component\HttpFoundation\Response;
15+
16+
class CommentController
17+
{
18+
public function getCommentAction($id)
19+
{
20+
return new Response("<html><body>$id</body>");
21+
}
22+
23+
public function getComments()
24+
{
25+
return new Response('<html><body>comments ..</body>');
26+
}
27+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSRestBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
return [
13+
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
14+
new \Symfony\Bundle\SecurityBundle\SecurityBundle(),
15+
new \FOS\RestBundle\FOSRestBundle(),
16+
new \FOS\RestBundle\Tests\Functional\Bundle\TestBundle\TestBundle(),
17+
];
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
imports:
2+
- { resource: ../config/default.yml }
3+
- { resource: security.php }
4+
5+
framework:
6+
serializer:
7+
enabled: true
8+
router: { resource: "%kernel.project_dir%/AccessDeniedListener/routing.yml" }
9+
10+
fos_rest:
11+
access_denied_listener:
12+
json: true
13+
exception:
14+
exception_listener: false
15+
serialize_exceptions: false
16+
routing_loader: false
17+
service:
18+
templating: null
19+
view:
20+
default_engine: null
21+
force_redirects: []
22+
zone:
23+
- { path: ^/api/* }
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
api:
2+
path: /api/comments
3+
defaults:
4+
_controller: FOS\RestBundle\Tests\Functional\Bundle\TestBundle\Controller\Api\CommentController::getComments
5+
_format: json
6+
7+
admin:
8+
path: /admin/comments
9+
defaults:
10+
_controller: FOS\RestBundle\Tests\Functional\Bundle\TestBundle\Controller\CommentController::getComments
11+
_format: html
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSRestBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
use Symfony\Component\Security\Core\Security;
13+
use Symfony\Component\Security\Http\Controller\UserValueResolver;
14+
15+
$defaultFirewall = [];
16+
if (method_exists(Security::class, 'getUser') && !class_exists(UserValueResolver::class)) {
17+
$defaultFirewall['logout_on_user_change'] = true;
18+
}
19+
20+
$container->loadFromExtension('security', [
21+
'encoders' => ['Symfony\Component\Security\Core\User\User' => 'plaintext'],
22+
'providers' => [
23+
'in_memory' => [
24+
'memory' => [
25+
'users' => [
26+
'restapi' => ['password' => 'secretpw', 'roles' => ['ROLE_API']],
27+
'admin' => ['password' => 'secretpw', 'roles' => ['ROLE_ADMIN']],
28+
],
29+
],
30+
],
31+
],
32+
'firewalls' => [
33+
'api' => array_merge($defaultFirewall, [
34+
'pattern' => '^/api',
35+
'stateless' => true,
36+
'http_basic' => ['realm' => 'Demo REST API'],
37+
'json_login' => [
38+
'check_path' => '/api/login',
39+
],
40+
]),
41+
'default' => array_merge($defaultFirewall, [
42+
'anonymous' => null,
43+
'form_login' => null,
44+
]),
45+
],
46+
'access_control' => [
47+
['path' => '^/admin', 'roles' => 'ROLE_ADMIN'],
48+
['path' => '^/api', 'roles' => 'ROLE_API'],
49+
],
50+
]);

0 commit comments

Comments
 (0)