diff options
author | Jeremy Benoist <jeremy.benoist@gmail.com> | 2017-06-09 09:45:43 +0200 |
---|---|---|
committer | Jeremy Benoist <jeremy.benoist@gmail.com> | 2017-06-09 09:45:43 +0200 |
commit | f81a34e37929a822755d120215d2f18f042ff713 (patch) | |
tree | 086049b73cf8d547f18c28963a60f36c5679cfcc /tests/Wallabag | |
parent | fa1c9d7cc7f3c4d2f9167a5b62bbc8cd1f9df59b (diff) | |
download | wallabag-f81a34e37929a822755d120215d2f18f042ff713.tar.gz wallabag-f81a34e37929a822755d120215d2f18f042ff713.tar.zst wallabag-f81a34e37929a822755d120215d2f18f042ff713.zip |
Use a listener to catch auth failure
Diffstat (limited to 'tests/Wallabag')
-rw-r--r-- | tests/Wallabag/UserBundle/EventListener/AuthenticationFailureListenerTest.php | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/tests/Wallabag/UserBundle/EventListener/AuthenticationFailureListenerTest.php b/tests/Wallabag/UserBundle/EventListener/AuthenticationFailureListenerTest.php new file mode 100644 index 00000000..6191ea13 --- /dev/null +++ b/tests/Wallabag/UserBundle/EventListener/AuthenticationFailureListenerTest.php | |||
@@ -0,0 +1,66 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Tests\Wallabag\UserBundle\EventListener; | ||
4 | |||
5 | use Symfony\Component\EventDispatcher\EventDispatcher; | ||
6 | use Symfony\Component\HttpFoundation\Request; | ||
7 | use Wallabag\UserBundle\EventListener\AuthenticationFailureListener; | ||
8 | use Monolog\Logger; | ||
9 | use Monolog\Handler\TestHandler; | ||
10 | use Symfony\Component\HttpFoundation\RequestStack; | ||
11 | use Symfony\Component\Security\Core\AuthenticationEvents; | ||
12 | use Symfony\Component\Security\Core\Event\AuthenticationFailureEvent; | ||
13 | |||
14 | class AuthenticationFailureListenerTest extends \PHPUnit_Framework_TestCase | ||
15 | { | ||
16 | private $requestStack; | ||
17 | private $logHandler; | ||
18 | private $listener; | ||
19 | private $dispatcher; | ||
20 | |||
21 | protected function setUp() | ||
22 | { | ||
23 | $request = Request::create('/'); | ||
24 | $request->request->set('_username', 'admin'); | ||
25 | |||
26 | $this->requestStack = new RequestStack(); | ||
27 | $this->requestStack->push($request); | ||
28 | |||
29 | $this->logHandler = new TestHandler(); | ||
30 | $logger = new Logger('test', [$this->logHandler]); | ||
31 | |||
32 | $this->listener = new AuthenticationFailureListener( | ||
33 | $this->requestStack, | ||
34 | $logger | ||
35 | ); | ||
36 | |||
37 | $this->dispatcher = new EventDispatcher(); | ||
38 | $this->dispatcher->addSubscriber($this->listener); | ||
39 | } | ||
40 | |||
41 | public function testOnAuthenticationFailure() | ||
42 | { | ||
43 | $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface') | ||
44 | ->disableOriginalConstructor() | ||
45 | ->getMock(); | ||
46 | |||
47 | $exception = $this->getMockBuilder('Symfony\Component\Security\Core\Exception\AuthenticationException') | ||
48 | ->disableOriginalConstructor() | ||
49 | ->getMock(); | ||
50 | |||
51 | $event = new AuthenticationFailureEvent( | ||
52 | $token, | ||
53 | $exception | ||
54 | ); | ||
55 | |||
56 | $this->dispatcher->dispatch( | ||
57 | AuthenticationEvents::AUTHENTICATION_FAILURE, | ||
58 | $event | ||
59 | ); | ||
60 | |||
61 | $records = $this->logHandler->getRecords(); | ||
62 | |||
63 | $this->assertCount(1, $records); | ||
64 | $this->assertSame('Authentication failure for user "admin", from IP "127.0.0.1", with UA: "Symfony/3.X".', $records[0]['message']); | ||
65 | } | ||
66 | } | ||