Skip to content

Commit

Permalink
Merge branch '4.4' into 5.2
Browse files Browse the repository at this point in the history
* 4.4:
  do not mock the Request class
  do not mock event classes
  recover from failed deserializations
  [ErrorHandle] Remove a link from the exception page
  [Validator] Added Ukrainian translations
  [GHA] Clarify some bits in the deps=high script
  • Loading branch information
xabbuh committed Jul 10, 2021
2 parents 3de5da6 + 9ef9b5f commit b29c191
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 190 deletions.
44 changes: 6 additions & 38 deletions Tests/Firewall/AbstractPreAuthenticatedListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
Expand Down Expand Up @@ -62,14 +63,7 @@ public function testHandleWithValidValues()
->method('getPreAuthenticatedData')
->willReturn($userCredentials);

$event = $this->createMock(RequestEvent::class);
$event
->expects($this->any())
->method('getRequest')
->willReturn($request)
;

$listener($event);
$listener(new RequestEvent($this->createMock(HttpKernelInterface::class), $request, HttpKernelInterface::MASTER_REQUEST));
}

public function testHandleWhenAuthenticationFails()
Expand Down Expand Up @@ -109,14 +103,7 @@ public function testHandleWhenAuthenticationFails()
->method('getPreAuthenticatedData')
->willReturn($userCredentials);

$event = $this->createMock(RequestEvent::class);
$event
->expects($this->any())
->method('getRequest')
->willReturn($request)
;

$listener($event);
$listener(new RequestEvent($this->createMock(HttpKernelInterface::class), $request, HttpKernelInterface::MASTER_REQUEST));
}

public function testHandleWhenAuthenticationFailsWithDifferentToken()
Expand Down Expand Up @@ -158,12 +145,7 @@ public function testHandleWhenAuthenticationFailsWithDifferentToken()
->method('getPreAuthenticatedData')
->willReturn($userCredentials);

$event = $this->createMock(RequestEvent::class);
$event
->expects($this->any())
->method('getRequest')
->willReturn($request)
;
$event = new RequestEvent($this->createMock(HttpKernelInterface::class), $request, HttpKernelInterface::MASTER_REQUEST);

$listener($event);
}
Expand Down Expand Up @@ -200,14 +182,7 @@ public function testHandleWithASimilarAuthenticatedToken()
->method('getPreAuthenticatedData')
->willReturn($userCredentials);

$event = $this->createMock(RequestEvent::class);
$event
->expects($this->any())
->method('getRequest')
->willReturn($request)
;

$listener($event);
$listener(new RequestEvent($this->createMock(HttpKernelInterface::class), $request, HttpKernelInterface::MASTER_REQUEST));
}

public function testHandleWithAnInvalidSimilarToken()
Expand Down Expand Up @@ -250,13 +225,6 @@ public function testHandleWithAnInvalidSimilarToken()
->method('getPreAuthenticatedData')
->willReturn($userCredentials);

$event = $this->createMock(RequestEvent::class);
$event
->expects($this->any())
->method('getRequest')
->willReturn($request)
;

$listener($event);
$listener(new RequestEvent($this->createMock(HttpKernelInterface::class), $request, HttpKernelInterface::MASTER_REQUEST));
}
}
56 changes: 10 additions & 46 deletions Tests/Firewall/BasicAuthenticationListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
use Symfony\Component\Security\Core\Authentication\AuthenticationProviderManager;
use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface;
Expand Down Expand Up @@ -64,14 +65,7 @@ public function testHandleWithValidUsernameAndPasswordServerParameters()
$this->createMock(AuthenticationEntryPointInterface::class)
);

$event = $this->createMock(RequestEvent::class);
$event
->expects($this->any())
->method('getRequest')
->willReturn($request)
;

$listener($event);
$listener(new RequestEvent($this->createMock(HttpKernelInterface::class), $request, HttpKernelInterface::MASTER_REQUEST));
}

public function testHandleWhenAuthenticationFails()
Expand Down Expand Up @@ -109,19 +103,11 @@ public function testHandleWhenAuthenticationFails()
$authenticationEntryPoint
);

$event = $this->createMock(RequestEvent::class);
$event
->expects($this->any())
->method('getRequest')
->willReturn($request)
;
$event
->expects($this->once())
->method('setResponse')
->with($this->equalTo($response))
;
$event = new RequestEvent($this->createMock(HttpKernelInterface::class), $request, HttpKernelInterface::MASTER_REQUEST);

$listener($event);

$this->assertSame($response, $event->getResponse());
}

public function testHandleWithNoUsernameServerParameter()
Expand All @@ -141,14 +127,7 @@ public function testHandleWithNoUsernameServerParameter()
$this->createMock(AuthenticationEntryPointInterface::class)
);

$event = $this->createMock(RequestEvent::class);
$event
->expects($this->any())
->method('getRequest')
->willReturn($request)
;

$listener($event);
$listener(new RequestEvent($this->createMock(HttpKernelInterface::class), $request, HttpKernelInterface::MASTER_REQUEST));
}

public function testHandleWithASimilarAuthenticatedToken()
Expand Down Expand Up @@ -177,14 +156,7 @@ public function testHandleWithASimilarAuthenticatedToken()
$this->createMock(AuthenticationEntryPointInterface::class)
);

$event = $this->createMock(RequestEvent::class);
$event
->expects($this->any())
->method('getRequest')
->willReturn($request)
;

$listener($event);
$listener(new RequestEvent($this->createMock(HttpKernelInterface::class), $request, HttpKernelInterface::MASTER_REQUEST));
}

public function testItRequiresProviderKey()
Expand Down Expand Up @@ -236,18 +208,10 @@ public function testHandleWithADifferentAuthenticatedToken()
$authenticationEntryPoint
);

$event = $this->createMock(RequestEvent::class);
$event
->expects($this->any())
->method('getRequest')
->willReturn($request)
;
$event
->expects($this->once())
->method('setResponse')
->with($this->equalTo($response))
;
$event = new RequestEvent($this->createMock(HttpKernelInterface::class), $request, HttpKernelInterface::MASTER_REQUEST);

$listener($event);

$this->assertSame($response, $event->getResponse());
}
}
55 changes: 13 additions & 42 deletions Tests/Firewall/ChannelListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Security\Http\AccessMapInterface;
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
use Symfony\Component\Security\Http\Firewall\ChannelListener;
Expand Down Expand Up @@ -44,19 +45,12 @@ public function testHandleWithNotSecuredRequestAndHttpChannel()
->method('start')
;

$event = $this->createMock(RequestEvent::class);
$event
->expects($this->any())
->method('getRequest')
->willReturn($request)
;
$event
->expects($this->never())
->method('setResponse')
;
$event = new RequestEvent($this->createMock(HttpKernelInterface::class), $request, HttpKernelInterface::MASTER_REQUEST);

$listener = new ChannelListener($accessMap, $entryPoint);
$listener($event);

$this->assertNull($event->getResponse());
}

public function testHandleWithSecuredRequestAndHttpsChannel()
Expand All @@ -82,19 +76,12 @@ public function testHandleWithSecuredRequestAndHttpsChannel()
->method('start')
;

$event = $this->createMock(RequestEvent::class);
$event
->expects($this->any())
->method('getRequest')
->willReturn($request)
;
$event
->expects($this->never())
->method('setResponse')
;
$event = new RequestEvent($this->createMock(HttpKernelInterface::class), $request, HttpKernelInterface::MASTER_REQUEST);

$listener = new ChannelListener($accessMap, $entryPoint);
$listener($event);

$this->assertNull($event->getResponse());
}

public function testHandleWithNotSecuredRequestAndHttpsChannel()
Expand Down Expand Up @@ -124,20 +111,12 @@ public function testHandleWithNotSecuredRequestAndHttpsChannel()
->willReturn($response)
;

$event = $this->createMock(RequestEvent::class);
$event
->expects($this->any())
->method('getRequest')
->willReturn($request)
;
$event
->expects($this->once())
->method('setResponse')
->with($this->equalTo($response))
;
$event = new RequestEvent($this->createMock(HttpKernelInterface::class), $request, HttpKernelInterface::MASTER_REQUEST);

$listener = new ChannelListener($accessMap, $entryPoint);
$listener($event);

$this->assertSame($response, $event->getResponse());
}

public function testHandleWithSecuredRequestAndHttpChannel()
Expand Down Expand Up @@ -167,19 +146,11 @@ public function testHandleWithSecuredRequestAndHttpChannel()
->willReturn($response)
;

$event = $this->createMock(RequestEvent::class);
$event
->expects($this->any())
->method('getRequest')
->willReturn($request)
;
$event
->expects($this->once())
->method('setResponse')
->with($this->equalTo($response))
;
$event = new RequestEvent($this->createMock(HttpKernelInterface::class), $request, HttpKernelInterface::MASTER_REQUEST);

$listener = new ChannelListener($accessMap, $entryPoint);
$listener($event);

$this->assertSame($response, $event->getResponse());
}
}
22 changes: 3 additions & 19 deletions Tests/Firewall/ContextListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,9 @@ public function testOnKernelResponseWithoutSessionNorToken()
public function testInvalidTokenInSession($token)
{
$tokenStorage = $this->createMock(TokenStorageInterface::class);
$event = $this->createMock(RequestEvent::class);
$request = $this->createMock(Request::class);
$session = $this->createMock(SessionInterface::class);

$event->expects($this->any())
->method('getRequest')
->willReturn($request);
$request->expects($this->any())
->method('hasPreviousSession')
->willReturn(true);
Expand All @@ -168,7 +164,7 @@ public function testInvalidTokenInSession($token)
->with(null);

$listener = new ContextListener($tokenStorage, [], 'key123');
$listener($event);
$listener(new RequestEvent($this->createMock(HttpKernelInterface::class), $request, HttpKernelInterface::MASTER_REQUEST));
}

public function provideInvalidToken()
Expand All @@ -186,22 +182,13 @@ public function testHandleAddsKernelResponseListener()
{
$tokenStorage = $this->createMock(TokenStorageInterface::class);
$dispatcher = $this->createMock(EventDispatcherInterface::class);
$event = $this->createMock(RequestEvent::class);

$listener = new ContextListener($tokenStorage, [], 'key123', null, $dispatcher);

$event->expects($this->any())
->method('isMasterRequest')
->willReturn(true);
$event->expects($this->any())
->method('getRequest')
->willReturn($this->createMock(Request::class));

$dispatcher->expects($this->once())
->method('addListener')
->with(KernelEvents::RESPONSE, [$listener, 'onKernelResponse']);

$listener($event);
$listener(new RequestEvent($this->createMock(HttpKernelInterface::class), new Request(), HttpKernelInterface::MASTER_REQUEST));
}

public function testOnKernelResponseListenerRemovesItself()
Expand Down Expand Up @@ -234,14 +221,11 @@ public function testHandleRemovesTokenIfNoPreviousSessionWasFound()
$request = $this->createMock(Request::class);
$request->expects($this->any())->method('hasPreviousSession')->willReturn(false);

$event = $this->createMock(RequestEvent::class);
$event->expects($this->any())->method('getRequest')->willReturn($request);

$tokenStorage = $this->createMock(TokenStorageInterface::class);
$tokenStorage->expects($this->once())->method('setToken')->with(null);

$listener = new ContextListener($tokenStorage, [], 'key123');
$listener($event);
$listener(new RequestEvent($this->createMock(HttpKernelInterface::class), $request, HttpKernelInterface::MASTER_REQUEST));
}

public function testIfTokenIsDeauthenticated()
Expand Down
Loading

0 comments on commit b29c191

Please sign in to comment.