Skip to content

Commit

Permalink
fix: #3910 - propagate non-model auth to nested types (#4477)
Browse files Browse the repository at this point in the history
  • Loading branch information
Attila Hajdrik authored Jun 5, 2020
1 parent 4e49e0c commit 493e631
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/graphql-auth-transformer/src/ModelAuthTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,9 @@ Static group authorization should perform as expected.`,
this.unauthPolicyResources.add(`${nonModelFieldType.name.value}/null`);
this.authPolicyResources.add(`${nonModelFieldType.name.value}/null`);
}

// Recursively process the nested types if there is any
this.propagateAuthDirectivesToNestedTypes(<ObjectTypeDefinitionNode>nonModelFieldType, rules, ctx);
}
}

Expand Down
52 changes: 52 additions & 0 deletions packages/graphql-auth-transformer/src/__tests__/MultiAuth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ const getSchemaWithNonModelField = (authDirective: string) => {
type Location {
name: String
address: Address
}
type Address {
street: String
city: String
state: String
zip: String
}
enum Status {
Expand Down Expand Up @@ -429,6 +437,7 @@ describe('Type directive transformation tests', () => {
const schemaDoc = parse(out.schema);

const locationType = getObjectType(schemaDoc, 'Location');
const addressType = getObjectType(schemaDoc, 'Address');
const expectedDirectiveNames = [userPoolsDirectiveName, apiKeyDirectiveName];

if (expectedDirectiveNames && expectedDirectiveNames.length > 0) {
Expand All @@ -440,6 +449,15 @@ describe('Type directive transformation tests', () => {
}

expect(expectedDireciveNameCount).toEqual(locationType.directives.length);

expectedDireciveNameCount = 0;

for (const expectedDirectiveName of expectedDirectiveNames) {
expect(addressType.directives.find(d => d.name.value === expectedDirectiveName)).toBeDefined();
expectedDireciveNameCount++;
}

expect(expectedDireciveNameCount).toEqual(addressType.directives.length);
}
});

Expand All @@ -451,6 +469,7 @@ describe('Type directive transformation tests', () => {
const schemaDoc = parse(out.schema);

const locationType = getObjectType(schemaDoc, 'Location');
const addressType = getObjectType(schemaDoc, 'Address');
const expectedDirectiveNames = [userPoolsDirectiveName, iamDirectiveName];

if (expectedDirectiveNames && expectedDirectiveNames.length > 0) {
Expand All @@ -463,6 +482,15 @@ describe('Type directive transformation tests', () => {

expect(expectedDireciveNameCount).toEqual(locationType.directives.length);

expectedDireciveNameCount = 0;

for (const expectedDirectiveName of expectedDirectiveNames) {
expect(addressType.directives.find(d => d.name.value === expectedDirectiveName)).toBeDefined();
expectedDireciveNameCount++;
}

expect(expectedDireciveNameCount).toEqual(addressType.directives.length);

expect(out.rootStack.Resources.AuthRolePolicy01).toBeDefined();

const locationPolicy = out.rootStack.Resources.AuthRolePolicy01.Properties.PolicyDocument.Statement[0].Resource.filter(
Expand All @@ -474,6 +502,16 @@ describe('Type directive transformation tests', () => {
r['Fn::Sub'][1].typeName === 'Location',
);
expect(locationPolicy).toBeDefined();

const addressPolicy = out.rootStack.Resources.AuthRolePolicy01.Properties.PolicyDocument.Statement[0].Resource.filter(
r =>
r['Fn::Sub'] &&
r['Fn::Sub'].length &&
r['Fn::Sub'].length === 2 &&
r['Fn::Sub'][1].typeName &&
r['Fn::Sub'][1].typeName === 'Address',
);
expect(addressPolicy).toBeDefined();
}
});

Expand All @@ -485,8 +523,10 @@ describe('Type directive transformation tests', () => {
const schemaDoc = parse(out.schema);

const locationType = getObjectType(schemaDoc, 'Location');
const addressType = getObjectType(schemaDoc, 'Address');

expect(locationType.directives.length).toBe(0);
expect(addressType.directives.length).toBe(0);

expect(out.rootStack.Resources.AuthRolePolicy01).toBeUndefined();
});
Expand All @@ -499,8 +539,10 @@ describe('Type directive transformation tests', () => {
const schemaDoc = parse(out.schema);

const locationType = getObjectType(schemaDoc, 'Location');
const addressType = getObjectType(schemaDoc, 'Address');

expect(locationType.directives.length).toBe(0);
expect(addressType.directives.length).toBe(0);

expect(out.rootStack.Resources.AuthRolePolicy01).toBeDefined();

Expand All @@ -513,6 +555,16 @@ describe('Type directive transformation tests', () => {
r['Fn::Sub'][1].typeName === 'Location',
);
expect(locationPolicy).toBeDefined();

const addressPolicy = out.rootStack.Resources.AuthRolePolicy01.Properties.PolicyDocument.Statement[0].Resource.filter(
r =>
r['Fn::Sub'] &&
r['Fn::Sub'].length &&
r['Fn::Sub'].length === 2 &&
r['Fn::Sub'][1].typeName &&
r['Fn::Sub'][1].typeName === 'Address',
);
expect(addressPolicy).toBeDefined();
});

// Disabling until troubleshooting the changes
Expand Down

0 comments on commit 493e631

Please sign in to comment.