Passed
Push — master ( b33732...fee3f1 )
by Robbie
13:28 queued 09:27
created

testSetWhitelistedIpsSupportedNestedStringListsInsideArrays()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 0
dl 0
loc 17
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
namespace CWP\Core\Tests\Control;
4
5
use CWP\Core\Control\CwpBasicAuthMiddleware;
6
use SilverStripe\Control\HTTPRequest;
7
use SilverStripe\Control\HTTPResponse;
8
use SilverStripe\Core\Config\Config;
9
use SilverStripe\Core\Injector\Injector;
10
use SilverStripe\Dev\SapphireTest;
11
use SilverStripe\Security\BasicAuth;
12
use SilverStripe\Security\BasicAuthMiddleware;
13
14
class CwpBasicAuthMiddlewareTest extends SapphireTest
15
{
16
    /**
17
     * @var CwpBasicAuthMiddleware
18
     */
19
    protected $middleware;
20
21
    /**
22
     * @var array
23
     */
24
    protected $originalServersVars = [];
25
26
    protected function setUp()
27
    {
28
        parent::setUp();
29
30
        $this->middleware = Injector::inst()->get(BasicAuthMiddleware::class);
31
        $this->originalServersVars = $_SERVER;
32
33
        Config::modify()->set(BasicAuth::class, 'ignore_cli', false);
34
    }
35
36
    protected function tearDown()
37
    {
38
        $_SERVER = $this->originalServersVars;
39
40
        parent::tearDown();
41
    }
42
43
    public function testSetWhitelistedIpsAcceptsStrings()
44
    {
45
        $this->middleware->setWhitelistedIps('127.0.0.1,127.0.0.2');
46
        $this->assertSame([
47
            '127.0.0.1',
48
            '127.0.0.2',
49
        ], $this->middleware->getWhitelistedIps(), 'Accepts comma delimited strings');
50
    }
51
52
    public function testSetWhitelistedIpsAcceptsArraysOfStrings()
53
    {
54
        $this->middleware->setWhitelistedIps(['127.0.0.1']);
55
        $this->assertSame(['127.0.0.1'], $this->middleware->getWhitelistedIps(), 'Accepts array values');
56
    }
57
58
    public function testSetWhitelistedIpsSupportedNestedStringListsInsideArrays()
59
    {
60
        $this->middleware->setWhitelistedIps([
61
            '127.0.0.1,127.0.0.2', // Example of `CWP_IP_BYPASS_BASICAUTH` env var value
62
            ' 137.0.0.1 , 127.0.0.2', // Example of `CWP_IP_BYPASS_BASICAUTH` env var value with added spaces
63
            '127.0.0.3',
64
            '127.0.0.3', // check results are unique
65
            '127.0.0.4',
66
        ]);
67
68
        $this->assertSame([
69
            '127.0.0.1',
70
            '127.0.0.2',
71
            '137.0.0.1',
72
            '127.0.0.3',
73
            '127.0.0.4',
74
        ], $this->middleware->getWhitelistedIps(), 'Accepts IP list strings inside arrays');
75
    }
76
77
    /**
78
     * @param string $currentIp
79
     * @param int $expected
80
     * @dataProvider whitelistingProvider
81
     */
82
    public function testIpWhitelisting($currentIp, $expected)
83
    {
84
        // Enable basic auth everywhere
85
        $this->middleware->setURLPatterns(['#.*#' => true]);
86
87
        // Set a whitelisted IP address
88
        $_SERVER['REMOTE_ADDR'] = $currentIp;
89
        $this->middleware->setWhitelistedIps(['127.0.0.1']);
90
91
        $response = $this->mockRequest();
92
93
        $this->assertEquals($expected, $response->getStatusCode());
94
    }
95
96
    /**
97
     * @return array[]
98
     */
99
    public function whitelistingProvider()
100
    {
101
        return [
102
            'IP not in whitelist' => ['123.456.789.012', 401],
103
            'IP in whitelist' => ['127.0.0.1', 200],
104
        ];
105
    }
106
107
    public function testMiddlewareProvidesUatServerPermissions()
108
    {
109
        $this->assertArrayHasKey('ACCESS_UAT_SERVER', $this->middleware->providePermissions());
110
    }
111
112
    /**
113
     * Perform a mock middleware request. Will return 200 if everything is OK.
114
     *
115
     * @param string $url
116
     * @return HTTPResponse
117
     */
118
    protected function mockRequest($url = '/foo')
119
    {
120
        $request = new HTTPRequest('GET', $url);
121
122
        return $this->middleware->process($request, function () {
123
            return new HTTPResponse('OK', 200);
124
        });
125
    }
126
}
127