From a22f06652b5f1ab275aa4d78ca01f25b5a7d644e Mon Sep 17 00:00:00 2001 From: Karl Li Date: Tue, 8 May 2018 17:08:17 +0800 Subject: [PATCH 1/3] add custom error msg for AuthorizationError --- src/Folklore/GraphQL/Support/Field.php | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/Folklore/GraphQL/Support/Field.php b/src/Folklore/GraphQL/Support/Field.php index b730b0e3..aa3509d1 100644 --- a/src/Folklore/GraphQL/Support/Field.php +++ b/src/Folklore/GraphQL/Support/Field.php @@ -7,7 +7,6 @@ class Field extends Fluent { - /** * Override this in your queries or mutations * to provide custom authorization @@ -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 []; @@ -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); From 52247ed39f0af5e696c371d54904fcfb232021a3 Mon Sep 17 00:00:00 2001 From: Karl Li Date: Tue, 8 May 2018 17:25:17 +0800 Subject: [PATCH 2/3] add custom authorize test --- tests/GraphQLQueryTest.php | 12 +++++++++++ .../Objects/ExamplesCustomAuthorizeQuery.php | 21 +++++++++++++++++++ tests/Objects/queries.php | 8 +++++++ tests/TestCase.php | 1 + 4 files changed, 42 insertions(+) create mode 100644 tests/Objects/ExamplesCustomAuthorizeQuery.php diff --git a/tests/GraphQLQueryTest.php b/tests/GraphQLQueryTest.php index f8a71a99..cb47d4bf 100644 --- a/tests/GraphQLQueryTest.php +++ b/tests/GraphQLQueryTest.php @@ -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 * diff --git a/tests/Objects/ExamplesCustomAuthorizeQuery.php b/tests/Objects/ExamplesCustomAuthorizeQuery.php new file mode 100644 index 00000000..d7f6badf --- /dev/null +++ b/tests/Objects/ExamplesCustomAuthorizeQuery.php @@ -0,0 +1,21 @@ + 'Examples authorize query' + ]; + + public function authorize($root, $args) + { + return false; + } + + protected function unauthorized() + { + return 'custom'; + } +} diff --git a/tests/Objects/queries.php b/tests/Objects/queries.php index 0bcebcb2..4d8e9e7d 100644 --- a/tests/Objects/queries.php +++ b/tests/Objects/queries.php @@ -49,6 +49,14 @@ } ", + 'examplesWithCustomAuthorize' => " + query QueryExamplesCustomAuthorize { + examplesCustomAuthorize { + test + } + } + ", + 'examplesWithAuthenticated' => " query QueryExamplesAuthenticated { examplesAuthenticated { diff --git a/tests/TestCase.php b/tests/TestCase.php index 3ac5406b..13a49d2e 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -26,6 +26,7 @@ protected function getEnvironmentSetUp($app) 'examplesContext' => ExamplesContextQuery::class, 'examplesRoot' => ExamplesRootQuery::class, 'examplesAuthorize' => ExamplesAuthorizeQuery::class, + 'examplesCustomAuthorize' => ExamplesCustomAuthorizeQuery::class, 'examplesAuthenticated' => ExamplesAuthenticatedQuery::class, 'examplesPagination' => ExamplesPaginationQuery::class, ], From 064adcce3966d196e0823c7dfa377352c5f8e22a Mon Sep 17 00:00:00 2001 From: Karl Li Date: Tue, 8 May 2018 17:27:40 +0800 Subject: [PATCH 3/3] add custom authenticated test --- tests/GraphQLQueryTest.php | 12 +++++++++++ .../ExamplesCustomAuthenticatedQuery.php | 21 +++++++++++++++++++ tests/Objects/queries.php | 8 +++++++ tests/TestCase.php | 1 + 4 files changed, 42 insertions(+) create mode 100644 tests/Objects/ExamplesCustomAuthenticatedQuery.php diff --git a/tests/GraphQLQueryTest.php b/tests/GraphQLQueryTest.php index cb47d4bf..22ef5e32 100644 --- a/tests/GraphQLQueryTest.php +++ b/tests/GraphQLQueryTest.php @@ -138,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 * diff --git a/tests/Objects/ExamplesCustomAuthenticatedQuery.php b/tests/Objects/ExamplesCustomAuthenticatedQuery.php new file mode 100644 index 00000000..1d1d477e --- /dev/null +++ b/tests/Objects/ExamplesCustomAuthenticatedQuery.php @@ -0,0 +1,21 @@ + 'Examples authenticate query' + ]; + + public function authenticated($root, $args, $context) + { + return false; + } + + protected function unauthenticated() + { + return 'custom'; + } +} diff --git a/tests/Objects/queries.php b/tests/Objects/queries.php index 4d8e9e7d..720ed1af 100644 --- a/tests/Objects/queries.php +++ b/tests/Objects/queries.php @@ -65,6 +65,14 @@ } ", + 'examplesWithCustomAuthenticated' => " + query QueryExamplesCustomAuthenticated { + examplesCustomAuthenticated { + test + } + } + ", + 'examplesWithRoot' => " query QueryExamplesRoot { examplesRoot { diff --git a/tests/TestCase.php b/tests/TestCase.php index 13a49d2e..6583fcae 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -28,6 +28,7 @@ protected function getEnvironmentSetUp($app) 'examplesAuthorize' => ExamplesAuthorizeQuery::class, 'examplesCustomAuthorize' => ExamplesCustomAuthorizeQuery::class, 'examplesAuthenticated' => ExamplesAuthenticatedQuery::class, + 'examplesCustomAuthenticated' => ExamplesCustomAuthenticatedQuery::class, 'examplesPagination' => ExamplesPaginationQuery::class, ], 'mutation' => [