Skip to content

Commit

Permalink
test: fix velocity test for new default
Browse files Browse the repository at this point in the history
  • Loading branch information
danielleadams committed Apr 15, 2022
1 parent bc86f51 commit 0e91a17
Show file tree
Hide file tree
Showing 6 changed files with 1,464 additions and 38 deletions.
6 changes: 4 additions & 2 deletions packages/amplify-util-mock/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
"test-watch": "jest --watch",
"build": "tsc",
"watch": "tsc -w",
"clean": "rimraf lib tsconfig.tsbuildinfo"
"clean": "rimraf lib tsconfig.tsbuildinfo",
"velocity": "jest ./src/__tests__/velocity/*.test.ts",
"jest": "jest"
},
"dependencies": {
"@hapi/topo": "^5.0.0",
Expand Down Expand Up @@ -116,4 +118,4 @@
"usePathForSuiteName": "true",
"addFileAttribute": "true"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import { ModelTransformer } from '@aws-amplify/graphql-model-transformer';
import { GraphQLTransform } from '@aws-amplify/graphql-transformer-core';
import { AppSyncAuthConfiguration } from '@aws-amplify/graphql-transformer-interfaces';
import { AmplifyAppSyncSimulatorAuthenticationType, AppSyncGraphQLExecutionContext } from 'amplify-appsync-simulator';
import { VelocityTemplateSimulator, AppSyncVTLContext, getJWTToken, getIAMToken } from '../../velocity';
import {
VelocityTemplateSimulator, AppSyncVTLContext, getIAMToken,
} from '../../velocity';
import { featureFlags } from './test-helper';

jest.mock('amplify-prompts');

describe('admin roles query checks', () => {
const ADMIN_UI_ROLE = 'us-fake-1_uuid_Full-access/CognitoIdentityCredentials';
Expand Down Expand Up @@ -34,12 +39,13 @@ describe('admin roles query checks', () => {
adminRoles: [ADMIN_UI_ROLE],
}),
],
featureFlags,
});

vtlTemplate = new VelocityTemplateSimulator({ authConfig });
});

test('test schema with field auth', () => {
test('schema with field auth', () => {
const validSchema = `
type Student @model @auth(rules: [{ allow: groups, groups: ["staff"] }, { allow: owner }]) {
id: ID!
Expand Down Expand Up @@ -85,3 +91,90 @@ describe('admin roles query checks', () => {
expect(createStudentResponse.result).toEqual('{}');
});
});

describe('identity claim feature flag disabled', () => {
describe('admin roles query checks', () => {
const ADMIN_UI_ROLE = 'us-fake-1_uuid_Full-access/CognitoIdentityCredentials';
let vtlTemplate: VelocityTemplateSimulator;
let transformer: GraphQLTransform;
const adminFullAccessRequest: AppSyncGraphQLExecutionContext = {
requestAuthorizationMode: AmplifyAppSyncSimulatorAuthenticationType.AWS_IAM,
iamToken: getIAMToken('us-fake-1_uuid_Full-access'),
headers: {},
};

beforeEach(() => {
const authConfig: AppSyncAuthConfiguration = {
defaultAuthentication: {
authenticationType: 'AMAZON_COGNITO_USER_POOLS',
},
additionalAuthenticationProviders: [
{
authenticationType: 'AWS_IAM',
},
],
};
transformer = new GraphQLTransform({
authConfig,
transformers: [
new ModelTransformer(),
new AuthTransformer({
adminRoles: [ADMIN_UI_ROLE],
}),
],
featureFlags: {
...featureFlags,
...{ getBoolean: () => false },
},
});

vtlTemplate = new VelocityTemplateSimulator({ authConfig });
});

test('schema with field auth', () => {
const validSchema = `
type Student @model @auth(rules: [{ allow: groups, groups: ["staff"] }, { allow: owner }]) {
id: ID!
name: String
description: String
secretValue: String @auth(rules: [{ allow: owner }])
}`;
const out = transformer.transform(validSchema);

// field resolver
const secretValueTemplate = out.resolvers['Student.secretValue.req.vtl'];
const iamFieldContext: AppSyncVTLContext = {
source: {
secretValue: 'secretValue001',
},
};

const secretValueResponse = vtlTemplate.render(secretValueTemplate, {
context: iamFieldContext,
requestParameters: adminFullAccessRequest,
});
expect(secretValueResponse.hadException).toEqual(false);
expect(secretValueResponse.result).toEqual('secretValue001');

// mutation resolver
const createStudentTemplate = out.resolvers['Mutation.createStudent.auth.1.req.vtl'];
const iamCreateContext: AppSyncVTLContext = {
arguments: {
input: {
id: '001',
name: 'student0',
owner: 'student0',
},
},
};
const createStudentResponse = vtlTemplate.render(createStudentTemplate, {
context: iamCreateContext,
requestParameters: adminFullAccessRequest,
});

expect(createStudentResponse.hadException).toEqual(false);
// we can exit early with a object since the next function will run the mutation request
expect(createStudentResponse.result).toEqual('{}');
});
});
});
Loading

0 comments on commit 0e91a17

Please sign in to comment.