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