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
|
|
View Code Duplication |
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
|
|
|
public function testFullyAuthenticatedUserAccessToUserName() |
100
|
|
|
{ |
101
|
|
|
$expected = [ |
102
|
|
|
'data' => [ |
103
|
|
|
'user' => [ |
104
|
|
|
'name' => 'Dan', |
105
|
|
|
], |
106
|
|
|
], |
107
|
|
|
]; |
108
|
|
|
|
109
|
|
|
$this->assertResponse($this->userNameQuery, $expected, static::USER_RYAN, 'access'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function testNotAuthenticatedUserAccessToUserRoles() |
113
|
|
|
{ |
114
|
|
|
$this->assertResponse($this->userRolesQuery, $this->expectedFailedUserRoles(), static::ANONYMOUS_USER, 'access'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testAuthenticatedUserAccessToUserRolesWithoutEnoughRights() |
118
|
|
|
{ |
119
|
|
|
$this->assertResponse($this->userRolesQuery, $this->expectedFailedUserRoles(), static::USER_RYAN, 'access'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function testUserWithCorrectRightsAccessToUserRoles() |
123
|
|
|
{ |
124
|
|
|
$expected = [ |
125
|
|
|
'data' => [ |
126
|
|
|
'user' => [ |
127
|
|
|
'roles' => ['ROLE_USER'], |
128
|
|
|
], |
129
|
|
|
], |
130
|
|
|
]; |
131
|
|
|
|
132
|
|
|
$this->assertResponse($this->userRolesQuery, $expected, static::USER_ADMIN, 'access'); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
View Code Duplication |
public function testUserForbiddenField() |
|
|
|
|
136
|
|
|
{ |
137
|
|
|
$expected = [ |
138
|
|
|
'data' => [ |
139
|
|
|
'user' => null, |
140
|
|
|
], |
141
|
|
|
'extensions' => [ |
142
|
|
|
'warnings' => [ |
143
|
|
|
[ |
144
|
|
|
'message' => 'Access denied to this field.', |
145
|
|
|
'locations' => [ |
146
|
|
|
[ |
147
|
|
|
'line' => 3, |
148
|
|
|
'column' => 5, |
149
|
|
|
], |
150
|
|
|
], |
151
|
|
|
'path' => ['user', 'forbidden'], |
152
|
|
|
], |
153
|
|
|
], |
154
|
|
|
], |
155
|
|
|
]; |
156
|
|
|
|
157
|
|
|
$query = <<<'EOF' |
158
|
|
|
query MyQuery { |
159
|
|
|
user { |
160
|
|
|
forbidden |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
EOF; |
164
|
|
|
|
165
|
|
|
$this->assertResponse($query, $expected, static::USER_ADMIN, 'access'); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function testUserAccessToUserFriends() |
169
|
|
|
{ |
170
|
|
|
$expected = [ |
171
|
|
|
'data' => [ |
172
|
|
|
'user' => [ |
173
|
|
|
'friends' => [ |
174
|
|
|
'edges' => [ |
175
|
|
|
['node' => ['name' => 'Nick']], |
176
|
|
|
['node' => null], |
177
|
|
|
], |
178
|
|
|
], |
179
|
|
|
], |
180
|
|
|
], |
181
|
|
|
]; |
182
|
|
|
|
183
|
|
|
$this->assertResponse($this->userFriendsQuery, $expected, static::USER_ADMIN, 'access'); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function testMutationAllowedUser() |
187
|
|
|
{ |
188
|
|
|
$result = 123; |
189
|
|
|
|
190
|
|
|
$expected = [ |
191
|
|
|
'data' => [ |
192
|
|
|
'simpleMutationWithThunkFields' => [ |
193
|
|
|
'result' => $result, |
194
|
|
|
'clientMutationId' => 'bac', |
195
|
|
|
], |
196
|
|
|
], |
197
|
|
|
]; |
198
|
|
|
|
199
|
|
|
$this->assertResponse(sprintf($this->simpleMutationWithThunkQuery, $result), $expected, static::USER_ADMIN, 'access'); |
200
|
|
|
$this->assertTrue(SimpleMutationWithThunkFieldsMutation::hasMutate(true)); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
public function testMutationAllowedButNoRightsToDisplayPayload() |
204
|
|
|
{ |
205
|
|
|
$expected = [ |
206
|
|
|
'data' => [ |
207
|
|
|
'simpleMutationWithThunkFields' => [ |
208
|
|
|
'result' => null, |
209
|
|
|
'clientMutationId' => 'bac', |
210
|
|
|
], |
211
|
|
|
], |
212
|
|
|
'extensions' => [ |
213
|
|
|
'warnings' => [ |
214
|
|
|
[ |
215
|
|
|
'message' => 'Access denied to this field.', |
216
|
|
|
'locations' => [ |
217
|
|
|
[ |
218
|
|
|
'line' => 3, |
219
|
|
|
'column' => 5, |
220
|
|
|
], |
221
|
|
|
], |
222
|
|
|
'path' => ['simpleMutationWithThunkFields', 'result'], |
223
|
|
|
], |
224
|
|
|
], |
225
|
|
|
], |
226
|
|
|
]; |
227
|
|
|
|
228
|
|
|
$this->assertResponse(sprintf($this->simpleMutationWithThunkQuery, 321), $expected, static::USER_ADMIN, 'access'); |
229
|
|
|
$this->assertTrue(SimpleMutationWithThunkFieldsMutation::hasMutate(true)); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
public function testMutationNotAllowedUser() |
233
|
|
|
{ |
234
|
|
|
$expected = [ |
235
|
|
|
'data' => [ |
236
|
|
|
'simpleMutationWithThunkFields' => null, |
237
|
|
|
], |
238
|
|
|
'errors' => [ |
239
|
|
|
[ |
240
|
|
|
'message' => 'Access denied to this field.', |
241
|
|
|
'locations' => [ |
242
|
|
|
[ |
243
|
|
|
'line' => 2, |
244
|
|
|
'column' => 3, |
245
|
|
|
], |
246
|
|
|
], |
247
|
|
|
'path' => ['simpleMutationWithThunkFields'], |
248
|
|
|
], |
249
|
|
|
], |
250
|
|
|
]; |
251
|
|
|
|
252
|
|
|
$this->assertResponse(sprintf($this->simpleMutationWithThunkQuery, 123), $expected, static::USER_RYAN, 'access'); |
253
|
|
|
$this->assertFalse(SimpleMutationWithThunkFieldsMutation::hasMutate(true)); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
private function expectedFailedUserRoles() |
257
|
|
|
{ |
258
|
|
|
return [ |
259
|
|
|
'data' => [ |
260
|
|
|
'user' => [ |
261
|
|
|
'roles' => [0 => null], |
262
|
|
|
], |
263
|
|
|
], |
264
|
|
|
]; |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.