Skip to content

Commit

Permalink
add a test for nested policy with required arg + default value (#311)
Browse files Browse the repository at this point in the history
* add a test for nested policy with required arg + default value

* Add mandatory arg w/o default

Co-authored-by: AleF83 <[email protected]>
  • Loading branch information
thewizarodofoz and AleF83 authored May 10, 2021
1 parent 38c3364 commit 8da841a
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,26 @@ Object {
}
`;

exports[`Policies as query field mandatory_arg_with_default_nested 1`] = `
Object {
"policy": Object {
"e2e_policies_as_query_field___mandatory_arg_with_default_nested": Object {
"allow": true,
},
},
}
`;

exports[`Policies as query field mandatory_args_with_default_and_without 1`] = `
Object {
"policy": Object {
"e2e_policies_as_query_field___mandatory_args_with_default_and_without": Object {
"allow": true,
},
},
}
`;

exports[`Policies as query field no_args 1`] = `
Object {
"policy": Object {
Expand Down
64 changes: 62 additions & 2 deletions services/tests/e2e/basic/policy-as-query-field.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { print } from 'graphql';
import { gql } from 'apollo-server-core';
import { GraphQLClient } from 'graphql-request';
import { PolicyArgsDefinitions, PolicyDefinition, PolicyType } from '../../../src/modules/resource-repository';
import {
PolicyArgsDefinitions,
PolicyDefinition,
PolicyQuery,
PolicyType,
} from '../../../src/modules/resource-repository';
import GraphQLErrorSerializer from '../../utils/graphql-error-serializer';
import { updateGatewaySchema } from '../../helpers/utility';
import { RegistryMutationResponse, updatePoliciesMutation } from '../../helpers/registry-request-builder';
Expand All @@ -11,6 +16,7 @@ interface TestCase {
code: string;
args?: PolicyArgsDefinitions;
argsLiteral?: string;
query?: PolicyQuery;
}

const gatewayClient = new GraphQLClient('http://localhost:8080/graphql');
Expand Down Expand Up @@ -118,6 +124,59 @@ const testCases: [string, TestCase][] = [
},
},
],
[
'mandatory_args_with_default_and_without',
{
code: `
default allow = false
allow {
input.args.arg1
input.args.arg2
}
`,
args: {
arg1: {
type: 'Boolean!',
default: '{true}',
},
arg2: {
type: 'Boolean!',
},
},
argsLiteral: '(arg2: true)',
},
],
[
'mandatory_arg_with_default_nested',
{
code: `
default allow = false
allow {
input.query.policy.e2e_policies_as_query_field___mandatory_args_with_default_and_without.allow
}
`,
args: {
arg2: {
type: 'Boolean!',
},
},
query: {
gql: print(gql`
query($arg2: Boolean!) {
policy {
e2e_policies_as_query_field___mandatory_args_with_default_and_without(arg2: $arg2) {
allow
}
}
}
`),
variables: {
arg2: '{arg2}',
},
},
argsLiteral: '(arg2: true)',
},
],
];

describe('Policies as query field', () => {
Expand All @@ -131,14 +190,15 @@ describe('Policies as query field', () => {
});

test('Setup policies', async () => {
const policies: PolicyDefinition[] = testCases.map(([name, { code, args }]) => ({
const policies: PolicyDefinition[] = testCases.map(([name, { code, args, query }]) => ({
metadata: {
namespace,
name,
},
type: PolicyType.opa,
code,
args,
query,
}));

const policiesResponse = await registryClient.request<RegistryMutationResponse>(updatePoliciesMutation, {
Expand Down

0 comments on commit 8da841a

Please sign in to comment.