Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 4 additions & 43 deletions Tests/Functional/Generator/TypeGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@

class TypeGeneratorTest extends TestCase
{
private $user = 'ryan';
private $adminUser = 'admin';

public function testPublicCallback()
{
$expected = [
Expand All @@ -29,19 +26,15 @@ public function testPublicCallback()
],
];

$client = static::query(
'query { object { name privateData } }',
$this->adminUser
);

$this->assertResponse('query { object { name privateData } }', $expected, $this->adminUser);
$this->assertResponse('query { object { name privateData } }', $expected, self::USER_ADMIN, 'public');

$this->assertEquals(
'Cannot query field "privateData" on type "ObjectWithPrivateField".',
json_decode(
static::query(
'query { object { name privateData } }',
$this->user
self::USER_RYAN,
'public'
)->getResponse()->getContent(),
true
)['errors'][0]['message']
Expand All @@ -50,38 +43,6 @@ public function testPublicCallback()
$expectedWithoutPrivateData = $expected;
unset($expectedWithoutPrivateData['data']['object']['privateData']);

$this->assertResponse('query { object { name } }', $expectedWithoutPrivateData, $this->user);
}

private static function assertResponse($query, array $expected, $username)
{
$client = self::query($query, $username);
$result = $client->getResponse()->getContent();

static::assertEquals($expected, json_decode($result, true), $result);

return $client;
}

private static function query($query, $username)
{
$client = self::createClientAuthenticated($username);
$client->request('GET', '/', ['query' => $query]);

return $client;
}

private static function createClientAuthenticated($username)
{
$client = static::createClient(['test_case' => 'public']);

if ($username) {
$client->setServerParameters([
'PHP_AUTH_USER' => $username,
'PHP_AUTH_PW' => '123',
]);
}

return $client;
$this->assertResponse('query { object { name } }', $expectedWithoutPrivateData, self::USER_RYAN, 'public');
}
}
60 changes: 15 additions & 45 deletions Tests/Functional/Security/AccessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,14 @@

class AccessTest extends TestCase
{
const USER_RYAN = 'ryan';
const USER_ADMIN = 'admin';
const ANONYMOUS_USER = null;
private $userNameQuery = 'query { user { name } }';

private $userNameQuery = 'query MyQuery { user { name } }';
private $userRolesQuery = 'query { user { roles } }';

private $userRolesQuery = 'query MyQuery { user { roles } }';

private $userIsEnabledQuery = 'query MyQuery { user { isEnabled } }';
private $userIsEnabledQuery = 'query { user { isEnabled } }';

private $userFriendsQuery = <<<'EOF'
query MyQuery {
query {
user {
friends(first: 2) {
edges {
Expand Down Expand Up @@ -61,14 +57,14 @@ public function testNotAuthenticatedUserAccessToUserName()
'warnings' => [
[
'message' => 'Access denied to this field.',
'locations' => [['line' => 1, 'column' => 24]],
'locations' => [['line' => 1, 'column' => 16]],
'path' => ['user', 'name'],
],
],
],
];

$this->assertResponse($this->userNameQuery, $expected, static::ANONYMOUS_USER);
$this->assertResponse($this->userNameQuery, $expected, static::ANONYMOUS_USER, 'access');
}

public function testFullyAuthenticatedUserAccessToUserName()
Expand All @@ -81,17 +77,17 @@ public function testFullyAuthenticatedUserAccessToUserName()
],
];

$this->assertResponse($this->userNameQuery, $expected, static::USER_RYAN);
$this->assertResponse($this->userNameQuery, $expected, static::USER_RYAN, 'access');
}

public function testNotAuthenticatedUserAccessToUserRoles()
{
$this->assertResponse($this->userRolesQuery, $this->expectedFailedUserRoles(), static::ANONYMOUS_USER);
$this->assertResponse($this->userRolesQuery, $this->expectedFailedUserRoles(), static::ANONYMOUS_USER, 'access');
}

public function testAuthenticatedUserAccessToUserRolesWithoutEnoughRights()
{
$this->assertResponse($this->userRolesQuery, $this->expectedFailedUserRoles(), static::USER_RYAN);
$this->assertResponse($this->userRolesQuery, $this->expectedFailedUserRoles(), static::USER_RYAN, 'access');
}

public function testUserWithCorrectRightsAccessToUserRoles()
Expand All @@ -104,7 +100,7 @@ public function testUserWithCorrectRightsAccessToUserRoles()
],
];

$this->assertResponse($this->userRolesQuery, $expected, static::USER_ADMIN);
$this->assertResponse($this->userRolesQuery, $expected, static::USER_ADMIN, 'access');
}

public function testUserForbiddenField()
Expand Down Expand Up @@ -137,7 +133,7 @@ public function testUserForbiddenField()
}
EOF;

$this->assertResponse($query, $expected, static::USER_ADMIN);
$this->assertResponse($query, $expected, static::USER_ADMIN, 'access');
}

public function testUserAccessToUserFriends()
Expand All @@ -155,7 +151,7 @@ public function testUserAccessToUserFriends()
],
];

$this->assertResponse($this->userFriendsQuery, $expected, static::USER_ADMIN);
$this->assertResponse($this->userFriendsQuery, $expected, static::USER_ADMIN, 'access');
}

public function testMutationAllowedUser()
Expand All @@ -171,7 +167,7 @@ public function testMutationAllowedUser()
],
];

$this->assertResponse(sprintf($this->simpleMutationWithThunkQuery, $result), $expected, static::USER_ADMIN);
$this->assertResponse(sprintf($this->simpleMutationWithThunkQuery, $result), $expected, static::USER_ADMIN, 'access');
$this->assertTrue(SimpleMutationWithThunkFieldsMutation::hasMutate(true));
}

Expand Down Expand Up @@ -200,7 +196,7 @@ public function testMutationAllowedButNoRightsToDisplayPayload()
],
];

$this->assertResponse(sprintf($this->simpleMutationWithThunkQuery, 321), $expected, static::USER_ADMIN);
$this->assertResponse(sprintf($this->simpleMutationWithThunkQuery, 321), $expected, static::USER_ADMIN, 'access');
$this->assertTrue(SimpleMutationWithThunkFieldsMutation::hasMutate(true));
}

Expand All @@ -224,7 +220,7 @@ public function testMutationNotAllowedUser()
],
];

$this->assertResponse(sprintf($this->simpleMutationWithThunkQuery, 123), $expected, static::USER_RYAN);
$this->assertResponse(sprintf($this->simpleMutationWithThunkQuery, 123), $expected, static::USER_RYAN, 'access');
$this->assertFalse(SimpleMutationWithThunkFieldsMutation::hasMutate(true));
}

Expand All @@ -238,30 +234,4 @@ private function expectedFailedUserRoles()
],
];
}

private static function assertResponse($query, array $expected, $username)
{
$client = self::createClientAuthenticated($username);
$client->request('GET', '/', ['query' => $query]);

$result = $client->getResponse()->getContent();

static::assertEquals($expected, json_decode($result, true), $result);

return $client;
}

private static function createClientAuthenticated($username)
{
$client = static::createClient(['test_case' => 'access']);

if ($username) {
$client->setServerParameters([
'PHP_AUTH_USER' => $username,
'PHP_AUTH_PW' => '123',
]);
}

return $client;
}
}
20 changes: 4 additions & 16 deletions Tests/Functional/Security/QueryComplexityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class QueryComplexityTest extends TestCase
{
private $userFriendsWithoutLimitQuery = <<<'EOF'
query MyQuery {
query {
user {
friends {
edges {
Expand All @@ -30,7 +30,7 @@ class QueryComplexityTest extends TestCase
EOF;

private $userFriendsWithLimitQuery = <<<'EOF'
query MyQuery {
query {
user {
friends(first: 1) {
edges {
Expand All @@ -53,7 +53,7 @@ public function testComplexityReachLimitation()
],
];

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

public function testComplexityUnderLimitation()
Expand All @@ -70,18 +70,6 @@ public function testComplexityUnderLimitation()
],
];

$this->assertResponse($this->userFriendsWithLimitQuery, $expected);
}

private static function assertResponse($query, array $expected)
{
$client = static::createClient(['test_case' => 'queryComplexity']);
$client->request('GET', '/', ['query' => $query]);

$result = $client->getResponse()->getContent();

static::assertEquals($expected, json_decode($result, true), $result);

return $client;
$this->assertResponse($this->userFriendsWithLimitQuery, $expected, self::ANONYMOUS_USER, 'queryComplexity');
}
}
39 changes: 39 additions & 0 deletions Tests/Functional/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
*/
abstract class TestCase extends WebTestCase
{
const USER_RYAN = 'ryan';
const USER_ADMIN = 'admin';
const ANONYMOUS_USER = null;
const DEFAULT_PASSWORD = '123';

/**
* @var AppKernel[]
*/
Expand Down Expand Up @@ -115,4 +120,38 @@ protected static function getContainer()
{
return static::$kernel->getContainer();
}

protected static function query($query, $username, $testCase, $password = self::DEFAULT_PASSWORD)
{
$client = static::createClientAuthenticated($username, $testCase, $password);
$client->request('GET', '/', ['query' => $query]);

return $client;
}

protected static function createClientAuthenticated($username, $testCase, $password = self::DEFAULT_PASSWORD)
{
$client = static::createClient(['test_case' => $testCase]);

if ($username) {
$client->setServerParameters([
'PHP_AUTH_USER' => $username,
'PHP_AUTH_PW' => $password,
]);
}

return $client;
}

protected static function assertResponse($query, array $expected, $username, $testCase, $password = self::DEFAULT_PASSWORD)
{
$client = self::createClientAuthenticated($username, $testCase, $password);
$client->request('GET', '/', ['query' => $query]);

$result = $client->getResponse()->getContent();

static::assertEquals($expected, json_decode($result, true), $result);

return $client;
}
}
21 changes: 1 addition & 20 deletions Tests/Functional/app/config/access/config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
imports:
- { resource: ../config.yml }
- { resource: ../security.yml }
- { resource: ../connection/services.yml }
- { resource: ../mutation/services.yml }

Expand All @@ -16,23 +17,3 @@ overblog_graphql:
-
type: yml
dir: "%kernel.root_dir%/config/access/mapping"

security:
providers:
in_memory:
memory:
users:
ryan:
password: 123
roles: 'ROLE_USER'
admin:
password: 123
roles: 'ROLE_ADMIN'
encoders:
Symfony\Component\Security\Core\User\User: plaintext
firewalls:
graph:
pattern: ^/
http_basic: ~
stateless: true
anonymous: true
21 changes: 1 addition & 20 deletions Tests/Functional/app/config/public/config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
imports:
- { resource: ../config.yml }
- { resource: ../security.yml }

parameters:
overblog_graphql.type_class_namespace: "Overblog\\GraphQLBundle\\PublicField\\__DEFINITIONS__"
Expand All @@ -13,23 +14,3 @@ overblog_graphql:
-
type: yml
dir: "%kernel.root_dir%/config/public/mapping"

security:
providers:
in_memory:
memory:
users:
ryan:
password: 123
roles: 'ROLE_USER'
admin:
password: 123
roles: 'ROLE_ADMIN'
encoders:
Symfony\Component\Security\Core\User\User: plaintext
firewalls:
graph:
pattern: ^/
http_basic: ~
stateless: true
anonymous: true
19 changes: 19 additions & 0 deletions Tests/Functional/app/config/security.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
security:
providers:
in_memory:
memory:
users:
ryan:
password: 123
roles: 'ROLE_USER'
admin:
password: 123
roles: 'ROLE_ADMIN'
encoders:
Symfony\Component\Security\Core\User\User: plaintext
firewalls:
graph:
pattern: ^/
http_basic: ~
stateless: true
anonymous: true