GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 932629...d7a7a8 )
by François
03:12
created

ConnectionsModuleTest::testConnectInAcl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
dl 18
loc 18
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 0
1
<?php
2
/**
3
 *  Copyright (C) 2016 SURFnet.
4
 *
5
 *  This program is free software: you can redistribute it and/or modify
6
 *  it under the terms of the GNU Affero General Public License as
7
 *  published by the Free Software Foundation, either version 3 of the
8
 *  License, or (at your option) any later version.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU Affero General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU Affero General Public License
16
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace SURFnet\VPN\Server\Tests\Api;
20
21
use DateTime;
22
use Otp\Otp;
23
use ParagonIE\ConstantTime\Encoding;
24
use PDO;
25
use PHPUnit_Framework_TestCase;
26
use SURFnet\VPN\Common\Config;
27
use SURFnet\VPN\Common\Http\BasicAuthenticationHook;
28
use SURFnet\VPN\Common\Http\Request;
29
use SURFnet\VPN\Common\Http\Service;
30
use SURFnet\VPN\Server\Acl\Provider\StaticProvider;
31
use SURFnet\VPN\Server\Api\ConnectionsModule;
32
use SURFnet\VPN\Server\Storage;
33
34
class ConnectionsModuleTest extends PHPUnit_Framework_TestCase
35
{
36
    /** @var \SURFnet\VPN\Common\Http\Service */
37
    private $service;
38
39
    public function setUp()
0 ignored issues
show
Coding Style introduced by
setUp uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
40
    {
41
        $storage = new Storage(
42
            new PDO(
43
                $GLOBALS['DB_DSN'],
44
                $GLOBALS['DB_USER'],
45
                $GLOBALS['DB_PASSWD']
46
            ),
47
            new DateTime()
48
        );
49
        $storage->init();
50
        $storage->addCertificate('foo', '12345678901234567890123456789012', '12345678901234567890123456789012', new DateTime('@12345678'), new DateTime('@23456789'));
51
        $storage->setTotpSecret('foo', 'CN2XAL23SIFTDFXZ');
52
        $storage->clientConnect('internet', '12345678901234567890123456789012', '10.10.10.10', 'fd00:4242:4242:4242::', new DateTime('@12345678'));
53
54
        $config = Config::fromFile(sprintf('%s/data/config.php', __DIR__));
55
56
        $groupProviders = [
57
            new StaticProvider(
58
                $config->getSection('groupProviders')->getSection('StaticProvider')
59
            ),
60
        ];
61
62
        $this->service = new Service();
63
        $this->service->addModule(
64
            new ConnectionsModule(
65
                $config,
66
                $storage,
67
                $groupProviders
68
            )
69
        );
70
71
        $bearerAuthentication = new BasicAuthenticationHook(
72
            [
73
                'vpn-server-node' => 'aabbcc',
74
            ]
75
        );
76
77
        $this->service->addBeforeHook('auth', $bearerAuthentication);
78
    }
79
80 View Code Duplication
    public function testConnect()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
    {
82
        $this->assertTrue(
83
            $this->makeRequest(
84
                ['vpn-server-node', 'aabbcc'],
85
                'POST',
86
                'connect',
87
                [],
88
                [
89
                    'profile_id' => 'internet',
90
                    'common_name' => '12345678901234567890123456789012',
91
                    'ip4' => '10.10.10.10',
92
                    'ip6' => 'fd00:4242:4242:4242::',
93
                    'connected_at' => 12345678,
94
                ]
95
            )
96
        );
97
    }
98
99 View Code Duplication
    public function testConnectInAcl()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
    {
101
        $this->assertTrue(
102
            $this->makeRequest(
103
                ['vpn-server-node', 'aabbcc'],
104
                'POST',
105
                'connect',
106
                [],
107
                [
108
                    'profile_id' => 'acl',
109
                    'common_name' => '12345678901234567890123456789012',
110
                    'ip4' => '10.10.10.10',
111
                    'ip6' => 'fd00:4242:4242:4242::',
112
                    'connected_at' => 12345678,
113
                ]
114
            )
115
        );
116
    }
117
118 View Code Duplication
    public function testConnectNotInAcl()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
119
    {
120
        $this->assertSame(
121
            [
122
                'ok' => false,
123
                'error' => '[VPN] unable to connect, account not a member of required group',
124
            ],
125
            $this->makeRequest(
126
                ['vpn-server-node', 'aabbcc'],
127
                'POST',
128
                'connect',
129
                [],
130
                [
131
                    'profile_id' => 'acl2',
132
                    'common_name' => '12345678901234567890123456789012',
133
                    'ip4' => '10.10.10.10',
134
                    'ip6' => 'fd00:4242:4242:4242::',
135
                    'connected_at' => 12345678,
136
                ]
137
            )
138
        );
139
    }
140
141 View Code Duplication
    public function testDisconnect()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
142
    {
143
        $this->assertTrue(
144
            $this->makeRequest(
145
                ['vpn-server-node', 'aabbcc'],
146
                'POST',
147
                'disconnect',
148
                [],
149
                [
150
                    'profile_id' => 'internet',
151
                    'common_name' => '12345678901234567890123456789012',
152
                    'ip4' => '10.10.10.10',
153
                    'ip6' => 'fd00:4242:4242:4242::',
154
                    'connected_at' => 12345678,
155
                    'disconnected_at' => 23456789,
156
                    'bytes_transferred' => 2222222,
157
                ]
158
            )
159
        );
160
    }
161
162 View Code Duplication
    public function testVerifyOtp()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
163
    {
164
        $otp = new Otp();
165
        $totpSecret = 'CN2XAL23SIFTDFXZ';
166
        $totpKey = $otp->totp(Encoding::base32DecodeUpper($totpSecret));
167
168
        $this->assertTrue(
169
            $this->makeRequest(
170
                ['vpn-server-node', 'aabbcc'],
171
                'POST',
172
                'verify_two_factor',
173
                [],
174
                [
175
                    'common_name' => '12345678901234567890123456789012',
176
                    'two_factor_type' => 'totp',
177
                    'two_factor_value' => $totpKey,
178
                ]
179
            )
180
        );
181
    }
182
183 View Code Duplication
    private function makeRequest(array $basicAuth, $requestMethod, $pathInfo, array $getData = [], array $postData = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
184
    {
185
        $response = $this->service->run(
186
            new Request(
187
                [
188
                    'SERVER_PORT' => 80,
189
                    'SERVER_NAME' => 'vpn.example',
190
                    'REQUEST_METHOD' => $requestMethod,
191
                    'SCRIPT_NAME' => '/index.php',
192
                    'REQUEST_URI' => sprintf('/%s', $pathInfo),
193
                    'PHP_AUTH_USER' => $basicAuth[0],
194
                    'PHP_AUTH_PW' => $basicAuth[1],
195
                ],
196
                $getData,
197
                $postData
198
            )
199
        );
200
201
        $responseArray = json_decode($response->getBody(), true)[$pathInfo];
202
        if ($responseArray['ok']) {
203
            if (array_key_exists('data', $responseArray)) {
204
                return $responseArray['data'];
205
            }
206
207
            return true;
208
        }
209
210
        // in case of errors...
211
        return $responseArray;
212
    }
213
}
214