aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/Wallabag/UserBundle/EventListener/AuthenticationFailureListenerTest.php
blob: 6e37cfc4827ed2300bbaa639e17120640b03b4c7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php

namespace Tests\Wallabag\UserBundle\EventListener;

use Monolog\Handler\TestHandler;
use Monolog\Logger;
use PHPUnit\Framework\TestCase;
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;
use Wallabag\UserBundle\EventListener\AuthenticationFailureListener;

class AuthenticationFailureListenerTest extends TestCase
{
    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']);
    }
}