24
24
use Symfony \Component \Security \Http \Authentication \AuthenticationFailureHandlerInterface ;
25
25
use Symfony \Component \Security \Http \Authentication \AuthenticationSuccessHandlerInterface ;
26
26
use Symfony \Component \Security \Http \Firewall \UsernamePasswordJsonAuthenticationListener ;
27
+ use Symfony \Component \Security \Http \HttpUtils ;
27
28
28
29
/**
29
30
* @author Kévin Dunglas <dunglas@gmail.com>
@@ -35,9 +36,15 @@ class UsernamePasswordJsonAuthenticationListenerTest extends TestCase
35
36
*/
36
37
private $ listener ;
37
38
38
- private function createListener (array $ options = array (), $ success = true )
39
+ private function createListener (array $ options = array (), $ success = true , $ matchCheckPath = true )
39
40
{
40
41
$ tokenStorage = $ this ->getMockBuilder (TokenStorageInterface::class)->getMock ();
42
+ $ httpUtils = $ this ->getMockBuilder (HttpUtils::class)->getMock ();
43
+ $ httpUtils
44
+ ->expects ($ this ->any ())
45
+ ->method ('checkRequestPath ' )
46
+ ->will ($ this ->returnValue ($ matchCheckPath ))
47
+ ;
41
48
$ authenticationManager = $ this ->getMockBuilder (AuthenticationManagerInterface::class)->getMock ();
42
49
43
50
$ authenticatedToken = $ this ->getMockBuilder (TokenInterface::class)->getMock ();
@@ -53,7 +60,7 @@ private function createListener(array $options = array(), $success = true)
53
60
$ authenticationFailureHandler = $ this ->getMockBuilder (AuthenticationFailureHandlerInterface::class)->getMock ();
54
61
$ authenticationFailureHandler ->method ('onAuthenticationFailure ' )->willReturn (new Response ('ko ' ));
55
62
56
- $ this ->listener = new UsernamePasswordJsonAuthenticationListener ($ tokenStorage , $ authenticationManager , 'providerKey ' , $ authenticationSuccessHandler , $ authenticationFailureHandler , $ options );
63
+ $ this ->listener = new UsernamePasswordJsonAuthenticationListener ($ tokenStorage , $ authenticationManager , $ httpUtils , 'providerKey ' , $ authenticationSuccessHandler , $ authenticationFailureHandler , $ options );
57
64
}
58
65
59
66
public function testHandleSuccess ()
@@ -136,4 +143,25 @@ public function testAttemptAuthenticationUsernameTooLong()
136
143
$ this ->listener ->handle ($ event );
137
144
$ this ->assertSame ('ko ' , $ event ->getResponse ()->getContent ());
138
145
}
146
+
147
+ public function testDoesNotAttemptAuthenticationIfRequestPathDoesNotMatchCheckPath ()
148
+ {
149
+ $ this ->createListener (array ('check_path ' => '/ ' ), true , false );
150
+ $ request = new Request ();
151
+ $ event = new GetResponseEvent ($ this ->getMockBuilder (KernelInterface::class)->getMock (), $ request , KernelInterface::MASTER_REQUEST );
152
+ $ event ->setResponse (new Response ('original ' ));
153
+
154
+ $ this ->listener ->handle ($ event );
155
+ $ this ->assertSame ('original ' , $ event ->getResponse ()->getContent ());
156
+ }
157
+
158
+ public function testAttemptAuthenticationIfRequestPathMatchesCheckPath ()
159
+ {
160
+ $ this ->createListener (array ('check_path ' => '/ ' ));
161
+ $ request = new Request (array (), array (), array (), array (), array (), array (), '{"username": "dunglas", "password": "foo"} ' );
162
+ $ event = new GetResponseEvent ($ this ->getMockBuilder (KernelInterface::class)->getMock (), $ request , KernelInterface::MASTER_REQUEST );
163
+
164
+ $ this ->listener ->handle ($ event );
165
+ $ this ->assertEquals ('ok ' , $ event ->getResponse ()->getContent ());
166
+ }
139
167
}
0 commit comments