View source
<?php
namespace Drupal\Tests\graphql\Kernel\Framework;
use Drupal\graphql\GraphQL\QueryProvider\QueryProviderInterface;
use Drupal\Tests\graphql\Kernel\GraphQLTestBase;
use PhpParser\Node\Arg;
use Prophecy\Argument;
class PermissionsTest extends GraphQLTestBase {
protected function userPermissions() {
return [];
}
protected function setUp() {
parent::setUp();
$this
->mockField('root', [
'name' => 'root',
'type' => 'String',
'secure' => TRUE,
], 'test');
$queryProvider = $this
->prophesize(QueryProviderInterface::class);
$queryProvider
->getQuery(Argument::any(), Argument::any())
->willReturn(NULL);
$queryProvider
->getQuery('persisted:a', Argument::any())
->willReturn('{ root }');
$this->container
->set('graphql.query_provider', $queryProvider
->reveal());
}
public function testNoPermissions() {
$this->accountProphecy
->hasPermission(Argument::any())
->willReturn(FALSE);
$this
->assertEquals(403, $this
->query('query')
->getStatusCode());
$this
->assertEquals(403, $this
->persistedQuery('persisted:a')
->getStatusCode());
$batched = $this
->batchedQueries([
[
'query' => '{ root }',
],
[
'queryId' => 'persisted:a',
],
]);
$this
->assertEquals(403, $batched
->getStatusCode());
}
public function testPersistedQueryAccess() {
$this->accountProphecy
->hasPermission(Argument::is('execute persisted graphql requests'))
->willReturn(TRUE);
$this->accountProphecy
->hasPermission(Argument::not('execute persisted graphql requests'))
->willReturn(FALSE);
$this
->assertEquals(403, $this
->query('{ root }')
->getStatusCode());
$this
->assertEquals(200, $this
->persistedQuery('persisted:a')
->getStatusCode());
$batched = $this
->batchedQueries([
[
'query' => '{ root }',
],
[
'queryId' => 'persisted:a',
],
]);
$this
->assertEquals(403, $batched
->getStatusCode());
}
public function testFullQueryAccess() {
$this->accountProphecy
->hasPermission(Argument::is('execute graphql requests'))
->willReturn(TRUE);
$this->accountProphecy
->hasPermission(Argument::not('execute graphql requests'))
->willReturn(FALSE);
$this
->assertEquals(200, $this
->query('{ root }')
->getStatusCode());
$this
->assertEquals(200, $this
->persistedQuery('persisted:a')
->getStatusCode());
$batched = $this
->batchedQueries([
[
'query' => '{ root }',
],
[
'queryId' => 'persisted:a',
],
]);
$this
->assertEquals(200, $batched
->getStatusCode());
$data = [
'data' => [
'root' => 'test',
],
];
$this
->assertEquals([
$data,
$data,
], json_decode($batched
->getContent(), TRUE));
}
}