Skip to content

Commit

Permalink
Added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
queil committed Jun 13, 2022
1 parent 1ca4126 commit e97e2b7
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,30 @@ public async Task DefaultPolicy_NotFound(Action<IRequestExecutorBuilder> configu
result.MatchSnapshot();
}

[Theory]
[ClassData(typeof(AuthorizationTestData))]
[ClassData(typeof(AuthorizationAttributeTestData))]
public async Task Policy_NotFound(Action<IRequestExecutorBuilder> configure)
{
// arrange
TestServer server = CreateTestServer(
builder =>
{
configure(builder);
builder.Services.AddAuthorization();

},
SetUpHttpContext);

// act
ClientQueryResult result =
await server.PostAsync(new ClientQueryRequest { Query = "{ age }" });

// assert
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
result.MatchSnapshot();
}

[Theory]
[ClassData(typeof(AuthorizationTestData))]
[ClassData(typeof(AuthorizationAttributeTestData))]
Expand All @@ -72,7 +96,44 @@ public async Task Policy_NotAuthorized(Action<IRequestExecutorBuilder> configure
SetUpHttpContext + (Action<HttpContext>)(c =>
{
c.Request.Headers["Authorization"] =
"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c";
"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lI" +
"iwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c";
}));

var hasAgeDefinedPolicy = await File.ReadAllTextAsync("policies/has_age_defined.rego");
using var client = new HttpClient { BaseAddress = new Uri("http://127.0.0.1:8181") };

HttpResponseMessage putPolicyResponse = await client.PutAsync("/v1/policies/has_age_defined", new StringContent(hasAgeDefinedPolicy));
putPolicyResponse.EnsureSuccessStatusCode();

// act
ClientQueryResult result =
await server.PostAsync(new ClientQueryRequest { Query = "{ age }" });

// assert
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
result.MatchSnapshot();
}

[Theory]
[ClassData(typeof(AuthorizationTestData))]
[ClassData(typeof(AuthorizationAttributeTestData))]
public async Task Policy_Authorized(Action<IRequestExecutorBuilder> configure)
{
// arrange
TestServer server = CreateTestServer(
builder =>
{
configure(builder);
builder.Services.AddAuthorization();

},
SetUpHttpContext + (Action<HttpContext>)(c =>
{
c.Request.Headers["Authorization"] =
"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lI" +
"iwiaWF0IjoxNTE2MjM5MDIyLCJiaXJ0aGRhdGUiOiIxNy0xMS0yMDAwIn0.p88IUnrabPMh6LVi4DIYsDeZozjfj4Ofwg" +
"jXBglnxac";
}));

var hasAgeDefinedPolicy = await File.ReadAllTextAsync("policies/has_age_defined.rego");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package graphql.authz.has_age_defined
# Warning: this policy exists only for testing purposes.
# How to correctly validate JWT tokens in OPA: https://www.openpolicyagent.org/docs/latest/oauth-oidc/

package graphql.authz.has_age_defined

import input.request

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
"Errors": [
{
"message": "The `HasAgeDefined` authorization policy does not exist.",
"message": "The `graphql/authz/has_age_defined/allow` authorization policy does not exist.",
"locations": [
{
"line": 1,
Expand Down

0 comments on commit e97e2b7

Please sign in to comment.