|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sylius package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Paweł Jędrzejewski |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sylius\Tests\Controller; |
|
13
|
|
|
|
|
14
|
|
|
use Lakion\ApiTestCase\JsonApiTestCase; |
|
15
|
|
|
use Sylius\Component\Core\Model\AdminUserInterface; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @author Anna Walasek <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class AdminUserApiTest extends JsonApiTestCase |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
private static $authorizedHeaderWithContentType = [ |
|
27
|
|
|
'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ', |
|
28
|
|
|
'CONTENT_TYPE' => 'application/json', |
|
29
|
|
|
]; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
private static $authorizedHeaderWithAccept = [ |
|
35
|
|
|
'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ', |
|
36
|
|
|
'ACCEPT' => 'application/json', |
|
37
|
|
|
]; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @test |
|
41
|
|
|
*/ |
|
42
|
|
|
public function it_denies_an_admin_user_creation_for_not_authenticated_users() |
|
43
|
|
|
{ |
|
44
|
|
|
$this->client->request('POST', '/api/v1/users/'); |
|
45
|
|
|
|
|
46
|
|
|
$response = $this->client->getResponse(); |
|
47
|
|
|
$this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @test |
|
52
|
|
|
*/ |
|
53
|
|
|
public function it_does_not_allow_to_create_an_admin_user_without_specifying_required_data() |
|
54
|
|
|
{ |
|
55
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
|
56
|
|
|
|
|
57
|
|
|
$this->client->request('POST', '/api/v1/users/', [], [], static::$authorizedHeaderWithContentType); |
|
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
$response = $this->client->getResponse(); |
|
60
|
|
|
$this->assertResponse($response, 'admin_user/create_validation_fail_response', Response::HTTP_BAD_REQUEST); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @test |
|
65
|
|
|
*/ |
|
66
|
|
|
public function it_allows_to_create_an_admin_user() |
|
67
|
|
|
{ |
|
68
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
|
69
|
|
|
$this->loadFixturesFromFile('resources/locales.yml'); |
|
70
|
|
|
|
|
71
|
|
|
$data = |
|
72
|
|
|
<<<EOT |
|
73
|
|
|
{ |
|
74
|
|
|
"username": "Barlog", |
|
75
|
|
|
"email": "[email protected]", |
|
76
|
|
|
"plainPassword": "youShallNotPass", |
|
77
|
|
|
"localeCode": "en_US" |
|
78
|
|
|
} |
|
79
|
|
|
EOT; |
|
80
|
|
|
|
|
81
|
|
|
$this->client->request('POST', '/api/v1/users/', [], [], static::$authorizedHeaderWithContentType, $data); |
|
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
$response = $this->client->getResponse(); |
|
84
|
|
|
$this->assertResponse($response, 'admin_user/create_response', Response::HTTP_CREATED); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @test |
|
89
|
|
|
*/ |
|
90
|
|
|
public function it_allows_to_create_an_admin_user_with_not_required_fields() |
|
91
|
|
|
{ |
|
92
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
|
93
|
|
|
$this->loadFixturesFromFile('resources/locales.yml'); |
|
94
|
|
|
|
|
95
|
|
|
$data = |
|
96
|
|
|
<<<EOT |
|
97
|
|
|
{ |
|
98
|
|
|
"firstName": "Orange", |
|
99
|
|
|
"lastName": "Annoying", |
|
100
|
|
|
"username": "Nenene", |
|
101
|
|
|
"email": "[email protected]", |
|
102
|
|
|
"plainPassword": "hejPear!", |
|
103
|
|
|
"localeCode": "en_US", |
|
104
|
|
|
"enabled": "true" |
|
105
|
|
|
} |
|
106
|
|
|
EOT; |
|
107
|
|
|
|
|
108
|
|
|
$this->client->request('POST', '/api/v1/users/', [], [], static::$authorizedHeaderWithContentType, $data); |
|
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
$response = $this->client->getResponse(); |
|
111
|
|
|
$this->assertResponse($response, 'admin_user/create_with_additional_fields_response', Response::HTTP_CREATED); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @test |
|
116
|
|
|
*/ |
|
117
|
|
|
public function it_denies_access_to_an_admin_users_list_for_not_authenticated_user() |
|
118
|
|
|
{ |
|
119
|
|
|
$this->client->request('GET', '/api/v1/users/'); |
|
120
|
|
|
|
|
121
|
|
|
$response = $this->client->getResponse(); |
|
122
|
|
|
$this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @test |
|
127
|
|
|
*/ |
|
128
|
|
|
public function it_allows_to_get_an_admin_users_list() |
|
129
|
|
|
{ |
|
130
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
|
131
|
|
|
$this->loadFixturesFromFile('resources/admin_users.yml'); |
|
132
|
|
|
|
|
133
|
|
|
$this->client->request('GET', '/api/v1/users/', [], [], static::$authorizedHeaderWithAccept); |
|
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
$response = $this->client->getResponse(); |
|
136
|
|
|
$this->assertResponse($response, 'admin_user/index_response', Response::HTTP_OK); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @test |
|
141
|
|
|
*/ |
|
142
|
|
|
public function it_denies_access_to_an_admin_user_details_for_not_authenticated_users() |
|
143
|
|
|
{ |
|
144
|
|
|
$this->client->request('GET', '/api/v1/users/1'); |
|
145
|
|
|
|
|
146
|
|
|
$response = $this->client->getResponse(); |
|
147
|
|
|
$this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @test |
|
152
|
|
|
*/ |
|
153
|
|
|
public function it_returns_not_found_response_when_requesting_details_of_an_admin_user_which_does_not_exist() |
|
154
|
|
|
{ |
|
155
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
|
156
|
|
|
|
|
157
|
|
|
$this->client->request('GET', '/api/v1/users/-1', [], [], static::$authorizedHeaderWithAccept); |
|
|
|
|
|
|
158
|
|
|
|
|
159
|
|
|
$response = $this->client->getResponse(); |
|
160
|
|
|
$this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @test |
|
165
|
|
|
*/ |
|
166
|
|
|
public function it_shows_an_admin_user_details() |
|
167
|
|
|
{ |
|
168
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
|
169
|
|
|
$users = $this->loadFixturesFromFile('resources/admin_users.yml'); |
|
170
|
|
|
|
|
171
|
|
|
$this->client->request('GET', $this->getAdminUserUrl($users['admin1']), [], [], static::$authorizedHeaderWithAccept); |
|
|
|
|
|
|
172
|
|
|
|
|
173
|
|
|
$response = $this->client->getResponse(); |
|
174
|
|
|
$this->assertResponse($response, 'admin_user/show_response', Response::HTTP_OK); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @test |
|
179
|
|
|
*/ |
|
180
|
|
|
public function it_denies_an_admin_user_full_update_for_not_authenticated_users() |
|
181
|
|
|
{ |
|
182
|
|
|
$this->client->request('PUT', '/api/v1/users/1'); |
|
183
|
|
|
|
|
184
|
|
|
$response = $this->client->getResponse(); |
|
185
|
|
|
|
|
186
|
|
|
$this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* @test |
|
191
|
|
|
*/ |
|
192
|
|
|
public function it_returns_not_found_response_when_requesting_full_update_of_an_admin_user_which_does_not_exist() |
|
193
|
|
|
{ |
|
194
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
|
195
|
|
|
|
|
196
|
|
|
$this->client->request('PUT', '/api/v1/users/-1', [], [], static::$authorizedHeaderWithAccept); |
|
|
|
|
|
|
197
|
|
|
|
|
198
|
|
|
$response = $this->client->getResponse(); |
|
199
|
|
|
$this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* @test |
|
204
|
|
|
*/ |
|
205
|
|
|
public function it_does_not_allow_to_update_an_admin_user_fully_without_specifying_required_data() |
|
206
|
|
|
{ |
|
207
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
|
208
|
|
|
$users = $this->loadFixturesFromFile('resources/admin_users.yml'); |
|
209
|
|
|
|
|
210
|
|
|
$this->client->request('PUT', $this->getAdminUserUrl($users['admin1']), [], [], static::$authorizedHeaderWithContentType); |
|
|
|
|
|
|
211
|
|
|
|
|
212
|
|
|
$response = $this->client->getResponse(); |
|
213
|
|
|
$this->assertResponse($response, 'admin_user/update_validation_fail_response', Response::HTTP_BAD_REQUEST); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* @test |
|
218
|
|
|
*/ |
|
219
|
|
|
public function it_allows_to_update_an_admin_user_fully() |
|
220
|
|
|
{ |
|
221
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
|
222
|
|
|
$users = $this->loadFixturesFromFile('resources/admin_users.yml'); |
|
223
|
|
|
$this->loadFixturesFromFile('resources/locales.yml'); |
|
224
|
|
|
|
|
225
|
|
|
$data = |
|
226
|
|
|
<<<EOT |
|
227
|
|
|
{ |
|
228
|
|
|
"firstName": "Orange", |
|
229
|
|
|
"lastName": "Annoying", |
|
230
|
|
|
"username": "Nenene", |
|
231
|
|
|
"email": "[email protected]", |
|
232
|
|
|
"plainPassword": "hejPear!", |
|
233
|
|
|
"localeCode": "fr_FR", |
|
234
|
|
|
"enabled": "true" |
|
235
|
|
|
} |
|
236
|
|
|
EOT; |
|
237
|
|
|
|
|
238
|
|
|
$this->client->request('PUT', $this->getAdminUserUrl($users['admin1']), [], [], static::$authorizedHeaderWithContentType, $data); |
|
|
|
|
|
|
239
|
|
|
|
|
240
|
|
|
$response = $this->client->getResponse(); |
|
241
|
|
|
$this->assertResponseCode($response, Response::HTTP_NO_CONTENT); |
|
242
|
|
|
|
|
243
|
|
|
$this->client->request('GET', $this->getAdminUserUrl($users['admin1']), [], [], static::$authorizedHeaderWithAccept); |
|
|
|
|
|
|
244
|
|
|
|
|
245
|
|
|
$response = $this->client->getResponse(); |
|
246
|
|
|
$this->assertResponse($response, 'admin_user/update_response', Response::HTTP_OK); |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* @test |
|
251
|
|
|
*/ |
|
252
|
|
|
public function it_returns_not_found_response_when_requesting_partial_update_of_an_admin_user_which_does_not_exist() |
|
253
|
|
|
{ |
|
254
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
|
255
|
|
|
|
|
256
|
|
|
$this->client->request('PATCH', '/api/v1/users/-1', [], [], static::$authorizedHeaderWithAccept); |
|
|
|
|
|
|
257
|
|
|
|
|
258
|
|
|
$response = $this->client->getResponse(); |
|
259
|
|
|
$this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND); |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* @test |
|
264
|
|
|
*/ |
|
265
|
|
|
public function it_allows_to_update_an_admin_user_partially() |
|
266
|
|
|
{ |
|
267
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
|
268
|
|
|
$users = $this->loadFixturesFromFile('resources/admin_users.yml'); |
|
269
|
|
|
|
|
270
|
|
|
$data = |
|
271
|
|
|
<<<EOT |
|
272
|
|
|
{ |
|
273
|
|
|
"firstName": "John", |
|
274
|
|
|
"lastName": "Doe" |
|
275
|
|
|
} |
|
276
|
|
|
EOT; |
|
277
|
|
|
|
|
278
|
|
|
$this->client->request('PATCH', $this->getAdminUserUrl($users['admin1']), [], [], static::$authorizedHeaderWithContentType, $data); |
|
|
|
|
|
|
279
|
|
|
|
|
280
|
|
|
$response = $this->client->getResponse(); |
|
281
|
|
|
$this->assertResponseCode($response, Response::HTTP_NO_CONTENT); |
|
282
|
|
|
|
|
283
|
|
|
$this->client->request('GET', $this->getAdminUserUrl($users['admin1']), [], [], static::$authorizedHeaderWithAccept); |
|
|
|
|
|
|
284
|
|
|
|
|
285
|
|
|
$response = $this->client->getResponse(); |
|
286
|
|
|
$this->assertResponse($response, 'admin_user/partial_update_response', Response::HTTP_OK); |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
/** |
|
290
|
|
|
* @test |
|
291
|
|
|
*/ |
|
292
|
|
|
public function it_denies_an_admin_user_deletion_for_not_authenticated_users() |
|
293
|
|
|
{ |
|
294
|
|
|
$this->client->request('DELETE', '/api/v1/users/1'); |
|
295
|
|
|
|
|
296
|
|
|
$response = $this->client->getResponse(); |
|
297
|
|
|
$this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED); |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
/** |
|
301
|
|
|
* @test |
|
302
|
|
|
*/ |
|
303
|
|
|
public function it_returns_not_found_response_when_requesting_deletion_of_an_admin_user_which_does_not_exist() |
|
304
|
|
|
{ |
|
305
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
|
306
|
|
|
|
|
307
|
|
|
$this->client->request('DELETE', '/api/v1/users/-1', [], [], static::$authorizedHeaderWithAccept); |
|
|
|
|
|
|
308
|
|
|
|
|
309
|
|
|
$response = $this->client->getResponse(); |
|
310
|
|
|
$this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND); |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
/** |
|
314
|
|
|
* @test |
|
315
|
|
|
*/ |
|
316
|
|
|
public function it_allows_to_delete_an_admin_user() |
|
317
|
|
|
{ |
|
318
|
|
|
$this->loadFixturesFromFile('authentication/api_administrator.yml'); |
|
319
|
|
|
$users = $this->loadFixturesFromFile('resources/admin_users.yml'); |
|
320
|
|
|
|
|
321
|
|
|
$this->client->request('DELETE', $this->getAdminUserUrl($users['admin1']), [], [], static::$authorizedHeaderWithContentType); |
|
|
|
|
|
|
322
|
|
|
|
|
323
|
|
|
$response = $this->client->getResponse(); |
|
324
|
|
|
$this->assertResponseCode($response, Response::HTTP_NO_CONTENT); |
|
325
|
|
|
|
|
326
|
|
|
$this->client->request('GET', $this->getAdminUserUrl($users['admin1']), [], [], static::$authorizedHeaderWithAccept); |
|
|
|
|
|
|
327
|
|
|
|
|
328
|
|
|
$response = $this->client->getResponse(); |
|
329
|
|
|
$this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND); |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
/** |
|
333
|
|
|
* @test |
|
334
|
|
|
*/ |
|
335
|
|
|
public function it_does_not_allow_to_delete_current_logged_an_admin_user() |
|
336
|
|
|
{ |
|
337
|
|
|
$user = $this->loadFixturesFromFile('authentication/api_administrator.yml'); |
|
338
|
|
|
$this->loadFixturesFromFile('resources/admin_users.yml'); |
|
339
|
|
|
|
|
340
|
|
|
$this->client->request('DELETE', $this->getAdminUserUrl($user['admin']), [], [], static::$authorizedHeaderWithContentType); |
|
|
|
|
|
|
341
|
|
|
|
|
342
|
|
|
$response = $this->client->getResponse(); |
|
343
|
|
|
$this->assertResponse($response, 'admin_user/deletion_fail_response', Response::HTTP_UNPROCESSABLE_ENTITY); |
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
|
|
/** |
|
347
|
|
|
* @param AdminUserInterface $user |
|
348
|
|
|
* |
|
349
|
|
|
* @return string |
|
350
|
|
|
*/ |
|
351
|
|
|
private function getAdminUserUrl(AdminUserInterface $user) |
|
352
|
|
|
{ |
|
353
|
|
|
return '/api/v1/users/' . $user->getId(); |
|
354
|
|
|
} |
|
355
|
|
|
} |
|
356
|
|
|
|
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: