From cbc69bbfa2d039f23cad1a09298eae8447e92d5f Mon Sep 17 00:00:00 2001 From: mrcotrmpr Date: Thu, 30 Sep 2021 17:06:59 +0200 Subject: [PATCH 1/4] set up basic test structure --- .../AuthenticationSuccessHandlerTest.php | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/tests/Security/Http/Authentication/AuthenticationSuccessHandlerTest.php b/tests/Security/Http/Authentication/AuthenticationSuccessHandlerTest.php index bac6aea..5c2e715 100644 --- a/tests/Security/Http/Authentication/AuthenticationSuccessHandlerTest.php +++ b/tests/Security/Http/Authentication/AuthenticationSuccessHandlerTest.php @@ -10,6 +10,7 @@ use ConnectHolland\SecureJWTBundle\Resolver\RememberDeviceResolver; use ConnectHolland\SecureJWTBundle\Security\Http\Authentication\AuthenticationSuccessHandler; use Doctrine\Persistence\ManagerRegistry; +use http\Cookie; use Lexik\Bundle\JWTAuthenticationBundle\Encoder\JWTEncoderInterface; use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent; use Lexik\Bundle\JWTAuthenticationBundle\Events; @@ -107,12 +108,27 @@ public function testRememberDeviceCookieIsSetAfterAuthenticationSuccess() $this->assertSame('REMEMBER_DEVICE', $cookies[1]->getName()); } + public function testRememberDeviceCookieIsReplacedAfterNewUserAuthenticates() + { + $request = $this->getRequest(); + $token = $this->getToken(); + + $response = (new AuthenticationSuccessHandler(new LexikAuthenticationSuccessHandler($this->getJWTManager('secrettoken'), $this->getDispatcher()), $this->getEncoder(), 'strict', $this->getRememberDeviceResolver(true), $this->getDoctrine())) + ->onAuthenticationSuccess($request, $token); + + $cookies = $response->headers->getCookies(); + $this->assertCount(2, $cookies); + $this->assertSame('BEARER', $cookies[0]->getName()); + $this->assertSame('REMEMBER_DEVICE', $cookies[1]->getName()); + + } + private function getEncoder(): JWTEncoderInterface { $encoder = $this->createMock(JWTEncoderInterface::class); $encoder - ->expects($this->once()) + ->expects($this->any()) ->method('decode') ->willReturn(['user' => 'example@example.org', 'exp' => 1627902433]); @@ -139,12 +155,24 @@ protected function getRequest() ->disableOriginalConstructor() ->getMock(); + $request->cookies = $this + ->getMockBuilder('Symfony\Component\HttpFoundation\Cookie') + ->setMethods(['get']) + ->disableOriginalConstructor() + ->getMock(); + $request->request ->expects($this->any()) ->method('get') ->with('username') ->will($this->returnValue('name')); + $request->cookies + ->expects($this->any()) + ->method('get') + ->will($this->returnValue('username')); + + return $request; } From 1c0debff385188b3ff55f6666c38202dbbd13f88 Mon Sep 17 00:00:00 2001 From: mrcotrmpr Date: Thu, 30 Sep 2021 17:10:01 +0200 Subject: [PATCH 2/4] removed useless import --- .../Http/Authentication/AuthenticationSuccessHandlerTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Security/Http/Authentication/AuthenticationSuccessHandlerTest.php b/tests/Security/Http/Authentication/AuthenticationSuccessHandlerTest.php index 5c2e715..cc4e324 100644 --- a/tests/Security/Http/Authentication/AuthenticationSuccessHandlerTest.php +++ b/tests/Security/Http/Authentication/AuthenticationSuccessHandlerTest.php @@ -10,7 +10,6 @@ use ConnectHolland\SecureJWTBundle\Resolver\RememberDeviceResolver; use ConnectHolland\SecureJWTBundle\Security\Http\Authentication\AuthenticationSuccessHandler; use Doctrine\Persistence\ManagerRegistry; -use http\Cookie; use Lexik\Bundle\JWTAuthenticationBundle\Encoder\JWTEncoderInterface; use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent; use Lexik\Bundle\JWTAuthenticationBundle\Events; From 5a02fb68f0da28b2c9faf436c01c26a8dace9890 Mon Sep 17 00:00:00 2001 From: mrcotrmpr Date: Mon, 11 Oct 2021 15:22:58 +0200 Subject: [PATCH 3/4] improve tests --- .../AuthenticationSuccessHandlerTest.php | 21 +------------------ 1 file changed, 1 insertion(+), 20 deletions(-) diff --git a/tests/Security/Http/Authentication/AuthenticationSuccessHandlerTest.php b/tests/Security/Http/Authentication/AuthenticationSuccessHandlerTest.php index cc4e324..4653142 100644 --- a/tests/Security/Http/Authentication/AuthenticationSuccessHandlerTest.php +++ b/tests/Security/Http/Authentication/AuthenticationSuccessHandlerTest.php @@ -105,20 +105,7 @@ public function testRememberDeviceCookieIsSetAfterAuthenticationSuccess() $this->assertCount(2, $cookies); $this->assertSame('BEARER', $cookies[0]->getName()); $this->assertSame('REMEMBER_DEVICE', $cookies[1]->getName()); - } - - public function testRememberDeviceCookieIsReplacedAfterNewUserAuthenticates() - { - $request = $this->getRequest(); - $token = $this->getToken(); - - $response = (new AuthenticationSuccessHandler(new LexikAuthenticationSuccessHandler($this->getJWTManager('secrettoken'), $this->getDispatcher()), $this->getEncoder(), 'strict', $this->getRememberDeviceResolver(true), $this->getDoctrine())) - ->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('encoded_value', $cookies[1]->getValue()); } @@ -166,12 +153,6 @@ protected function getRequest() ->with('username') ->will($this->returnValue('name')); - $request->cookies - ->expects($this->any()) - ->method('get') - ->will($this->returnValue('username')); - - return $request; } From c6c67955b6d5b7095aba1d65f5436f11b432b525 Mon Sep 17 00:00:00 2001 From: mrcotrmpr Date: Mon, 11 Oct 2021 15:35:29 +0200 Subject: [PATCH 4/4] add new test --- .../AuthenticationSuccessHandlerTest.php | 37 +++++++++++++++++-- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/tests/Security/Http/Authentication/AuthenticationSuccessHandlerTest.php b/tests/Security/Http/Authentication/AuthenticationSuccessHandlerTest.php index 4653142..bdb1267 100644 --- a/tests/Security/Http/Authentication/AuthenticationSuccessHandlerTest.php +++ b/tests/Security/Http/Authentication/AuthenticationSuccessHandlerTest.php @@ -109,14 +109,45 @@ public function testRememberDeviceCookieIsSetAfterAuthenticationSuccess() } - private function getEncoder(): JWTEncoderInterface + 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' => 'example@example.org', 'exp' => 1627902433], $encoder->decode($cookies[1]->getValue())); + + $encoder = $this->getEncoder('newuser@example.org'); + $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' => 'newuser@example.org', 'exp' => 1627902433], $encoder->decode($cookies[1]->getValue())); + + } + + private function getEncoder($user = 'example@example.org'): JWTEncoderInterface { $encoder = $this->createMock(JWTEncoderInterface::class); $encoder ->expects($this->any()) ->method('decode') - ->willReturn(['user' => 'example@example.org', 'exp' => 1627902433]); + ->willReturn(['user' => $user, 'exp' => 1627902433]); $encoder ->expects($this->any()) @@ -211,7 +242,7 @@ private function getDispatcher() ->getMock(); $dispatcher - ->expects($this->once()) + ->expects($this->any()) ->method('dispatch') ->with( $this->isInstanceOf(AuthenticationSuccessEvent::class),