Skip to content
Permalink
Browse files

tests: Implemented all integration test cases.

  • Loading branch information...
tarlepp committed Jun 1, 2019
1 parent 80a49f5 commit 24bbdd079dae3d33bbb88d3b992d10964eae192b
Showing with 81 additions and 0 deletions.
  1. +81 −0 tests/Integration/Security/Authenticator/ApiKeyAuthenticatorTest.php
@@ -16,6 +16,10 @@
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\User\User;
use function json_encode;
/**
@@ -97,6 +101,68 @@ public function testThatGetUserReturnsExpectedWhenCredentialsIsInvalid($credenti
static::assertNull($authenticator->getUser($credentials, $apiKeyUserProvider));
}
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
/**
* @dataProvider dataProviderTestThatCheckCredentialsThrowsAnException
*
* @expectedException \Symfony\Component\Security\Core\Exception\AuthenticationException
* @expectedExceptionMessage Invalid token
*
* @param mixed $credentials
*/
public function testThatCheckCredentialsThrowsAnException($credentials): void
{
/**
* @var MockObject|ApiKeyUserProvider $apiKeyUserProvider
*/
$apiKeyUserProvider = $this->getMockBuilder(ApiKeyUserProvider::class)->disableOriginalConstructor()->getMock();
(new ApiKeyAuthenticator($apiKeyUserProvider))->checkCredentials($credentials, new User('user', 'password'));
}
public function testThatOnAuthenticationSuccessReturnsNull(): void
{
/**
* @var MockObject|ApiKeyUserProvider $apiKeyUserProvider
*/
$apiKeyUserProvider = $this->getMockBuilder(ApiKeyUserProvider::class)->disableOriginalConstructor()->getMock();
$authenticator = new ApiKeyAuthenticator($apiKeyUserProvider);
static::assertNull($authenticator->onAuthenticationSuccess(
new Request(),
new AnonymousToken('secret', 'user'),
'foobar'
));
}
public function testThatOnAuthenticationFailureReturnsExpected(): void
{
/**
* @var MockObject|ApiKeyUserProvider $apiKeyUserProvider
*/
$apiKeyUserProvider = $this->getMockBuilder(ApiKeyUserProvider::class)->disableOriginalConstructor()->getMock();
$output = (new ApiKeyAuthenticator($apiKeyUserProvider))
->onAuthenticationFailure(new Request(), new AuthenticationException('foobar'));
static::assertSame(Response::HTTP_FORBIDDEN, $output->getStatusCode());
static::assertJsonStringEqualsJsonString(
json_encode(['message' => 'An authentication exception occurred.']),
$output->getContent()
);
}
public function testThatSupportsRememberMeReturnsFalse(): void
{
/**
* @var MockObject|ApiKeyUserProvider $apiKeyUserProvider
*/
$apiKeyUserProvider = $this->getMockBuilder(ApiKeyUserProvider::class)->disableOriginalConstructor()->getMock();
static::assertFalse((new ApiKeyAuthenticator($apiKeyUserProvider))->supportsRememberMe());
}
/**
* @return Generator
*/
@@ -147,4 +213,19 @@ public function dataProviderTestThatGetUserReturnsExpected(): Generator
yield [['foobar' => 'barfoo']];
yield [['token' => null]];
}
/**
* @return Generator
*/
public function dataProviderTestThatCheckCredentialsThrowsAnException(): Generator
{
yield [null];
yield ['foobar'];
yield [123];
yield [new stdClass()];
yield [[]];
yield [['foobar']];
yield [['foobar' => 'barfoo']];
yield [['token' => null]];
}
}

0 comments on commit 24bbdd0

Please sign in to comment.
You can’t perform that action at this time.