2017-06-09 07:45:43 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Wallabag\UserBundle\EventListener;
|
|
|
|
|
2017-07-01 07:52:38 +00:00
|
|
|
use Monolog\Handler\TestHandler;
|
|
|
|
use Monolog\Logger;
|
2017-12-16 21:17:42 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2017-06-09 07:45:43 +00:00
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
use Symfony\Component\HttpFoundation\RequestStack;
|
|
|
|
use Symfony\Component\Security\Core\AuthenticationEvents;
|
|
|
|
use Symfony\Component\Security\Core\Event\AuthenticationFailureEvent;
|
2017-07-01 07:52:38 +00:00
|
|
|
use Wallabag\UserBundle\EventListener\AuthenticationFailureListener;
|
2017-06-09 07:45:43 +00:00
|
|
|
|
2017-12-16 21:17:42 +00:00
|
|
|
class AuthenticationFailureListenerTest extends TestCase
|
2017-06-09 07:45:43 +00:00
|
|
|
{
|
|
|
|
private $requestStack;
|
|
|
|
private $logHandler;
|
|
|
|
private $listener;
|
|
|
|
private $dispatcher;
|
|
|
|
|
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
$request = Request::create('/');
|
|
|
|
$request->request->set('_username', 'admin');
|
|
|
|
|
|
|
|
$this->requestStack = new RequestStack();
|
|
|
|
$this->requestStack->push($request);
|
|
|
|
|
|
|
|
$this->logHandler = new TestHandler();
|
|
|
|
$logger = new Logger('test', [$this->logHandler]);
|
|
|
|
|
|
|
|
$this->listener = new AuthenticationFailureListener(
|
|
|
|
$this->requestStack,
|
|
|
|
$logger
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->dispatcher = new EventDispatcher();
|
|
|
|
$this->dispatcher->addSubscriber($this->listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testOnAuthenticationFailure()
|
|
|
|
{
|
|
|
|
$token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$exception = $this->getMockBuilder('Symfony\Component\Security\Core\Exception\AuthenticationException')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$event = new AuthenticationFailureEvent(
|
|
|
|
$token,
|
|
|
|
$exception
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->dispatcher->dispatch(
|
|
|
|
AuthenticationEvents::AUTHENTICATION_FAILURE,
|
|
|
|
$event
|
|
|
|
);
|
|
|
|
|
|
|
|
$records = $this->logHandler->getRecords();
|
|
|
|
|
|
|
|
$this->assertCount(1, $records);
|
|
|
|
$this->assertSame('Authentication failure for user "admin", from IP "127.0.0.1", with UA: "Symfony/3.X".', $records[0]['message']);
|
|
|
|
}
|
|
|
|
}
|