Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TrackerDisallowedIpTest.php « System « PHPUnit « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3b0c1f6192b172b96dba8b3a98bd9dde453e6e02 (plain)
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
<?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\Tests\System;

use Piwik\Common;
use Piwik\Config;
use Piwik\Db;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;

/**
 * @group Core
 * @group Tracker
 * @group TrackerDisallowedIp
 */
class TrackerDisallowedIpTest extends IntegrationTestCase
{
    public function setUp(): void
    {
        parent::setUp();

        Fixture::createWebsite('2014-02-04');
        Fixture::createSuperUser(false);
    }

    public function test_authenticatedRequest_ShouldWorkWhenAuthenticationRequired()
    {
        // authentication required due to an older date and custom ip
        $tracker = Fixture::getTracker(1, '2021-02-02 16:00:00', $defaultInit = true, $useLocalTracker = false);
        $tracker->setTokenAuth(Fixture::getTokenAuth());
        Fixture::checkResponse($tracker->doTrackPageView('test'));

        $this->assertEquals(1, Db::fetchOne('SELECT count(*) FROM ' . Common::prefixTable('log_visit')));
    }

    public function test_unauthenticatedRequest_ShouldWorkWhenAuthenticationNotRequired()
    {
        $tracker = Fixture::getTracker(1, date('Y-m-d H:i:s'), $defaultInit = false, $useLocalTracker = false);
        Fixture::checkResponse($tracker->doTrackPageView('test'));

        $this->assertEquals(1, Db::fetchOne('SELECT count(*) FROM ' . Common::prefixTable('log_visit')));
    }

    public function test_unauthenticatedRequest_ShouldNotWorkWhenAuthenticationRequired()
    {
        // authentication required due to an older date
        $tracker = Fixture::getTracker(1, '2021-02-02 16:00:00', $defaultInit = false, $useLocalTracker = false);
        Fixture::checkTrackingFailureResponse($tracker->doTrackPageView('test'));

        $this->assertEquals(0, Db::fetchOne('SELECT count(*) FROM ' . Common::prefixTable('log_visit')));
    }

    public static function provideContainerConfigBeforeClass()
    {
        return [
            'observers.global' => \DI\add(array(
                array('Environment.bootstrapped', \DI\value(function () {
                    // ensure tracking request uses an IP that is not local or on allow list
                    $_SERVER['REMOTE_ADDR'] = '3.3.3.3';
                }))
            )),
            Config::class => \DI\decorate(function (Config $config) {
                $config->General['login_allowlist_ip'] = ['1.1.1.1'];
                return $config;
            }),
        ];
    }

}