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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\CoreHome\tests\Integration;
use Piwik\Common;
use Piwik\Config;
use Piwik\NoAccessException;
use Piwik\Plugins\CoreHome\LoginWhitelist;
use Piwik\Tests\Framework\Mock\FakeAccess;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
class CustomLoginWhitelist extends LoginWhitelist {
public function getWhitelistedLoginIps()
{
return parent::getWhitelistedLoginIps();
}
public function isIpWhitelisted($ip)
{
return parent::isIpWhitelisted($ip);
}
}
/**
* @group Plugins
* @group LoginWhitelist
* @group LoginWhitelistTest
*/
class LoginWhitelistTest extends IntegrationTestCase
{
/**
* @var CustomLoginWhitelist
*/
private $whitelist;
private $cliMode;
public function setUp(): void
{
parent::setUp();
$this->cliMode = Common::$isCliMode;
Common::$isCliMode = false;
$this->whitelist = new CustomLoginWhitelist();
}
public function tearDown(): void
{
Common::$isCliMode = $this->cliMode;
parent::tearDown();
}
public function test_shouldWhitelistApplyToAPI_shouldBeEnabledByDefault()
{
$this->assertTrue($this->whitelist->shouldWhitelistApplyToAPI());
}
public function test_shouldWhitelistApplyToAPI_canBeDisabled()
{
$this->setGeneralConfig('login_whitelist_apply_to_reporting_api_requests', '0');
$this->assertFalse($this->whitelist->shouldWhitelistApplyToAPI());
}
public function test_shouldWhitelistApplyToAPI_enabled()
{
$this->setGeneralConfig('login_whitelist_apply_to_reporting_api_requests', '1');
$this->assertTrue($this->whitelist->shouldWhitelistApplyToAPI());
}
public function test_shouldCheckWhitelist_shouldNotBeCheckedByDefaultAndNotHaveAnyIps()
{
$this->assertFalse($this->whitelist->shouldCheckWhitelist());
}
public function test_shouldCheckWhitelist_shouldBeCheckedIfHasAtLeastOneIp()
{
$this->setGeneralConfig('login_whitelist_ip', ['192.168.33.1']);
$this->assertTrue($this->whitelist->shouldCheckWhitelist());
}
public function test_shouldCheckWhitelist_shouldNotBeCheckedIfExecutedFromCLI()
{
Common::$isCliMode = true;
$this->setGeneralConfig('login_whitelist_ip', ['192.168.33.1']);
$this->assertFalse($this->whitelist->shouldCheckWhitelist());
}
public function test_shouldCheckWhitelist_shouldNotBeCheckedIfOnlyEmptyEntries()
{
$this->setGeneralConfig('login_whitelist_ip', ['', ' ']);
$this->assertFalse($this->whitelist->shouldCheckWhitelist());
}
public function test_getWhitelistedLoginIps_shouldReturnEmptyArrayByDefault()
{
$this->assertSame($this->whitelist->getWhitelistedLoginIps(), []);
}
public function test_getWhitelistedLoginIps_shouldReturnIpsAndTrimIfNeeded()
{
$this->setGeneralConfig('login_whitelist_ip', ['192.168.33.1', ' 127.0.0.1 ', '2001:0db8:85a3:0000:0000:8a2e:0370:7334']);
$this->assertSame(['192.168.33.1', '127.0.0.1', '2001:0db8:85a3:0000:0000:8a2e:0370:7334'], $this->whitelist->getWhitelistedLoginIps());
}
public function test_getWhitelistedLoginIps_shouldResolveIp()
{
$this->setGeneralConfig('login_whitelist_ip', ['192.168.33.1', 'matomo.org', '127.0.0.1']);
$this->assertSame(['192.168.33.1', '185.31.40.177', '127.0.0.1'], $this->whitelist->getWhitelistedLoginIps());
}
public function test_getWhitelistedLoginIps_shouldNotBeCheckedIfOnlyEmptyEntries()
{
$this->setGeneralConfig('login_whitelist_ip', ['', '192.168.33.1 ', ' ']);
$this->assertSame(['192.168.33.1'], $this->whitelist->getWhitelistedLoginIps());
}
public function test_getWhitelistedLoginIps_shouldNotReturnDuplicates()
{
$this->setGeneralConfig('login_whitelist_ip', [' 192.168.33.1', '192.168.33.1 ', ' 192.168.33.1 ', '192.168.33.1']);
$this->assertSame(['192.168.33.1'], $this->whitelist->getWhitelistedLoginIps());
}
/**
* @dataProvider getIpWhitelistedTests
*/
public function test_isIpWhitelisted($expectedIsWhitelisted, $ipString)
{
$ipsWhitelisted = [
'127.0.0.1',
'192.168.33.1',
'2001:0db8:85a3:0000:0000:8a2e:0370:7334',
'204.93.240.*',
'204.93.177.0/25',
'2001:db9::/48'
];
$this->setGeneralConfig('login_whitelist_ip', $ipsWhitelisted);
$this->assertSame($expectedIsWhitelisted, $this->whitelist->isIpWhitelisted($ipString));
}
/**
* @dataProvider getIpWhitelistedTests
*/
public function test_isIpWhitelisted_WhenNoIpsConfigured_AllIpsAreWhitelisted($expectedIsWhitelisted, $ipString)
{
$this->assertFalse($this->whitelist->isIpWhitelisted($ipString));
}
/**
* @dataProvider getIpWhitelistedTests
*/
public function test_checkIsWhitelisted($expectedIsWhitelisted, $ipString)
{
$ipsWhitelisted = [
'127.0.0.1',
'192.168.33.1',
'2001:0db8:85a3:0000:0000:8a2e:0370:7334',
'204.93.240.*',
'204.93.177.0/25',
'2001:db9::/48'
];
$this->setGeneralConfig('login_whitelist_ip', $ipsWhitelisted);
if ($expectedIsWhitelisted) {
$this->whitelist->checkIsWhitelisted($ipString);
$this->assertTrue(true);
} else {
try {
$this->whitelist->checkIsWhitelisted($ipString);
$this->fail('An expected exception has not been thrown');
} catch (NoAccessException $e) {
$this->assertTrue(true);
}
}
}
public function getIpWhitelistedTests()
{
return array(
array(true, '127.0.0.1'),
array(true, '192.168.33.1'),
array(true, '2001:0db8:85a3:0000:0000:8a2e:0370:7334'),
array(true, '204.93.240.5'),
array(true, '204.93.177.5'),
array(true, '2001:db9:0000:ffff:ffff:ffff:ffff:ffff'),
array(false, '127.0.0.2'),
array(false, '192.168.33.2'),
array(false, '2001:0db8:85a3:0000:0000:8a2e:0370:7333'),
array(false, '204.93.239.5'),
array(false, '204.93.177.255'),
array(false, '2001:db8:0000:ffff:ffff:ffff:ffff:ffff'),
);
}
private function setGeneralConfig($name, $value)
{
$config = Config::getInstance();
$general = $config->General;
$general[$name] = $value;
$config->General = $general;
$config->forceSave();
}
public function provideContainerConfig()
{
return array(
'Piwik\Access' => new FakeAccess()
);
}
}
|