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;
|
2022-07-21 13:02:32 +03:00
|
|
|
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
|
2021-02-05 14:47:54 +03:00
|
|
|
|
|
|
|
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 16:25:53 +03:00
|
|
|
$auth = new FrontApiClientAuthenticator($errorResponseFactory, $security);
|
2022-07-20 13:58:16 +03:00
|
|
|
$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 16:25:53 +03:00
|
|
|
|
|
|
|
$auth = new FrontApiClientAuthenticator($errorResponseFactory, $security);
|
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 16:25:53 +03:00
|
|
|
$auth = new FrontApiClientAuthenticator($errorResponseFactory, $security);
|
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 16:25:53 +03:00
|
|
|
$auth = new FrontApiClientAuthenticator($errorResponseFactory, $security);
|
2022-07-20 13:58:16 +03:00
|
|
|
|
|
|
|
$passport = $auth->authenticate(new Request([], [FrontApiClientAuthenticator::AUTH_FIELD => '123']));
|
2022-07-21 13:02:32 +03:00
|
|
|
static::assertTrue($passport->hasBadge(UserBadge::class));
|
|
|
|
static::assertEquals(
|
|
|
|
$user->getUserIdentifier(),
|
|
|
|
$passport->getBadge(UserBadge::class)->getUserIdentifier()
|
|
|
|
);
|
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 16:25:53 +03:00
|
|
|
|
|
|
|
$auth = new FrontApiClientAuthenticator($errorResponseFactory, $security);
|
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
|
|
|
}
|