Skip to content

Commit

Permalink
Merge pull request #35 from ConnectHolland/fix-test
Browse files Browse the repository at this point in the history
Add more tests for remember device functionality
  • Loading branch information
mrcotrmpr authored Oct 11, 2021
2 parents 5dbc3e5 + c6c6795 commit f3544ad
Showing 1 changed file with 43 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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')
Expand Down Expand Up @@ -203,7 +242,7 @@ private function getDispatcher()
->getMock();

$dispatcher
->expects($this->once())
->expects($this->any())
->method('dispatch')
->with(
$this->isInstanceOf(AuthenticationSuccessEvent::class),
Expand Down

0 comments on commit f3544ad

Please sign in to comment.