-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from ConnectHolland/fix-test
Add more tests for remember device functionality
- Loading branch information
Showing
1 changed file
with
43 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -105,16 +105,49 @@ public function testRememberDeviceCookieIsSetAfterAuthenticationSuccess() | |
$this->assertCount(2, $cookies); | ||
$this->assertSame('BEARER', $cookies[0]->getName()); | ||
$this->assertSame('REMEMBER_DEVICE', $cookies[1]->getName()); | ||
$this->assertSame('encoded_value', $cookies[1]->getValue()); | ||
|
||
} | ||
|
||
public function testRememberDeviceCookieIsReplacedAfterNewAuthenticationSuccess() | ||
{ | ||
$request = $this->getRequest(); | ||
$token = $this->getToken(); | ||
$manager = $this->getJWTManager('secrettoken'); | ||
$dispatcher = $this->getDispatcher(); | ||
$encoder = $this->getEncoder(); | ||
$resolver = $this->getRememberDeviceResolver(true); | ||
$doctrine = $this->getDoctrine(); | ||
|
||
$response = (new AuthenticationSuccessHandler(new LexikAuthenticationSuccessHandler($manager, $dispatcher), $encoder, 'strict', $resolver, $doctrine)) | ||
->onAuthenticationSuccess($request, $token); | ||
|
||
$cookies = $response->headers->getCookies(); | ||
$this->assertCount(2, $cookies); | ||
$this->assertSame('BEARER', $cookies[0]->getName()); | ||
$this->assertSame('REMEMBER_DEVICE', $cookies[1]->getName()); | ||
$this->assertSame(['user' => '[email protected]', 'exp' => 1627902433], $encoder->decode($cookies[1]->getValue())); | ||
|
||
$encoder = $this->getEncoder('[email protected]'); | ||
$response = (new AuthenticationSuccessHandler(new LexikAuthenticationSuccessHandler($manager, $dispatcher), $encoder, 'strict', $resolver, $doctrine)) | ||
->onAuthenticationSuccess($request, $token); | ||
|
||
$cookies = $response->headers->getCookies(); | ||
$this->assertCount(2, $cookies); | ||
$this->assertSame('BEARER', $cookies[0]->getName()); | ||
$this->assertSame('REMEMBER_DEVICE', $cookies[1]->getName()); | ||
$this->assertSame(['user' => '[email protected]', 'exp' => 1627902433], $encoder->decode($cookies[1]->getValue())); | ||
|
||
} | ||
|
||
private function getEncoder(): JWTEncoderInterface | ||
private function getEncoder($user = '[email protected]'): JWTEncoderInterface | ||
{ | ||
$encoder = $this->createMock(JWTEncoderInterface::class); | ||
|
||
$encoder | ||
->expects($this->once()) | ||
->expects($this->any()) | ||
->method('decode') | ||
->willReturn(['user' => '[email protected]', 'exp' => 1627902433]); | ||
->willReturn(['user' => $user, 'exp' => 1627902433]); | ||
|
||
$encoder | ||
->expects($this->any()) | ||
|
@@ -139,6 +172,12 @@ protected function getRequest() | |
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$request->cookies = $this | ||
->getMockBuilder('Symfony\Component\HttpFoundation\Cookie') | ||
->setMethods(['get']) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$request->request | ||
->expects($this->any()) | ||
->method('get') | ||
|
@@ -203,7 +242,7 @@ private function getDispatcher() | |
->getMock(); | ||
|
||
$dispatcher | ||
->expects($this->once()) | ||
->expects($this->any()) | ||
->method('dispatch') | ||
->with( | ||
$this->isInstanceOf(AuthenticationSuccessEvent::class), | ||
|