Skip to content

Commit

Permalink
fix: vtl list utils & update model test scenarios (#8701)
Browse files Browse the repository at this point in the history
  • Loading branch information
SwaySway committed Nov 7, 2021
1 parent f798bbf commit 402cd9b
Show file tree
Hide file tree
Showing 9 changed files with 741 additions and 93 deletions.
@@ -1,4 +1,5 @@
import { create } from '../../../velocity/util/index';
import { map as valueMap } from '../../../velocity/value-mapper/mapper';
import { GraphQLResolveInfo } from 'graphql';
import { map, random } from 'lodash';
import { AppSyncGraphQLExecutionContext } from '../../../utils/graphql-runner';
Expand All @@ -19,17 +20,27 @@ beforeEach(() => {
});

describe('$utils.list.copyAndRetainAll', () => {
it('should retain', () => {
it('should retain numbers list', () => {
const myList = [1, 2, 3, 4, 5];
expect(util.list.copyAndRetainAll(myList, [2, 4])).toEqual([2, 4]);
});
it('should retain java array of java strings', () => {
const myList = valueMap(['foo', 'bar', 'baz', 'qux']);
const result = util.list.copyAndRetainAll(myList, valueMap(['foo', 'bar']));
expect(result.toJSON()).toEqual(['foo', 'bar']);
});
});

describe('$utils.list.copyAndRemoveAll', () => {
it('should remove', () => {
it('should remove numbers', () => {
const myList = [1, 2, 3, 4, 5];
expect(util.list.copyAndRemoveAll(myList, [2, 4])).toEqual([1, 3, 5]);
});
it('should remove java array of java strings', () => {
const myList = valueMap(['foo', 'bar', 'baz', 'qux']);
const result = util.list.copyAndRemoveAll(myList, valueMap(['bar', 'qux']));
expect(result.toJSON()).toEqual(['foo', 'baz']);
});
});

describe('$utils.list.sortList', () => {
Expand Down
Expand Up @@ -18,8 +18,19 @@ export type JWTToken = {
nbf?: number;
username?: string;
email?: string;
groups?: string[];
'cognito:username'?: string;
'cognitio:groups'?: string[];
'cognito:groups'?: string[];
};

export type IAMToken = {
accountId: string;
userArn: string;
username: string;
cognitoIdentityPoolId?: string;
cognitoIdentityId?: string;
cognitoIdentityAuthType?: string;
cognitoIdentityAuthProvider?: string;
};

export function extractJwtToken(authorization: string): JWTToken {
Expand Down
@@ -1,9 +1,10 @@
import { JWTToken } from '../auth-helpers/helpers';
import { JWTToken, IAMToken } from '../auth-helpers/helpers';
import { AmplifyAppSyncSimulatorAuthenticationType } from '../../type-definition';

export type AppSyncGraphQLExecutionContext = {
readonly jwt?: JWTToken;
readonly sourceIp?: string;
readonly jwt?: JWTToken;
readonly iamToken?: IAMToken;
headers: Record<string, string | string[]>;
appsyncErrors?: Error[];
requestAuthorizationMode: AmplifyAppSyncSimulatorAuthenticationType;
Expand Down
15 changes: 13 additions & 2 deletions packages/amplify-appsync-simulator/src/velocity/index.ts
Expand Up @@ -99,7 +99,7 @@ export class VelocityTemplate {
info: GraphQLResolveInfo,
): any {
const { source, arguments: argument, result, stash, prevResult, error } = ctxValues;
const { jwt } = requestContext;
const { jwt, sourceIp, iamToken } = requestContext;
const { iss: issuer, sub, 'cognito:username': cognitoUserName, username } = jwt || {};

const util = createUtil([], new Date(Date.now()), info, requestContext);
Expand All @@ -110,21 +110,32 @@ export class VelocityTemplate {
identity = convertToJavaTypes({
sub,
issuer,
sourceIp,
claims: requestContext.jwt,
});
} else if (requestContext.requestAuthorizationMode === AmplifyAppSyncSimulatorAuthenticationType.AMAZON_COGNITO_USER_POOLS) {
identity = convertToJavaTypes({
sub,
issuer,
sourceIp,
'cognito:username': cognitoUserName,
username: username || cognitoUserName,
sourceIp: requestContext.sourceIp,
claims: requestContext.jwt,
...(this.simulatorContext.appSyncConfig.defaultAuthenticationType.authenticationType ===
AmplifyAppSyncSimulatorAuthenticationType.AMAZON_COGNITO_USER_POOLS
? { defaultAuthStrategy: 'ALLOW' }
: {}),
});
} else if (requestContext.requestAuthorizationMode === AmplifyAppSyncSimulatorAuthenticationType.AWS_IAM) {
identity = convertToJavaTypes({
sourceIp,
username: iamToken.username,
userArn: iamToken.userArn,
cognitoIdentityPoolId: iamToken?.cognitoIdentityPoolId,
cognitoIdentityId: iamToken?.cognitoIdentityId,
cognitoIdentityAuthType: iamToken?.cognitoIdentityAuthType,
cognitoIdentityAuthProvider: iamToken?.cognitoIdentityAuthProvider,
});
}

const vtlContext = {
Expand Down
16 changes: 14 additions & 2 deletions packages/amplify-appsync-simulator/src/velocity/util/list-utils.ts
@@ -1,11 +1,23 @@
import { identity, isObject, negate, orderBy, some } from 'lodash';
import { JavaArray } from '../value-mapper/array';
import { map as valueMap } from '../value-mapper/mapper';

export const listUtils = {
copyAndRetainAll(list: any[], intersect: any[]) {
return list.filter(value => intersect.indexOf(value) !== -1);
if (list instanceof JavaArray && intersect instanceof JavaArray) {
return valueMap(list.toJSON().filter(value => intersect.toJSON().includes(value)));
} else {
return list.filter(value => intersect.indexOf(value) !== -1);
}
},
copyAndRemoveAll(list: any[], toRemove: any[]) {
return list.filter(value => toRemove.indexOf(value) === -1);
if (list instanceof JavaArray && toRemove instanceof JavaArray) {
// we convert back to js array to filter and then re-create as java array when filtering is done
// this is avoid using filtering within the java array is that can create a '0' entry
return valueMap(list.toJSON().filter(value => !toRemove.toJSON().includes(value)));
} else {
return list.filter(value => toRemove.indexOf(value) === -1);
}
},
sortList(list: any[], desc: boolean, property: string) {
if (list.length === 0 || list.length > 1000) {
Expand Down

This file was deleted.

0 comments on commit 402cd9b

Please sign in to comment.