Skip to content

Commit 637167c

Browse files
committed
feat(tests): Add DenyAccessListener tests
1 parent 9ce8b6d commit 637167c

File tree

2 files changed

+90
-6
lines changed

2 files changed

+90
-6
lines changed

src/Symfony/EventListener/DenyAccessListener.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,12 @@ private function checkSecurity(Request $request, string $attribute, array $extra
8686
$message = $operation->getSecurityMessage();
8787
}
8888

89-
if (null === $isGranted) {
90-
return;
91-
}
92-
9389
$extraVariables += $request->attributes->all();
9490
$extraVariables['object'] = $request->attributes->get('data');
9591
$extraVariables['previous_object'] = $request->attributes->get('previous_data');
9692
$extraVariables['request'] = $request;
9793

98-
if (!$this->resourceAccessChecker->isGranted($attributes['resource_class'], $isGranted, $extraVariables)) {
94+
if ($isGranted && !$this->resourceAccessChecker->isGranted($attributes['resource_class'], $isGranted, $extraVariables)) {
9995
throw new AccessDeniedException($message ?? 'Access Denied.');
10096
}
10197

@@ -105,7 +101,7 @@ private function checkSecurity(Request $request, string $attribute, array $extra
105101
continue;
106102
}
107103

108-
if (!$this->resourceAccessChecker->isGranted($uriVariable->getToProperty(), $uriVariable->getSecurity(), $extraVariables)) {
104+
if (!$this->resourceAccessChecker->isGranted($uriVariable->getFromClass(), $uriVariable->getSecurity(), $extraVariables)) {
109105
throw new AccessDeniedException($uriVariable->getSecurityMessage() ?? 'Access Denied.');
110106
}
111107
}

tests/Symfony/EventListener/DenyAccessListenerTest.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
use ApiPlatform\Metadata\ApiResource;
1717
use ApiPlatform\Metadata\Get;
18+
use ApiPlatform\Metadata\GetCollection;
19+
use ApiPlatform\Metadata\Link;
1820
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
1921
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
2022
use ApiPlatform\Symfony\EventListener\DenyAccessListener;
@@ -140,6 +142,92 @@ public function testSecurityMessage(): void
140142
$listener->onSecurity($event);
141143
}
142144

145+
public function testIsGrantedLink(): void
146+
{
147+
$request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_operation_name' => 'get_collection']);
148+
149+
$eventProphecy = $this->prophesize(RequestEvent::class);
150+
$eventProphecy->getRequest()->willReturn($request)->shouldBeCalled();
151+
$event = $eventProphecy->reveal();
152+
153+
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
154+
$resourceMetadataFactoryProphecy->create('Foo')->shouldBeCalled()->willReturn(new ResourceMetadataCollection('Foo', [
155+
new ApiResource(
156+
uriTemplate: '/bars/{barId}/foos',
157+
operations: [
158+
'get_collection' => new GetCollection(uriVariables: [
159+
'barId' => new Link(toProperty: 'bar', fromClass: 'Bar', security: 'is_granted("some_voter", "bar")'),
160+
], ),
161+
],
162+
),
163+
]));
164+
165+
$resourceAccessCheckerProphecy = $this->prophesize(ResourceAccessCheckerInterface::class);
166+
$resourceAccessCheckerProphecy->isGranted('Bar', 'is_granted("some_voter", "bar")', Argument::type('array'))->willReturn(true)->shouldBeCalled();
167+
168+
$listener = $this->getListener($resourceMetadataFactoryProphecy->reveal(), $resourceAccessCheckerProphecy->reveal());
169+
$listener->onSecurity($event);
170+
}
171+
172+
public function testIsNotGrantedLink(): void
173+
{
174+
$this->expectException(AccessDeniedException::class);
175+
176+
$request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_operation_name' => 'get_collection']);
177+
178+
$eventProphecy = $this->prophesize(RequestEvent::class);
179+
$eventProphecy->getRequest()->willReturn($request)->shouldBeCalled();
180+
$event = $eventProphecy->reveal();
181+
182+
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
183+
$resourceMetadataFactoryProphecy->create('Foo')->shouldBeCalled()->willReturn(new ResourceMetadataCollection('Foo', [
184+
new ApiResource(
185+
uriTemplate: '/bars/{barId}/foos',
186+
operations: [
187+
'get_collection' => new GetCollection(uriVariables: [
188+
'barId' => new Link(toProperty: 'bar', fromClass: 'Bar', security: 'is_granted("some_voter", "bar")'),
189+
], ),
190+
],
191+
),
192+
]));
193+
194+
$resourceAccessCheckerProphecy = $this->prophesize(ResourceAccessCheckerInterface::class);
195+
$resourceAccessCheckerProphecy->isGranted('Bar', 'is_granted("some_voter", "bar")', Argument::type('array'))->willReturn(false)->shouldBeCalled();
196+
197+
$listener = $this->getListener($resourceMetadataFactoryProphecy->reveal(), $resourceAccessCheckerProphecy->reveal());
198+
$listener->onSecurity($event);
199+
}
200+
201+
public function testSecurityMessageLink(): void
202+
{
203+
$this->expectException(AccessDeniedException::class);
204+
$this->expectExceptionMessage('You are not admin.');
205+
206+
$request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_operation_name' => 'get_collection']);
207+
208+
$eventProphecy = $this->prophesize(RequestEvent::class);
209+
$eventProphecy->getRequest()->willReturn($request)->shouldBeCalled();
210+
$event = $eventProphecy->reveal();
211+
212+
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
213+
$resourceMetadataFactoryProphecy->create('Foo')->shouldBeCalled()->willReturn(new ResourceMetadataCollection('Foo', [
214+
new ApiResource(
215+
uriTemplate: '/bars/{barId}/foos',
216+
operations: [
217+
'get_collection' => new GetCollection(uriVariables: [
218+
'barId' => new Link(toProperty: 'bar', fromClass: 'Bar', security: 'is_granted("some_voter", "bar")', securityMessage: 'You are not admin.'),
219+
], ),
220+
],
221+
),
222+
]));
223+
224+
$resourceAccessCheckerProphecy = $this->prophesize(ResourceAccessCheckerInterface::class);
225+
$resourceAccessCheckerProphecy->isGranted('Bar', 'is_granted("some_voter", "bar")', Argument::type('array'))->willReturn(false)->shouldBeCalled();
226+
227+
$listener = $this->getListener($resourceMetadataFactoryProphecy->reveal(), $resourceAccessCheckerProphecy->reveal());
228+
$listener->onSecurity($event);
229+
}
230+
143231
public function testSecurityComponentNotAvailable(): void
144232
{
145233
$request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_operation_name' => 'get']);

0 commit comments

Comments
 (0)