8000 if not authenticated at all · symfony/symfony@64b0bdd · GitHub
[go: up one dir, main page]

Skip to content

Commit 64b0bdd

Browse files
committed
if not authenticated at all
add tests for not authenticated
1 parent 1d65615 commit 64b0bdd

File tree

2 files changed

+63
-9
lines changed

2 files changed

+63
-9
lines changed

src/Symfony/Component/Security/Http/Firewall/ExceptionListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ private function handleAccessDeniedException(ExceptionEvent $event, AccessDenied
129129

130130
$token = $this->tokenStorage->getToken();
131131
if (!$this->authenticationTrustResolver->isFullFledged($token)) {
132-
$response = (null === $this->notFullFledgedHandler ? true : $this->notFullFledgedHandler->handle($event->getRequest(), $exception));
132+
$response = ((!$this->authenticationTrustResolver->isAuthenticated($token)) || (null === $this->notFullFledgedHandler) ? true : $this->notFullFledgedHandler->handle($event->getRequest(), $exception));
133133

134134
if ($response instanceof Response) {
135135
$event->setResponse($response);

src/Symfony/Component/Security/Http/Tests/Firewall/ExceptionListenerTest.php

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,67 @@ public function testAccessDeniedExceptionNotFullFledged(\Exception $exception, ?
152152
/**
153153
* @dataProvider getAccessDeniedExceptionProvider
154154
*/
155-
public function testAccessDeniedExceptionNotFullFledgedWithHandlerResponseTrue(\Exception $exception, ?\Exception $eventException = null)
155+
public function testAccessDeniedExceptionNotFullFledgedWithHandlerResponseTrueNotAuthenticated(\Exception $exception, ?\Exception $eventException = null)
156156
{
157157
$event = $this->createEvent($exception);
158158

159159
$tokenStorage = $this->createMock(TokenStorageInterface::class);
160160
$tokenStorage->expects($this->once())->method('getToken')->willReturn($this->createMock(TokenInterface::class));
161161

162-
$listener = $this->createExceptionListener($tokenStorage, $this->createTrustResolver(false), null, $this->createEntryPointWithoutStartCalled(), null, null, $this->createNotFullFledgedHandler(false));
162+
$listener = $this->createExceptionListener($tokenStorage, $this->createTrustResolver(false,false), null, $this->createEntryPoint(), null, null, $this->createNotFullFledgedHandler(false));
163+
$listener->onKernelException($event);
164+
165+
$this->assertEquals('OK', $event->getResponse()->getContent());
166+
$this->assertSame($eventException ?? $exception, $event->getThrowable()->getPrevious());
167+
}
168+
169+
/**
170+
* @dataProvider getAccessDeniedExceptionProvider
171+
*/
172+
public function testAccessDeniedExceptionNotFullFledgedWithHandlerResponseFalseNotAuthenticated(\Exception $exception, ?\Exception $eventException = null)
173+
{
174+
$event = $this->createEvent($exception);
175+
176+
$tokenStorage = $this->createMock(TokenStorageInterface::class);
177+
$tokenStorage->expects($this->once())->method('getToken')->willReturn($this->createMock(TokenInterface::class));
178+
179+
$listener = $this->createExceptionListener($tokenStorage, $this->createTrustResolver(false,false), null, $this->createEntryPoint(), null, null, $this->createNotFullFledgedHandler(true));
180+
$listener->onKernelException($event);
181+
182+
$this->assertEquals('OK', $event->getResponse()->getContent());
183+
$this->assertSame($eventException ?? $exception, $event->getThrowable()->getPrevious());
184+
}
185+
186+
/**
187+
* @dataProvider getAccessDeniedExceptionProvider
188+
*/
189+
public function testAccessDeniedExceptionNotFullFledgedWithHandlerResponseCustomNotAuthenticated(\Exception $exception, ?\Exception $eventException = null)
190+
{
191+
$event = $this->createEvent($exception);
192+
193+
$tokenStorage = $this->createMock(TokenStorageInterface::class);
194+
$tokenStorage->expects($this->once())->method('getToken')->willReturn($this->createMock(TokenInterface::class));
195+
196+
$listener = $this->createExceptionListener($tokenStorage, $this->createTrustResolver(false,false), null, $this->createEntryPoint(), null, null, $this->createNotFullFledgedHandler(new Response('Full Fledged Response', 401)));
197+
$listener->onKernelException($event);
198+
199+
var_dump($event->getResponse());
200+
201+
$this->assertEquals('OK', $event->getResponse()->getContent());
202+
$this->assertSame($eventException ?? $exception, $event->getThrowable()->getPrevious());
203+
}
204+
205+
/**
206+
* @dataProvider getAccessDeniedExceptionProvider
207+
*/
208+
public function testAccessDeniedExceptionNotFullFledgedWithHandlerResponseTrueAuthenticated(\Exception $exception, ?\Exception $eventException = null)
209+
{
210+
$event = $this->createEvent($exception);
211+
212+
$tokenStorage = $this->createMock(TokenStorageInterface::class);
213+
$tokenStorage->expects($this->once())->method('getToken')->willReturn($this->createMock(TokenInterface::class));
214+
215+
$listener = $this->createExceptionListener($tokenStorage, $this->createTrustResolver(false,true), null, $this->createEntryPointWithoutStartCalled(), null, null, $this->createNotFullFledgedHandler(false));
163216
$listener->onKernelException($event);
164217

165218
$this->assertNull($event->getResponse());
@@ -169,14 +222,14 @@ public function testAccessDeniedExceptionNotFullFledgedWithHandlerResponseTrue(\
169222
/**
170223
* @dataProvider getAccessDeniedExceptionProvider
171224
*/
172-
public function testAccessDeniedExceptionNotFullFledgedWithHandlerResponseFalse(\Exception $exception, ?\Exception $eventException = null)
225+
public function testAccessDeniedExceptionNotFullFledgedWithHandlerResponseFalseAuthenticated(\Exception $exception, ?\Exception $eventException = null)
173226
{
174227
$event = $this->createEvent($exception);
175228

176229
$tokenStorage = $this->createMock(TokenStorageInterface::class);
177230
$tokenStorage->expects($this->once())->method('getToken')->willReturn($this->createMock(TokenInterface::class));
178231

179-
$listener = $this->createExceptionListener($tokenStorage, $this->createTrustResolver(false), null, $this->createEntryPoint(), null, null, $this->createNotFullFledgedHandler(true));
232+
$listener = $this->createExceptionListener($tokenStorage, $this->createTrustResolver(false,true), null, $this->createEntryPoint(), null, null, $this->createNotFullFledgedHandler(true));
180233
$listener->onKernelException($event);
181234

182235
$this->assertEquals('OK', $event->getResponse()->getContent());
@@ -186,14 +239,14 @@ public function testAccessDeniedExceptionNotFullFledgedWithHandlerResponseFalse(
186239
/**
187240
* @dataProvider getAccessDeniedExceptionProvider
188241
*/
189-
public function testAccessDeniedExceptionNotFullFledgedWithHandlerResponseCustom(\Exception $exception, ?\Exception $eventException = null)
242+
public function testAccessDeniedExceptionNotFullFledgedWithHandlerResponseCustomAuthenticated(\Exception $exception, ?\Exception $eventException = null)
190243
{
191244
$event = $this->createEvent($exception);
192245

193246
$tokenStorage = $this->createMock(TokenStorageInterface::class);
194247
$tokenStorage->expects($this->once())->method('getToken')->willReturn($this->createMock(TokenInterface::class));
195248

196-
$listener = $this->createExceptionListener($tokenStorage, $this->createTrustResolver(false), null, $this->createEntryPointWithoutStartCalled(), null, null, $this->createNotFullFledgedHandler(new Response('Full Fledged Response', 401)));
249+
$listener = $this->createExceptionListener($tokenStorage, $this->createTrustResolver(false,true), null, $this->createEntryPointWithoutStartCalled(), null, null, $this->createNotFullFledgedHandler(new Response('Full Fledged Response', 401)));
197250
$listener->onKernelException($event);
198251

199252
$this->assertEquals('Full Fledged Response', $event->getResponse()->getContent());
@@ -251,10 +304,11 @@ private function createEntryPointWithoutStartCalled()
251304
return $entryPoint;
252305
}
253306

254-
private function createTrustResolver($fullFledged)
307+
private function createTrustResolver($fullFledged, $authenticate = false)
255308
{
256309
$trustResolver = $this->createMock(AuthenticationTrustResolverInterface::class);
257310
$trustResolver->expects($this->once())->method('isFullFledged')->willReturn($fullFledged);
311+
$trustResolver->method('isAuthenticated')->willReturn($authenticate);
258312

259313
return $trustResolver;
260314
}
@@ -285,7 +339,7 @@ private function createExceptionListener(?TokenStorageInterface $tokenStorage =
285339
private function createNotFullFledgedHandler(bool|Response $response = false)
286340
{
287341
$entryPoint = $this->createMock(NotFullFledgedHandlerInterface::class);
288-
$entryPoint->expects($this->once())->method('handle')->willReturn($response);
342+
$entryPoint->method('handle')->willReturn($response);
289343

290344
return $entryPoint;
291345
}

0 commit comments

Comments
 (0)
0