Skip to content

Commit 4d453b2

Browse files
committed
Merge branch '2.x'
* 2.x: add access denied listener tests
2 parents f50003a + e5d1474 commit 4d453b2

File tree

7 files changed

+240
-0
lines changed

7 files changed

+240
-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+
}
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\Api;
13+
14+
use Symfony\Component\HttpFoundation\JsonResponse;
15+
16+
class CommentController
17+
{
18+
public function getCommentAction($id)
19+
{
20+
return new JsonResponse(array('id' => (int) $id));
21+
}
22+
23+
public function getComments()
24+
{
25+
return new JsonResponse(array());
26+
}
27+
}
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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
$container->loadFromExtension('security', [
13+
'encoders' => ['Symfony\Component\Security\Core\User\User' => 'plaintext'],
14+
'providers' => [
15+
'in_memory' => [
16+
'memory' => [
17+
'users' => [
18+
'restapi' => ['password' => 'secretpw', 'roles' => ['ROLE_API']],
19+
'admin' => ['password' => 'secretpw', 'roles' => ['ROLE_ADMIN']],
20+
],
21+
],
22+
],
23+
],
24+
'firewalls' => [
25+
'api' => [
26+
'pattern' => '^/api',
27+
'stateless' => true,
28+
'http_basic' => ['realm' => 'Demo REST API'],
29+
'json_login' => [
30+
'check_path' => '/api/login',
31+
],
32+
],
33+
'default' => [
34+
'anonymous' => null,
35+
'form_login' => null,
36+
],
37+
],
38+
'access_control' => [
39+
['path' => '^/admin', 'roles' => 'ROLE_ADMIN'],
40+
['path' => '^/api', 'roles' => 'ROLE_API'],
41+
],
42+
]);

0 commit comments

Comments
 (0)