|
1
|
|
|
<?php |
|
2
|
|
|
namespace Azine\GeoBlockingBundle\Tests\DependencyInjection; |
|
3
|
|
|
|
|
4
|
|
|
use Azine\GeoBlockingBundle\DependencyInjection\AzineGeoBlockingExtension; |
|
5
|
|
|
|
|
6
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
7
|
|
|
use Symfony\Component\Yaml\Parser; |
|
8
|
|
|
|
|
9
|
|
|
class AzineGeoBlockingExtensionTest extends \PHPUnit_Framework_TestCase |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var ContainerBuilder */ |
|
12
|
|
|
protected $configuration; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* This should not throw an exception |
|
16
|
|
|
*/ |
|
17
|
|
|
public function testMinimalConfig() |
|
18
|
|
|
{ |
|
19
|
|
|
$loader = new AzineGeoBlockingExtension(); |
|
20
|
|
|
$config = $this->getMinimalConfig(); |
|
21
|
|
|
$loader->load(array($config), new ContainerBuilder()); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* This should not throw an exception |
|
26
|
|
|
*/ |
|
27
|
|
|
public function testFullConfig() |
|
28
|
|
|
{ |
|
29
|
|
|
$loader = new AzineGeoBlockingExtension(); |
|
30
|
|
|
$config = $this->getFullConfig(); |
|
31
|
|
|
$loader->load(array($config), new ContainerBuilder()); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function testCustomConfigurationWithWhiteList() |
|
35
|
|
|
{ |
|
36
|
|
|
$this->configuration = new ContainerBuilder(); |
|
37
|
|
|
$loader = new AzineGeoBlockingExtension(); |
|
38
|
|
|
$config = $this->getFullConfig(); |
|
39
|
|
|
$config['countries']['whitelist'][] = 'UK'; |
|
40
|
|
|
$config['countries']['whitelist'][] = 'CH'; |
|
41
|
|
|
$config['countries']['whitelist'][] = 'DE'; |
|
42
|
|
|
$config['countries']['blacklist'][] = 'RU'; |
|
43
|
|
|
$config['countries']['blacklist'][] = 'CN'; |
|
44
|
|
|
$config['routes']['whitelist'][] = 'some_allowed_route'; |
|
45
|
|
|
$config['routes']['blacklist'][] = 'some_blocked_route'; |
|
46
|
|
|
$config['access_denied_view'] = 'AcmeFooBundle:Geoblocking:accessDenied.html.twig'; |
|
47
|
|
|
$config['block_anonymouse_users_only'] = false; |
|
48
|
|
|
$config['login_route'] = 'the_login_route'; |
|
49
|
|
|
$config['lookup_adapter'] = 'service.name.of.lookup.adapter'; |
|
50
|
|
|
$config['allow_private_ips'] = false; |
|
51
|
|
|
|
|
52
|
|
|
$loader->load(array($config), $this->configuration); |
|
53
|
|
|
|
|
54
|
|
|
$countryWL = $this->configuration->getParameter('azine_geo_blocking_countries_whitelist'); |
|
55
|
|
|
$countryBL = $this->configuration->getParameter('azine_geo_blocking_countries_blacklist'); |
|
56
|
|
|
|
|
57
|
|
|
$this->assertContains("UK", $countryWL); |
|
58
|
|
|
$this->assertContains("DE", $countryWL); |
|
59
|
|
|
$this->assertContains("CH", $countryWL); |
|
60
|
|
|
$this->assertNotContains("RU", $countryWL); |
|
61
|
|
|
$this->assertNotContains("CN", $countryWL); |
|
62
|
|
|
|
|
63
|
|
|
// if a whiteList is present, the blacklist is cleared and ignored |
|
64
|
|
|
$this->assertEmpty($countryBL); |
|
65
|
|
|
|
|
66
|
|
|
$routeWL = $this->configuration->getParameter('azine_geo_blocking_routes_whitelist'); |
|
67
|
|
|
$routeBL = $this->configuration->getParameter('azine_geo_blocking_routes_blacklist'); |
|
68
|
|
|
|
|
69
|
|
|
$this->assertContains("some_allowed_route", $routeWL); |
|
70
|
|
|
$this->assertNotContains("some_blocked_route", $routeWL); |
|
71
|
|
|
// if a whiteList is present, the blacklist is cleared and ignored |
|
72
|
|
|
$this->assertEmpty($routeBL); |
|
73
|
|
|
|
|
74
|
|
|
$this->assertParameter('AcmeFooBundle:Geoblocking:accessDenied.html.twig', 'azine_geo_blocking_access_denied_view'); |
|
75
|
|
|
$this->assertParameter(false, 'azine_geo_blocking_block_anonymouse_users_only'); |
|
76
|
|
|
$this->assertParameter('the_login_route', 'azine_geo_blocking_login_route'); |
|
77
|
|
|
$this->assertAlias('service.name.of.lookup.adapter', 'azine_geo_blocking_lookup_adapter'); |
|
78
|
|
|
$this->assertParameter(false, 'azine_geo_blocking_allow_private_ips'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function testCustomConfigurationWithBlackList() |
|
82
|
|
|
{ |
|
83
|
|
|
$this->configuration = new ContainerBuilder(); |
|
84
|
|
|
$loader = new AzineGeoBlockingExtension(); |
|
85
|
|
|
$config = $this->getFullConfig(); |
|
86
|
|
|
$config['countries']['blacklist'][] = 'RU'; |
|
87
|
|
|
$config['countries']['blacklist'][] = 'CN'; |
|
88
|
|
|
$config['routes']['blacklist'] = array('some_blocked_route'); |
|
89
|
|
|
$config['routes']['whitelist'] = array(); |
|
90
|
|
|
|
|
91
|
|
|
$loader->load(array($config), $this->configuration); |
|
92
|
|
|
|
|
93
|
|
|
$countryWL = $this->configuration->getParameter('azine_geo_blocking_countries_whitelist'); |
|
94
|
|
|
$countryBL = $this->configuration->getParameter('azine_geo_blocking_countries_blacklist'); |
|
95
|
|
|
|
|
96
|
|
|
$this->assertContains("RU", $countryBL); |
|
97
|
|
|
$this->assertContains("CN", $countryBL); |
|
98
|
|
|
// if the whiteList is empty, the blacklist is not cleared |
|
99
|
|
|
$this->assertEmpty($countryWL); |
|
100
|
|
|
|
|
101
|
|
|
$routeWL = $this->configuration->getParameter('azine_geo_blocking_routes_whitelist'); |
|
102
|
|
|
$routeBL = $this->configuration->getParameter('azine_geo_blocking_routes_blacklist'); |
|
103
|
|
|
|
|
104
|
|
|
$this->assertNotContains("some_allowed_route", $routeBL); |
|
105
|
|
|
$this->assertContains("some_blocked_route", $routeBL); |
|
106
|
|
|
// if a whiteList is present, the blacklist is cleared and ignored |
|
107
|
|
|
$this->assertEmpty($routeWL); |
|
108
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Get the minimal config |
|
113
|
|
|
* @return array |
|
114
|
|
|
*/ |
|
115
|
|
|
protected function getMinimalConfig() |
|
116
|
|
|
{ |
|
117
|
|
|
$yaml = <<<EOF |
|
118
|
|
|
# true|false : turn the whole bundle on/off |
|
119
|
|
|
enabled: true |
|
120
|
|
|
EOF; |
|
121
|
|
|
$parser = new Parser(); |
|
122
|
|
|
|
|
123
|
|
|
return $parser->parse($yaml); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Get a full config for this bundle |
|
129
|
|
|
*/ |
|
130
|
|
|
protected function getFullConfig() |
|
131
|
|
|
{ |
|
132
|
|
|
$yaml = <<<EOF |
|
133
|
|
|
# true|false : turn the whole bundle on/off |
|
134
|
|
|
enabled: true |
|
135
|
|
|
|
|
136
|
|
|
# the view to be rendered as 'blocked' page |
|
137
|
|
|
access_denied_view: AzineGeoBlockingBundle::accessDenied.html.twig |
|
138
|
|
|
|
|
139
|
|
|
# block all users or only users that are not logged in yet |
|
140
|
|
|
block_anonymouse_users_only: true |
|
141
|
|
|
|
|
142
|
|
|
# route name to the login-form (only relevant if block_anonymouse_users_only is set to true) |
|
143
|
|
|
login_route: fos_user_security_login |
|
144
|
|
|
|
|
145
|
|
|
# id of the lookup-adapter you would like to use |
|
146
|
|
|
lookup_adapter: azine_geo_blocking.default.lookup.adapter |
|
147
|
|
|
|
|
148
|
|
|
# true | false : also applie the rules to private IPs e.g. 127.0.0.1 or 192.168.xxx.yyy etc. |
|
149
|
|
|
allow_private_ips: true |
|
150
|
|
|
|
|
151
|
|
|
# only whitelist or blacklist can contain values. |
|
152
|
|
|
countries: |
|
153
|
|
|
|
|
154
|
|
|
# e.g. 'CH','FR','DE' etc. => access is allowed to visitors from these countries |
|
155
|
|
|
whitelist: [] |
|
156
|
|
|
|
|
157
|
|
|
# e.g. 'US','CN' etc. => access is denied to visitors from these countries |
|
158
|
|
|
blacklist: [] |
|
159
|
|
|
|
|
160
|
|
|
# only whitelist or blacklist can contain values. |
|
161
|
|
|
routes: |
|
162
|
|
|
|
|
163
|
|
|
# list of routes, that never should be blocked for access from unliked locations (e.g. the login-routes). |
|
164
|
|
|
whitelist: |
|
165
|
|
|
|
|
166
|
|
|
# Defaults: |
|
167
|
|
|
- fos_user_security_login |
|
168
|
|
|
- fos_user_security_login_check |
|
169
|
|
|
- fos_user_security_logout |
|
170
|
|
|
|
|
171
|
|
|
# list of routes, that always should be blocked for access from unliked locations. |
|
172
|
|
|
blacklist: [] |
|
173
|
|
|
EOF; |
|
174
|
|
|
$parser = new Parser(); |
|
175
|
|
|
|
|
176
|
|
|
return $parser->parse($yaml); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @param string $value |
|
181
|
|
|
* @param string $key |
|
182
|
|
|
*/ |
|
183
|
|
|
private function assertAlias($value, $key) |
|
184
|
|
|
{ |
|
185
|
|
|
$this->assertEquals($value, (string) $this->configuration->getAlias($key), sprintf('%s alias is correct', $key)); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* @param mixed $value |
|
190
|
|
|
* @param string $key |
|
191
|
|
|
*/ |
|
192
|
|
|
private function assertParameter($value, $key) |
|
193
|
|
|
{ |
|
194
|
|
|
$this->assertEquals($value, $this->configuration->getParameter($key), sprintf('%s parameter is correct', $key)); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
protected function tearDown() |
|
198
|
|
|
{ |
|
199
|
|
|
unset($this->configuration); |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|