Passed
Pull Request — master (#30)
by Robbie
03:06
created

CwpBasicAuthMiddlewareTest::whitelistingProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
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;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Security\BasicAuthMiddleware was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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 testSetWhitelistedIps()
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
        $this->middleware->setWhitelistedIps(['127.0.0.1']);
52
        $this->assertSame(['127.0.0.1'], $this->middleware->getWhitelistedIps(), 'Accepts array values');
53
    }
54
55
    /**
56
     * @param string $currentIp
57
     * @param int $expected
58
     * @dataProvider whitelistingProvider
59
     */
60
    public function testIpWhitelisting($currentIp, $expected)
61
    {
62
        // Enable basic auth everywhere
63
        $this->middleware->setURLPatterns(['#.*#' => true]);
64
65
        // Set a whitelisted IP address
66
        $_SERVER['REMOTE_ADDR'] = $currentIp;
67
        $this->middleware->setWhitelistedIps(['127.0.0.1']);
68
69
        $response = $this->mockRequest();
70
71
        $this->assertEquals($expected, $response->getStatusCode());
72
    }
73
74
    /**
75
     * @return array[]
76
     */
77
    public function whitelistingProvider()
78
    {
79
        return [
80
            'IP not in whitelist' => ['123.456.789.012', 401],
81
            'IP in whitelist' => ['127.0.0.1', 200],
82
        ];
83
    }
84
85
    public function testMiddlewareProvidesUatServerPermissions()
86
    {
87
        $this->assertArrayHasKey('ACCESS_UAT_SERVER', $this->middleware->providePermissions());
88
    }
89
90
    /**
91
     * Perform a mock middleware request. Will return 200 if everything is OK.
92
     *
93
     * @param string $url
94
     * @return HTTPResponse
95
     */
96
    protected function mockRequest($url = '/foo')
97
    {
98
        $request = new HTTPRequest('GET', $url);
99
100
        return $this->middleware->process($request, function () {
101
            return new HTTPResponse('OK', 200);
102
        });
103
    }
104
}
105