2021-02-05 14:47:54 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace RetailCrm\ServiceBundle\Tests\Security;
|
|
|
|
|
2022-07-20 13:58:16 +03:00
|
|
|
use Doctrine\Persistence\ObjectRepository;
|
2021-02-05 14:47:54 +03:00
|
|
|
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;
|
|
|
|
|
|
|
|
class FrontApiClientAuthenticatorTest extends TestCase
|
|
|
|
{
|
|
|
|
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);
|
2022-07-20 13:58:16 +03:00
|
|
|
$userRepository = $this->createMock(ObjectRepository::class);
|
|
|
|
$auth = new FrontApiClientAuthenticator($errorResponseFactory, $security, $userRepository);
|
|
|
|
$result = $auth->onAuthenticationFailure(new Request(), new AuthenticationException());
|
2021-02-05 14:47:54 +03:00
|
|
|
|
|
|
|
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());
|
2022-07-20 13:58:16 +03:00
|
|
|
$userRepository = $this->createMock(ObjectRepository::class);
|
|
|
|
$auth = new FrontApiClientAuthenticator($errorResponseFactory, $security, $userRepository);
|
2021-02-05 14:47:54 +03:00
|
|
|
$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);
|
2022-07-20 13:58:16 +03:00
|
|
|
$userRepository = $this->createMock(ObjectRepository::class);
|
|
|
|
$auth = new FrontApiClientAuthenticator($errorResponseFactory, $security, $userRepository);
|
2021-02-05 14:47:54 +03:00
|
|
|
$result = $auth->supports(new Request([], [FrontApiClientAuthenticator::AUTH_FIELD => '123']));
|
|
|
|
|
|
|
|
static::assertTrue($result);
|
|
|
|
}
|
|
|
|
|
2022-07-20 13:58:16 +03:00
|
|
|
public function testAuthenticate(): void
|
2021-03-31 11:00:48 +03:00
|
|
|
{
|
|
|
|
$errorResponseFactory = $this->createMock(ErrorJsonResponseFactory::class);
|
|
|
|
$security = $this->createMock(Security::class);
|
|
|
|
|
|
|
|
$user = new User();
|
2022-07-20 13:58:16 +03:00
|
|
|
$userRepository = $this->createMock(ObjectRepository::class);
|
|
|
|
$userRepository
|
2021-03-31 11:00:48 +03:00
|
|
|
->expects(static::once())
|
2022-07-20 13:58:16 +03:00
|
|
|
->method('findOneBy')
|
|
|
|
->with([FrontApiClientAuthenticator::AUTH_FIELD => '123'])
|
2021-03-31 11:00:48 +03:00
|
|
|
->willReturn($user)
|
|
|
|
;
|
2022-07-20 13:58:16 +03:00
|
|
|
$auth = new FrontApiClientAuthenticator($errorResponseFactory, $security, $userRepository);
|
|
|
|
|
|
|
|
$passport = $auth->authenticate(new Request([], [FrontApiClientAuthenticator::AUTH_FIELD => '123']));
|
|
|
|
$authUser = $passport->getUser();
|
|
|
|
static::assertEquals($user, $authUser);
|
2021-03-31 11:00:48 +03:00
|
|
|
|
2022-07-20 13:58:16 +03:00
|
|
|
$this->expectException(AuthenticationException::class);
|
|
|
|
$auth->authenticate(new Request());
|
2021-03-31 11:00:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testOnAuthenticationSuccess(): void
|
|
|
|
{
|
|
|
|
$errorResponseFactory = $this->createMock(ErrorJsonResponseFactory::class);
|
|
|
|
$security = $this->createMock(Security::class);
|
|
|
|
$request = $this->createMock(Request::class);
|
|
|
|
$token = $this->createMock(TokenInterface::class);
|
2022-07-20 13:58:16 +03:00
|
|
|
$userRepository = $this->createMock(ObjectRepository::class);
|
|
|
|
$auth = new FrontApiClientAuthenticator($errorResponseFactory, $security, $userRepository);
|
2021-03-31 11:00:48 +03:00
|
|
|
|
|
|
|
$result = $auth->onAuthenticationSuccess($request, $token, 'key');
|
|
|
|
|
|
|
|
static::assertNull($result);
|
|
|
|
}
|
2021-02-05 14:47:54 +03:00
|
|
|
}
|