86 lines
3.1 KiB
PHP
86 lines
3.1 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Tests\Security;
|
||
|
|
|
||
|
|
use App\Entity\Account;
|
||
|
|
use App\Security\LoginFormAuthenticator;
|
||
|
|
use Doctrine\ORM\EntityManagerInterface;
|
||
|
|
use Doctrine\ORM\EntityRepository;
|
||
|
|
use PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations;
|
||
|
|
use PHPUnit\Framework\TestCase;
|
||
|
|
use Symfony\Bundle\SecurityBundle\Security;
|
||
|
|
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||
|
|
use Symfony\Component\HttpFoundation\Request;
|
||
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
||
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
||
|
|
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
|
||
|
|
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
|
||
|
|
|
||
|
|
#[AllowMockObjectsWithoutExpectations]
|
||
|
|
class LoginFormAuthenticatorTest extends TestCase
|
||
|
|
{
|
||
|
|
private $entityManager;
|
||
|
|
private $urlGenerator;
|
||
|
|
private $security;
|
||
|
|
private $authenticator;
|
||
|
|
|
||
|
|
protected function setUp(): void
|
||
|
|
{
|
||
|
|
$this->entityManager = $this->createMock(EntityManagerInterface::class);
|
||
|
|
$this->urlGenerator = $this->createMock(UrlGeneratorInterface::class);
|
||
|
|
$this->security = $this->createMock(Security::class);
|
||
|
|
|
||
|
|
$this->authenticator = new LoginFormAuthenticator(
|
||
|
|
$this->entityManager,
|
||
|
|
$this->urlGenerator,
|
||
|
|
$this->security
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function testSupports()
|
||
|
|
{
|
||
|
|
$request = Request::create('/login', 'POST');
|
||
|
|
$request->attributes->set('_route', 'app_home');
|
||
|
|
$request->headers->set('HOST', 'intranet.ludikevent.fr');
|
||
|
|
|
||
|
|
$this->assertTrue($this->authenticator->supports($request));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function testAuthenticateUserNotFound()
|
||
|
|
{
|
||
|
|
$request = Request::create('/login', 'POST', [
|
||
|
|
'_username' => 'unknown@test.com',
|
||
|
|
'_password' => 'pass',
|
||
|
|
'_csrf_token' => 'token'
|
||
|
|
]);
|
||
|
|
$request->setSession($this->createMock(SessionInterface::class));
|
||
|
|
|
||
|
|
$repo = $this->createMock(EntityRepository::class);
|
||
|
|
$this->entityManager->method('getRepository')->willReturn($repo);
|
||
|
|
$repo->method('findOneBy')->willReturn(null);
|
||
|
|
|
||
|
|
$this->expectException(CustomUserMessageAuthenticationException::class);
|
||
|
|
|
||
|
|
$passport = $this->authenticator->authenticate($request);
|
||
|
|
$passport->getBadge(\Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge::class)->getUserLoader()('unknown@test.com');
|
||
|
|
}
|
||
|
|
|
||
|
|
public function testOnAuthenticationSuccess()
|
||
|
|
{
|
||
|
|
$request = Request::create('/login');
|
||
|
|
$request->setSession($this->createMock(SessionInterface::class));
|
||
|
|
$token = $this->createMock(TokenInterface::class);
|
||
|
|
|
||
|
|
$this->urlGenerator->expects($this->once())
|
||
|
|
->method('generate')
|
||
|
|
->with('app_crm')
|
||
|
|
->willReturn('/crm');
|
||
|
|
|
||
|
|
$response = $this->authenticator->onAuthenticationSuccess($request, $token, 'main');
|
||
|
|
|
||
|
|
$this->assertInstanceOf(RedirectResponse::class, $response);
|
||
|
|
$this->assertEquals('/crm', $response->getTargetUrl());
|
||
|
|
}
|
||
|
|
}
|