Skip to content
This repository has been archived by the owner on Feb 10, 2019. It is now read-only.

add custom error msg for AuthorizationError #334

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
25 changes: 22 additions & 3 deletions src/Folklore/GraphQL/Support/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

class Field extends Fluent
{

/**
* Override this in your queries or mutations
* to provide custom authorization
Expand All @@ -26,6 +25,26 @@ public function authenticated($root, $args, $context)
return true;
}

/**
* Message of unauthorized error
*
* @return string
*/
protected function unauthorized()
{
return 'Unauthorized';
}

/**
* Message of unauthenticated error
*
* @return string
*/
protected function unauthenticated()
{
return 'Unauthenticated';
}

public function attributes()
{
return [];
Expand Down Expand Up @@ -56,12 +75,12 @@ protected function getResolver()

// Authenticated
if (call_user_func_array($authenticate, $args) !== true) {
throw new AuthorizationError('Unauthenticated');
throw new AuthorizationError($this->unauthenticated());
}

// Authorize
if (call_user_func_array($authorize, $args) !== true) {
throw new AuthorizationError('Unauthorized');
throw new AuthorizationError($this->unauthorized());
}

return call_user_func_array($resolver, $args);
Expand Down
24 changes: 24 additions & 0 deletions tests/GraphQLQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,18 @@ public function testQueryAndReturnResultWithAuthorize()
$this->assertEquals('Unauthorized', $result['errors'][0]['message']);
}

/**
* Test query with custom authorize msg
*
* @test
*/
public function testQueryAndReturnResultWithCustomAuthorize()
{
$result = GraphQL::query($this->queries['examplesWithCustomAuthorize']);
$this->assertNull($result['data']['examplesCustomAuthorize']);
$this->assertEquals('custom', $result['errors'][0]['message']);
}

/**
* Test query with authorize
*
Expand All @@ -126,6 +138,18 @@ public function testQueryAndReturnResultWithAuthenticated()
$this->assertEquals('Unauthenticated', $result['errors'][0]['message']);
}

/**
* Test query with authorize
*
* @test
*/
public function testQueryAndReturnResultWithCustomAuthenticated()
{
$result = GraphQL::query($this->queries['examplesWithCustomAuthenticated']);
$this->assertNull($result['data']['examplesCustomAuthenticated']);
$this->assertEquals('custom', $result['errors'][0]['message']);
}

/**
* Test query with schema
*
Expand Down
21 changes: 21 additions & 0 deletions tests/Objects/ExamplesCustomAuthenticatedQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use GraphQL\Type\Definition\Type;
use Folklore\GraphQL\Support\Query;

class ExamplesCustomAuthenticatedQuery extends ExamplesQuery
{
protected $attributes = [
'name' => 'Examples authenticate query'
];

public function authenticated($root, $args, $context)
{
return false;
}

protected function unauthenticated()
{
return 'custom';
}
}
21 changes: 21 additions & 0 deletions tests/Objects/ExamplesCustomAuthorizeQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use GraphQL\Type\Definition\Type;
use Folklore\GraphQL\Support\Query;

class ExamplesCustomAuthorizeQuery extends ExamplesQuery
{
protected $attributes = [
'name' => 'Examples authorize query'
];

public function authorize($root, $args)
{
return false;
}

protected function unauthorized()
{
return 'custom';
}
}
16 changes: 16 additions & 0 deletions tests/Objects/queries.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@
}
",

'examplesWithCustomAuthorize' => "
query QueryExamplesCustomAuthorize {
examplesCustomAuthorize {
test
}
}
",

'examplesWithAuthenticated' => "
query QueryExamplesAuthenticated {
examplesAuthenticated {
Expand All @@ -57,6 +65,14 @@
}
",

'examplesWithCustomAuthenticated' => "
query QueryExamplesCustomAuthenticated {
examplesCustomAuthenticated {
test
}
}
",

'examplesWithRoot' => "
query QueryExamplesRoot {
examplesRoot {
Expand Down
2 changes: 2 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ protected function getEnvironmentSetUp($app)
'examplesContext' => ExamplesContextQuery::class,
'examplesRoot' => ExamplesRootQuery::class,
'examplesAuthorize' => ExamplesAuthorizeQuery::class,
'examplesCustomAuthorize' => ExamplesCustomAuthorizeQuery::class,
'examplesAuthenticated' => ExamplesAuthenticatedQuery::class,
'examplesCustomAuthenticated' => ExamplesCustomAuthenticatedQuery::class,
'examplesPagination' => ExamplesPaginationQuery::class,
],
'mutation' => [
Expand Down