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

CertificatesModuleTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 128
Duplicated Lines 23.44 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 9
dl 30
loc 128
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B setUp() 0 34 1
A testGenerateCert() 0 18 1
A testServerInfo() 0 16 1
A testGenerateServerCert() 0 20 1
B makeRequest() 30 30 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 PDO;
23
use PHPUnit_Framework_TestCase;
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\Api\CertificatesModule;
28
use SURFnet\VPN\Server\Storage;
29
use SURFnet\VPN\Server\Tests\TestCa;
30
use SURFnet\VPN\Server\TlsAuth;
31
32
class CertificatesModuleTest extends PHPUnit_Framework_TestCase
33
{
34
    /** @var \SURFnet\VPN\Common\Http\Service */
35
    private $service;
36
37
    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...
38
    {
39
        $random = $this->getMockBuilder('SURFnet\VPN\Common\RandomInterface')->getMock();
40
        $random->method('get')->will($this->onConsecutiveCalls('random_1', 'random_2'));
41
42
        $storage = new Storage(
43
            new PDO(
44
                $GLOBALS['DB_DSN'],
45
                $GLOBALS['DB_USER'],
46
                $GLOBALS['DB_PASSWD']
47
            ),
48
            new DateTime()
49
        );
50
        $storage->init();
51
        $this->service = new Service();
52
        $this->service->addModule(
53
            new CertificatesModule(
54
                new TestCa(),
55
                $storage,
56
                new TlsAuth(sprintf('%s/data', dirname(__DIR__))),
57
                $random
58
            )
59
        );
60
61
        $bearerAuthentication = new BasicAuthenticationHook(
62
            [
63
                'vpn-user-portal' => 'abcdef',
64
                'vpn-admin-portal' => 'ffeedd',
65
                'vpn-server-node' => 'aabbcc',
66
            ]
67
        );
68
69
        $this->service->addBeforeHook('auth', $bearerAuthentication);
70
    }
71
72
    public function testGenerateCert()
73
    {
74
        $this->assertSame(
75
            [
76
                'certificate' => 'ClientCert for random_1',
77
                'private_key' => 'ClientKey for random_1',
78
                'valid_from' => 1234567890,
79
                'valid_to' => 2345678901,
80
            ],
81
            $this->makeRequest(
82
                ['vpn-user-portal', 'abcdef'],
83
                'POST',
84
                'add_client_certificate',
85
                [],
86
                ['user_id' => 'foo', 'display_name' => 'bar']
87
            )
88
        );
89
    }
90
91
    public function testServerInfo()
92
    {
93
        $this->assertSame(
94
            [
95
                'ta' => 'Test_Ta_Key',
96
                'ca' => 'Ca',
97
            ],
98
            $this->makeRequest(
99
                ['vpn-user-portal', 'abcdef'],
100
                'GET',
101
                'server_info',
102
                [],
103
                []
104
            )
105
        );
106
    }
107
108
    public function testGenerateServerCert()
109
    {
110
        $this->assertSame(
111
            [
112
                'certificate' => 'ServerCert for vpn.example',
113
                'private_key' => 'ServerCert for vpn.example',
114
                'valid_from' => 1234567890,
115
                'valid_to' => 2345678901,
116
                'ta' => 'Test_Ta_Key',
117
                'ca' => 'Ca',
118
            ],
119
            $this->makeRequest(
120
                ['vpn-server-node', 'aabbcc'],
121
                'POST',
122
                'add_server_certificate',
123
                [],
124
                ['common_name' => 'vpn.example']
125
            )
126
        );
127
    }
128
129 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...
130
    {
131
        $response = $this->service->run(
132
            new Request(
133
                [
134
                    'SERVER_PORT' => 80,
135
                    'SERVER_NAME' => 'vpn.example',
136
                    'REQUEST_METHOD' => $requestMethod,
137
                    'SCRIPT_NAME' => '/index.php',
138
                    'REQUEST_URI' => sprintf('/%s', $pathInfo),
139
                    'PHP_AUTH_USER' => $basicAuth[0],
140
                    'PHP_AUTH_PW' => $basicAuth[1],
141
                ],
142
                $getData,
143
                $postData
144
            )
145
        );
146
147
        $responseArray = json_decode($response->getBody(), true)[$pathInfo];
148
        if ($responseArray['ok']) {
149
            if (array_key_exists('data', $responseArray)) {
150
                return $responseArray['data'];
151
            }
152
153
            return true;
154
        }
155
156
        // in case of errors...
157
        return $responseArray;
158
    }
159
}
160