1
0
mirror of synced 2024-12-01 09:06:03 +03:00
service-bundle/Tests/Security/FrontApiClientAuthenticatorTest.php

157 lines
5.7 KiB
PHP
Raw Normal View History

2021-02-05 14:47:54 +03:00
<?php
namespace RetailCrm\ServiceBundle\Tests\Security;
use PHPUnit\Framework\TestCase;
use RetailCrm\ServiceBundle\Response\ErrorJsonResponseFactory;
use RetailCrm\ServiceBundle\Security\FrontApiClientAuthenticator;
use RetailCrm\ServiceBundle\Tests\DataFixtures\User;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
2021-03-31 11:00:48 +03:00
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
2021-02-05 14:47:54 +03:00
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Security;
2021-03-31 11:00:48 +03:00
use Symfony\Component\Security\Core\User\UserProviderInterface;
2021-02-05 14:47:54 +03:00
/**
* Class FrontApiClientAuthenticatorTest
*
* @package RetailCrm\ServiceBundle\Tests\Security
*/
class FrontApiClientAuthenticatorTest extends TestCase
{
public function testStart(): void
{
$errorResponseFactory = $this->createMock(ErrorJsonResponseFactory::class);
$errorResponseFactory
->expects(static::once())
->method('create')
->willReturn(
new JsonResponse(['message' => 'Authentication required'], Response::HTTP_UNAUTHORIZED)
);
$security = $this->createMock(Security::class);
$auth = new FrontApiClientAuthenticator($errorResponseFactory, $security);
$result = $auth->start(new Request(), new AuthenticationException());
static::assertInstanceOf(JsonResponse::class, $result);
static::assertEquals(Response::HTTP_UNAUTHORIZED, $result->getStatusCode());
}
public function testGetCredentials(): void
{
$errorResponseFactory = $this->createMock(ErrorJsonResponseFactory::class);
$security = $this->createMock(Security::class);
$auth = new FrontApiClientAuthenticator($errorResponseFactory, $security);
2021-03-31 11:00:48 +03:00
$result = $auth->getCredentials(new Request([], [FrontApiClientAuthenticator::AUTH_FIELD => '123']));
2021-02-05 14:47:54 +03:00
static::assertEquals('123', $result);
2021-03-31 11:00:48 +03:00
$result = $auth->getCredentials(new Request([FrontApiClientAuthenticator::AUTH_FIELD => '123']));
2021-02-05 14:47:54 +03:00
static::assertEquals('123', $result);
}
public function testCheckCredentials(): void
{
$errorResponseFactory = $this->createMock(ErrorJsonResponseFactory::class);
$security = $this->createMock(Security::class);
$auth = new FrontApiClientAuthenticator($errorResponseFactory, $security);
$result = $auth->checkCredentials(new Request(), new User());
static::assertTrue($result);
}
public function testOnAuthenticationFailure(): void
{
$errorResponseFactory = $this->createMock(ErrorJsonResponseFactory::class);
$errorResponseFactory
->expects(static::once())
->method('create')
->willReturn(
new JsonResponse(
['message' => 'An authentication exception occurred.'],
Response::HTTP_FORBIDDEN
)
);
$security = $this->createMock(Security::class);
$auth = new FrontApiClientAuthenticator($errorResponseFactory, $security);
$result = $auth->start(new Request(), new AuthenticationException());
static::assertInstanceOf(JsonResponse::class, $result);
static::assertEquals(Response::HTTP_FORBIDDEN, $result->getStatusCode());
}
public function testSupportsFalse(): void
{
$errorResponseFactory = $this->createMock(ErrorJsonResponseFactory::class);
$security = $this->createMock(Security::class);
$security->method('getUser')->willReturn(new User());
$auth = new FrontApiClientAuthenticator($errorResponseFactory, $security);
$result = $auth->supports(new Request());
static::assertFalse($result);
}
public function testSupportsTrue(): void
{
$errorResponseFactory = $this->createMock(ErrorJsonResponseFactory::class);
$security = $this->createMock(Security::class);
$security->method('getUser')->willReturn(null);
$auth = new FrontApiClientAuthenticator($errorResponseFactory, $security);
$result = $auth->supports(new Request([], [FrontApiClientAuthenticator::AUTH_FIELD => '123']));
static::assertTrue($result);
}
public function testSupportsRememberMe(): void
{
$errorResponseFactory = $this->createMock(ErrorJsonResponseFactory::class);
$security = $this->createMock(Security::class);
$auth = new FrontApiClientAuthenticator($errorResponseFactory, $security);
$result = $auth->supportsRememberMe();
static::assertTrue($result);
}
2021-03-31 11:00:48 +03:00
public function testGetUser(): void
{
$errorResponseFactory = $this->createMock(ErrorJsonResponseFactory::class);
$security = $this->createMock(Security::class);
$user = new User();
$auth = new FrontApiClientAuthenticator($errorResponseFactory, $security);
$userProvider = $this->createMock(UserProviderInterface::class);
$userProvider
->expects(static::once())
->method('loadUserByUsername')
->with('clientId')
->willReturn($user)
;
$result = $auth->getUser('clientId', $userProvider);
static::assertEquals($user, $result);
}
public function testOnAuthenticationSuccess(): void
{
$errorResponseFactory = $this->createMock(ErrorJsonResponseFactory::class);
$security = $this->createMock(Security::class);
$request = $this->createMock(Request::class);
$token = $this->createMock(TokenInterface::class);
$auth = new FrontApiClientAuthenticator($errorResponseFactory, $security);
$result = $auth->onAuthenticationSuccess($request, $token, 'key');
static::assertNull($result);
}
2021-02-05 14:47:54 +03:00
}