File: SystemSettingsTest.php

package info (click to toggle)
matomo 5.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 95,068 kB
  • sloc: php: 289,425; xml: 127,249; javascript: 112,130; python: 202; sh: 178; makefile: 20; sql: 10
file content (144 lines) | stat: -rw-r--r-- 4,458 bytes parent folder | download
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
<?php

/**
 * Matomo - free/libre analytics platform
 *
 * @link    https://matomo.org
 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Plugins\Login\tests\Integration;

use Piwik\Plugins\Login\SystemSettings;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;

/**
 * @group Login
 * @group BruteForceDetection
 */
class SystemSettingsTest extends IntegrationTestCase
{
    /**
     * @var SystemSettings
     */
    private $settings;

    private $exampleIps = array(
        '12.12.12.12/27',
        '14.14.14.14',
        '15.15.15.*',
        '2001:db8::/40',
        '2001:0db8:85a3:0000:0000:8a2e:0370:7334',
    );

    public function setUp(): void
    {
        parent::setUp();

        $this->settings = new SystemSettings();
    }

    public function testEnableBruteForceDetectionIsEnabledByDefault()
    {
        $this->assertTrue($this->settings->enableBruteForceDetection->getValue());
    }

    public function testLoginAttemptsTimeRangeHasCorrectDefaultValue()
    {
        $this->assertSame(60, $this->settings->loginAttemptsTimeRange->getValue());
    }

    public function testMaxFailedLoginsPerMinutesHasCorrectDefaultValue()
    {
        $this->assertSame(20, $this->settings->maxFailedLoginsPerMinutes->getValue());
    }

    public function testWhitelisteBruteForceIpsHasNoIpWhitelisted()
    {
        $this->assertSame([], $this->settings->whitelisteBruteForceIps->getValue());
    }

    public function testWhitelisteBruteForceIpsCanSuccessfullySetVariousIpsAndRanges()
    {
        $this->settings->whitelisteBruteForceIps->setValue($this->exampleIps);
        $this->assertSame($this->exampleIps, $this->settings->whitelisteBruteForceIps->getValue());
    }

    public function testWhitelisteBruteForceIpsFailsWhenContainsInvalidValue()
    {
        $this->expectException(\Exception::class);
        $this->expectExceptionMessage('SitesManager_ExceptionInvalidIPFormat');

        $this->settings->whitelisteBruteForceIps->setValue(array(
            '127.0.0.1', 'foobar',
        ));
    }

    public function testIsWhitelistedIpDoesNotWhitelistAnyIpsByDefault()
    {
        $this->assertFalse($this->settings->isWhitelistedIp('127.0.0.1'));
    }

    /**
     * @dataProvider getIpListedDataProvider
     */
    public function testIsWhitelistedIpIsIpInList($expected, $ip)
    {
        $this->settings->whitelisteBruteForceIps->setValue($this->exampleIps);
        $this->assertSame($expected, $this->settings->isWhitelistedIp($ip));
        $this->assertFalse($this->settings->isBlacklistedIp($ip));
    }

    public function testBlacklistedBruteForceIpsHasNoIpWhitelisted()
    {
        $this->assertSame([], $this->settings->blacklistedBruteForceIps->getValue());
    }

    public function testBlacklistedBruteForceIpsCanSuccessfullySetVariousIpsAndRanges()
    {
        $this->settings->blacklistedBruteForceIps->setValue($this->exampleIps);
        $this->assertSame($this->exampleIps, $this->settings->blacklistedBruteForceIps->getValue());
    }

    public function testBlacklistedBruteForceIpsFailsWhenContainsInvalidValue()
    {
        $this->expectException(\Exception::class);
        $this->expectExceptionMessage('SitesManager_ExceptionInvalidIPFormat');

        $this->settings->blacklistedBruteForceIps->setValue(array(
            '127.0.0.1', 'foobar',
        ));
    }

    /**
     * @dataProvider getIpListedDataProvider
     */
    public function testIsBlacklistedIpIsIpInList($expected, $ip)
    {
        $this->settings->blacklistedBruteForceIps->setValue($this->exampleIps);
        $this->assertSame($expected, $this->settings->isBlacklistedIp($ip));
        $this->assertFalse($this->settings->isWhitelistedIp($ip));
    }

    public function getIpListedDataProvider()
    {
        return array(
            array(true, '12.12.12.14'),
            array(true, '12.12.12.31'),
            array(true, '14.14.14.14'),
            array(true, '15.15.15.123'),
            array(true, '2001:0db8:85a3:0000:0000:8a2e:0370:7334'),

            array(false, ''),
            array(false, null),
            array(false, '12.12.12.32'),
            array(false, '14.14.14.12'),
            array(false, '2001:0db8:85a3:0000:0000:8a2e:0370:7333'),
        );
    }

    public function testIsBlacklistedIpDoesNotWhitelistAnyIpsByDefault()
    {
        $this->assertFalse($this->settings->isBlacklistedIp('127.0.0.1'));
    }
}