|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* eduVPN - End-user friendly VPN. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright: 2016-2017, The Commons Conservancy eduVPN Programme |
|
7
|
|
|
* SPDX-License-Identifier: AGPL-3.0+ |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace SURFnet\VPN\Server\Tests\Api; |
|
11
|
|
|
|
|
12
|
|
|
use PHPUnit_Framework_TestCase; |
|
13
|
|
|
use SURFnet\VPN\Common\Config; |
|
14
|
|
|
use SURFnet\VPN\Common\Http\BasicAuthenticationHook; |
|
15
|
|
|
use SURFnet\VPN\Common\Http\Request; |
|
16
|
|
|
use SURFnet\VPN\Common\Http\Service; |
|
17
|
|
|
use SURFnet\VPN\Server\Api\InfoModule; |
|
18
|
|
|
|
|
19
|
|
|
class InfoModuleTest extends PHPUnit_Framework_TestCase |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var \SURFnet\VPN\Common\Http\Service */ |
|
22
|
|
|
private $service; |
|
23
|
|
|
|
|
24
|
|
|
public function setUp() |
|
25
|
|
|
{ |
|
26
|
|
|
$config = Config::fromFile(sprintf('%s/data/info_module_config.php', __DIR__)); |
|
27
|
|
|
|
|
28
|
|
|
$this->service = new Service(); |
|
29
|
|
|
$this->service->addModule( |
|
30
|
|
|
new InfoModule( |
|
31
|
|
|
$config |
|
32
|
|
|
) |
|
33
|
|
|
); |
|
34
|
|
|
|
|
35
|
|
|
$bearerAuthentication = new BasicAuthenticationHook( |
|
36
|
|
|
[ |
|
37
|
|
|
'vpn-user-portal' => 'aabbcc', |
|
38
|
|
|
] |
|
39
|
|
|
); |
|
40
|
|
|
|
|
41
|
|
|
$this->service->addBeforeHook('auth', $bearerAuthentication); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function testProfileList() |
|
45
|
|
|
{ |
|
46
|
|
|
$this->assertSame( |
|
47
|
|
|
[ |
|
48
|
|
|
'internet' => [ |
|
49
|
|
|
'defaultGateway' => false, |
|
50
|
|
|
'routes' => [], |
|
51
|
|
|
'dns' => [], |
|
52
|
|
|
'useNat' => false, |
|
53
|
|
|
'twoFactor' => false, |
|
54
|
|
|
'clientToClient' => false, |
|
55
|
|
|
'listen' => '::', |
|
56
|
|
|
'enableLog' => false, |
|
57
|
|
|
'enableAcl' => false, |
|
58
|
|
|
'aclGroupList' => [], |
|
59
|
|
|
'managementIp' => '127.0.0.1', |
|
60
|
|
|
'blockSmb' => false, |
|
61
|
|
|
'reject4' => false, |
|
62
|
|
|
'reject6' => false, |
|
63
|
|
|
'vpnProtoPorts' => [ |
|
64
|
|
|
'udp/1194', |
|
65
|
|
|
'tcp/1194', |
|
66
|
|
|
], |
|
67
|
|
|
'hideProfile' => false, |
|
68
|
|
|
'tlsCrypt' => false, |
|
69
|
|
|
'profileNumber' => 1, |
|
70
|
|
|
'displayName' => 'Internet Access', |
|
71
|
|
|
'extIf' => 'eth0', |
|
72
|
|
|
'range' => '10.0.0.0/24', |
|
73
|
|
|
'range6' => 'fd00:4242:4242::/48', |
|
74
|
|
|
'hostName' => 'vpn.example', |
|
75
|
|
|
], |
|
76
|
|
|
], |
|
77
|
|
|
$this->makeRequest( |
|
78
|
|
|
['vpn-user-portal', 'aabbcc'], |
|
79
|
|
|
'GET', |
|
80
|
|
|
'profile_list', |
|
81
|
|
|
[], |
|
82
|
|
|
[] |
|
83
|
|
|
) |
|
84
|
|
|
); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
View Code Duplication |
private function makeRequest(array $basicAuth, $requestMethod, $pathInfo, array $getData = [], array $postData = []) |
|
|
|
|
|
|
88
|
|
|
{ |
|
89
|
|
|
$response = $this->service->run( |
|
90
|
|
|
new Request( |
|
91
|
|
|
[ |
|
92
|
|
|
'SERVER_PORT' => 80, |
|
93
|
|
|
'SERVER_NAME' => 'vpn.example', |
|
94
|
|
|
'REQUEST_METHOD' => $requestMethod, |
|
95
|
|
|
'SCRIPT_NAME' => '/index.php', |
|
96
|
|
|
'REQUEST_URI' => sprintf('/%s', $pathInfo), |
|
97
|
|
|
'PHP_AUTH_USER' => $basicAuth[0], |
|
98
|
|
|
'PHP_AUTH_PW' => $basicAuth[1], |
|
99
|
|
|
], |
|
100
|
|
|
$getData, |
|
101
|
|
|
$postData |
|
102
|
|
|
) |
|
103
|
|
|
); |
|
104
|
|
|
|
|
105
|
|
|
$responseArray = json_decode($response->getBody(), true)[$pathInfo]; |
|
106
|
|
|
if ($responseArray['ok']) { |
|
107
|
|
|
if (array_key_exists('data', $responseArray)) { |
|
108
|
|
|
return $responseArray['data']; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return true; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
// in case of errors... |
|
115
|
|
|
return $responseArray; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
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.