Add tests for translated error messages of json authentication

This commit is contained in:
Malte Schlüter 2020-09-03 09:46:27 +02:00
parent 7684663818
commit b50fc19af0
3 changed files with 55 additions and 7 deletions

View File

@ -14,12 +14,15 @@ namespace Symfony\Component\Security\Http\Tests\Authenticator;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Exception\BadCredentialsException; use Symfony\Component\Security\Core\Exception\BadCredentialsException;
use Symfony\Component\Security\Core\Security; use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserProviderInterface; use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Http\Authenticator\JsonLoginAuthenticator; use Symfony\Component\Security\Http\Authenticator\JsonLoginAuthenticator;
use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials; use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
use Symfony\Component\Security\Http\HttpUtils; use Symfony\Component\Security\Http\HttpUtils;
use Symfony\Component\Translation\Loader\ArrayLoader;
use Symfony\Component\Translation\Translator;
class JsonLoginAuthenticatorTest extends TestCase class JsonLoginAuthenticatorTest extends TestCase
{ {
@ -123,6 +126,27 @@ class JsonLoginAuthenticatorTest extends TestCase
yield [$request, 'Invalid username.', BadCredentialsException::class]; yield [$request, 'Invalid username.', BadCredentialsException::class];
} }
public function testAuthenticationFailureWithoutTranslator()
{
$this->setUpAuthenticator();
$response = $this->authenticator->onAuthenticationFailure(new Request(), new AuthenticationException());
$this->assertSame(['error' => 'An authentication exception occurred.'], json_decode($response->getContent(), true));
}
public function testAuthenticationFailureWithTranslator()
{
$translator = new Translator('en');
$translator->addLoader('array', new ArrayLoader());
$translator->addResource('array', ['An authentication exception occurred.' => 'foo'], 'en', 'security');
$this->setUpAuthenticator();
$this->authenticator->setTranslator($translator);
$response = $this->authenticator->onAuthenticationFailure(new Request(), new AuthenticationException());
$this->assertSame(['error' => 'foo'], json_decode($response->getContent(), true));
}
private function setUpAuthenticator(array $options = []) private function setUpAuthenticator(array $options = [])
{ {
$this->authenticator = new JsonLoginAuthenticator(new HttpUtils(), $this->userProvider, null, null, $options); $this->authenticator = new JsonLoginAuthenticator(new HttpUtils(), $this->userProvider, null, null, $options);

View File

@ -25,6 +25,8 @@ use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerI
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface; use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Http\Firewall\UsernamePasswordJsonAuthenticationListener; use Symfony\Component\Security\Http\Firewall\UsernamePasswordJsonAuthenticationListener;
use Symfony\Component\Security\Http\HttpUtils; use Symfony\Component\Security\Http\HttpUtils;
use Symfony\Component\Translation\Loader\ArrayLoader;
use Symfony\Component\Translation\Translator;
/** /**
* @author Kévin Dunglas <dunglas@gmail.com> * @author Kévin Dunglas <dunglas@gmail.com>
@ -36,7 +38,7 @@ class UsernamePasswordJsonAuthenticationListenerTest extends TestCase
*/ */
private $listener; private $listener;
private function createListener(array $options = [], $success = true, $matchCheckPath = true) private function createListener(array $options = [], $success = true, $matchCheckPath = true, $withMockedHandler = true)
{ {
$tokenStorage = $this->getMockBuilder(TokenStorageInterface::class)->getMock(); $tokenStorage = $this->getMockBuilder(TokenStorageInterface::class)->getMock();
$httpUtils = $this->getMockBuilder(HttpUtils::class)->getMock(); $httpUtils = $this->getMockBuilder(HttpUtils::class)->getMock();
@ -55,10 +57,15 @@ class UsernamePasswordJsonAuthenticationListenerTest extends TestCase
$authenticationManager->method('authenticate')->willThrowException(new AuthenticationException()); $authenticationManager->method('authenticate')->willThrowException(new AuthenticationException());
} }
$authenticationSuccessHandler = $this->getMockBuilder(AuthenticationSuccessHandlerInterface::class)->getMock(); $authenticationSuccessHandler = null;
$authenticationSuccessHandler->method('onAuthenticationSuccess')->willReturn(new Response('ok')); $authenticationFailureHandler = null;
$authenticationFailureHandler = $this->getMockBuilder(AuthenticationFailureHandlerInterface::class)->getMock();
$authenticationFailureHandler->method('onAuthenticationFailure')->willReturn(new Response('ko')); if ($withMockedHandler) {
$authenticationSuccessHandler = $this->getMockBuilder(AuthenticationSuccessHandlerInterface::class)->getMock();
$authenticationSuccessHandler->method('onAuthenticationSuccess')->willReturn(new Response('ok'));
$authenticationFailureHandler = $this->getMockBuilder(AuthenticationFailureHandlerInterface::class)->getMock();
$authenticationFailureHandler->method('onAuthenticationFailure')->willReturn(new Response('ko'));
}
$this->listener = new UsernamePasswordJsonAuthenticationListener($tokenStorage, $authenticationManager, $httpUtils, 'providerKey', $authenticationSuccessHandler, $authenticationFailureHandler, $options); $this->listener = new UsernamePasswordJsonAuthenticationListener($tokenStorage, $authenticationManager, $httpUtils, 'providerKey', $authenticationSuccessHandler, $authenticationFailureHandler, $options);
} }
@ -86,12 +93,28 @@ class UsernamePasswordJsonAuthenticationListenerTest extends TestCase
public function testHandleFailure() public function testHandleFailure()
{ {
$this->createListener([], false); $this->createListener([], false, true, false);
$request = new Request([], [], [], [], [], ['HTTP_CONTENT_TYPE' => 'application/json'], '{"username": "dunglas", "password": "foo"}'); $request = new Request([], [], [], [], [], ['HTTP_CONTENT_TYPE' => 'application/json'], '{"username": "dunglas", "password": "foo"}');
$event = new RequestEvent($this->getMockBuilder(KernelInterface::class)->getMock(), $request, KernelInterface::MASTER_REQUEST); $event = new RequestEvent($this->getMockBuilder(KernelInterface::class)->getMock(), $request, KernelInterface::MASTER_REQUEST);
($this->listener)($event); ($this->listener)($event);
$this->assertEquals('ko', $event->getResponse()->getContent()); $this->assertSame(['error' => 'An authentication exception occurred.'], json_decode($event->getResponse()->getContent(), true));
}
public function testTranslatedHandleFailure()
{
$translator = new Translator('en');
$translator->addLoader('array', new ArrayLoader());
$translator->addResource('array', ['An authentication exception occurred.' => 'foo'], 'en', 'security');
$this->createListener([], false, true, false);
$this->listener->setTranslator($translator);
$request = new Request([], [], [], [], [], ['HTTP_CONTENT_TYPE' => 'application/json'], '{"username": "dunglas", "password": "foo"}');
$event = new RequestEvent($this->getMockBuilder(KernelInterface::class)->getMock(), $request, KernelInterface::MASTER_REQUEST);
($this->listener)($event);
$this->assertSame(['error' => 'foo'], json_decode($event->getResponse()->getContent(), true));
} }
public function testUsePath() public function testUsePath()

View File

@ -27,6 +27,7 @@
"require-dev": { "require-dev": {
"symfony/routing": "^4.4|^5.0", "symfony/routing": "^4.4|^5.0",
"symfony/security-csrf": "^4.4|^5.0", "symfony/security-csrf": "^4.4|^5.0",
"symfony/translation": "^4.4|^5.0",
"psr/log": "~1.0" "psr/log": "~1.0"
}, },
"conflict": { "conflict": {