|
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
|
|
|
namespace SURFnet\VPN\Server\Test; |
|
19
|
|
|
|
|
20
|
|
|
use SURFnet\VPN\Common\HttpClient\HttpClientInterface; |
|
21
|
|
|
use RuntimeException; |
|
22
|
|
|
use SURFnet\VPN\Server\PoolConfig; |
|
23
|
|
|
|
|
24
|
|
|
class TestHttpClient implements HttpClientInterface |
|
25
|
|
|
{ |
|
26
|
|
|
public function get($requestUri) |
|
27
|
|
|
{ |
|
28
|
|
|
switch ($requestUri) { |
|
29
|
|
|
case 'serverClient/has_otp_secret?user_id=foo': |
|
30
|
|
|
return self::wrap('has_otp_secret', true); |
|
31
|
|
|
case 'serverClient/has_otp_secret?user_id=bar': |
|
32
|
|
|
return self::wrap('has_otp_secret', false); |
|
33
|
|
|
|
|
34
|
|
|
case 'connectionServerClient/is_disabled_user?user_id=foo': |
|
35
|
|
|
return self::wrap('is_disabled_user', false); |
|
36
|
|
|
case 'connectionServerClient/is_disabled_user?user_id=bar': |
|
37
|
|
|
return self::wrap('is_disabled_user', true); |
|
38
|
|
|
case 'connectionServerClient/is_disabled_common_name?common_name=foo_bar': |
|
39
|
|
|
return self::wrap('is_disabled_common_name', false); |
|
40
|
|
|
case 'connectionServerClient/is_disabled_common_name?common_name=foo_baz': |
|
41
|
|
|
return self::wrap('is_disabled_common_name', true); |
|
42
|
|
|
case 'connectionServerClient/server_pool?pool_id=internet': |
|
43
|
|
|
$poolConfig = new PoolConfig([]); |
|
44
|
|
|
|
|
45
|
|
|
return self::wrap('server_pool', $poolConfig->v()); |
|
46
|
|
View Code Duplication |
case 'connectionServerClient/server_pool?pool_id=acl': |
|
|
|
|
|
|
47
|
|
|
$poolConfig = new PoolConfig( |
|
48
|
|
|
[ |
|
49
|
|
|
'enableAcl' => true, |
|
50
|
|
|
'aclGroupList' => ['all'], |
|
51
|
|
|
] |
|
52
|
|
|
); |
|
53
|
|
|
|
|
54
|
|
|
return self::wrap('server_pool', $poolConfig->v()); |
|
55
|
|
View Code Duplication |
case 'connectionServerClient/server_pool?pool_id=acl2': |
|
|
|
|
|
|
56
|
|
|
$poolConfig = new PoolConfig( |
|
57
|
|
|
[ |
|
58
|
|
|
'enableAcl' => true, |
|
59
|
|
|
'aclGroupList' => ['students'], |
|
60
|
|
|
] |
|
61
|
|
|
); |
|
62
|
|
|
|
|
63
|
|
|
return self::wrap('server_pool', $poolConfig->v()); |
|
64
|
|
|
case 'connectionServerClient/user_groups?user_id=foo': |
|
65
|
|
|
return self::wrap( |
|
66
|
|
|
'user_groups', |
|
67
|
|
|
[ |
|
68
|
|
|
['id' => 'all', 'displayName' => 'All'], |
|
69
|
|
|
] |
|
70
|
|
|
); |
|
71
|
|
|
default: |
|
72
|
|
|
throw new RuntimeException(sprintf('unexpected requestUri "%s"', $requestUri)); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function post($requestUri, array $postData) |
|
77
|
|
|
{ |
|
78
|
|
|
switch ($requestUri) { |
|
79
|
|
|
case 'serverClient/verify_otp_key': |
|
80
|
|
|
if ('123456' === $postData['otp_key']) { |
|
81
|
|
|
return self::wrap('verify_otp_key', true); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return self::wrap('verify_otp_key', false); |
|
85
|
|
|
case 'connectionServerClient/log_connect': |
|
86
|
|
|
return self::wrap('log_connect', true); |
|
87
|
|
|
case 'connectionServerClient/log_disconnect': |
|
88
|
|
|
return self::wrap('log_disconnect', true); |
|
89
|
|
|
default: |
|
90
|
|
|
throw new RuntimeException(sprintf('unexpected requestUri "%s"', $requestUri)); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
private static function wrap($key, $response) |
|
95
|
|
|
{ |
|
96
|
|
|
return [ |
|
97
|
|
|
'data' => [ |
|
98
|
|
|
$key => $response, |
|
99
|
|
|
], |
|
100
|
|
|
]; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
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.