|
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\SystemMessagesModule; |
|
28
|
|
|
use SURFnet\VPN\Server\Storage; |
|
29
|
|
|
|
|
30
|
|
|
class SystemMessagesTest extends PHPUnit_Framework_TestCase |
|
31
|
|
|
{ |
|
32
|
|
|
/** @var \SURFnet\VPN\Common\Http\Service */ |
|
33
|
|
|
private $service; |
|
34
|
|
|
|
|
35
|
|
|
public function setUp() |
|
|
|
|
|
|
36
|
|
|
{ |
|
37
|
|
|
$storage = new Storage( |
|
38
|
|
|
new PDO( |
|
39
|
|
|
$GLOBALS['DB_DSN'], |
|
40
|
|
|
$GLOBALS['DB_USER'], |
|
41
|
|
|
$GLOBALS['DB_PASSWD'] |
|
42
|
|
|
), |
|
43
|
|
|
new DateTime('2016-01-01 08:00:00') |
|
44
|
|
|
); |
|
45
|
|
|
$storage->init(); |
|
46
|
|
|
$storage->addSystemMessage('motd', 'Hello World!'); |
|
47
|
|
|
|
|
48
|
|
|
$this->service = new Service(); |
|
49
|
|
|
$this->service->addModule( |
|
50
|
|
|
new SystemMessagesModule( |
|
51
|
|
|
$storage |
|
52
|
|
|
) |
|
53
|
|
|
); |
|
54
|
|
|
|
|
55
|
|
|
$bearerAuthentication = new BasicAuthenticationHook( |
|
56
|
|
|
[ |
|
57
|
|
|
'vpn-admin-portal' => 'aabbcc', |
|
58
|
|
|
] |
|
59
|
|
|
); |
|
60
|
|
|
|
|
61
|
|
|
$this->service->addBeforeHook('auth', $bearerAuthentication); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function testGetSystemMessages() |
|
65
|
|
|
{ |
|
66
|
|
|
$this->assertSame( |
|
67
|
|
|
[ |
|
68
|
|
|
[ |
|
69
|
|
|
'id' => '1', |
|
70
|
|
|
'message' => 'Hello World!', |
|
71
|
|
|
'date_time' => '2016-01-01 08:00:00', |
|
72
|
|
|
], |
|
73
|
|
|
], |
|
74
|
|
|
$this->makeRequest( |
|
75
|
|
|
['vpn-admin-portal', 'aabbcc'], |
|
76
|
|
|
'GET', |
|
77
|
|
|
'system_messages', |
|
78
|
|
|
['message_type' => 'motd'], |
|
79
|
|
|
[] |
|
80
|
|
|
) |
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function testAddSystemMessage() |
|
85
|
|
|
{ |
|
86
|
|
|
$this->assertTrue( |
|
87
|
|
|
$this->makeRequest( |
|
88
|
|
|
['vpn-admin-portal', 'aabbcc'], |
|
89
|
|
|
'POST', |
|
90
|
|
|
'add_system_message', |
|
91
|
|
|
[], |
|
92
|
|
|
['message_type' => 'motd', 'message_body' => 'foo'] |
|
93
|
|
|
) |
|
94
|
|
|
); |
|
95
|
|
|
$this->assertSame( |
|
96
|
|
|
[ |
|
97
|
|
|
[ |
|
98
|
|
|
'id' => '1', |
|
99
|
|
|
'message' => 'Hello World!', |
|
100
|
|
|
'date_time' => '2016-01-01 08:00:00', |
|
101
|
|
|
], |
|
102
|
|
|
[ |
|
103
|
|
|
'id' => '2', |
|
104
|
|
|
'message' => 'foo', |
|
105
|
|
|
'date_time' => '2016-01-01 08:00:00', |
|
106
|
|
|
], |
|
107
|
|
|
], |
|
108
|
|
|
$this->makeRequest( |
|
109
|
|
|
['vpn-admin-portal', 'aabbcc'], |
|
110
|
|
|
'GET', |
|
111
|
|
|
'system_messages', |
|
112
|
|
|
['message_type' => 'motd'], |
|
113
|
|
|
[] |
|
114
|
|
|
) |
|
115
|
|
|
); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function testDeleteSystemMessage() |
|
119
|
|
|
{ |
|
120
|
|
|
$this->assertTrue( |
|
121
|
|
|
$this->makeRequest( |
|
122
|
|
|
['vpn-admin-portal', 'aabbcc'], |
|
123
|
|
|
'POST', |
|
124
|
|
|
'delete_system_message', |
|
125
|
|
|
[], |
|
126
|
|
|
['message_id' => 1] |
|
127
|
|
|
) |
|
128
|
|
|
); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
View Code Duplication |
private function makeRequest(array $basicAuth, $requestMethod, $pathInfo, array $getData = [], array $postData = []) |
|
|
|
|
|
|
132
|
|
|
{ |
|
133
|
|
|
$response = $this->service->run( |
|
134
|
|
|
new Request( |
|
135
|
|
|
[ |
|
136
|
|
|
'SERVER_PORT' => 80, |
|
137
|
|
|
'SERVER_NAME' => 'vpn.example', |
|
138
|
|
|
'REQUEST_METHOD' => $requestMethod, |
|
139
|
|
|
'SCRIPT_NAME' => '/index.php', |
|
140
|
|
|
'REQUEST_URI' => sprintf('/%s', $pathInfo), |
|
141
|
|
|
'PHP_AUTH_USER' => $basicAuth[0], |
|
142
|
|
|
'PHP_AUTH_PW' => $basicAuth[1], |
|
143
|
|
|
], |
|
144
|
|
|
$getData, |
|
145
|
|
|
$postData |
|
146
|
|
|
) |
|
147
|
|
|
); |
|
148
|
|
|
|
|
149
|
|
|
$responseArray = json_decode($response->getBody(), true)[$pathInfo]; |
|
150
|
|
|
if ($responseArray['ok']) { |
|
151
|
|
|
if (array_key_exists('data', $responseArray)) { |
|
152
|
|
|
return $responseArray['data']; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
return true; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
// in case of errors... |
|
159
|
|
|
return $responseArray; |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
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: