File size: 2,607 Bytes
bf8c0ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?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\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 testAuthenticatedRequestShouldWorkWhenAuthenticationRequired()
    {
        // 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 testUnauthenticatedRequestShouldWorkWhenAuthenticationNotRequired()
    {
        $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 testUnauthenticatedRequestShouldNotWorkWhenAuthenticationRequired()
    {
        // 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' => \Piwik\DI::add(array(
                array('Environment.bootstrapped', \Piwik\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 => \Piwik\DI::decorate(function (Config $config) {
                $config->General['login_allowlist_ip'] = ['1.1.1.1'];
                return $config;
            }),
        ];
    }
}