Skip to content

Commit b50fc19

Browse files
Add tests for translated error messages of json authentication
1 parent 7684663 commit b50fc19

File tree

3 files changed

+55
-7
lines changed

3 files changed

+55
-7
lines changed

src/Symfony/Component/Security/Http/Tests/Authenticator/JsonLoginAuthenticatorTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\HttpFoundation\Request;
1616
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
17+
use Symfony\Component\Security\Core\Exception\AuthenticationException;
1718
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
1819
use Symfony\Component\Security\Core\Security;
1920
use Symfony\Component\Security\Core\User\UserProviderInterface;
2021
use Symfony\Component\Security\Http\Authenticator\JsonLoginAuthenticator;
2122
use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
2223
use Symfony\Component\Security\Http\HttpUtils;
24+
use Symfony\Component\Translation\Loader\ArrayLoader;
25+
use Symfony\Component\Translation\Translator;
2326

2427
class JsonLoginAuthenticatorTest extends TestCase
2528
{
@@ -123,6 +126,27 @@ public function provideInvalidAuthenticateData()
123126
yield [$request, 'Invalid username.', BadCredentialsException::class];
124127
}
125128

129+
public function testAuthenticationFailureWithoutTranslator()
130+
{
131+
$this->setUpAuthenticator();
132+
133+
$response = $this->authenticator->onAuthenticationFailure(new Request(), new AuthenticationException());
134+
$this->assertSame(['error' => 'An authentication exception occurred.'], json_decode($response->getContent(), true));
135+
}
136+
137+
public function testAuthenticationFailureWithTranslator()
138+
{
139+
$translator = new Translator('en');
140+
$translator->addLoader('array', new ArrayLoader());
141+
$translator->addResource('array', ['An authentication exception occurred.' => 'foo'], 'en', 'security');
142+
143+
$this->setUpAuthenticator();
144+
$this->authenticator->setTranslator($translator);
145+
146+
$response = $this->authenticator->onAuthenticationFailure(new Request(), new AuthenticationException());
147+
$this->assertSame(['error' => 'foo'], json_decode($response->getContent(), true));
148+
}
149+
126150
private function setUpAuthenticator(array $options = [])
127151
{
128152
$this->authenticator = new JsonLoginAuthenticator(new HttpUtils(), $this->userProvider, null, null, $options);

src/Symfony/Component/Security/Http/Tests/Firewall/UsernamePasswordJsonAuthenticationListenerTest.php

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
2626
use Symfony\Component\Security\Http\Firewall\UsernamePasswordJsonAuthenticationListener;
2727
use Symfony\Component\Security\Http\HttpUtils;
28+
use Symfony\Component\Translation\Loader\ArrayLoader;
29+
use Symfony\Component\Translation\Translator;
2830

2931
/**
3032
* @author Kévin Dunglas <dunglas@gmail.com>
@@ -36,7 +38,7 @@ class UsernamePasswordJsonAuthenticationListenerTest extends TestCase
3638
*/
3739
private $listener;
3840

39-
private function createListener(array $options = [], $success = true, $matchCheckPath = true)
41+
private function createListener(array $options = [], $success = true, $matchCheckPath = true, $withMockedHandler = true)
4042
{
4143
$tokenStorage = $this->getMockBuilder(TokenStorageInterface::class)->getMock();
4244
$httpUtils = $this->getMockBuilder(HttpUtils::class)->getMock();
@@ -55,10 +57,15 @@ private function createListener(array $options = [], $success = true, $matchChec
5557
$authenticationManager->method('authenticate')->willThrowException(new AuthenticationException());
5658
}
5759

58-
$authenticationSuccessHandler = $this->getMockBuilder(AuthenticationSuccessHandlerInterface::class)->getMock();
59-
$authenticationSuccessHandler->method('onAuthenticationSuccess')->willReturn(new Response('ok'));
60-
$authenticationFailureHandler = $this->getMockBuilder(AuthenticationFailureHandlerInterface::class)->getMock();
61-
$authenticationFailureHandler->method('onAuthenticationFailure')->willReturn(new Response('ko'));
60+
$authenticationSuccessHandler = null;
61+
$authenticationFailureHandler = null;
62+
63+
if ($withMockedHandler) {
64+
$authenticationSuccessHandler = $this->getMockBuilder(AuthenticationSuccessHandlerInterface::class)->getMock();
65+
$authenticationSuccessHandler->method('onAuthenticationSuccess')->willReturn(new Response('ok'));
66+
$authenticationFailureHandler = $this->getMockBuilder(AuthenticationFailureHandlerInterface::class)->getMock();
67+
$authenticationFailureHandler->method('onAuthenticationFailure')->willReturn(new Response('ko'));
68+
}
6269

6370
$this->listener = new UsernamePasswordJsonAuthenticationListener($tokenStorage, $authenticationManager, $httpUtils, 'providerKey', $authenticationSuccessHandler, $authenticationFailureHandler, $options);
6471
}
@@ -86,12 +93,28 @@ public function testSuccessIfRequestFormatIsJsonLD()
8693

8794
public function testHandleFailure()
8895
{
89-
$this->createListener([], false);
96+
$this->createListener([], false, true, false);
97+
$request = new Request([], [], [], [], [], ['HTTP_CONTENT_TYPE' => 'application/json'], '{"username": "dunglas", "password": "foo"}');
98+
$event = new RequestEvent($this->getMockBuilder(KernelInterface::class)->getMock(), $request, KernelInterface::MASTER_REQUEST);
99+
100+
($this->listener)($event);
101+
$this->assertSame(['error' => 'An authentication exception occurred.'], json_decode($event->getResponse()->getContent(), true));
102+
}
103+
104+
public function testTranslatedHandleFailure()
105+
{
106+
$translator = new Translator('en');
107+
$translator->addLoader('array', new ArrayLoader());
108+
$translator->addResource('array', ['An authentication exception occurred.' => 'foo'], 'en', 'security');
109+
110+
$this->createListener([], false, true, false);
111+
$this->listener->setTranslator($translator);
112+
90113
$request = new Request([], [], [], [], [], ['HTTP_CONTENT_TYPE' => 'application/json'], '{"username": "dunglas", "password": "foo"}');
91114
$event = new RequestEvent($this->getMockBuilder(KernelInterface::class)->getMock(), $request, KernelInterface::MASTER_REQUEST);
92115

93116
($this->listener)($event);
94-
$this->assertEquals('ko', $event->getResponse()->getContent());
117+
$this->assertSame(['error' => 'foo'], json_decode($event->getResponse()->getContent(), true));
95118
}
96119

97120
public function testUsePath()

src/Symfony/Component/Security/Http/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"require-dev": {
2828
"symfony/routing": "^4.4|^5.0",
2929
"symfony/security-csrf": "^4.4|^5.0",
30+
"symfony/translation": "^4.4|^5.0",
3031
"psr/log": "~1.0"
3132
},
3233
"conflict": {

0 commit comments

Comments
 (0)