Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
16 | class GeoBlockingKernelRequestListenerTest extends \PHPUnit_Framework_TestCase |
||
17 | { |
||
18 | private $usIP = "17.149.160.49"; |
||
19 | private $localIP = "192.168.0.42"; |
||
20 | private $chIP = "194.150.248.201"; |
||
21 | private $googleBotIP = "66.249.78.150"; |
||
22 | private $msnBotIP = "157.56.93.153"; |
||
23 | |||
24 | public function testOnKernelRequestGeoBlocking_Disabled() |
||
25 | { |
||
26 | $parameters = $this->getDefaultParams(); |
||
27 | $parameters['enabled'] = false; |
||
28 | $eventAllowMock = $this->getMockBuilder("Symfony\Component\HttpKernel\Event\GetResponseEvent")->disableOriginalConstructor()->getMock(); |
||
29 | $loggerMock = $this->getMockBuilder("Psr\Log\LoggerInterface")->disableOriginalConstructor()->getMock(); |
||
30 | |||
31 | $containerMock = $this->getMockBuilder("Symfony\Component\DependencyInjection\Container")->disableOriginalConstructor()->getMock(); |
||
32 | |||
33 | $eventAllowMock->expects($this->never())->method("getRequest"); |
||
34 | $eventAllowMock->expects($this->never())->method("setResponse"); |
||
35 | $eventAllowMock->expects($this->never())->method("stopPropagation"); |
||
36 | |||
37 | $geoBlockingListener = new GeoBlockingKernelRequestListener($this->getTemplatingMock(false), new DefaultLookupAdapter(), $loggerMock, $containerMock, $parameters); |
||
38 | $geoBlockingListener->onKernelRequest($eventAllowMock); |
||
39 | |||
40 | } |
||
41 | |||
42 | public function testOnKernelRequestGeoBlocking_BlockAccess() |
||
43 | { |
||
44 | $parameters = $this->getDefaultParams(); |
||
45 | $eventBlockMock = $this->getMockBuilder("Symfony\Component\HttpKernel\Event\GetResponseEvent")->disableOriginalConstructor()->getMock(); |
||
46 | $requestMock = $this->getMockBuilder("Symfony\Component\HttpFoundation\Request")->disableOriginalConstructor()->getMock(); |
||
47 | $loggerMock = $this->getMockBuilder("Psr\Log\LoggerInterface")->disableOriginalConstructor()->getMock(); |
||
48 | |||
49 | $containerMock = $this->getMockBuilder("Symfony\Component\DependencyInjection\Container")->disableOriginalConstructor()->getMock(); |
||
50 | $securityContextMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage")->disableOriginalConstructor()->getMock(); |
||
51 | $tokenMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken")->disableOriginalConstructor()->getMock(); |
||
52 | |||
53 | $tokenMock->expects($this->once())->method("getUser")->will($this->returnValue(null)); |
||
54 | $securityContextMock->expects($this->once())->method("getToken")->will($this->returnValue($tokenMock)); |
||
55 | $containerMock->expects($this->once())->method("get")->will($this->returnValue($securityContextMock)); |
||
56 | |||
57 | $eventBlockMock->expects($this->once())->method("getRequestType")->will($this->returnValue(HttpKernelInterface::MASTER_REQUEST)); |
||
58 | $eventBlockMock->expects($this->once())->method("setResponse")->will($this->returnCallback(array($this, 'checkResponseCode'))); |
||
59 | $eventBlockMock->expects($this->once())->method("stopPropagation"); |
||
60 | $requestMock->expects($this->once())->method("getClientIp")->will($this->returnValue($this->usIP)); |
||
61 | $eventBlockMock->expects($this->once())->method("getRequest")->will($this->returnValue($requestMock)); |
||
62 | |||
63 | $geoBlockingListener = new GeoBlockingKernelRequestListener($this->getTemplatingMock(true), new DefaultLookupAdapter(), $loggerMock, $containerMock, $parameters); |
||
64 | $geoBlockingListener->onKernelRequest($eventBlockMock); |
||
65 | } |
||
66 | |||
67 | public function checkResponseCode(Response $response){ |
||
70 | |||
71 | public function testOnKernelRequestGeoBlocking_SubRequest() |
||
72 | { |
||
73 | $parameters = $this->getDefaultParams(); |
||
74 | $eventBlockMock = $this->getMockBuilder("Symfony\Component\HttpKernel\Event\GetResponseEvent")->disableOriginalConstructor()->getMock(); |
||
75 | $loggerMock = $this->getMockBuilder("Psr\Log\LoggerInterface")->disableOriginalConstructor()->getMock(); |
||
76 | |||
77 | $containerMock = $this->getMockBuilder("Symfony\Component\DependencyInjection\Container")->disableOriginalConstructor()->getMock(); |
||
78 | |||
79 | $eventBlockMock->expects($this->once())->method("getRequestType")->will($this->returnValue(HttpKernelInterface::SUB_REQUEST)); |
||
80 | $eventBlockMock->expects($this->never())->method("setResponse"); |
||
81 | $eventBlockMock->expects($this->never())->method("stopPropagation"); |
||
82 | $eventBlockMock->expects($this->never())->method("getRequest"); |
||
83 | |||
84 | $geoBlockingListener = new GeoBlockingKernelRequestListener($this->getTemplatingMock(false), new DefaultLookupAdapter(), $loggerMock, $containerMock, $parameters); |
||
85 | $geoBlockingListener->onKernelRequest($eventBlockMock); |
||
86 | } |
||
87 | |||
88 | public function testOnKernelRequestGeoBlocking_AnonOnlyBlockAll() |
||
89 | { |
||
90 | $parameters = $this->getDefaultParams(); |
||
91 | $parameters['blockAnonOnly'] = false; |
||
92 | $requestMock = $this->getMockBuilder("Symfony\Component\HttpFoundation\Request")->disableOriginalConstructor()->getMock(); |
||
93 | $loggerMock = $this->getMockBuilder("Psr\Log\LoggerInterface")->disableOriginalConstructor()->getMock(); |
||
94 | |||
95 | $containerMock = $this->getMockBuilder("Symfony\Component\DependencyInjection\Container")->disableOriginalConstructor()->getMock(); |
||
96 | $securityContextMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage")->disableOriginalConstructor()->getMock(); |
||
97 | $tokenMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken")->disableOriginalConstructor()->getMock(); |
||
98 | |||
99 | $tokenMock->expects($this->once())->method("getUser")->will($this->returnValue($this->getMockBuilder("Symfony\Component\Security\Core\User\UserInterface")->disableOriginalConstructor()->getMock())); |
||
100 | $securityContextMock->expects($this->once())->method("getToken")->will($this->returnValue($tokenMock)); |
||
101 | $containerMock->expects($this->once())->method("get")->will($this->returnValue($securityContextMock)); |
||
102 | |||
103 | $eventBlockMock = $this->getMockBuilder("Symfony\Component\HttpKernel\Event\GetResponseEvent")->disableOriginalConstructor()->getMock(); |
||
104 | $eventBlockMock->expects($this->once())->method("getRequestType")->will($this->returnValue(HttpKernelInterface::MASTER_REQUEST)); |
||
105 | $eventBlockMock->expects($this->once())->method("setResponse")->will($this->returnCallback(array($this, 'checkResponseCode'))); |
||
106 | $eventBlockMock->expects($this->once())->method("stopPropagation"); |
||
107 | $requestMock->expects($this->once())->method("getClientIp")->will($this->returnValue($this->usIP)); |
||
108 | $eventBlockMock->expects($this->once())->method("getRequest")->will($this->returnValue($requestMock)); |
||
109 | |||
110 | $geoBlockingListener = new GeoBlockingKernelRequestListener($this->getTemplatingMock(true), new DefaultLookupAdapter(), $loggerMock, $containerMock, $parameters); |
||
111 | $geoBlockingListener->onKernelRequest($eventBlockMock); |
||
112 | } |
||
113 | |||
114 | View Code Duplication | public function testOnKernelRequestGeoBlocking_AnonOnlyNotLoggedIn() |
|
|
|||
115 | { |
||
116 | $parameters = $this->getDefaultParams(); |
||
117 | $eventBlockMock = $this->getMockBuilder("Symfony\Component\HttpKernel\Event\GetResponseEvent")->disableOriginalConstructor()->getMock(); |
||
118 | $requestMock = $this->getMockBuilder("Symfony\Component\HttpFoundation\Request")->disableOriginalConstructor()->getMock(); |
||
119 | $loggerMock = $this->getMockBuilder("Psr\Log\LoggerInterface")->disableOriginalConstructor()->getMock(); |
||
120 | |||
121 | $containerMock = $this->getMockBuilder("Symfony\Component\DependencyInjection\Container")->disableOriginalConstructor()->getMock(); |
||
122 | $securityContextMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage")->disableOriginalConstructor()->getMock(); |
||
123 | $tokenMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken")->disableOriginalConstructor()->getMock(); |
||
124 | |||
125 | $tokenMock->expects($this->once())->method("getUser")->will($this->returnValue(null)); |
||
126 | $securityContextMock->expects($this->once())->method("getToken")->will($this->returnValue($tokenMock)); |
||
127 | $containerMock->expects($this->once())->method("get")->will($this->returnValue($securityContextMock)); |
||
128 | |||
129 | $eventBlockMock->expects($this->once())->method("getRequestType")->will($this->returnValue(HttpKernelInterface::MASTER_REQUEST)); |
||
130 | $eventBlockMock->expects($this->once())->method("setResponse")->will($this->returnCallback(array($this, 'checkResponseCode'))); |
||
131 | $requestMock->expects($this->once())->method("getClientIp")->will($this->returnValue($this->usIP)); |
||
132 | $requestMock->expects($this->once())->method("get"); |
||
133 | $eventBlockMock->expects($this->once())->method("stopPropagation"); |
||
134 | $eventBlockMock->expects($this->once())->method("getRequest")->will($this->returnValue($requestMock)); |
||
135 | |||
136 | $geoBlockingListener = new GeoBlockingKernelRequestListener($this->getTemplatingMock(true), new DefaultLookupAdapter(), $loggerMock, $containerMock, $parameters); |
||
137 | $geoBlockingListener->onKernelRequest($eventBlockMock); |
||
138 | |||
139 | } |
||
140 | |||
141 | View Code Duplication | public function testOnKernelRequestGeoBlocking_AnonOnlyLoggedIn() |
|
142 | { |
||
143 | $parameters = $this->getDefaultParams(); |
||
144 | $eventBlockMock = $this->getMockBuilder("Symfony\Component\HttpKernel\Event\GetResponseEvent")->disableOriginalConstructor()->getMock(); |
||
145 | $requestMock = $this->getMockBuilder("Symfony\Component\HttpFoundation\Request")->disableOriginalConstructor()->getMock(); |
||
146 | |||
147 | $loggerMock = $this->getMockBuilder("Psr\Log\LoggerInterface")->disableOriginalConstructor()->getMock(); |
||
148 | |||
149 | $containerMock = $this->getMockBuilder("Symfony\Component\DependencyInjection\Container")->disableOriginalConstructor()->getMock(); |
||
150 | $securityContextMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage")->disableOriginalConstructor()->getMock(); |
||
151 | $tokenMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken")->disableOriginalConstructor()->getMock(); |
||
152 | |||
153 | $tokenMock->expects($this->once())->method("getUser")->will($this->returnValue($this->getMockBuilder("Symfony\Component\Security\Core\User\UserInterface")->disableOriginalConstructor()->getMock())); |
||
154 | $securityContextMock->expects($this->once())->method("getToken")->will($this->returnValue($tokenMock)); |
||
155 | $containerMock->expects($this->once())->method("get")->will($this->returnValue($securityContextMock)); |
||
156 | |||
157 | $eventBlockMock->expects($this->once())->method("getRequestType")->will($this->returnValue(HttpKernelInterface::MASTER_REQUEST)); |
||
158 | $eventBlockMock->expects($this->once())->method("getRequest")->will($this->returnValue($requestMock)); |
||
159 | $requestMock->expects($this->never())->method("getClientIp")->will($this->returnValue($this->usIP)); |
||
160 | $requestMock->expects($this->never())->method("get"); |
||
161 | $eventBlockMock->expects($this->never())->method("setResponse"); |
||
162 | $eventBlockMock->expects($this->never())->method("stopPropagation"); |
||
163 | |||
164 | $geoBlockingListener = new GeoBlockingKernelRequestListener($this->getTemplatingMock(false), new DefaultLookupAdapter(), $loggerMock, $containerMock, $parameters); |
||
165 | $geoBlockingListener->onKernelRequest($eventBlockMock); |
||
166 | |||
167 | } |
||
168 | |||
169 | public function testOnKernelRequestGeoBlocking_AllowPrivateIPs() |
||
170 | { |
||
171 | $parameters = $this->getDefaultParams(); |
||
172 | $eventBlockMock = $this->getMockBuilder("Symfony\Component\HttpKernel\Event\GetResponseEvent")->disableOriginalConstructor()->getMock(); |
||
173 | $requestMock = $this->getMockBuilder("Symfony\Component\HttpFoundation\Request")->disableOriginalConstructor()->getMock(); |
||
174 | |||
175 | $lookUpMock = $this->getMockBuilder("Azine\GeoBlockingBundle\Adapter\DefaultLookupAdapter")->getMock(); |
||
176 | $loggerMock = $this->getMockBuilder("Psr\Log\LoggerInterface")->disableOriginalConstructor()->getMock(); |
||
177 | |||
178 | $containerMock = $this->getMockBuilder("Symfony\Component\DependencyInjection\Container")->disableOriginalConstructor()->getMock(); |
||
179 | $securityContextMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage")->disableOriginalConstructor()->getMock(); |
||
180 | $tokenMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken")->disableOriginalConstructor()->getMock(); |
||
181 | |||
182 | $tokenMock->expects($this->once())->method("getUser")->will($this->returnValue(null)); |
||
183 | $securityContextMock->expects($this->once())->method("getToken")->will($this->returnValue($tokenMock)); |
||
184 | $containerMock->expects($this->once())->method("get")->will($this->returnValue($securityContextMock)); |
||
185 | |||
186 | $eventBlockMock->expects($this->once())->method("getRequestType")->will($this->returnValue(HttpKernelInterface::MASTER_REQUEST)); |
||
187 | $eventBlockMock->expects($this->once())->method("getRequest")->will($this->returnValue($requestMock)); |
||
188 | |||
189 | $requestMock->expects($this->once())->method("getClientIp")->will($this->returnValue($this->localIP)); |
||
190 | $requestMock->expects($this->never())->method("get"); |
||
191 | $lookUpMock->expects($this->never())->method("getCountry"); |
||
192 | $eventBlockMock->expects($this->never())->method("setResponse"); |
||
193 | $eventBlockMock->expects($this->never())->method("stopPropagation"); |
||
194 | |||
195 | $geoBlockingListener = new GeoBlockingKernelRequestListener($this->getTemplatingMock(false), $lookUpMock, $loggerMock, $containerMock, $parameters); |
||
196 | $geoBlockingListener->onKernelRequest($eventBlockMock); |
||
197 | |||
198 | } |
||
199 | |||
200 | public function testOnKernelRequestGeoBlocking_RouteBlocking_BlockWithWhiteList() |
||
201 | { |
||
202 | $parameters = $this->getDefaultParams(); |
||
203 | $eventBlockMock = $this->getMockBuilder("Symfony\Component\HttpKernel\Event\GetResponseEvent")->disableOriginalConstructor()->getMock(); |
||
204 | $requestMock = $this->getMockBuilder("Symfony\Component\HttpFoundation\Request")->disableOriginalConstructor()->getMock(); |
||
205 | |||
206 | $lookUpMock = $this->getMockBuilder("Azine\GeoBlockingBundle\Adapter\DefaultLookupAdapter")->getMock(); |
||
207 | $loggerMock = $this->getMockBuilder("Psr\Log\LoggerInterface")->disableOriginalConstructor()->getMock(); |
||
208 | |||
209 | $containerMock = $this->getMockBuilder("Symfony\Component\DependencyInjection\Container")->disableOriginalConstructor()->getMock(); |
||
210 | $securityContextMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage")->disableOriginalConstructor()->getMock(); |
||
211 | $tokenMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken")->disableOriginalConstructor()->getMock(); |
||
212 | |||
213 | $tokenMock->expects($this->once())->method("getUser")->will($this->returnValue(null)); |
||
214 | $securityContextMock->expects($this->once())->method("getToken")->will($this->returnValue($tokenMock)); |
||
215 | $containerMock->expects($this->once())->method("get")->will($this->returnValue($securityContextMock)); |
||
216 | |||
217 | $eventBlockMock->expects($this->once())->method("getRequestType")->will($this->returnValue(HttpKernelInterface::MASTER_REQUEST)); |
||
218 | $eventBlockMock->expects($this->once())->method("getRequest")->will($this->returnValue($requestMock)); |
||
219 | $requestMock->expects($this->once())->method("getClientIp")->will($this->returnValue($this->chIP)); |
||
220 | $requestMock->expects($this->once())->method("get")->with("_route", null)->will($this->returnValue("notAllowedRoute")); |
||
221 | $lookUpMock->expects($this->once())->method("getCountry"); |
||
222 | $eventBlockMock->expects($this->once())->method("setResponse")->will($this->returnCallback(array($this, 'checkResponseCode'))); |
||
223 | $eventBlockMock->expects($this->once())->method("stopPropagation"); |
||
224 | |||
225 | $geoBlockingListener = new GeoBlockingKernelRequestListener($this->getTemplatingMock(true), $lookUpMock, $loggerMock, $containerMock, $parameters); |
||
226 | $geoBlockingListener->onKernelRequest($eventBlockMock); |
||
227 | |||
228 | } |
||
229 | |||
230 | public function testOnKernelRequestGeoBlocking_RouteBlocking_AllowWithWhiteList() |
||
231 | { |
||
232 | $parameters = $this->getDefaultParams(); |
||
233 | $eventBlockMock = $this->getMockBuilder("Symfony\Component\HttpKernel\Event\GetResponseEvent")->disableOriginalConstructor()->getMock(); |
||
234 | $requestMock = $this->getMockBuilder("Symfony\Component\HttpFoundation\Request")->disableOriginalConstructor()->getMock(); |
||
235 | |||
236 | $lookUpMock = $this->getMockBuilder("Azine\GeoBlockingBundle\Adapter\DefaultLookupAdapter")->getMock(); |
||
237 | $loggerMock = $this->getMockBuilder("Psr\Log\LoggerInterface")->disableOriginalConstructor()->getMock(); |
||
238 | |||
239 | $containerMock = $this->getMockBuilder("Symfony\Component\DependencyInjection\Container")->disableOriginalConstructor()->getMock(); |
||
240 | $securityContextMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage")->disableOriginalConstructor()->getMock(); |
||
241 | $tokenMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken")->disableOriginalConstructor()->getMock(); |
||
242 | |||
243 | $tokenMock->expects($this->once())->method("getUser")->will($this->returnValue(null)); |
||
244 | $securityContextMock->expects($this->once())->method("getToken")->will($this->returnValue($tokenMock)); |
||
245 | $containerMock->expects($this->once())->method("get")->will($this->returnValue($securityContextMock)); |
||
246 | |||
247 | $eventBlockMock->expects($this->once())->method("getRequestType")->will($this->returnValue(HttpKernelInterface::MASTER_REQUEST)); |
||
248 | $eventBlockMock->expects($this->once())->method("getRequest")->will($this->returnValue($requestMock)); |
||
249 | $requestMock->expects($this->once())->method("getClientIp")->will($this->returnValue($this->chIP)); |
||
250 | $requestMock->expects($this->once())->method("get")->with("_route", null)->will($this->returnValue("fos_user_security_login")); |
||
251 | $lookUpMock->expects($this->never())->method("getCountry"); |
||
252 | $eventBlockMock->expects($this->never())->method("setResponse"); |
||
253 | $eventBlockMock->expects($this->never())->method("stopPropagation"); |
||
254 | |||
255 | $geoBlockingListener = new GeoBlockingKernelRequestListener($this->getTemplatingMock(false), $lookUpMock, $loggerMock, $containerMock, $parameters); |
||
256 | $geoBlockingListener->onKernelRequest($eventBlockMock); |
||
257 | |||
258 | } |
||
259 | |||
260 | public function testOnKernelRequestGeoBlocking_RouteBlocking_BlockWithBlackList() |
||
261 | { |
||
262 | $parameters = $this->getDefaultParams(); |
||
263 | $parameters["countryWhitelist"] = array(); |
||
264 | $parameters["routeWhitelist"] = array(); |
||
265 | $parameters["routeBlacklist"] = array("notAllowedRoute"); |
||
266 | |||
267 | $eventBlockMock = $this->getMockBuilder("Symfony\Component\HttpKernel\Event\GetResponseEvent")->disableOriginalConstructor()->getMock(); |
||
268 | $requestMock = $this->getMockBuilder("Symfony\Component\HttpFoundation\Request")->disableOriginalConstructor()->getMock(); |
||
269 | |||
270 | $lookUpMock = $this->getMockBuilder("Azine\GeoBlockingBundle\Adapter\DefaultLookupAdapter")->getMock(); |
||
271 | $loggerMock = $this->getMockBuilder("Psr\Log\LoggerInterface")->disableOriginalConstructor()->getMock(); |
||
272 | |||
273 | $containerMock = $this->getMockBuilder("Symfony\Component\DependencyInjection\Container")->disableOriginalConstructor()->getMock(); |
||
274 | $securityContextMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage")->disableOriginalConstructor()->getMock(); |
||
275 | $tokenMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken")->disableOriginalConstructor()->getMock(); |
||
276 | |||
277 | $tokenMock->expects($this->once())->method("getUser")->will($this->returnValue(null)); |
||
278 | $securityContextMock->expects($this->once())->method("getToken")->will($this->returnValue($tokenMock)); |
||
279 | $containerMock->expects($this->once())->method("get")->will($this->returnValue($securityContextMock)); |
||
280 | |||
281 | $eventBlockMock->expects($this->once())->method("getRequestType")->will($this->returnValue(HttpKernelInterface::MASTER_REQUEST)); |
||
282 | $eventBlockMock->expects($this->once())->method("getRequest")->will($this->returnValue($requestMock)); |
||
283 | $requestMock->expects($this->once())->method("getClientIp")->will($this->returnValue($this->chIP)); |
||
284 | $requestMock->expects($this->once())->method("get")->with("_route", null)->will($this->returnValue("notAllowedRoute")); |
||
285 | $lookUpMock->expects($this->once())->method("getCountry"); |
||
286 | $eventBlockMock->expects($this->once())->method("setResponse")->will($this->returnCallback(array($this, 'checkResponseCode'))); |
||
287 | $eventBlockMock->expects($this->once())->method("stopPropagation"); |
||
288 | |||
289 | $geoBlockingListener = new GeoBlockingKernelRequestListener($this->getTemplatingMock(true), $lookUpMock, $loggerMock, $containerMock, $parameters); |
||
290 | $geoBlockingListener->onKernelRequest($eventBlockMock); |
||
291 | |||
292 | } |
||
293 | |||
294 | View Code Duplication | public function testOnKernelRequestGeoBlocking_RouteBlocking_AllowWithBlackList() |
|
295 | { |
||
296 | $parameters = $this->getDefaultParams(); |
||
297 | $parameters["routeWhitelist"] = array(); |
||
298 | $parameters["routeBlacklist"] = array("notAllowedRoute"); |
||
299 | |||
300 | $eventBlockMock = $this->getMockBuilder("Symfony\Component\HttpKernel\Event\GetResponseEvent")->disableOriginalConstructor()->getMock(); |
||
301 | $requestMock = $this->getMockBuilder("Symfony\Component\HttpFoundation\Request")->disableOriginalConstructor()->getMock(); |
||
302 | |||
303 | $lookUpMock = $this->getMockBuilder("Azine\GeoBlockingBundle\Adapter\DefaultLookupAdapter")->getMock(); |
||
304 | $loggerMock = $this->getMockBuilder("Psr\Log\LoggerInterface")->disableOriginalConstructor()->getMock(); |
||
305 | |||
306 | $containerMock = $this->getMockBuilder("Symfony\Component\DependencyInjection\Container")->disableOriginalConstructor()->getMock(); |
||
307 | $securityContextMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage")->disableOriginalConstructor()->getMock(); |
||
308 | $tokenMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken")->disableOriginalConstructor()->getMock(); |
||
309 | |||
310 | $tokenMock->expects($this->once())->method("getUser")->will($this->returnValue(null)); |
||
311 | $securityContextMock->expects($this->once())->method("getToken")->will($this->returnValue($tokenMock)); |
||
312 | $containerMock->expects($this->once())->method("get")->will($this->returnValue($securityContextMock)); |
||
313 | |||
314 | $eventBlockMock->expects($this->once())->method("getRequestType")->will($this->returnValue(HttpKernelInterface::MASTER_REQUEST)); |
||
315 | $eventBlockMock->expects($this->once())->method("getRequest")->will($this->returnValue($requestMock)); |
||
316 | $requestMock->expects($this->once())->method("getClientIp")->will($this->returnValue($this->chIP)); |
||
317 | $requestMock->expects($this->once())->method("get")->with("_route", null)->will($this->returnValue("someOtherRoute")); |
||
318 | $lookUpMock->expects($this->once())->method("getCountry")->with($this->chIP)->will($this->returnValue("CH")); |
||
319 | $eventBlockMock->expects($this->never())->method("setResponse"); |
||
320 | $eventBlockMock->expects($this->never())->method("stopPropagation"); |
||
321 | |||
322 | $geoBlockingListener = new GeoBlockingKernelRequestListener($this->getTemplatingMock(false), $lookUpMock, $loggerMock, $containerMock, $parameters); |
||
323 | $geoBlockingListener->onKernelRequest($eventBlockMock); |
||
324 | } |
||
325 | |||
326 | View Code Duplication | public function testOnKernelRequestGeoBlocking_CountryBlocking_AllowWithWhiteList() |
|
327 | { |
||
328 | $parameters = $this->getDefaultParams(); |
||
329 | $parameters["routeWhitelist"] = array(); |
||
330 | |||
331 | $eventBlockMock = $this->getMockBuilder("Symfony\Component\HttpKernel\Event\GetResponseEvent")->disableOriginalConstructor()->getMock(); |
||
332 | $requestMock = $this->getMockBuilder("Symfony\Component\HttpFoundation\Request")->disableOriginalConstructor()->getMock(); |
||
333 | |||
334 | $lookUpMock = $this->getMockBuilder("Azine\GeoBlockingBundle\Adapter\DefaultLookupAdapter")->getMock(); |
||
335 | $loggerMock = $this->getMockBuilder("Psr\Log\LoggerInterface")->disableOriginalConstructor()->getMock(); |
||
336 | |||
337 | $containerMock = $this->getMockBuilder("Symfony\Component\DependencyInjection\Container")->disableOriginalConstructor()->getMock(); |
||
338 | $securityContextMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage")->disableOriginalConstructor()->getMock(); |
||
339 | $tokenMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken")->disableOriginalConstructor()->getMock(); |
||
340 | |||
341 | $tokenMock->expects($this->once())->method("getUser")->will($this->returnValue(null)); |
||
342 | $securityContextMock->expects($this->once())->method("getToken")->will($this->returnValue($tokenMock)); |
||
343 | $containerMock->expects($this->once())->method("get")->will($this->returnValue($securityContextMock)); |
||
344 | |||
345 | $eventBlockMock->expects($this->once())->method("getRequestType")->will($this->returnValue(HttpKernelInterface::MASTER_REQUEST)); |
||
346 | $eventBlockMock->expects($this->once())->method("getRequest")->will($this->returnValue($requestMock)); |
||
347 | $requestMock->expects($this->once())->method("getClientIp")->will($this->returnValue($this->chIP)); |
||
348 | $requestMock->expects($this->once())->method("get")->with("_route", null)->will($this->returnValue("noWhiteListRoute")); |
||
349 | $lookUpMock->expects($this->once())->method("getCountry")->with($this->chIP)->will($this->returnValue("CH")); |
||
350 | $eventBlockMock->expects($this->never())->method("setResponse"); |
||
351 | $eventBlockMock->expects($this->never())->method("stopPropagation"); |
||
352 | |||
353 | $geoBlockingListener = new GeoBlockingKernelRequestListener($this->getTemplatingMock(false), $lookUpMock, $loggerMock, $containerMock, $parameters); |
||
354 | $geoBlockingListener->onKernelRequest($eventBlockMock); |
||
355 | } |
||
356 | |||
357 | public function testOnKernelRequestGeoBlocking_CountryBlocking_DenyWithWhiteList() |
||
358 | { |
||
359 | $parameters = $this->getDefaultParams(); |
||
360 | $parameters["routeWhitelist"] = array(); |
||
361 | |||
362 | $eventBlockMock = $this->getMockBuilder("Symfony\Component\HttpKernel\Event\GetResponseEvent")->disableOriginalConstructor()->getMock(); |
||
363 | $requestMock = $this->getMockBuilder("Symfony\Component\HttpFoundation\Request")->disableOriginalConstructor()->getMock(); |
||
364 | |||
365 | $lookUpMock = $this->getMockBuilder("Azine\GeoBlockingBundle\Adapter\DefaultLookupAdapter")->getMock(); |
||
366 | $loggerMock = $this->getMockBuilder("Psr\Log\LoggerInterface")->disableOriginalConstructor()->getMock(); |
||
367 | |||
368 | $containerMock = $this->getMockBuilder("Symfony\Component\DependencyInjection\Container")->disableOriginalConstructor()->getMock(); |
||
369 | $securityContextMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage")->disableOriginalConstructor()->getMock(); |
||
370 | $tokenMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken")->disableOriginalConstructor()->getMock(); |
||
371 | |||
372 | $tokenMock->expects($this->once())->method("getUser")->will($this->returnValue(null)); |
||
373 | $securityContextMock->expects($this->once())->method("getToken")->will($this->returnValue($tokenMock)); |
||
374 | $containerMock->expects($this->once())->method("get")->will($this->returnValue($securityContextMock)); |
||
375 | |||
376 | $eventBlockMock->expects($this->once())->method("getRequestType")->will($this->returnValue(HttpKernelInterface::MASTER_REQUEST)); |
||
377 | $eventBlockMock->expects($this->once())->method("getRequest")->will($this->returnValue($requestMock)); |
||
378 | $requestMock->expects($this->once())->method("getClientIp")->will($this->returnValue($this->usIP)); |
||
379 | $requestMock->expects($this->once())->method("get")->with("_route", null)->will($this->returnValue("noWhiteListRoute")); |
||
380 | $lookUpMock->expects($this->once())->method("getCountry")->with($this->usIP)->will($this->returnValue("US")); |
||
381 | $eventBlockMock->expects($this->once())->method("setResponse")->will($this->returnCallback(array($this, 'checkResponseCode'))); |
||
382 | $eventBlockMock->expects($this->once())->method("stopPropagation"); |
||
383 | |||
384 | $geoBlockingListener = new GeoBlockingKernelRequestListener($this->getTemplatingMock(true), $lookUpMock, $loggerMock, $containerMock, $parameters); |
||
385 | $geoBlockingListener->onKernelRequest($eventBlockMock); |
||
386 | } |
||
387 | |||
388 | public function testOnKernelRequestGeoBlocking_CountryBlocking_DenyWithBlackList() |
||
389 | { |
||
390 | $parameters = $this->getDefaultParams(); |
||
391 | $parameters["routeWhitelist"] = array(); |
||
392 | $parameters["countryWhitelist"] = array(); |
||
393 | $parameters["countryBlacklist"] = array("US"); |
||
394 | |||
395 | $eventBlockMock = $this->getMockBuilder("Symfony\Component\HttpKernel\Event\GetResponseEvent")->disableOriginalConstructor()->getMock(); |
||
396 | $requestMock = $this->getMockBuilder("Symfony\Component\HttpFoundation\Request")->disableOriginalConstructor()->getMock(); |
||
397 | |||
398 | $lookUpMock = $this->getMockBuilder("Azine\GeoBlockingBundle\Adapter\DefaultLookupAdapter")->getMock(); |
||
399 | $loggerMock = $this->getMockBuilder("Psr\Log\LoggerInterface")->disableOriginalConstructor()->getMock(); |
||
400 | |||
401 | $containerMock = $this->getMockBuilder("Symfony\Component\DependencyInjection\Container")->disableOriginalConstructor()->getMock(); |
||
402 | $securityContextMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage")->disableOriginalConstructor()->getMock(); |
||
403 | $tokenMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken")->disableOriginalConstructor()->getMock(); |
||
404 | |||
405 | $tokenMock->expects($this->once())->method("getUser")->will($this->returnValue(null)); |
||
406 | $securityContextMock->expects($this->once())->method("getToken")->will($this->returnValue($tokenMock)); |
||
407 | $containerMock->expects($this->once())->method("get")->will($this->returnValue($securityContextMock)); |
||
408 | |||
409 | $eventBlockMock->expects($this->once())->method("getRequestType")->will($this->returnValue(HttpKernelInterface::MASTER_REQUEST)); |
||
410 | $eventBlockMock->expects($this->once())->method("getRequest")->will($this->returnValue($requestMock)); |
||
411 | $requestMock->expects($this->once())->method("getClientIp")->will($this->returnValue($this->usIP)); |
||
412 | $requestMock->expects($this->once())->method("get")->with("_route", null)->will($this->returnValue("random_route")); |
||
413 | $lookUpMock->expects($this->once())->method("getCountry")->with($this->usIP)->will($this->returnValue("US")); |
||
414 | $eventBlockMock->expects($this->once())->method("setResponse")->will($this->returnCallback(array($this, 'checkResponseCode'))); |
||
415 | $eventBlockMock->expects($this->once())->method("stopPropagation"); |
||
416 | |||
417 | $geoBlockingListener = new GeoBlockingKernelRequestListener($this->getTemplatingMock(true), $lookUpMock, $loggerMock, $containerMock, $parameters); |
||
418 | $geoBlockingListener->onKernelRequest($eventBlockMock); |
||
419 | } |
||
420 | |||
421 | public function testOnKernelRequestGeoBlocking_CountryBlocking_AllowWithBlackList() |
||
422 | { |
||
423 | $parameters = $this->getDefaultParams(); |
||
424 | $parameters["routeWhitelist"] = array(); |
||
425 | $parameters["routeBlacklist"] = array('someNotAllowedRoute'); |
||
426 | $parameters["countryWhitelist"] = array(); |
||
427 | $parameters["countryBlacklist"] = array("US"); |
||
428 | |||
429 | $eventBlockMock = $this->getMockBuilder("Symfony\Component\HttpKernel\Event\GetResponseEvent")->disableOriginalConstructor()->getMock(); |
||
430 | $requestMock = $this->getMockBuilder("Symfony\Component\HttpFoundation\Request")->disableOriginalConstructor()->getMock(); |
||
431 | |||
432 | $lookUpMock = $this->getMockBuilder("Azine\GeoBlockingBundle\Adapter\DefaultLookupAdapter")->getMock(); |
||
433 | $loggerMock = $this->getMockBuilder("Psr\Log\LoggerInterface")->disableOriginalConstructor()->getMock(); |
||
434 | |||
435 | $containerMock = $this->getMockBuilder("Symfony\Component\DependencyInjection\Container")->disableOriginalConstructor()->getMock(); |
||
436 | $securityContextMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage")->disableOriginalConstructor()->getMock(); |
||
437 | $tokenMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken")->disableOriginalConstructor()->getMock(); |
||
438 | |||
439 | $tokenMock->expects($this->once())->method("getUser")->will($this->returnValue(null)); |
||
440 | $securityContextMock->expects($this->once())->method("getToken")->will($this->returnValue($tokenMock)); |
||
441 | $containerMock->expects($this->once())->method("get")->will($this->returnValue($securityContextMock)); |
||
442 | |||
443 | $eventBlockMock->expects($this->once())->method("getRequestType")->will($this->returnValue(HttpKernelInterface::MASTER_REQUEST)); |
||
444 | $eventBlockMock->expects($this->once())->method("getRequest")->will($this->returnValue($requestMock)); |
||
445 | $requestMock->expects($this->once())->method("getClientIp")->will($this->returnValue($this->chIP)); |
||
446 | $requestMock->expects($this->once())->method("get")->with("_route", null)->will($this->returnValue("noWhiteListRoute")); |
||
447 | $lookUpMock->expects($this->once())->method("getCountry")->with($this->chIP)->will($this->returnValue("CH")); |
||
448 | $eventBlockMock->expects($this->never())->method("setResponse"); |
||
449 | $eventBlockMock->expects($this->never())->method("stopPropagation"); |
||
450 | |||
451 | $geoBlockingListener = new GeoBlockingKernelRequestListener($this->getTemplatingMock(false), $lookUpMock, $loggerMock, $containerMock, $parameters); |
||
452 | $geoBlockingListener->onKernelRequest($eventBlockMock); |
||
453 | } |
||
454 | |||
455 | View Code Duplication | public function testOnKernelRequestGeoBlocking_IP_WhiteList_allow_regexp() |
|
456 | { |
||
457 | $parameters = $this->getDefaultParams(); |
||
458 | $parameters['ip_whitelist'] = array("/66\.249\.78\.\d{1,3}/"); |
||
459 | $eventBlockMock = $this->getMockBuilder("Symfony\Component\HttpKernel\Event\GetResponseEvent")->disableOriginalConstructor()->getMock(); |
||
460 | $requestMock = $this->getMockBuilder("Symfony\Component\HttpFoundation\Request")->disableOriginalConstructor()->getMock(); |
||
461 | |||
462 | $lookUpMock = $this->getMockBuilder("Azine\GeoBlockingBundle\Adapter\DefaultLookupAdapter")->getMock(); |
||
463 | $loggerMock = $this->getMockBuilder("Psr\Log\LoggerInterface")->disableOriginalConstructor()->getMock(); |
||
464 | |||
465 | $containerMock = $this->getMockBuilder("Symfony\Component\DependencyInjection\Container")->disableOriginalConstructor()->getMock(); |
||
466 | $securityContextMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage")->disableOriginalConstructor()->getMock(); |
||
467 | $tokenMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken")->disableOriginalConstructor()->getMock(); |
||
468 | |||
469 | $tokenMock->expects($this->once())->method("getUser")->will($this->returnValue(null)); |
||
470 | $securityContextMock->expects($this->once())->method("getToken")->will($this->returnValue($tokenMock)); |
||
471 | $containerMock->expects($this->once())->method("get")->will($this->returnValue($securityContextMock)); |
||
472 | |||
473 | $eventBlockMock->expects($this->once())->method("getRequestType")->will($this->returnValue(HttpKernelInterface::MASTER_REQUEST)); |
||
474 | $eventBlockMock->expects($this->once())->method("getRequest")->will($this->returnValue($requestMock)); |
||
475 | $requestMock->expects($this->once())->method("getClientIp")->will($this->returnValue($this->googleBotIP)); |
||
476 | $requestMock->expects($this->once())->method("get")->with("_route", null)->will($this->returnValue("random_route")); |
||
477 | $lookUpMock->expects($this->once())->method("getCountry")->with($this->googleBotIP)->will($this->returnValue("US")); |
||
478 | $eventBlockMock->expects($this->never())->method("setResponse"); |
||
479 | $eventBlockMock->expects($this->never())->method("stopPropagation"); |
||
480 | |||
481 | $geoBlockingListener = new GeoBlockingKernelRequestListener($this->getTemplatingMock(false), $lookUpMock, $loggerMock, $containerMock, $parameters); |
||
482 | $geoBlockingListener->onKernelRequest($eventBlockMock); |
||
483 | |||
484 | } |
||
485 | |||
486 | View Code Duplication | public function testOnKernelRequestGeoBlocking_IP_WhiteList_allow_ip() |
|
487 | { |
||
488 | $parameters = $this->getDefaultParams(); |
||
489 | $parameters['ip_whitelist'] = array($this->googleBotIP); |
||
490 | $eventBlockMock = $this->getMockBuilder("Symfony\Component\HttpKernel\Event\GetResponseEvent")->disableOriginalConstructor()->getMock(); |
||
491 | $requestMock = $this->getMockBuilder("Symfony\Component\HttpFoundation\Request")->disableOriginalConstructor()->getMock(); |
||
492 | |||
493 | $lookUpMock = $this->getMockBuilder("Azine\GeoBlockingBundle\Adapter\DefaultLookupAdapter")->getMock(); |
||
494 | $loggerMock = $this->getMockBuilder("Psr\Log\LoggerInterface")->disableOriginalConstructor()->getMock(); |
||
495 | |||
496 | $containerMock = $this->getMockBuilder("Symfony\Component\DependencyInjection\Container")->disableOriginalConstructor()->getMock(); |
||
497 | $securityContextMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage")->disableOriginalConstructor()->getMock(); |
||
498 | $tokenMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken")->disableOriginalConstructor()->getMock(); |
||
499 | |||
500 | $tokenMock->expects($this->once())->method("getUser")->will($this->returnValue(null)); |
||
501 | $securityContextMock->expects($this->once())->method("getToken")->will($this->returnValue($tokenMock)); |
||
502 | $containerMock->expects($this->once())->method("get")->will($this->returnValue($securityContextMock)); |
||
503 | |||
504 | $eventBlockMock->expects($this->once())->method("getRequestType")->will($this->returnValue(HttpKernelInterface::MASTER_REQUEST)); |
||
505 | $eventBlockMock->expects($this->once())->method("getRequest")->will($this->returnValue($requestMock)); |
||
506 | $requestMock->expects($this->once())->method("getClientIp")->will($this->returnValue($this->googleBotIP)); |
||
507 | $requestMock->expects($this->once())->method("get")->with("_route", null)->will($this->returnValue("random_route")); |
||
508 | $lookUpMock->expects($this->once())->method("getCountry")->with($this->googleBotIP)->will($this->returnValue("US")); |
||
509 | $eventBlockMock->expects($this->never())->method("setResponse"); |
||
510 | $eventBlockMock->expects($this->never())->method("stopPropagation"); |
||
511 | |||
512 | $loggerMock->expects($this->never())->method("warning"); |
||
513 | |||
514 | $geoBlockingListener = new GeoBlockingKernelRequestListener($this->getTemplatingMock(false), $lookUpMock, $loggerMock, $containerMock, $parameters); |
||
515 | $geoBlockingListener->onKernelRequest($eventBlockMock); |
||
516 | |||
517 | } |
||
518 | |||
519 | public function testOnKernelRequestGeoBlocking_IP_WhiteList_deny_ip() |
||
520 | { |
||
521 | $parameters = $this->getDefaultParams(); |
||
522 | $parameters['ip_whitelist'] = array($this->googleBotIP); |
||
523 | $eventBlockMock = $this->getMockBuilder("Symfony\Component\HttpKernel\Event\GetResponseEvent")->disableOriginalConstructor()->getMock(); |
||
524 | $requestMock = $this->getMockBuilder("Symfony\Component\HttpFoundation\Request")->disableOriginalConstructor()->getMock(); |
||
525 | |||
526 | $lookUpMock = $this->getMockBuilder("Azine\GeoBlockingBundle\Adapter\DefaultLookupAdapter")->getMock(); |
||
527 | $loggerMock = $this->getMockBuilder("Psr\Log\LoggerInterface")->disableOriginalConstructor()->getMock(); |
||
528 | |||
529 | $containerMock = $this->getMockBuilder("Symfony\Component\DependencyInjection\Container")->disableOriginalConstructor()->getMock(); |
||
530 | $securityContextMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage")->disableOriginalConstructor()->getMock(); |
||
531 | $tokenMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken")->disableOriginalConstructor()->getMock(); |
||
532 | |||
533 | $tokenMock->expects($this->once())->method("getUser")->will($this->returnValue(null)); |
||
534 | $securityContextMock->expects($this->once())->method("getToken")->will($this->returnValue($tokenMock)); |
||
535 | $containerMock->expects($this->once())->method("get")->will($this->returnValue($securityContextMock)); |
||
536 | |||
537 | $eventBlockMock->expects($this->once())->method("getRequestType")->will($this->returnValue(HttpKernelInterface::MASTER_REQUEST)); |
||
538 | $eventBlockMock->expects($this->once())->method("getRequest")->will($this->returnValue($requestMock)); |
||
539 | $requestMock->expects($this->once())->method("getClientIp")->will($this->returnValue($this->usIP)); |
||
540 | $requestMock->expects($this->once())->method("get")->with("_route", null)->will($this->returnValue("random_route")); |
||
541 | $lookUpMock->expects($this->once())->method("getCountry")->with($this->usIP)->will($this->returnValue("US")); |
||
542 | $eventBlockMock->expects($this->once())->method("setResponse")->will($this->returnCallback(array($this, 'checkResponseCode'))); |
||
543 | $eventBlockMock->expects($this->once())->method("stopPropagation"); |
||
544 | |||
545 | $loggerMock->expects($this->once())->method("warning"); |
||
546 | |||
547 | $geoBlockingListener = new GeoBlockingKernelRequestListener($this->getTemplatingMock(true), $lookUpMock, $loggerMock, $containerMock, $parameters); |
||
548 | $geoBlockingListener->onKernelRequest($eventBlockMock); |
||
549 | |||
550 | } |
||
551 | |||
552 | public function testOnKernelRequestGeoBlocking_Log_Blocked_Requests() |
||
553 | { |
||
554 | $parameters = $this->getDefaultParams(); |
||
555 | $parameters["routeWhitelist"] = array(); |
||
556 | $parameters["countryWhitelist"] = array(); |
||
557 | $parameters["countryBlacklist"] = array("US"); |
||
558 | $parameters["logBlockedRequests"] = true; |
||
559 | |||
560 | $_SERVER['HTTP_USER_AGENT'] = "some blocked visitors UA."; |
||
561 | |||
562 | $eventBlockMock = $this->getMockBuilder("Symfony\Component\HttpKernel\Event\GetResponseEvent")->disableOriginalConstructor()->getMock(); |
||
563 | $requestMock = $this->getMockBuilder("Symfony\Component\HttpFoundation\Request")->disableOriginalConstructor()->getMock(); |
||
564 | |||
565 | $lookUpMock = $this->getMockBuilder("Azine\GeoBlockingBundle\Adapter\DefaultLookupAdapter")->getMock(); |
||
566 | $loggerMock = $this->getMockBuilder("Psr\Log\LoggerInterface")->disableOriginalConstructor()->getMock(); |
||
567 | |||
568 | $containerMock = $this->getMockBuilder("Symfony\Component\DependencyInjection\Container")->disableOriginalConstructor()->getMock(); |
||
569 | $securityContextMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage")->disableOriginalConstructor()->getMock(); |
||
570 | $tokenMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken")->disableOriginalConstructor()->getMock(); |
||
571 | |||
572 | $tokenMock->expects($this->once())->method("getUser")->will($this->returnValue(null)); |
||
573 | $securityContextMock->expects($this->once())->method("getToken")->will($this->returnValue($tokenMock)); |
||
574 | $containerMock->expects($this->once())->method("get")->will($this->returnValue($securityContextMock)); |
||
575 | |||
576 | $eventBlockMock->expects($this->once())->method("getRequestType")->will($this->returnValue(HttpKernelInterface::MASTER_REQUEST)); |
||
577 | $eventBlockMock->expects($this->exactly(2))->method("getRequest")->will($this->returnValue($requestMock)); |
||
578 | $requestMock->expects($this->exactly(2))->method("getClientIp")->will($this->returnValue($this->usIP)); |
||
579 | $requestMock->expects($this->exactly(2))->method("get")->with("_route", null)->will($this->returnValue("random_route")); |
||
580 | $lookUpMock->expects($this->once())->method("getCountry")->with($this->usIP)->will($this->returnValue("US")); |
||
581 | $eventBlockMock->expects($this->once())->method("setResponse")->will($this->returnCallback(array($this, 'checkResponseCode'))); |
||
582 | $eventBlockMock->expects($this->once())->method("stopPropagation"); |
||
583 | |||
584 | $loggerMock->expects($this->exactly(2))->method("warning"); |
||
585 | |||
586 | $geoBlockingListener = new GeoBlockingKernelRequestListener($this->getTemplatingMock(true), $lookUpMock, $loggerMock, $containerMock, $parameters); |
||
587 | $geoBlockingListener->onKernelRequest($eventBlockMock); |
||
588 | |||
589 | } |
||
590 | |||
591 | View Code Duplication | public function testOnKernelRequestGeoBlocking_Search_Engine_Bot_Allow_Google() |
|
592 | { |
||
593 | $parameters = $this->getDefaultParams(); |
||
594 | $parameters['allow_search_bots'] = true; |
||
595 | $eventBlockMock = $this->getMockBuilder("Symfony\Component\HttpKernel\Event\GetResponseEvent")->disableOriginalConstructor()->getMock(); |
||
596 | $requestMock = $this->getMockBuilder("Symfony\Component\HttpFoundation\Request")->disableOriginalConstructor()->getMock(); |
||
597 | |||
598 | $lookUpMock = $this->getMockBuilder("Azine\GeoBlockingBundle\Adapter\DefaultLookupAdapter")->getMock(); |
||
599 | $loggerMock = $this->getMockBuilder("Psr\Log\LoggerInterface")->disableOriginalConstructor()->getMock(); |
||
600 | |||
601 | $containerMock = $this->getMockBuilder("Symfony\Component\DependencyInjection\Container")->disableOriginalConstructor()->getMock(); |
||
602 | $securityContextMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage")->disableOriginalConstructor()->getMock(); |
||
603 | $tokenMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken")->disableOriginalConstructor()->getMock(); |
||
604 | |||
605 | $tokenMock->expects($this->once())->method("getUser")->will($this->returnValue(null)); |
||
606 | $securityContextMock->expects($this->once())->method("getToken")->will($this->returnValue($tokenMock)); |
||
607 | $containerMock->expects($this->once())->method("get")->will($this->returnValue($securityContextMock)); |
||
608 | |||
609 | $eventBlockMock->expects($this->once())->method("getRequestType")->will($this->returnValue(HttpKernelInterface::MASTER_REQUEST)); |
||
610 | $eventBlockMock->expects($this->once())->method("getRequest")->will($this->returnValue($requestMock)); |
||
611 | $requestMock->expects($this->once())->method("getClientIp")->will($this->returnValue($this->googleBotIP)); |
||
612 | $requestMock->expects($this->once())->method("get")->with("_route", null)->will($this->returnValue("random_route")); |
||
613 | $lookUpMock->expects($this->once())->method("getCountry")->with($this->googleBotIP)->will($this->returnValue("US")); |
||
614 | $eventBlockMock->expects($this->never())->method("setResponse"); |
||
615 | $eventBlockMock->expects($this->never())->method("stopPropagation"); |
||
616 | |||
617 | $loggerMock->expects($this->never())->method("warning"); |
||
618 | |||
619 | $geoBlockingListener = new GeoBlockingKernelRequestListener($this->getTemplatingMock(false), $lookUpMock, $loggerMock, $containerMock, $parameters); |
||
620 | $geoBlockingListener->onKernelRequest($eventBlockMock); |
||
621 | } |
||
622 | |||
623 | View Code Duplication | public function testOnKernelRequestGeoBlocking_Search_Engine_Bot_Allow_MSN() |
|
624 | { |
||
625 | $parameters = $this->getDefaultParams(); |
||
626 | $parameters['allow_search_bots'] = true; |
||
627 | $eventBlockMock = $this->getMockBuilder("Symfony\Component\HttpKernel\Event\GetResponseEvent")->disableOriginalConstructor()->getMock(); |
||
628 | $requestMock = $this->getMockBuilder("Symfony\Component\HttpFoundation\Request")->disableOriginalConstructor()->getMock(); |
||
629 | |||
630 | $lookUpMock = $this->getMockBuilder("Azine\GeoBlockingBundle\Adapter\DefaultLookupAdapter")->getMock(); |
||
631 | $loggerMock = $this->getMockBuilder("Psr\Log\LoggerInterface")->disableOriginalConstructor()->getMock(); |
||
632 | |||
633 | $containerMock = $this->getMockBuilder("Symfony\Component\DependencyInjection\Container")->disableOriginalConstructor()->getMock(); |
||
634 | $securityContextMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage")->disableOriginalConstructor()->getMock(); |
||
635 | $tokenMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken")->disableOriginalConstructor()->getMock(); |
||
636 | |||
637 | $tokenMock->expects($this->once())->method("getUser")->will($this->returnValue(null)); |
||
638 | $securityContextMock->expects($this->once())->method("getToken")->will($this->returnValue($tokenMock)); |
||
639 | $containerMock->expects($this->once())->method("get")->will($this->returnValue($securityContextMock)); |
||
640 | |||
641 | $eventBlockMock->expects($this->once())->method("getRequestType")->will($this->returnValue(HttpKernelInterface::MASTER_REQUEST)); |
||
642 | $eventBlockMock->expects($this->once())->method("getRequest")->will($this->returnValue($requestMock)); |
||
643 | $requestMock->expects($this->once())->method("getClientIp")->will($this->returnValue($this->msnBotIP)); |
||
644 | $requestMock->expects($this->once())->method("get")->with("_route", null)->will($this->returnValue("random_route")); |
||
645 | $lookUpMock->expects($this->once())->method("getCountry")->with($this->msnBotIP)->will($this->returnValue("US")); |
||
646 | $eventBlockMock->expects($this->never())->method("setResponse"); |
||
647 | $eventBlockMock->expects($this->never())->method("stopPropagation"); |
||
648 | |||
649 | $loggerMock->expects($this->never())->method("warning"); |
||
650 | |||
651 | $geoBlockingListener = new GeoBlockingKernelRequestListener($this->getTemplatingMock(false), $lookUpMock, $loggerMock, $containerMock, $parameters); |
||
652 | $geoBlockingListener->onKernelRequest($eventBlockMock); |
||
653 | } |
||
654 | |||
655 | View Code Duplication | public function testOnKernelRequestGeoBlocking_Search_Engine_Bot_Deny_usIP() |
|
656 | { |
||
657 | $parameters = $this->getDefaultParams(); |
||
658 | $parameters['allow_search_bots'] = true; |
||
659 | |||
660 | $eventBlockMock = $this->getMockBuilder("Symfony\Component\HttpKernel\Event\GetResponseEvent")->disableOriginalConstructor()->getMock(); |
||
661 | $requestMock = $this->getMockBuilder("Symfony\Component\HttpFoundation\Request")->disableOriginalConstructor()->getMock(); |
||
662 | |||
663 | $lookUpMock = $this->getMockBuilder("Azine\GeoBlockingBundle\Adapter\DefaultLookupAdapter")->getMock(); |
||
664 | $loggerMock = $this->getMockBuilder("Psr\Log\LoggerInterface")->disableOriginalConstructor()->getMock(); |
||
665 | |||
666 | $containerMock = $this->getMockBuilder("Symfony\Component\DependencyInjection\Container")->disableOriginalConstructor()->getMock(); |
||
667 | $securityContextMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage")->disableOriginalConstructor()->getMock(); |
||
668 | $tokenMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken")->disableOriginalConstructor()->getMock(); |
||
669 | |||
670 | $tokenMock->expects($this->once())->method("getUser")->will($this->returnValue(null)); |
||
671 | $securityContextMock->expects($this->once())->method("getToken")->will($this->returnValue($tokenMock)); |
||
672 | $containerMock->expects($this->once())->method("get")->will($this->returnValue($securityContextMock)); |
||
673 | |||
674 | $eventBlockMock->expects($this->once())->method("getRequestType")->will($this->returnValue(HttpKernelInterface::MASTER_REQUEST)); |
||
675 | $eventBlockMock->expects($this->once())->method("getRequest")->will($this->returnValue($requestMock)); |
||
676 | $requestMock->expects($this->once())->method("getClientIp")->will($this->returnValue($this->usIP)); |
||
677 | $requestMock->expects($this->once())->method("get")->with("_route", null)->will($this->returnValue("random_route")); |
||
678 | $lookUpMock->expects($this->once())->method("getCountry")->with($this->usIP)->will($this->returnValue("US")); |
||
679 | |||
680 | $eventBlockMock->expects($this->once())->method("setResponse")->will($this->returnCallback(array($this, 'checkResponseCode'))); |
||
681 | $eventBlockMock->expects($this->once())->method("stopPropagation"); |
||
682 | |||
683 | $geoBlockingListener = new GeoBlockingKernelRequestListener($this->getTemplatingMock(true), $lookUpMock, $loggerMock, $containerMock, $parameters); |
||
684 | $geoBlockingListener->onKernelRequest($eventBlockMock); |
||
685 | } |
||
686 | |||
687 | public function testOnKernelRequestGeoBlocking_allow_by_cookie_yes(){ |
||
720 | |||
721 | |||
722 | public function testOnKernelRequestGeoBlocking_allow_by_cookie_no(){ |
||
723 | $parameters = $this->getDefaultParams(); |
||
724 | $parameters['allow_by_cookie'] = true; |
||
725 | |||
726 | $eventBlockMock = $this->getMockBuilder("Symfony\Component\HttpKernel\Event\GetResponseEvent")->disableOriginalConstructor()->getMock(); |
||
727 | $requestMock = $this->getMockBuilder("Symfony\Component\HttpFoundation\Request")->disableOriginalConstructor()->getMock(); |
||
728 | |||
729 | $loggerMock = $this->getMockBuilder("Psr\Log\LoggerInterface")->disableOriginalConstructor()->getMock(); |
||
730 | |||
731 | $containerMock = $this->getMockBuilder("Symfony\Component\DependencyInjection\Container")->disableOriginalConstructor()->getMock(); |
||
732 | $securityContextMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage")->disableOriginalConstructor()->getMock(); |
||
733 | $tokenMock = $this->getMockBuilder("Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken")->disableOriginalConstructor()->getMock(); |
||
734 | |||
735 | $tokenMock->expects($this->once())->method("getUser")->will($this->returnValue("anon")); |
||
736 | $securityContextMock->expects($this->once())->method("getToken")->will($this->returnValue($tokenMock)); |
||
737 | $containerMock->expects($this->once())->method("get")->will($this->returnValue($securityContextMock)); |
||
738 | |||
739 | $eventBlockMock->expects($this->once())->method("getRequestType")->will($this->returnValue(HttpKernelInterface::MASTER_REQUEST)); |
||
740 | $eventBlockMock->expects($this->once())->method("getRequest")->will($this->returnValue($requestMock)); |
||
741 | // make sure this line is in the code is executed |
||
742 | $requestMock->expects($this->once())->method("getClientIp")->will($this->returnValue($this->localIP)); |
||
753 | |||
754 | private function getTemplatingMock($showBlockingPage) |
||
765 | |||
766 | private function getDefaultParams() |
||
788 | } |
||
789 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.