1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the OverblogGraphQLBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Overblog <http://github.com/overblog/> |
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 Overblog\GraphQLBundle\Tests\Functional\Security; |
13
|
|
|
|
14
|
|
|
use Overblog\GraphQLBundle\Tests\Functional\app\Mutation\SimpleMutationWithThunkFieldsMutation; |
15
|
|
|
use Overblog\GraphQLBundle\Tests\Functional\TestCase; |
16
|
|
|
|
17
|
|
|
class AccessTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
const USER_RYAN = 'ryan'; |
20
|
|
|
const USER_ADMIN = 'admin'; |
21
|
|
|
const ANONYMOUS_USER = null; |
22
|
|
|
|
23
|
|
|
private $userNameQuery = 'query MyQuery { user { name } }'; |
24
|
|
|
|
25
|
|
|
private $userRolesQuery = 'query MyQuery { user { roles } }'; |
26
|
|
|
|
27
|
|
|
private $userIsEnabledQuery = 'query MyQuery { user { isEnabled } }'; |
28
|
|
|
|
29
|
|
|
private $userFriendsQuery = <<<EOF |
30
|
|
|
query MyQuery { |
31
|
|
|
user { |
32
|
|
|
friends(first: 2) { |
33
|
|
|
edges { |
34
|
|
|
node { |
35
|
|
|
name |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
EOF; |
42
|
|
|
|
43
|
|
|
private $simpleMutationWithThunkQuery = <<<EOF |
44
|
|
|
mutation M { |
45
|
|
|
simpleMutationWithThunkFields(input: {inputData: %d, clientMutationId: "bac"}) { |
46
|
|
|
result |
47
|
|
|
clientMutationId |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
EOF; |
51
|
|
|
|
52
|
|
View Code Duplication |
public function testNotAuthenticatedUserAccessToUserName() |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
$expected = [ |
55
|
|
|
'data' => [ |
56
|
|
|
'user' => [ |
57
|
|
|
'name' => null, |
58
|
|
|
], |
59
|
|
|
], |
60
|
|
|
'extensions' => [ |
61
|
|
|
'warnings' => [ |
62
|
|
|
[ |
63
|
|
|
'message' => 'Access denied to this field.', |
64
|
|
|
'locations' => [['line' => 1, 'column' => 24]], |
65
|
|
|
], |
66
|
|
|
], |
67
|
|
|
], |
68
|
|
|
]; |
69
|
|
|
|
70
|
|
|
$this->assertResponse($this->userNameQuery, $expected, static::ANONYMOUS_USER); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testFullyAuthenticatedUserAccessToUserName() |
74
|
|
|
{ |
75
|
|
|
$expected = [ |
76
|
|
|
'data' => [ |
77
|
|
|
'user' => [ |
78
|
|
|
'name' => 'Dan', |
79
|
|
|
], |
80
|
|
|
], |
81
|
|
|
]; |
82
|
|
|
|
83
|
|
|
$this->assertResponse($this->userNameQuery, $expected, static::USER_RYAN); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testNotAuthenticatedUserAccessToUserRoles() |
87
|
|
|
{ |
88
|
|
|
$this->assertResponse($this->userRolesQuery, $this->expectedFailedUserRoles(), static::ANONYMOUS_USER); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testAuthenticatedUserAccessToUserRolesWithoutEnoughRights() |
92
|
|
|
{ |
93
|
|
|
$this->assertResponse($this->userRolesQuery, $this->expectedFailedUserRoles(), static::USER_RYAN); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function testUserWithCorrectRightsAccessToUserRoles() |
97
|
|
|
{ |
98
|
|
|
$expected = [ |
99
|
|
|
'data' => [ |
100
|
|
|
'user' => [ |
101
|
|
|
'roles' => ['ROLE_USER'], |
102
|
|
|
], |
103
|
|
|
], |
104
|
|
|
]; |
105
|
|
|
|
106
|
|
|
$this->assertResponse($this->userRolesQuery, $expected, static::USER_ADMIN); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
View Code Duplication |
public function testUserForbiddenField() |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
$expected = [ |
112
|
|
|
'data' => [ |
113
|
|
|
'user' => null, |
114
|
|
|
], |
115
|
|
|
'extensions' => [ |
116
|
|
|
'warnings' => [ |
117
|
|
|
[ |
118
|
|
|
'message' => 'Access denied to this field.', |
119
|
|
|
'locations' => [ |
120
|
|
|
[ |
121
|
|
|
'line' => 3, |
122
|
|
|
'column' => 5, |
123
|
|
|
], |
124
|
|
|
], |
125
|
|
|
], |
126
|
|
|
], |
127
|
|
|
], |
128
|
|
|
]; |
129
|
|
|
|
130
|
|
|
$query = <<<EOF |
131
|
|
|
query MyQuery { |
132
|
|
|
user { |
133
|
|
|
forbidden |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
EOF; |
137
|
|
|
|
138
|
|
|
$this->assertResponse($query, $expected, static::USER_ADMIN); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function testUserAccessToUserFriends() |
142
|
|
|
{ |
143
|
|
|
$expected = [ |
144
|
|
|
'data' => [ |
145
|
|
|
'user' => [ |
146
|
|
|
'friends' => [ |
147
|
|
|
'edges' => [ |
148
|
|
|
['node' => ['name' => 'Nick']], |
149
|
|
|
['node' => null], |
150
|
|
|
], |
151
|
|
|
], |
152
|
|
|
], |
153
|
|
|
], |
154
|
|
|
]; |
155
|
|
|
|
156
|
|
|
$this->assertResponse($this->userFriendsQuery, $expected, static::USER_ADMIN); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
View Code Duplication |
public function testUserAccessToUserIsEnabledWithExpressionLanguageEvaluationFailed() |
|
|
|
|
160
|
|
|
{ |
161
|
|
|
$expected = [ |
162
|
|
|
'data' => [ |
163
|
|
|
'user' => [ |
164
|
|
|
'isEnabled' => null, |
165
|
|
|
], |
166
|
|
|
], |
167
|
|
|
'extensions' => [ |
168
|
|
|
'warnings' => [ |
169
|
|
|
[ |
170
|
|
|
'message' => 'Access denied to this field.', |
171
|
|
|
'locations' => [['line' => 1, 'column' => 24]], |
172
|
|
|
], |
173
|
|
|
], |
174
|
|
|
], |
175
|
|
|
]; |
176
|
|
|
|
177
|
|
|
$this->assertResponse($this->userIsEnabledQuery, $expected, static::USER_ADMIN); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function testMutationAllowedUser() |
181
|
|
|
{ |
182
|
|
|
$result = 123; |
183
|
|
|
|
184
|
|
|
$expected = [ |
185
|
|
|
'data' => [ |
186
|
|
|
'simpleMutationWithThunkFields' => [ |
187
|
|
|
'result' => $result, |
188
|
|
|
'clientMutationId' => 'bac', |
189
|
|
|
], |
190
|
|
|
], |
191
|
|
|
]; |
192
|
|
|
|
193
|
|
|
$this->assertResponse(sprintf($this->simpleMutationWithThunkQuery, $result), $expected, static::USER_ADMIN); |
194
|
|
|
$this->assertTrue(SimpleMutationWithThunkFieldsMutation::hasMutate(true)); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function testMutationAllowedButNoRightsToDisplayPayload() |
198
|
|
|
{ |
199
|
|
|
$expected = [ |
200
|
|
|
'data' => [ |
201
|
|
|
'simpleMutationWithThunkFields' => [ |
202
|
|
|
'result' => null, |
203
|
|
|
'clientMutationId' => 'bac', |
204
|
|
|
], |
205
|
|
|
], |
206
|
|
|
'extensions' => [ |
207
|
|
|
'warnings' => [ |
208
|
|
|
[ |
209
|
|
|
'message' => 'Access denied to this field.', |
210
|
|
|
'locations' => [ |
211
|
|
|
[ |
212
|
|
|
'line' => 3, |
213
|
|
|
'column' => 5, |
214
|
|
|
], |
215
|
|
|
], |
216
|
|
|
], |
217
|
|
|
], |
218
|
|
|
], |
219
|
|
|
]; |
220
|
|
|
|
221
|
|
|
$this->assertResponse(sprintf($this->simpleMutationWithThunkQuery, 321), $expected, static::USER_ADMIN); |
222
|
|
|
$this->assertTrue(SimpleMutationWithThunkFieldsMutation::hasMutate(true)); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
public function testMutationNotAllowedUser() |
226
|
|
|
{ |
227
|
|
|
$expected = [ |
228
|
|
|
'data' => [ |
229
|
|
|
'simpleMutationWithThunkFields' => null, |
230
|
|
|
], |
231
|
|
|
'errors' => [ |
232
|
|
|
[ |
233
|
|
|
'message' => 'Access denied to this field.', |
234
|
|
|
'locations' => [ |
235
|
|
|
[ |
236
|
|
|
'line' => 2, |
237
|
|
|
'column' => 3, |
238
|
|
|
], |
239
|
|
|
], |
240
|
|
|
], |
241
|
|
|
], |
242
|
|
|
]; |
243
|
|
|
|
244
|
|
|
$this->assertResponse(sprintf($this->simpleMutationWithThunkQuery, 123), $expected, static::USER_RYAN); |
245
|
|
|
$this->assertFalse(SimpleMutationWithThunkFieldsMutation::hasMutate(true)); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
private function expectedFailedUserRoles() |
249
|
|
|
{ |
250
|
|
|
return [ |
251
|
|
|
'data' => [ |
252
|
|
|
'user' => [ |
253
|
|
|
'roles' => [0 => null], |
254
|
|
|
], |
255
|
|
|
], |
256
|
|
|
]; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
View Code Duplication |
private static function assertResponse($query, array $expected, $username) |
|
|
|
|
260
|
|
|
{ |
261
|
|
|
$client = self::createClientAuthenticated($username); |
262
|
|
|
$client->request('GET', '/', ['query' => $query]); |
263
|
|
|
|
264
|
|
|
$result = $client->getResponse()->getContent(); |
265
|
|
|
|
266
|
|
|
static::assertEquals($expected, json_decode($result, true), $result); |
267
|
|
|
|
268
|
|
|
return $client; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
private static function createClientAuthenticated($username) |
272
|
|
|
{ |
273
|
|
|
$client = static::createClient(['test_case' => 'access']); |
274
|
|
|
|
275
|
|
|
if ($username) { |
276
|
|
|
$client->setServerParameters([ |
277
|
|
|
'PHP_AUTH_USER' => $username, |
278
|
|
|
'PHP_AUTH_PW' => '123', |
279
|
|
|
]); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
return $client; |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
|
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.