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 Composer\Autoload\ClassLoader; |
15
|
|
|
use Overblog\GraphQLBundle\Tests\Functional\App\Mutation\SimpleMutationWithThunkFieldsMutation; |
16
|
|
|
use Overblog\GraphQLBundle\Tests\Functional\TestCase; |
17
|
|
|
use Symfony\Component\HttpKernel\Kernel; |
18
|
|
|
|
19
|
|
|
class AccessTest extends TestCase |
20
|
|
|
{ |
21
|
|
|
/** @var ClassLoader */ |
22
|
|
|
private $loader; |
23
|
|
|
|
24
|
|
|
private $userNameQuery = 'query { user { name } }'; |
25
|
|
|
|
26
|
|
|
private $userRolesQuery = 'query { user { roles } }'; |
27
|
|
|
|
28
|
|
|
private $userIsEnabledQuery = 'query { user { isEnabled } }'; |
29
|
|
|
|
30
|
|
|
private $userFriendsQuery = <<<'EOF' |
31
|
|
|
query { |
32
|
|
|
user { |
33
|
|
|
friends(first: 2) { |
34
|
|
|
edges { |
35
|
|
|
node { |
36
|
|
|
name |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
EOF; |
43
|
|
|
|
44
|
|
|
private $simpleMutationWithThunkQuery = <<<'EOF' |
45
|
|
|
mutation M { |
46
|
|
|
simpleMutationWithThunkFields(input: {inputData: %d, clientMutationId: "bac"}) { |
47
|
|
|
result |
48
|
|
|
clientMutationId |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
EOF; |
52
|
|
|
|
53
|
|
|
public function setUp() |
54
|
|
|
{ |
55
|
|
|
parent::setUp(); |
56
|
|
|
// load types |
57
|
|
|
/** @var ClassLoader $loader */ |
58
|
|
|
$loader = new ClassLoader(); |
59
|
|
|
$loader->addPsr4( |
60
|
|
|
'Overblog\\GraphQLBundle\\Access\\__DEFINITIONS__\\', |
61
|
|
|
'/tmp/OverblogGraphQLBundle/'.Kernel::VERSION.'/access/cache/overbloggraphbundletestaccess/overblog/graphql-bundle/__definitions__' |
62
|
|
|
); |
63
|
|
|
$loader->register(); |
64
|
|
|
$this->loader = $loader; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @expectedException \RuntimeException |
69
|
|
|
* @expectedExceptionMessage Type class "Overblog\\GraphQLBundle\\Access\\__DEFINITIONS__\\PageInfoType" not found. If you are using your own classLoader verify the path and the namespace please. |
70
|
|
|
*/ |
71
|
|
|
public function testCustomClassLoaderNotRegister() |
72
|
|
|
{ |
73
|
|
|
$this->loader->unregister(); |
74
|
|
|
$this->assertResponse($this->userNameQuery, [], static::ANONYMOUS_USER, 'access'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testNotAuthenticatedUserAccessToUserName() |
78
|
|
|
{ |
79
|
|
|
$expected = [ |
80
|
|
|
'data' => [ |
81
|
|
|
'user' => [ |
82
|
|
|
'name' => null, |
83
|
|
|
], |
84
|
|
|
], |
85
|
|
|
'extensions' => [ |
86
|
|
|
'warnings' => [ |
87
|
|
|
[ |
88
|
|
|
'message' => 'Access denied to this field.', |
89
|
|
|
'locations' => [['line' => 1, 'column' => 16]], |
90
|
|
|
'path' => ['user', 'name'], |
91
|
|
|
], |
92
|
|
|
], |
93
|
|
|
], |
94
|
|
|
]; |
95
|
|
|
|
96
|
|
|
$this->assertResponse($this->userNameQuery, $expected, static::ANONYMOUS_USER, 'access'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
View Code Duplication |
public function testNonAuthenticatedUserAccessSecuredFieldWhichInitiallyResolvesToArray(): void |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
$expected = [ |
102
|
|
|
'data' => [ |
103
|
|
|
'youShallNotSeeThisUnauthenticated' => null, |
104
|
|
|
], |
105
|
|
|
'extensions' => [ |
106
|
|
|
'warnings' => [ |
107
|
|
|
[ |
108
|
|
|
'message' => 'Access denied to this field.', |
109
|
|
|
'locations' => [ |
110
|
|
|
[ |
111
|
|
|
'line' => 2, |
112
|
|
|
'column' => 3, |
113
|
|
|
], |
114
|
|
|
], |
115
|
|
|
'path' => ['youShallNotSeeThisUnauthenticated'], |
116
|
|
|
], |
117
|
|
|
], |
118
|
|
|
], |
119
|
|
|
]; |
120
|
|
|
$query = <<<'EOF' |
121
|
|
|
{ |
122
|
|
|
youShallNotSeeThisUnauthenticated { |
123
|
|
|
secretValue |
124
|
|
|
youAreAuthenticated |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
EOF; |
128
|
|
|
$this->assertResponse($query, $expected, static::ANONYMOUS_USER, 'access'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function testFullyAuthenticatedUserAccessToUserName() |
132
|
|
|
{ |
133
|
|
|
$expected = [ |
134
|
|
|
'data' => [ |
135
|
|
|
'user' => [ |
136
|
|
|
'name' => 'Dan', |
137
|
|
|
], |
138
|
|
|
], |
139
|
|
|
]; |
140
|
|
|
|
141
|
|
|
$this->assertResponse($this->userNameQuery, $expected, static::USER_RYAN, 'access'); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function testNotAuthenticatedUserAccessToUserRoles() |
145
|
|
|
{ |
146
|
|
|
$this->assertResponse($this->userRolesQuery, $this->expectedFailedUserRoles(), static::ANONYMOUS_USER, 'access'); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function testAuthenticatedUserAccessToUserRolesWithoutEnoughRights() |
150
|
|
|
{ |
151
|
|
|
$this->assertResponse($this->userRolesQuery, $this->expectedFailedUserRoles(), static::USER_RYAN, 'access'); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function testUserWithCorrectRightsAccessToUserRoles() |
155
|
|
|
{ |
156
|
|
|
$expected = [ |
157
|
|
|
'data' => [ |
158
|
|
|
'user' => [ |
159
|
|
|
'roles' => ['ROLE_USER'], |
160
|
|
|
], |
161
|
|
|
], |
162
|
|
|
]; |
163
|
|
|
|
164
|
|
|
$this->assertResponse($this->userRolesQuery, $expected, static::USER_ADMIN, 'access'); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
View Code Duplication |
public function testUserForbiddenField() |
|
|
|
|
168
|
|
|
{ |
169
|
|
|
$expected = [ |
170
|
|
|
'data' => [ |
171
|
|
|
'user' => null, |
172
|
|
|
], |
173
|
|
|
'extensions' => [ |
174
|
|
|
'warnings' => [ |
175
|
|
|
[ |
176
|
|
|
'message' => 'Access denied to this field.', |
177
|
|
|
'locations' => [ |
178
|
|
|
[ |
179
|
|
|
'line' => 3, |
180
|
|
|
'column' => 5, |
181
|
|
|
], |
182
|
|
|
], |
183
|
|
|
'path' => ['user', 'forbidden'], |
184
|
|
|
], |
185
|
|
|
], |
186
|
|
|
], |
187
|
|
|
]; |
188
|
|
|
|
189
|
|
|
$query = <<<'EOF' |
190
|
|
|
query MyQuery { |
191
|
|
|
user { |
192
|
|
|
forbidden |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
EOF; |
196
|
|
|
|
197
|
|
|
$this->assertResponse($query, $expected, static::USER_ADMIN, 'access'); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function testUserAccessToUserFriends() |
201
|
|
|
{ |
202
|
|
|
$expected = [ |
203
|
|
|
'data' => [ |
204
|
|
|
'user' => [ |
205
|
|
|
'friends' => [ |
206
|
|
|
'edges' => [ |
207
|
|
|
['node' => ['name' => 'Nick']], |
208
|
|
|
['node' => null], |
209
|
|
|
], |
210
|
|
|
], |
211
|
|
|
], |
212
|
|
|
], |
213
|
|
|
]; |
214
|
|
|
|
215
|
|
|
$this->assertResponse($this->userFriendsQuery, $expected, static::USER_ADMIN, 'access'); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function testMutationAllowedUser() |
219
|
|
|
{ |
220
|
|
|
$result = 123; |
221
|
|
|
|
222
|
|
|
$expected = [ |
223
|
|
|
'data' => [ |
224
|
|
|
'simpleMutationWithThunkFields' => [ |
225
|
|
|
'result' => $result, |
226
|
|
|
'clientMutationId' => 'bac', |
227
|
|
|
], |
228
|
|
|
], |
229
|
|
|
]; |
230
|
|
|
|
231
|
|
|
$this->assertResponse(sprintf($this->simpleMutationWithThunkQuery, $result), $expected, static::USER_ADMIN, 'access'); |
232
|
|
|
$this->assertTrue(SimpleMutationWithThunkFieldsMutation::hasMutate(true)); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
public function testMutationAllowedButNoRightsToDisplayPayload() |
236
|
|
|
{ |
237
|
|
|
$expected = [ |
238
|
|
|
'data' => [ |
239
|
|
|
'simpleMutationWithThunkFields' => [ |
240
|
|
|
'result' => null, |
241
|
|
|
'clientMutationId' => 'bac', |
242
|
|
|
], |
243
|
|
|
], |
244
|
|
|
'extensions' => [ |
245
|
|
|
'warnings' => [ |
246
|
|
|
[ |
247
|
|
|
'message' => 'Access denied to this field.', |
248
|
|
|
'locations' => [ |
249
|
|
|
[ |
250
|
|
|
'line' => 3, |
251
|
|
|
'column' => 5, |
252
|
|
|
], |
253
|
|
|
], |
254
|
|
|
'path' => ['simpleMutationWithThunkFields', 'result'], |
255
|
|
|
], |
256
|
|
|
], |
257
|
|
|
], |
258
|
|
|
]; |
259
|
|
|
|
260
|
|
|
$this->assertResponse(sprintf($this->simpleMutationWithThunkQuery, 321), $expected, static::USER_ADMIN, 'access'); |
261
|
|
|
$this->assertTrue(SimpleMutationWithThunkFieldsMutation::hasMutate(true)); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
public function testMutationNotAllowedUser() |
265
|
|
|
{ |
266
|
|
|
$expected = [ |
267
|
|
|
'data' => [ |
268
|
|
|
'simpleMutationWithThunkFields' => null, |
269
|
|
|
], |
270
|
|
|
'errors' => [ |
271
|
|
|
[ |
272
|
|
|
'message' => 'Access denied to this field.', |
273
|
|
|
'locations' => [ |
274
|
|
|
[ |
275
|
|
|
'line' => 2, |
276
|
|
|
'column' => 3, |
277
|
|
|
], |
278
|
|
|
], |
279
|
|
|
'path' => ['simpleMutationWithThunkFields'], |
280
|
|
|
], |
281
|
|
|
], |
282
|
|
|
]; |
283
|
|
|
|
284
|
|
|
$this->assertResponse(sprintf($this->simpleMutationWithThunkQuery, 123), $expected, static::USER_RYAN, 'access'); |
285
|
|
|
$this->assertFalse(SimpleMutationWithThunkFieldsMutation::hasMutate(true)); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
private function expectedFailedUserRoles() |
289
|
|
|
{ |
290
|
|
|
return [ |
291
|
|
|
'data' => [ |
292
|
|
|
'user' => [ |
293
|
|
|
'roles' => [0 => null], |
294
|
|
|
], |
295
|
|
|
], |
296
|
|
|
]; |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
|
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.