Skip to content

Commit 0fc439c

Browse files
authored
Merge pull request #127 from mcg-web/refactor-public-access-complexity-query-tests
Refactor access public and complexity query tests
2 parents c150367 + cc66b32 commit 0fc439c

File tree

7 files changed

+83
-144
lines changed

7 files changed

+83
-144
lines changed

Tests/Functional/Generator/TypeGeneratorTest.php

Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515

1616
class TypeGeneratorTest extends TestCase
1717
{
18-
private $user = 'ryan';
19-
private $adminUser = 'admin';
20-
2118
public function testPublicCallback()
2219
{
2320
$expected = [
@@ -29,19 +26,15 @@ public function testPublicCallback()
2926
],
3027
];
3128

32-
$client = static::query(
33-
'query { object { name privateData } }',
34-
$this->adminUser
35-
);
36-
37-
$this->assertResponse('query { object { name privateData } }', $expected, $this->adminUser);
29+
$this->assertResponse('query { object { name privateData } }', $expected, self::USER_ADMIN, 'public');
3830

3931
$this->assertEquals(
4032
'Cannot query field "privateData" on type "ObjectWithPrivateField".',
4133
json_decode(
4234
static::query(
4335
'query { object { name privateData } }',
44-
$this->user
36+
self::USER_RYAN,
37+
'public'
4538
)->getResponse()->getContent(),
4639
true
4740
)['errors'][0]['message']
@@ -50,38 +43,6 @@ public function testPublicCallback()
5043
$expectedWithoutPrivateData = $expected;
5144
unset($expectedWithoutPrivateData['data']['object']['privateData']);
5245

53-
$this->assertResponse('query { object { name } }', $expectedWithoutPrivateData, $this->user);
54-
}
55-
56-
private static function assertResponse($query, array $expected, $username)
57-
{
58-
$client = self::query($query, $username);
59-
$result = $client->getResponse()->getContent();
60-
61-
static::assertEquals($expected, json_decode($result, true), $result);
62-
63-
return $client;
64-
}
65-
66-
private static function query($query, $username)
67-
{
68-
$client = self::createClientAuthenticated($username);
69-
$client->request('GET', '/', ['query' => $query]);
70-
71-
return $client;
72-
}
73-
74-
private static function createClientAuthenticated($username)
75-
{
76-
$client = static::createClient(['test_case' => 'public']);
77-
78-
if ($username) {
79-
$client->setServerParameters([
80-
'PHP_AUTH_USER' => $username,
81-
'PHP_AUTH_PW' => '123',
82-
]);
83-
}
84-
85-
return $client;
46+
$this->assertResponse('query { object { name } }', $expectedWithoutPrivateData, self::USER_RYAN, 'public');
8647
}
8748
}

Tests/Functional/Security/AccessTest.php

Lines changed: 15 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,14 @@
1616

1717
class AccessTest extends TestCase
1818
{
19-
const USER_RYAN = 'ryan';
20-
const USER_ADMIN = 'admin';
21-
const ANONYMOUS_USER = null;
19+
private $userNameQuery = 'query { user { name } }';
2220

23-
private $userNameQuery = 'query MyQuery { user { name } }';
21+
private $userRolesQuery = 'query { user { roles } }';
2422

25-
private $userRolesQuery = 'query MyQuery { user { roles } }';
26-
27-
private $userIsEnabledQuery = 'query MyQuery { user { isEnabled } }';
23+
private $userIsEnabledQuery = 'query { user { isEnabled } }';
2824

2925
private $userFriendsQuery = <<<'EOF'
30-
query MyQuery {
26+
query {
3127
user {
3228
friends(first: 2) {
3329
edges {
@@ -61,14 +57,14 @@ public function testNotAuthenticatedUserAccessToUserName()
6157
'warnings' => [
6258
[
6359
'message' => 'Access denied to this field.',
64-
'locations' => [['line' => 1, 'column' => 24]],
60+
'locations' => [['line' => 1, 'column' => 16]],
6561
'path' => ['user', 'name'],
6662
],
6763
],
6864
],
6965
];
7066

71-
$this->assertResponse($this->userNameQuery, $expected, static::ANONYMOUS_USER);
67+
$this->assertResponse($this->userNameQuery, $expected, static::ANONYMOUS_USER, 'access');
7268
}
7369

7470
public function testFullyAuthenticatedUserAccessToUserName()
@@ -81,17 +77,17 @@ public function testFullyAuthenticatedUserAccessToUserName()
8177
],
8278
];
8379

84-
$this->assertResponse($this->userNameQuery, $expected, static::USER_RYAN);
80+
$this->assertResponse($this->userNameQuery, $expected, static::USER_RYAN, 'access');
8581
}
8682

8783
public function testNotAuthenticatedUserAccessToUserRoles()
8884
{
89-
$this->assertResponse($this->userRolesQuery, $this->expectedFailedUserRoles(), static::ANONYMOUS_USER);
85+
$this->assertResponse($this->userRolesQuery, $this->expectedFailedUserRoles(), static::ANONYMOUS_USER, 'access');
9086
}
9187

9288
public function testAuthenticatedUserAccessToUserRolesWithoutEnoughRights()
9389
{
94-
$this->assertResponse($this->userRolesQuery, $this->expectedFailedUserRoles(), static::USER_RYAN);
90+
$this->assertResponse($this->userRolesQuery, $this->expectedFailedUserRoles(), static::USER_RYAN, 'access');
9591
}
9692

9793
public function testUserWithCorrectRightsAccessToUserRoles()
@@ -104,7 +100,7 @@ public function testUserWithCorrectRightsAccessToUserRoles()
104100
],
105101
];
106102

107-
$this->assertResponse($this->userRolesQuery, $expected, static::USER_ADMIN);
103+
$this->assertResponse($this->userRolesQuery, $expected, static::USER_ADMIN, 'access');
108104
}
109105

110106
public function testUserForbiddenField()
@@ -137,7 +133,7 @@ public function testUserForbiddenField()
137133
}
138134
EOF;
139135

140-
$this->assertResponse($query, $expected, static::USER_ADMIN);
136+
$this->assertResponse($query, $expected, static::USER_ADMIN, 'access');
141137
}
142138

143139
public function testUserAccessToUserFriends()
@@ -155,7 +151,7 @@ public function testUserAccessToUserFriends()
155151
],
156152
];
157153

158-
$this->assertResponse($this->userFriendsQuery, $expected, static::USER_ADMIN);
154+
$this->assertResponse($this->userFriendsQuery, $expected, static::USER_ADMIN, 'access');
159155
}
160156

161157
public function testMutationAllowedUser()
@@ -171,7 +167,7 @@ public function testMutationAllowedUser()
171167
],
172168
];
173169

174-
$this->assertResponse(sprintf($this->simpleMutationWithThunkQuery, $result), $expected, static::USER_ADMIN);
170+
$this->assertResponse(sprintf($this->simpleMutationWithThunkQuery, $result), $expected, static::USER_ADMIN, 'access');
175171
$this->assertTrue(SimpleMutationWithThunkFieldsMutation::hasMutate(true));
176172
}
177173

@@ -200,7 +196,7 @@ public function testMutationAllowedButNoRightsToDisplayPayload()
200196
],
201197
];
202198

203-
$this->assertResponse(sprintf($this->simpleMutationWithThunkQuery, 321), $expected, static::USER_ADMIN);
199+
$this->assertResponse(sprintf($this->simpleMutationWithThunkQuery, 321), $expected, static::USER_ADMIN, 'access');
204200
$this->assertTrue(SimpleMutationWithThunkFieldsMutation::hasMutate(true));
205201
}
206202

@@ -224,7 +220,7 @@ public function testMutationNotAllowedUser()
224220
],
225221
];
226222

227-
$this->assertResponse(sprintf($this->simpleMutationWithThunkQuery, 123), $expected, static::USER_RYAN);
223+
$this->assertResponse(sprintf($this->simpleMutationWithThunkQuery, 123), $expected, static::USER_RYAN, 'access');
228224
$this->assertFalse(SimpleMutationWithThunkFieldsMutation::hasMutate(true));
229225
}
230226

@@ -238,30 +234,4 @@ private function expectedFailedUserRoles()
238234
],
239235
];
240236
}
241-
242-
private static function assertResponse($query, array $expected, $username)
243-
{
244-
$client = self::createClientAuthenticated($username);
245-
$client->request('GET', '/', ['query' => $query]);
246-
247-
$result = $client->getResponse()->getContent();
248-
249-
static::assertEquals($expected, json_decode($result, true), $result);
250-
251-
return $client;
252-
}
253-
254-
private static function createClientAuthenticated($username)
255-
{
256-
$client = static::createClient(['test_case' => 'access']);
257-
258-
if ($username) {
259-
$client->setServerParameters([
260-
'PHP_AUTH_USER' => $username,
261-
'PHP_AUTH_PW' => '123',
262-
]);
263-
}
264-
265-
return $client;
266-
}
267237
}

Tests/Functional/Security/QueryComplexityTest.php

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
class QueryComplexityTest extends TestCase
1717
{
1818
private $userFriendsWithoutLimitQuery = <<<'EOF'
19-
query MyQuery {
19+
query {
2020
user {
2121
friends {
2222
edges {
@@ -30,7 +30,7 @@ class QueryComplexityTest extends TestCase
3030
EOF;
3131

3232
private $userFriendsWithLimitQuery = <<<'EOF'
33-
query MyQuery {
33+
query {
3434
user {
3535
friends(first: 1) {
3636
edges {
@@ -53,7 +53,7 @@ public function testComplexityReachLimitation()
5353
],
5454
];
5555

56-
$this->assertResponse($this->userFriendsWithoutLimitQuery, $expected);
56+
$this->assertResponse($this->userFriendsWithoutLimitQuery, $expected, self::ANONYMOUS_USER, 'queryComplexity');
5757
}
5858

5959
public function testComplexityUnderLimitation()
@@ -70,18 +70,6 @@ public function testComplexityUnderLimitation()
7070
],
7171
];
7272

73-
$this->assertResponse($this->userFriendsWithLimitQuery, $expected);
74-
}
75-
76-
private static function assertResponse($query, array $expected)
77-
{
78-
$client = static::createClient(['test_case' => 'queryComplexity']);
79-
$client->request('GET', '/', ['query' => $query]);
80-
81-
$result = $client->getResponse()->getContent();
82-
83-
static::assertEquals($expected, json_decode($result, true), $result);
84-
85-
return $client;
73+
$this->assertResponse($this->userFriendsWithLimitQuery, $expected, self::ANONYMOUS_USER, 'queryComplexity');
8674
}
8775
}

Tests/Functional/TestCase.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
*/
2222
abstract class TestCase extends WebTestCase
2323
{
24+
const USER_RYAN = 'ryan';
25+
const USER_ADMIN = 'admin';
26+
const ANONYMOUS_USER = null;
27+
const DEFAULT_PASSWORD = '123';
28+
2429
/**
2530
* @var AppKernel[]
2631
*/
@@ -115,4 +120,38 @@ protected static function getContainer()
115120
{
116121
return static::$kernel->getContainer();
117122
}
123+
124+
protected static function query($query, $username, $testCase, $password = self::DEFAULT_PASSWORD)
125+
{
126+
$client = static::createClientAuthenticated($username, $testCase, $password);
127+
$client->request('GET', '/', ['query' => $query]);
128+
129+
return $client;
130+
}
131+
132+
protected static function createClientAuthenticated($username, $testCase, $password = self::DEFAULT_PASSWORD)
133+
{
134+
$client = static::createClient(['test_case' => $testCase]);
135+
136+
if ($username) {
137+
$client->setServerParameters([
138+
'PHP_AUTH_USER' => $username,
139+
'PHP_AUTH_PW' => $password,
140+
]);
141+
}
142+
143+
return $client;
144+
}
145+
146+
protected static function assertResponse($query, array $expected, $username, $testCase, $password = self::DEFAULT_PASSWORD)
147+
{
148+
$client = self::createClientAuthenticated($username, $testCase, $password);
149+
$client->request('GET', '/', ['query' => $query]);
150+
151+
$result = $client->getResponse()->getContent();
152+
153+
static::assertEquals($expected, json_decode($result, true), $result);
154+
155+
return $client;
156+
}
118157
}
Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
imports:
22
- { resource: ../config.yml }
3+
- { resource: ../security.yml }
34
- { resource: ../connection/services.yml }
45
- { resource: ../mutation/services.yml }
56

@@ -16,23 +17,3 @@ overblog_graphql:
1617
-
1718
type: yml
1819
dir: "%kernel.root_dir%/config/access/mapping"
19-
20-
security:
21-
providers:
22-
in_memory:
23-
memory:
24-
users:
25-
ryan:
26-
password: 123
27-
roles: 'ROLE_USER'
28-
admin:
29-
password: 123
30-
roles: 'ROLE_ADMIN'
31-
encoders:
32-
Symfony\Component\Security\Core\User\User: plaintext
33-
firewalls:
34-
graph:
35-
pattern: ^/
36-
http_basic: ~
37-
stateless: true
38-
anonymous: true
Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
imports:
22
- { resource: ../config.yml }
3+
- { resource: ../security.yml }
34

45
parameters:
56
overblog_graphql.type_class_namespace: "Overblog\\GraphQLBundle\\PublicField\\__DEFINITIONS__"
@@ -13,23 +14,3 @@ overblog_graphql:
1314
-
1415
type: yml
1516
dir: "%kernel.root_dir%/config/public/mapping"
16-
17-
security:
18-
providers:
19-
in_memory:
20-
memory:
21-
users:
22-
ryan:
23-
password: 123
24-
roles: 'ROLE_USER'
25-
admin:
26-
password: 123
27-
roles: 'ROLE_ADMIN'
28-
encoders:
29-
Symfony\Component\Security\Core\User\User: plaintext
30-
firewalls:
31-
graph:
32-
pattern: ^/
33-
http_basic: ~
34-
stateless: true
35-
anonymous: true
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
security:
2+
providers:
3+
in_memory:
4+
memory:
5+
users:
6+
ryan:
7+
password: 123
8+
roles: 'ROLE_USER'
9+
admin:
10+
password: 123
11+
roles: 'ROLE_ADMIN'
12+
encoders:
13+
Symfony\Component\Security\Core\User\User: plaintext
14+
firewalls:
15+
graph:
16+
pattern: ^/
17+
http_basic: ~
18+
stateless: true
19+
anonymous: true

0 commit comments

Comments
 (0)