|
1
|
|
|
<?php |
|
2
|
|
|
namespace Mezon\Service\Tests; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Class ServiceTests |
|
6
|
|
|
* |
|
7
|
|
|
* @package Service |
|
8
|
|
|
* @subpackage ServiceTests |
|
9
|
|
|
* @author Dodonov A.A. |
|
10
|
|
|
* @version v.1.0 (2019/08/17) |
|
11
|
|
|
* @copyright Copyright (c) 2019, aeon.org |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Predefined set of tests for service |
|
16
|
|
|
* |
|
17
|
|
|
* @author Dodonov A.A. |
|
18
|
|
|
* @group baseTests |
|
19
|
|
|
*/ |
|
20
|
|
|
class ServiceTests extends \PHPUnit\Framework\TestCase |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Session id |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $sessionId = false; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Server path |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $serverPath = false; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Headers |
|
35
|
|
|
* |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $headers = false; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Method asserts for errors and warnings in the html code |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $content |
|
44
|
|
|
* Asserting content |
|
45
|
|
|
* @param string $message |
|
46
|
|
|
* Message to be displayed in case of error |
|
47
|
|
|
*/ |
|
48
|
|
|
protected function assertErrors($content, $message) |
|
49
|
|
|
{ |
|
50
|
|
|
if (strpos($content, 'Warning') !== false || strpos($content, 'Error') !== false || |
|
51
|
|
|
strpos($content, 'Fatal error') !== false || strpos($content, 'Access denied') !== false || |
|
52
|
|
|
strpos($content, "doesn't exist in statement") !== false) { |
|
53
|
|
|
throw (new \Exception($message . "\r\n" . $content)); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$this->addToAssertionCount(1); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Method asserts JSON |
|
61
|
|
|
* |
|
62
|
|
|
* @param mixed $jsonResult |
|
63
|
|
|
* Result of the call |
|
64
|
|
|
* @param string $result |
|
65
|
|
|
* Raw result of the call |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function assertJsonResponse($jsonResult, string $result) |
|
68
|
|
|
{ |
|
69
|
|
|
if ($jsonResult === null && $result !== '') { |
|
70
|
|
|
$this->fail("JSON result is invalid because of:\r\n$result"); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
if (isset($jsonResult->message)) { |
|
74
|
|
|
print("message : " . $jsonResult->message . "\r\n"); |
|
75
|
|
|
print("code : " . ($jsonResult->code ?? '') . "\r\n"); |
|
76
|
|
|
print("call_stack : "); |
|
77
|
|
|
print(json_encode($jsonResult->call_stack ?? 'not provided')); |
|
78
|
|
|
$this->fail(); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Method sends post request |
|
84
|
|
|
* |
|
85
|
|
|
* @param array $data |
|
86
|
|
|
* Request data |
|
87
|
|
|
* @param string $url |
|
88
|
|
|
* Requesting endpoint |
|
89
|
|
|
* @return mixed Request result |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function postHttpRequest(array $data, string $url) |
|
92
|
|
|
{ |
|
93
|
|
|
$options = [ |
|
94
|
|
|
'http' => [ |
|
95
|
|
|
'header' => "Content-Type: application/x-www-form-urlencoded\r\n" . |
|
96
|
|
|
"User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0\r\n" . |
|
97
|
|
|
($this->sessionId !== false ? "Cgi-Authorization: Basic " . $this->sessionId . "\r\n" : '') . |
|
|
|
|
|
|
98
|
|
|
($this->headers !== false ? implode("\r\n", $this->headers) . "\r\n" : ''), |
|
|
|
|
|
|
99
|
|
|
'method' => 'POST', |
|
100
|
|
|
'content' => http_build_query($data) |
|
101
|
|
|
] |
|
102
|
|
|
]; |
|
103
|
|
|
|
|
104
|
|
|
$context = stream_context_create($options); |
|
105
|
|
|
$result = file_get_contents($url, false, $context); |
|
106
|
|
|
|
|
107
|
|
|
$this->assertErrors($result, 'Request have returned warnings/errors'); |
|
108
|
|
|
|
|
109
|
|
|
$jsonResult = json_decode($result); |
|
110
|
|
|
|
|
111
|
|
|
return $jsonResult; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Method prepares GET request options |
|
116
|
|
|
*/ |
|
117
|
|
|
protected function prepareGetOptions() |
|
118
|
|
|
{ |
|
119
|
|
|
return [ |
|
120
|
|
|
'http' => [ |
|
121
|
|
|
'header' => "Content-Type: application/x-www-form-urlencoded\r\n" . |
|
122
|
|
|
"User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0\r\n" . |
|
123
|
|
|
($this->sessionId !== false ? "Cgi-Authorization: Basic " . $this->sessionId . "\r\n" : '') . |
|
|
|
|
|
|
124
|
|
|
($this->headers !== false ? implode("\r\n", $this->headers) . "\r\n" : ''), |
|
|
|
|
|
|
125
|
|
|
'method' => 'GET' |
|
126
|
|
|
] |
|
127
|
|
|
]; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Method sends GET request |
|
132
|
|
|
* |
|
133
|
|
|
* @param string $url |
|
134
|
|
|
* Requesting URL |
|
135
|
|
|
* @return mixed Result off the request |
|
136
|
|
|
*/ |
|
137
|
|
|
protected function getHttpRequest(string $url) |
|
138
|
|
|
{ |
|
139
|
|
|
$options = $this->prepareGetOptions(); |
|
140
|
|
|
|
|
141
|
|
|
$context = stream_context_create($options); |
|
142
|
|
|
$result = file_get_contents($url, false, $context); |
|
143
|
|
|
|
|
144
|
|
|
$this->assertErrors($result, 'Request have returned warnings/errors'); |
|
145
|
|
|
|
|
146
|
|
|
$jsonResult = json_decode($result); |
|
147
|
|
|
|
|
148
|
|
|
return $jsonResult; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Method returns test data |
|
153
|
|
|
* |
|
154
|
|
|
* @return array Test data |
|
155
|
|
|
*/ |
|
156
|
|
|
protected function getUserData(): array |
|
157
|
|
|
{ |
|
158
|
|
|
return [ |
|
159
|
|
|
'login' => '[email protected]', // TODO store this data in configs |
|
160
|
|
|
'password' => 'root' |
|
161
|
|
|
]; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Method performs valid connect. |
|
166
|
|
|
* |
|
167
|
|
|
* @return mixed Result of the connection. |
|
168
|
|
|
*/ |
|
169
|
|
|
protected function validConnect() |
|
170
|
|
|
{ |
|
171
|
|
|
$data = $this->getUserData(); |
|
172
|
|
|
|
|
173
|
|
|
$url = $this->serverPath . '/connect/'; |
|
174
|
|
|
|
|
175
|
|
|
$result = $this->postHttpRequest($data, $url); |
|
176
|
|
|
|
|
177
|
|
|
if (isset($result->session_id) !== false) { |
|
178
|
|
|
$this->sessionId = $result->session_id; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
return $result; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Testing API connection. |
|
186
|
|
|
*/ |
|
187
|
|
|
public function testValidConnect() |
|
188
|
|
|
{ |
|
189
|
|
|
// authorization |
|
190
|
|
|
$result = $this->validConnect(); |
|
191
|
|
|
|
|
192
|
|
|
$this->assertNotEquals($result, null, 'Connection failed'); |
|
193
|
|
|
|
|
194
|
|
|
if (isset($result->session_id) === false) { |
|
195
|
|
|
$this->assertEquals(true, false, 'Field "session_id" was not set'); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
$this->sessionId = $result->session_id; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Testing API invalid connection. |
|
203
|
|
|
*/ |
|
204
|
|
|
public function testInvalidConnect() |
|
205
|
|
|
{ |
|
206
|
|
|
// setup |
|
207
|
|
|
$data = $this->getUserData(); |
|
208
|
|
|
$data['password'] = '1234'; |
|
209
|
|
|
$url = $this->serverPath . '/connect/'; |
|
210
|
|
|
|
|
211
|
|
|
// test body |
|
212
|
|
|
$result = $this->postHttpRequest($data, $url); |
|
213
|
|
|
|
|
214
|
|
|
// assertions |
|
215
|
|
|
$this->assertTrue(isset($result->message)); |
|
216
|
|
|
$this->assertTrue(isset($result->code)); |
|
217
|
|
|
$this->assertTrue($result->code == -1 || $result->code == 4); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Testing setting valid token. |
|
222
|
|
|
*/ |
|
223
|
|
|
public function testSetValidToken() |
|
224
|
|
|
{ |
|
225
|
|
|
$this->testValidConnect(); |
|
226
|
|
|
|
|
227
|
|
|
$data = [ |
|
228
|
|
|
'token' => $this->sessionId |
|
229
|
|
|
]; |
|
230
|
|
|
|
|
231
|
|
|
$url = $this->serverPath . '/token/' . $this->sessionId . '/'; |
|
232
|
|
|
|
|
233
|
|
|
$result = $this->postHttpRequest($data, $url); |
|
234
|
|
|
|
|
235
|
|
|
$this->assertEquals(isset($result->session_id), true, 'Connection failed'); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* Testing setting invalid token. |
|
240
|
|
|
*/ |
|
241
|
|
|
public function testSetInvalidToken() |
|
242
|
|
|
{ |
|
243
|
|
|
try { |
|
244
|
|
|
$this->testValidConnect(); |
|
245
|
|
|
|
|
246
|
|
|
$data = [ |
|
247
|
|
|
'token' => '' |
|
248
|
|
|
]; |
|
249
|
|
|
|
|
250
|
|
|
$url = $this->serverPath . '/token/unexisting/'; |
|
251
|
|
|
|
|
252
|
|
|
$this->postHttpRequest($data, $url); |
|
253
|
|
|
} catch (\Exception $e) { |
|
254
|
|
|
// set token method either throws exception or not |
|
255
|
|
|
// both is correct behaviour |
|
256
|
|
|
$this->assertEquals($e->getMessage(), 'Invalid session token', 'Invalid error message'); |
|
257
|
|
|
$this->assertEquals($e->getCode(), 2, 'Invalid error code'); |
|
258
|
|
|
} |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* Testing login under another user |
|
263
|
|
|
*/ |
|
264
|
|
|
public function testLoginAs() |
|
265
|
|
|
{ |
|
266
|
|
|
// setup |
|
267
|
|
|
$this->testValidConnect(); |
|
268
|
|
|
|
|
269
|
|
|
// test body |
|
270
|
|
|
$data = [ |
|
271
|
|
|
'login' => '[email protected]' |
|
272
|
|
|
]; |
|
273
|
|
|
|
|
274
|
|
|
$url = $this->serverPath . '/login-as/'; |
|
275
|
|
|
|
|
276
|
|
|
$this->postHttpRequest($data, $url); |
|
277
|
|
|
|
|
278
|
|
|
// assertions |
|
279
|
|
|
$url = $this->serverPath . '/self/login/'; |
|
280
|
|
|
|
|
281
|
|
|
$result = $this->getHttpRequest($url); |
|
282
|
|
|
|
|
283
|
|
|
$this->assertEquals( |
|
284
|
|
|
'[email protected]', |
|
285
|
|
|
\Mezon\Functional\Fetcher::getField($result, 'login'), |
|
286
|
|
|
'Session user must be [email protected]'); |
|
287
|
|
|
} |
|
288
|
|
|
} |
|
289
|
|
|
|