|
2 | 2 |
|
3 | 3 | namespace Lexik\Bundle\JWTAuthenticationBundle\Tests\Security\Http\Authentication;
|
4 | 4 |
|
| 5 | +use Exception; |
5 | 6 | use Lexik\Bundle\JWTAuthenticationBundle\Security\Http\Authentication\AuthenticationFailureHandler;
|
6 | 7 | use PHPUnit\Framework\TestCase;
|
7 | 8 | use Symfony\Component\Security\Core\Exception\AuthenticationException;
|
@@ -35,6 +36,63 @@ public function testOnAuthenticationFailure()
|
35 | 36 | $this->assertEquals($authenticationException->getMessageKey(), $content['message']);
|
36 | 37 | }
|
37 | 38 |
|
| 39 | + /** |
| 40 | + * test onAuthenticationFailure method. |
| 41 | + */ |
| 42 | + public function testOnAuthenticationFailureWithANonDefaultHttpFailureStatusCode() |
| 43 | + { |
| 44 | + $dispatcher = $this |
| 45 | + ->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface') |
| 46 | + ->disableOriginalConstructor() |
| 47 | + ->getMock(); |
| 48 | + |
| 49 | + $authenticationException = new AuthenticationException('', 403); |
| 50 | + |
| 51 | + $handler = new AuthenticationFailureHandler($dispatcher); |
| 52 | + $response = $handler->onAuthenticationFailure($this->getRequest(), $authenticationException); |
| 53 | + $content = json_decode($response->getContent(), true); |
| 54 | + |
| 55 | + $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response); |
| 56 | + $this->assertEquals(403, $response->getStatusCode()); |
| 57 | + $this->assertEquals(403, $content['code']); |
| 58 | + $this->assertEquals($authenticationException->getMessageKey(), $content['message']); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * test onAuthenticationFailure method. |
| 63 | + * |
| 64 | + * @dataProvider nonHttpStatusCodeProvider |
| 65 | + * |
| 66 | + * @param string|int $nonHttpStatusCode |
| 67 | + */ |
| 68 | + public function testOnAuthenticationFailureWithANonHttpStatusCode($nonHttpStatusCode) |
| 69 | + { |
| 70 | + $dispatcher = $this |
| 71 | + ->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface') |
| 72 | + ->disableOriginalConstructor() |
| 73 | + ->getMock(); |
| 74 | + |
| 75 | + $authenticationException = new AuthenticationException('', $nonHttpStatusCode); |
| 76 | + |
| 77 | + $handler = new AuthenticationFailureHandler($dispatcher); |
| 78 | + $response = $handler->onAuthenticationFailure($this->getRequest(), $authenticationException); |
| 79 | + $content = json_decode($response->getContent(), true); |
| 80 | + |
| 81 | + $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response); |
| 82 | + $this->assertEquals(401, $response->getStatusCode()); |
| 83 | + $this->assertEquals(401, $content['code']); |
| 84 | + $this->assertEquals($authenticationException->getMessageKey(), $content['message']); |
| 85 | + } |
| 86 | + |
| 87 | + public static function nonHttpStatusCodeProvider(): iterable |
| 88 | + { |
| 89 | + yield 'server error HTTP status code' => [500]; |
| 90 | + yield 'redirection HTTP status code' => [500]; |
| 91 | + yield 'success HTTP status code' => [500]; |
| 92 | + yield 'non HTTP status code' => [1302]; |
| 93 | + yield 'default status code' => [0]; |
| 94 | + } |
| 95 | + |
38 | 96 | /**
|
39 | 97 | * @return \PHPUnit_Framework_MockObject_MockObject
|
40 | 98 | */
|
|
0 commit comments