Skip to content

Commit 6f552f6

Browse files
committed
more tests more better.
1 parent 65435b7 commit 6f552f6

File tree

3 files changed

+162
-4
lines changed

3 files changed

+162
-4
lines changed

lib/graphql/stitching/request.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ def normalized_string
7777

7878
# @return [String] a digest of the original document string. Generally faster but less consistent.
7979
def digest
80-
@digest ||= Stitching.digest.call("#{Stitching::VERSION}/#{string}")
80+
@digest ||= Stitching.digest.call(digest_base(string))
8181
end
8282

8383
# @return [String] a digest of the normalized document string. Slower but more consistent.
8484
def normalized_digest
85-
@normalized_digest ||= Stitching.digest.call("#{Stitching::VERSION}/#{normalized_string}")
85+
@normalized_digest ||= Stitching.digest.call(digest_base(normalized_string))
8686
end
8787

8888
# @return [GraphQL::Language::Nodes::OperationDefinition] The selected root operation for the request.
@@ -235,6 +235,13 @@ def add_subscription_update_handler
235235
result
236236
}
237237
end
238+
239+
def digest_base(content)
240+
base = String.new(Stitching::VERSION)
241+
base << "/" << content
242+
@claims.each { |claim| base << "/" << claim } if @claims
243+
base
244+
end
238245
end
239246
end
240247
end

test/graphql/stitching/integration/authorizations_test.rb

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,155 @@ def test_expected_results_with_proper_permissions
119119

120120
assert_equal expected, result.to_h
121121
end
122+
123+
def test_errors_unauthorized_root_field_selections
124+
query = %|{
125+
a1: orderA(id: "1") { shippingAddress }
126+
a2: productA(id: "1") { name }
127+
...on Query {
128+
b1: orderA(id: "1") { shippingAddress }
129+
b2: productA(id: "1") { description }
130+
... QueryAttrs
131+
}
132+
}
133+
fragment QueryAttrs on Query {
134+
c1: orderA(id: "1") { shippingAddress }
135+
c2: productA(id: "1") { price }
136+
}|
137+
138+
result = plan_and_execute(@supergraph, query)
139+
expected = {
140+
"data" => {
141+
"a1" => nil,
142+
"a2" => nil,
143+
"b1" => nil,
144+
"b2" => { "description" => nil },
145+
"c1" => nil,
146+
"c2" => nil,
147+
},
148+
"errors" => [{
149+
"message" => "Unauthorized access",
150+
"path" => ["a1"],
151+
"extensions" => { "code" => "unauthorized" },
152+
}, {
153+
"message" => "Unauthorized access",
154+
"path" => ["b1"],
155+
"extensions" => { "code" => "unauthorized" },
156+
}, {
157+
"message" => "Unauthorized access",
158+
"path" => ["c1"],
159+
"extensions" => { "code" => "unauthorized" },
160+
}, {
161+
"message" => "Unauthorized access",
162+
"path" => ["a2", "name"],
163+
"extensions" => { "code" => "unauthorized" },
164+
}, {
165+
"message" => "Unauthorized access",
166+
"path" => ["b2", "description"],
167+
"extensions" => { "code" => "unauthorized" },
168+
}, {
169+
"message" => "Unauthorized access",
170+
"path" => ["c2", "price"],
171+
"extensions" => { "code" => "unauthorized" },
172+
}],
173+
}
174+
175+
assert_equal expected, result.to_h
176+
end
177+
178+
def test_stitches_around_unauthorized_access
179+
query = %|{
180+
orderA(id: "1") {
181+
open
182+
customer1 {
183+
email
184+
}
185+
customer2 {
186+
email
187+
}
188+
product {
189+
description
190+
open
191+
}
192+
}
193+
}|
194+
195+
result = plan_and_execute(@supergraph, query, claims: ["orders"])
196+
expected = {
197+
"data" => {
198+
"orderA" => {
199+
"open" => true,
200+
"customer1" => nil,
201+
"customer2" => nil,
202+
"product" => {
203+
"description" => nil,
204+
"open" => true,
205+
}
206+
}
207+
},
208+
"errors" => [{
209+
"message" => "Unauthorized access",
210+
"path" => ["orderA", "customer1", "email"],
211+
"extensions" => { "code" => "unauthorized" },
212+
}, {
213+
"message" => "Unauthorized access",
214+
"path" => ["orderA", "customer2"],
215+
"extensions" => { "code" => "unauthorized" },
216+
}, {
217+
"message" => "Unauthorized access",
218+
"path" => ["orderA", "product", "description"],
219+
"extensions" => { "code" => "unauthorized" },
220+
}],
221+
}
222+
223+
assert_equal expected, result.to_h
224+
end
225+
226+
def test_stitches_around_unauthorized_access_from_opposing_entrypoint
227+
query = %|{
228+
orderB(id: "1") {
229+
open
230+
customer1 {
231+
email
232+
}
233+
customer2 {
234+
email
235+
}
236+
product {
237+
description
238+
open
239+
}
240+
}
241+
}|
242+
243+
result = plan_and_execute(@supergraph, query, claims: ["orders"])
244+
expected = {
245+
"data" => {
246+
"orderB" => {
247+
"open" => true,
248+
"customer1" => nil,
249+
"customer2" => nil,
250+
"product" => {
251+
"description" => nil,
252+
"open" => true,
253+
}
254+
}
255+
},
256+
"errors" => [{
257+
"message" => "Unauthorized access",
258+
"path" => ["orderB", "customer2"],
259+
"extensions" => { "code" => "unauthorized" },
260+
}, {
261+
"message" => "Unauthorized access",
262+
"path" => ["orderB", "customer1", "email"],
263+
"extensions" => { "code" => "unauthorized" },
264+
}, {
265+
"message" => "Unauthorized access",
266+
"path" => ["orderB", "product", "description"],
267+
"extensions" => { "code" => "unauthorized" },
268+
}],
269+
}
270+
271+
assert_equal expected, result.to_h
272+
end
122273
end

test/schemas/authorizations.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def customer2
5353
end
5454

5555
class Query < GraphQL::Schema::Object
56-
field :product_a, Product, null: false do
56+
field :product_a, Product, null: true do
5757
directive GraphQL::Stitching::Directives::Stitch, key: "id"
5858
argument :id, ID, required: true
5959
end
@@ -62,7 +62,7 @@ def product_a(id:)
6262
PRODUCTS.find { _1[:id] == id }
6363
end
6464

65-
field :order_a, Order, null: false do
65+
field :order_a, Order, null: true do
6666
directive GraphQL::Stitching::Directives::Authorization, scopes: [["orders"]]
6767
directive GraphQL::Stitching::Directives::Stitch, key: "id"
6868
argument :id, ID, required: true

0 commit comments

Comments
 (0)