Skip to content

Commit e953890

Browse files
authored
Add test for checking policy on input types (#18)
1 parent 4db05bb commit e953890

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

src/GraphQL.Authorization.Tests/AuthorizationValidationRuleTests.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,40 @@ public void nested_type_policy_fail()
109109
});
110110
}
111111

112+
[Fact]
113+
public void passes_with_claim_on_input_type()
114+
{
115+
Settings.AddPolicy("FieldPolicy", _ =>
116+
{
117+
_.RequireClaim("admin");
118+
});
119+
120+
ShouldPassRule(_=>
121+
{
122+
_.Query = @"query { author(input: { name: ""Quinn"" }) }";
123+
_.Schema = TypedSchema();
124+
_.User = CreatePrincipal(claims: new Dictionary<string, string>
125+
{
126+
{"Admin", "true"}
127+
});
128+
});
129+
}
130+
131+
[Fact]
132+
public void fails_on_missing_claim_on_input_type()
133+
{
134+
Settings.AddPolicy("FieldPolicy", _ =>
135+
{
136+
_.RequireClaim("admin");
137+
});
138+
139+
ShouldFailRule(_=>
140+
{
141+
_.Query = @"query { author(input: { name: ""Quinn"" }) }";
142+
_.Schema = TypedSchema();
143+
});
144+
}
145+
112146
private ISchema BasicSchema()
113147
{
114148
var defs = @"
@@ -172,5 +206,24 @@ public class Author
172206
{
173207
public string Name { get; set;}
174208
}
209+
210+
private ISchema TypedSchema()
211+
{
212+
var query = new ObjectGraphType();
213+
query.Field<StringGraphType>(
214+
"author",
215+
arguments: new QueryArguments(new QueryArgument<AuthorInputType> { Name = "input" }),
216+
resolve: context => "testing"
217+
);
218+
return new Schema { Query = query };
219+
}
220+
221+
public class AuthorInputType : InputObjectGraphType<Author>
222+
{
223+
public AuthorInputType()
224+
{
225+
Field(x => x.Name).AuthorizeWith("FieldPolicy");
226+
}
227+
}
175228
}
176229
}

src/GraphQL.Authorization/AuthorizationValidationRule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public INodeVisitor Validate(ValidationContext context)
4141
if (argumentType == null)
4242
return;
4343

44-
var fieldType = argumentType.Fields.First(p => p.Name == objectFieldAst.Name);
44+
var fieldType = argumentType.GetField(objectFieldAst.Name);
4545
CheckAuth(objectFieldAst, fieldType, userContext, context, operationType);
4646
});
4747

0 commit comments

Comments
 (0)