Skip to content

Commit 51c0c28

Browse files
author
Robert Mosolgo
authored
Merge pull request rmosolgo#2136 from rmosolgo/query-auth-fix
Support returning false from Query.authorized?
2 parents b690902 + 73bb004 commit 51c0c28

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

lib/graphql/unauthorized_error.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class UnauthorizedError < GraphQL::Error
1515
attr_reader :context
1616

1717
def initialize(message = nil, object: nil, type: nil, context: nil)
18-
if message.nil? && object.nil?
18+
if message.nil? && object.nil? && type.nil?
1919
raise ArgumentError, "#{self.class.name} requires either a message or keywords"
2020
end
2121

spec/graphql/authorization_spec.rb

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,10 @@ class LandscapeFeature < BaseEnum
236236
end
237237

238238
class Query < BaseObject
239+
def self.authorized?(obj, ctx)
240+
!ctx[:query_unauthorized]
241+
end
242+
239243
field :hidden, Integer, null: false
240244
field :unauthorized, Integer, null: true, method: :itself
241245
field :int2, Integer, null: true do
@@ -386,7 +390,7 @@ def self.unauthorized_object(err)
386390
elsif err.object == :replace
387391
33
388392
else
389-
raise GraphQL::ExecutionError, "Unauthorized #{err.type.graphql_name}: #{err.object}"
393+
raise GraphQL::ExecutionError, "Unauthorized #{err.type.graphql_name}: #{err.object.inspect}"
390394
end
391395
end
392396

@@ -679,7 +683,7 @@ def auth_execute(*args)
679683
it "adds the error to the errors key" do
680684
query = "{ unauthorized }"
681685
response = AuthTest::Schema.execute(query, root_value: :hide)
682-
assert_equal ["Unauthorized Query: hide"], response["errors"].map { |e| e["message"] }
686+
assert_equal ["Unauthorized Query: :hide"], response["errors"].map { |e| e["message"] }
683687
end
684688
end
685689
end
@@ -820,7 +824,7 @@ def auth_execute(*args)
820824
assert_nil unauthorized_res["data"].fetch("a")
821825
assert_equal "b", unauthorized_res["data"]["b"]["value"]
822826
# Also, the custom handler was called:
823-
assert_equal ["Unauthorized UnauthorizedCheckBox: a"], unauthorized_res["errors"].map { |e| e["message"] }
827+
assert_equal ["Unauthorized UnauthorizedCheckBox: \"a\""], unauthorized_res["errors"].map { |e| e["message"] }
824828
end
825829

826830
it "Works for lazy connections" do
@@ -885,7 +889,7 @@ def auth_execute(*args)
885889

886890
res = auth_execute(query)
887891
# An error from two, values from the others
888-
assert_equal ["Unauthorized UnauthorizedCheckBox: a", "Unauthorized UnauthorizedCheckBox: a"], res["errors"].map { |e| e["message"] }
892+
assert_equal ["Unauthorized UnauthorizedCheckBox: \"a\"", "Unauthorized UnauthorizedCheckBox: \"a\""], res["errors"].map { |e| e["message"] }
889893
assert_equal [{"value" => "z"}, {"value" => "z2"}, nil, nil], res["data"]["unauthorizedLazyListInterface"]
890894
end
891895

@@ -897,5 +901,15 @@ def auth_execute(*args)
897901
res = auth_execute(query, context: { replace_me: false })
898902
assert_equal false, res["data"]["replacedObject"]["replaced"]
899903
end
904+
905+
it "works when the query hook returns false and there's no root object" do
906+
query = "{ __typename }"
907+
res = auth_execute(query)
908+
assert_equal "Query", res["data"]["__typename"]
909+
910+
unauth_res = auth_execute(query, context: { query_unauthorized: true })
911+
assert_nil unauth_res["data"]
912+
assert_equal [{"message"=>"Unauthorized Query: nil"}], unauth_res["errors"]
913+
end
900914
end
901915
end

0 commit comments

Comments
 (0)