Skip to content

Add tests for translated error messages of json authentication #38044

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
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\Security;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Http\Authenticator\JsonLoginAuthenticator;
use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
use Symfony\Component\Security\Http\HttpUtils;
use Symfony\Component\Translation\Loader\ArrayLoader;
use Symfony\Component\Translation\Translator;

class JsonLoginAuthenticatorTest extends TestCase
{
Expand Down Expand Up @@ -123,6 +126,27 @@ public function provideInvalidAuthenticateData()
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 = [])
{
$this->authenticator = new JsonLoginAuthenticator(new HttpUtils(), $this->userProvider, null, null, $options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Http\Firewall\UsernamePasswordJsonAuthenticationListener;
use Symfony\Component\Security\Http\HttpUtils;
use Symfony\Component\Translation\Loader\ArrayLoader;
use Symfony\Component\Translation\Translator;

/**
* @author Kévin Dunglas <dunglas@gmail.com>
Expand All @@ -36,7 +38,7 @@ class UsernamePasswordJsonAuthenticationListenerTest extends TestCase
*/
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();
$httpUtils = $this->getMockBuilder(HttpUtils::class)->getMock();
Expand All @@ -55,10 +57,15 @@ private function createListener(array $options = [], $success = true, $matchChec
$authenticationManager->method('authenticate')->willThrowException(new AuthenticationException());
}

$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'));
$authenticationSuccessHandler = null;
$authenticationFailureHandler = null;

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);
}
Expand Down Expand Up @@ -86,12 +93,28 @@ public function testSuccessIfRequestFormatIsJsonLD()

public function testHandleFailure()
{
$this->createListener([], false);
$this->createListener([], false, true, false);
$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' => '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->assertEquals('ko', $event->getResponse()->getContent());
$this->assertSame(['error' => 'foo'], json_decode($event->getResponse()->getContent(), true));
}

public function testUsePath()
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Security/Http/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"require-dev": {
"symfony/routing": "^4.4|^5.0",
"symfony/security-csrf": "^4.4|^5.0",
"symfony/translation": "^4.4|^5.0",
"psr/log": "~1.0"
},
"conflict": {
Expand Down