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 ( cc47df...2bcdb5 )
by François
02:37
created

ConnectionsModuleTest::testConnect()   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\Api;
20
21
use PDO;
22
use PHPUnit_Framework_TestCase;
23
use SURFnet\VPN\Common\Config;
24
use SURFnet\VPN\Common\Http\BasicAuthenticationHook;
25
use SURFnet\VPN\Common\Http\Request;
26
use SURFnet\VPN\Common\Http\Service;
27
use SURFnet\VPN\Server\Acl\Provider\StaticProvider;
28
use SURFnet\VPN\Server\Storage;
29
30
class ConnectionsModuleTest extends PHPUnit_Framework_TestCase
31
{
32
    /** @var \SURFnet\VPN\Common\Http\Service */
33
    private $service;
34
35
    public function setUp()
36
    {
37
        $random = $this->getMockBuilder('SURFnet\VPN\Common\RandomInterface')->getMock();
38
        $random->method('get')->will($this->onConsecutiveCalls('random_1', 'random_2'));
39
40
        $storage = new Storage(
41
            new PDO('sqlite::memory:'),
42
            $random
43
        );
44
        $storage->init();
45
        $storage->addCertificate('foo', '12345678901234567890123456789012', '12345678901234567890123456789012', 12345678, 23456789);
46
        $storage->clientConnect('internet', '12345678901234567890123456789012', '10.10.10.10', 'fd00:4242:4242:4242::', 12345678);
47
48
        $config = Config::fromFile(sprintf('%s/data/config.yaml', __DIR__));
49
50
        $groupProviders = [
51
            new StaticProvider(
52
                new Config(
53
                    $config->v('groupProviders', 'StaticProvider')
54
                )
55
            ),
56
        ];
57
58
        $this->service = new Service();
59
        $this->service->addModule(
60
            new ConnectionsModule(
61
                $config,
62
                $storage,
63
                $groupProviders
64
            )
65
        );
66
67
        $bearerAuthentication = new BasicAuthenticationHook(
68
            [
69
                'vpn-server-node' => 'aabbcc',
70
            ]
71
        );
72
73
        $this->service->addBeforeHook('auth', $bearerAuthentication);
74
    }
75
76 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...
77
    {
78
        $this->assertTrue(
79
            $this->makeRequest(
80
                ['vpn-server-node', 'aabbcc'],
81
                'POST',
82
                'connect',
83
                [],
84
                [
85
                    'profile_id' => 'internet',
86
                    'common_name' => '12345678901234567890123456789012',
87
                    'ip4' => '10.10.10.10',
88
                    'ip6' => 'fd00:4242:4242:4242::',
89
                    'connected_at' => 12345678,
90
                ]
91
            )
92
        );
93
    }
94
95 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...
96
    {
97
        $this->assertTrue(
98
            $this->makeRequest(
99
                ['vpn-server-node', 'aabbcc'],
100
                'POST',
101
                'connect',
102
                [],
103
                [
104
                    'profile_id' => 'acl',
105
                    'common_name' => '12345678901234567890123456789012',
106
                    'ip4' => '10.10.10.10',
107
                    'ip6' => 'fd00:4242:4242:4242::',
108
                    'connected_at' => 12345678,
109
                ]
110
            )
111
        );
112
    }
113
114 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...
115
    {
116
        $this->assertSame(
117
            [
118
                'ok' => false,
119
                'error' => 'user not in ACL',
120
            ],
121
            $this->makeRequest(
122
                ['vpn-server-node', 'aabbcc'],
123
                'POST',
124
                'connect',
125
                [],
126
                [
127
                    'profile_id' => 'acl2',
128
                    'common_name' => '12345678901234567890123456789012',
129
                    'ip4' => '10.10.10.10',
130
                    'ip6' => 'fd00:4242:4242:4242::',
131
                    'connected_at' => 12345678,
132
                ]
133
            )
134
        );
135
    }
136
137 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...
138
    {
139
        $this->assertTrue(
140
            $this->makeRequest(
141
                ['vpn-server-node', 'aabbcc'],
142
                'POST',
143
                'disconnect',
144
                [],
145
                [
146
                    'profile_id' => 'internet',
147
                    'common_name' => '12345678901234567890123456789012',
148
                    'ip4' => '10.10.10.10',
149
                    'ip6' => 'fd00:4242:4242:4242::',
150
                    'connected_at' => 12345678,
151
                    'disconnected_at' => 23456789,
152
                    'bytes_transferred' => 2222222,
153
                ]
154
            )
155
        );
156
    }
157
158 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...
159
    {
160
        $response = $this->service->run(
161
            new Request(
162
                [
163
                    'SERVER_PORT' => 80,
164
                    'SERVER_NAME' => 'vpn.example',
165
                    'REQUEST_METHOD' => $requestMethod,
166
                    'PATH_INFO' => sprintf('/%s', $pathInfo),
167
                    'REQUEST_URI' => sprintf('/%s', $pathInfo),
168
                    'PHP_AUTH_USER' => $basicAuth[0],
169
                    'PHP_AUTH_PW' => $basicAuth[1],
170
                ],
171
                $getData,
172
                $postData
173
            )
174
        );
175
176
        $responseArray = json_decode($response->getBody(), true)[$pathInfo];
177
        if ($responseArray['ok']) {
178
            if (array_key_exists('data', $responseArray)) {
179
                return $responseArray['data'];
180
            }
181
182
            return true;
183
        }
184
185
        // in case of errors...
186
        return $responseArray;
187
    }
188
}
189