Skip to content

Commit

Permalink
fix: add missing salt to deployment logicalId (#9234)
Browse files Browse the repository at this point in the history
* fix: add missing salt to deployment logicalId

* test: update e2e test to call rest api endpoints
  • Loading branch information
jhockett committed Dec 10, 2021
1 parent 452b115 commit d4109e2
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as lambda from '@aws-cdk/aws-lambda';
import * as cdk from '@aws-cdk/core';
import { $TSObject, JSONUtilities } from 'amplify-cli-core';
import _ from 'lodash';
import { v4 as uuid } from 'uuid';
import { ADMIN_QUERIES_NAME } from '../../../category-constants';
import { AmplifyApigwResourceTemplate, ApigwInputs, ApigwPathPolicy, Path, PermissionSetting } from './types';

Expand Down Expand Up @@ -284,7 +285,8 @@ export class AmplifyApigwResourceStack extends cdk.Stack implements AmplifyApigw
}

private _setDeploymentResource = (resourceName: string) => {
this.deploymentResource = new apigw.CfnDeployment(this, `DeploymentAPIGW${resourceName}`, {
const [shortId] = uuid().split('-');
this.deploymentResource = new apigw.CfnDeployment(this, `DeploymentAPIGW${resourceName}${shortId}`, {
description: 'The Development stage deployment of your API.',
stageName: cdk.Fn.conditionIf('ShouldNotCreateEnvResources', 'Prod', cdk.Fn.ref('env')).toString(),
restApiId: cdk.Fn.ref(resourceName),
Expand Down
1 change: 1 addition & 0 deletions packages/amplify-e2e-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"graphql-transformer-core": "7.3.0",
"jest-environment-node": "^26.6.2",
"lodash": "^4.17.21",
"node-fetch": "^2.6.1",
"node-pty-prebuilt-multiarch": "^0.9.0",
"retimer": "2.0.0",
"rimraf": "^3.0.0",
Expand Down
75 changes: 42 additions & 33 deletions packages/amplify-e2e-core/src/categories/api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as fs from 'fs-extra';
import _ from 'lodash';
import * as path from 'path';
import { addFeatureFlag, getCLIPath, nspawn as spawn, setTransformerVersionFlag, updateSchema } from '..';
import { addFeatureFlag, ExecutionContext, getCLIPath, nspawn as spawn, setTransformerVersionFlag, updateSchema } from '..';
import { multiSelect, singleSelect } from '../utils/selectors';
import { selectRuntime, selectTemplate } from './lambda-function';
import { modifiedApi } from './resources/modified-api-index';
Expand Down Expand Up @@ -446,13 +446,18 @@ export function addRestApi(cwd: string, settings: any) {
.sendCarriageReturn() // Add another path
.wait('Provide a path')
.sendLine(settings.path)
.wait('Choose a lambda source')
.sendKeyDown()
.sendCarriageReturn() // Existing lambda
.wait('Choose the Lambda function to invoke by this path');

if (settings.projectContainsFunctions) {
chain.sendCarriageReturn(); // Pick first one
.wait('Choose a lambda source');

if (settings.existingLambda) {
chain
.sendKeyDown()
.sendCarriageReturn() // Existing lambda
.wait('Choose the Lambda function to invoke by this path');
if (settings.projectContainsFunctions) {
chain.sendCarriageReturn(); // Pick first one
}
} else {
chooseLambdaFunctionForRestApi(chain, settings);
}

chain
Expand Down Expand Up @@ -482,31 +487,7 @@ export function addRestApi(cwd: string, settings: any) {
.sendCarriageReturn() // Existing lambda
.wait('Choose the Lambda function to invoke by this path'); // Expect only 1 Lambda is present
} else {
if (settings.projectContainsFunctions) {
chain.sendCarriageReturn(); // Create new Lambda function
}
chain.wait('Provide an AWS Lambda function name').sendCarriageReturn();

selectRuntime(chain, 'nodejs');

const templateName = settings.isCrud
? 'CRUD function for DynamoDB (Integration with API Gateway)'
: 'Serverless ExpressJS function (Integration with API Gateway)';
selectTemplate(chain, templateName, 'nodejs');

if (settings.isCrud) {
chain
.wait('Choose a DynamoDB data source option')
.sendCarriageReturn() // Use DDB table configured in current project
.wait('Choose from one of the already configured DynamoDB tables')
.sendCarriageReturn(); // Use first one in the list
}

chain
.wait('Do you want to configure advanced settings?')
.sendConfirmNo()
.wait('Do you want to edit the local lambda function now')
.sendConfirmNo();
chooseLambdaFunctionForRestApi(chain, settings);
}

chain.wait('Restrict API access');
Expand Down Expand Up @@ -545,6 +526,34 @@ export function addRestApi(cwd: string, settings: any) {
return chain.runAsync();
}

function chooseLambdaFunctionForRestApi(chain: ExecutionContext, settings: any) {
if (settings.projectContainsFunctions) {
chain.sendCarriageReturn(); // Create new Lambda function
}
chain.wait('Provide an AWS Lambda function name').sendCarriageReturn();

selectRuntime(chain, 'nodejs');

const templateName = settings.isCrud
? 'CRUD function for DynamoDB (Integration with API Gateway)'
: 'Serverless ExpressJS function (Integration with API Gateway)';
selectTemplate(chain, templateName, 'nodejs');

if (settings.isCrud) {
chain
.wait('Choose a DynamoDB data source option')
.sendCarriageReturn() // Use DDB table configured in current project
.wait('Choose from one of the already configured DynamoDB tables')
.sendCarriageReturn(); // Use first one in the list
}

chain
.wait('Do you want to configure advanced settings?')
.sendConfirmNo()
.wait('Do you want to edit the local lambda function now')
.sendConfirmNo();
}

const updateRestApiDefaultSettings = {
updateOperation: 'Add another path' as 'Add another path' | 'Update path' | 'Remove path',
expectMigration: false,
Expand Down
5 changes: 5 additions & 0 deletions packages/amplify-e2e-core/src/utils/request.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const https = require('https');
import fetch from 'node-fetch';

export function post({ body, ...options }) {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -27,3 +28,7 @@ export function post({ body, ...options }) {
req.end();
});
}

export async function get(url: string) {
return fetch(url);
}
30 changes: 28 additions & 2 deletions packages/amplify-e2e-tests/src/__tests__/apigw.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
addRestApi,
createNewProjectDir,
get,
initJSProjectWithProfile,
deleteProject,
deleteProjectDir,
Expand All @@ -26,21 +27,46 @@ afterAll(async () => {

describe('API Gateway e2e tests', () => {
it('adds multiple rest apis and pushes', async () => {
await addRestApi(projRoot, {});
const firstRestApi = `firstE2eRestApi${shortId}`;
const secondRestApi = `secondE2eRestApi${shortId}`;

await addRestApi(projRoot, { apiName: firstRestApi });
await amplifyPushAuth(projRoot);
await addAuthWithGroupsAndAdminAPI(projRoot); // Groups: Admins, Users
await amplifyPushAuth(projRoot);
await addRestApi(projRoot, { isFirstRestApi: false, path: '/foo', projectContainsFunctions: true });
await addRestApi(projRoot, { isFirstRestApi: false, path: '/foo', projectContainsFunctions: true }); // Add a path
await addRestApi(projRoot, {
apiName: secondRestApi,
isFirstRestApi: false,
restrictAccess: true,
allowGuestUsers: true,
hasUserPoolGroups: true,
projectContainsFunctions: true,
});
await amplifyPushAuth(projRoot); // Pushes multiple rest api updates

const projMeta = getProjectMeta(projRoot);
expect(projMeta).toBeDefined();
expect(projMeta.api).toBeDefined();
expect(projMeta.api.AdminQueries).toBeDefined();
expect(projMeta.api[firstRestApi]).toBeDefined();
expect(projMeta.api[secondRestApi]).toBeDefined();

const firstRootUrl = projMeta.api[firstRestApi].output?.RootUrl;
const secondRootUrl = projMeta.api[secondRestApi].output?.RootUrl;
expect(firstRootUrl).toBeDefined();
expect(secondRootUrl).toBeDefined();

const firstItemsResponse = await get(`${firstRootUrl}/items`);
const fooResponse = await get(`${firstRootUrl}/foo`);
const secondItemsResponse = await get(`${secondRootUrl}/items`);

const firstItemsResJson = await firstItemsResponse.json();
const fooResJson = await fooResponse.json();
const secondItemsResJson = await secondItemsResponse.json();

expect(firstItemsResJson).toEqual({ success: 'get call succeed!', url: '/items' });
expect(fooResJson).toEqual({ success: 'get call succeed!', url: '/foo' });
expect(secondItemsResJson).toEqual({ message: 'Missing Authentication Token' }); // Restricted API
});
});

0 comments on commit d4109e2

Please sign in to comment.