11using System . Collections . Generic ;
22using GraphQL ;
33using GraphQL . Types ;
4+ using GraphQL . Types . Relay . DataObjects ;
45using Xunit ;
56
67namespace GraphQL . Authorization . Tests
@@ -13,7 +14,7 @@ public void class_policy_success()
1314 Settings . AddPolicy ( "ClassPolicy" , _ => _ . RequireClaim ( "admin" ) ) ;
1415 Settings . AddPolicy ( "FieldPolicy" , _ => _ . RequireClaim ( "admin" ) ) ;
1516
16- ShouldPassRule ( _=>
17+ ShouldPassRule ( _ =>
1718 {
1819 _ . Query = @"query { post }" ;
1920 _ . Schema = BasicSchema ( ) ;
@@ -32,7 +33,7 @@ public void class_policy_fail()
3233 _ . RequireClaim ( "admin" ) ;
3334 } ) ;
3435
35- ShouldFailRule ( _=>
36+ ShouldFailRule ( _ =>
3637 {
3738 _ . Query = @"query { post }" ;
3839 _ . Schema = BasicSchema ( ) ;
@@ -45,7 +46,7 @@ public void field_policy_success()
4546 Settings . AddPolicy ( "ClassPolicy" , _ => _ . RequireClaim ( "admin" ) ) ;
4647 Settings . AddPolicy ( "FieldPolicy" , _ => _ . RequireClaim ( "admin" ) ) ;
4748
48- ShouldPassRule ( _=>
49+ ShouldPassRule ( _ =>
4950 {
5051 _ . Query = @"query { post }" ;
5152 _ . Schema = BasicSchema ( ) ;
@@ -64,7 +65,7 @@ public void field_policy_fail()
6465 _ . RequireClaim ( "admin" ) ;
6566 } ) ;
6667
67- ShouldFailRule ( _=>
68+ ShouldFailRule ( _ =>
6869 {
6970 _ . Query = @"query { post }" ;
7071 _ . Schema = BasicSchema ( ) ;
@@ -79,7 +80,7 @@ public void nested_type_policy_success()
7980 _ . RequireClaim ( "admin" ) ;
8081 } ) ;
8182
82- ShouldPassRule ( _=>
83+ ShouldPassRule ( _ =>
8384 {
8485 _ . Query = @"query { post }" ;
8586 _ . Schema = NestedSchema ( ) ;
@@ -98,7 +99,7 @@ public void nested_type_policy_fail()
9899 _ . RequireClaim ( "admin" ) ;
99100 } ) ;
100101
101- ShouldFailRule ( _=>
102+ ShouldFailRule ( _ =>
102103 {
103104 _ . Query = @"query { post }" ;
104105 _ . Schema = NestedSchema ( ) ;
@@ -113,7 +114,7 @@ public void nested_type_list_policy_fail()
113114 _ . RequireClaim ( "admin" ) ;
114115 } ) ;
115116
116- ShouldFailRule ( _=>
117+ ShouldFailRule ( _ =>
117118 {
118119 _ . Query = @"query { posts }" ;
119120 _ . Schema = NestedSchema ( ) ;
@@ -128,7 +129,7 @@ public void nested_type_list_non_null_policy_fail()
128129 _ . RequireClaim ( "admin" ) ;
129130 } ) ;
130131
131- ShouldFailRule ( _=>
132+ ShouldFailRule ( _ =>
132133 {
133134 _ . Query = @"query { postsNonNull }" ;
134135 _ . Schema = NestedSchema ( ) ;
@@ -143,7 +144,7 @@ public void passes_with_claim_on_input_type()
143144 _ . RequireClaim ( "admin" ) ;
144145 } ) ;
145146
146- ShouldPassRule ( _=>
147+ ShouldPassRule ( _ =>
147148 {
148149 _ . Query = @"query { author(input: { name: ""Quinn"" }) }" ;
149150 _ . Schema = TypedSchema ( ) ;
@@ -162,7 +163,7 @@ public void fails_on_missing_claim_on_input_type()
162163 _ . RequireClaim ( "admin" ) ;
163164 } ) ;
164165
165- ShouldFailRule ( _=>
166+ ShouldFailRule ( _ =>
166167 {
167168 _ . Query = @"query { author(input: { name: ""Quinn"" }) }" ;
168169 _ . Schema = TypedSchema ( ) ;
@@ -187,6 +188,35 @@ public void passes_with_multiple_policies_on_field_and_single_on_input_type()
187188 } ) ;
188189 }
189190
191+ [ Fact ]
192+ public void passes_with_policy_on_connection_type ( )
193+ {
194+ Settings . AddPolicy ( "ConnectionPolicy" , _ => _ . RequireClaim ( "admin" ) ) ;
195+
196+ ShouldPassRule ( _ =>
197+ {
198+ _ . Query = @"query { posts { items { id } } }" ;
199+ _ . Schema = TypedSchema ( ) ;
200+ _ . User = CreatePrincipal ( claims : new Dictionary < string , string >
201+ {
202+ { "Admin" , "true" }
203+ } ) ;
204+ } ) ;
205+ }
206+
207+ [ Fact ]
208+ public void fails_on_missing_claim_on_connection_type ( )
209+ {
210+ Settings . AddPolicy ( "ConnectionPolicy" , _ => _ . RequireClaim ( "admin" ) ) ;
211+
212+ ShouldFailRule ( _ =>
213+ {
214+ _ . Query = @"query { posts { items { id } } }" ;
215+ _ . Schema = TypedSchema ( ) ;
216+ _ . User = CreatePrincipal ( ) ;
217+ } ) ;
218+ }
219+
190220 private ISchema BasicSchema ( )
191221 {
192222 var defs = @"
@@ -258,9 +288,17 @@ public class Post
258288 public string Id { get ; set ; }
259289 }
260290
291+ public class PostGraphType : ObjectGraphType < Post >
292+ {
293+ public PostGraphType ( )
294+ {
295+ Field ( p => p . Id ) ;
296+ }
297+ }
298+
261299 public class Author
262300 {
263- public string Name { get ; set ; }
301+ public string Name { get ; set ; }
264302 }
265303
266304 private ISchema TypedSchema ( )
@@ -272,6 +310,11 @@ private ISchema TypedSchema()
272310 resolve : context => "testing"
273311 ) ;
274312
313+ query . Connection < PostGraphType > ( )
314+ . Name ( "posts" )
315+ . AuthorizeWith ( "ConnectionPolicy" )
316+ . Resolve ( ctx => new Connection < Post > ( ) ) ;
317+
275318 query . Field < StringGraphType > (
276319 "project" ,
277320 arguments : new QueryArguments ( new QueryArgument < AuthorInputType > { Name = "input" } ) ,
0 commit comments