Skip to content

Commit

Permalink
test(api): split api_5 test into two (#9782)
Browse files Browse the repository at this point in the history
* test(api): split api_5 test into two

* test(api): split api_5 test into two

* correct file name

* correct file name

* address code review comments
  • Loading branch information
sundersc committed Feb 16, 2022
1 parent 0c6508b commit 2bd4172
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 105 deletions.
36 changes: 35 additions & 1 deletion packages/amplify-e2e-core/src/categories/api.ts
@@ -1,7 +1,7 @@
import * as fs from 'fs-extra';
import _ from 'lodash';
import * as path from 'path';
import { addFeatureFlag, ExecutionContext, getCLIPath, nspawn as spawn, setTransformerVersionFlag, updateSchema } from '..';
import { addFeatureFlag, checkIfBucketExists, ExecutionContext, getCLIPath, getProjectMeta, 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 @@ -850,3 +850,37 @@ export function cancelAmplifyMockApi(cwd: string, settings: any = {}): Promise<v
});
});
}

export async function validateRestApiMeta(projRoot: string, meta?: any) {
meta = meta ?? getProjectMeta(projRoot);
expect(meta.providers.awscloudformation).toBeDefined();
const {
AuthRoleArn: authRoleArn,
UnauthRoleArn: unauthRoleArn,
DeploymentBucketName: bucketName,
Region: region,
StackId: stackId,
} = meta.providers.awscloudformation;
expect(authRoleArn).toBeDefined();
expect(unauthRoleArn).toBeDefined();
expect(region).toBeDefined();
expect(stackId).toBeDefined();
const bucketExists = await checkIfBucketExists(bucketName, region);
expect(bucketExists).toMatchObject({});

expect(meta.function).toBeDefined();
let seenAtLeastOneFunc = false;
for (let key of Object.keys(meta.function)) {
const { service, build, lastBuildTimeStamp, lastPackageTimeStamp, distZipFilename, lastPushTimeStamp, lastPushDirHash } =
meta.function[key];
expect(service).toBe('Lambda');
expect(build).toBeTruthy();
expect(lastBuildTimeStamp).toBeDefined();
expect(lastPackageTimeStamp).toBeDefined();
expect(distZipFilename).toBeDefined();
expect(lastPushTimeStamp).toBeDefined();
expect(lastPushDirHash).toBeDefined();
seenAtLeastOneFunc = true;
}
expect(seenAtLeastOneFunc).toBe(true);
};
109 changes: 5 additions & 104 deletions packages/amplify-e2e-tests/src/__tests__/api_5.test.ts
@@ -1,30 +1,18 @@
import {
addApiWithoutSchema,
addFeatureFlag,
addFunction,
addRestApi,
addSimpleDDB,
amplifyPushGraphQlWithCognitoPrompt,
amplifyPushUpdate,
checkIfBucketExists,
createNewProjectDir,
deleteProject,
deleteProjectDir,
getAppSyncApi,
getProjectMeta,
initJSProjectWithProfile,
listAttachedRolePolicies,
listRolePolicies,
updateApiSchema,
updateAuthAddAdminQueries,
validateRestApiMeta,
} from 'amplify-e2e-core';
import { readdirSync, readFileSync } from 'fs';
import * as path from 'path';

// to deal with bug in cognito-identity-js
(global as any).fetch = require('node-fetch');
// to deal with subscriptions in node env
(global as any).WebSocket = require('ws');

describe('amplify add api (REST)', () => {
let projRoot: string;
Expand All @@ -37,40 +25,6 @@ describe('amplify add api (REST)', () => {
deleteProjectDir(projRoot);
});

const validateMeta = async (meta?) => {
meta = meta ?? getProjectMeta(projRoot);
expect(meta.providers.awscloudformation).toBeDefined();
const {
AuthRoleArn: authRoleArn,
UnauthRoleArn: unauthRoleArn,
DeploymentBucketName: bucketName,
Region: region,
StackId: stackId,
} = meta.providers.awscloudformation;
expect(authRoleArn).toBeDefined();
expect(unauthRoleArn).toBeDefined();
expect(region).toBeDefined();
expect(stackId).toBeDefined();
const bucketExists = await checkIfBucketExists(bucketName, region);
expect(bucketExists).toMatchObject({});

expect(meta.function).toBeDefined();
let seenAtLeastOneFunc = false;
for (let key of Object.keys(meta.function)) {
const { service, build, lastBuildTimeStamp, lastPackageTimeStamp, distZipFilename, lastPushTimeStamp, lastPushDirHash } =
meta.function[key];
expect(service).toBe('Lambda');
expect(build).toBeTruthy();
expect(lastBuildTimeStamp).toBeDefined();
expect(lastPackageTimeStamp).toBeDefined();
expect(distZipFilename).toBeDefined();
expect(lastPushTimeStamp).toBeDefined();
expect(lastPushDirHash).toBeDefined();
seenAtLeastOneFunc = true;
}
expect(seenAtLeastOneFunc).toBe(true);
};

it('init a project, add a DDB, then add a crud rest api', async () => {
const randomId = await global.getRandomId();
const DDB_NAME = `ddb${randomId}`;
Expand All @@ -85,22 +39,22 @@ describe('amplify add api (REST)', () => {
expect(service).toBe('DynamoDB');
expect(lastPushTimeStamp).toBeDefined();
expect(lastPushDirHash).toBeDefined();
validateMeta(meta);
validateRestApiMeta(projRoot, meta);
});

it('init a project, then add a serverless rest api', async () => {
await initJSProjectWithProfile(projRoot, {});
await addRestApi(projRoot, { isCrud: false });
await amplifyPushUpdate(projRoot);
validateMeta();
validateRestApiMeta(projRoot);
});

it('init a project, create lambda and attach it to an api', async () => {
await initJSProjectWithProfile(projRoot, {});
await addFunction(projRoot, { functionTemplate: 'Hello World' }, 'nodejs');
await addRestApi(projRoot, { existingLambda: true });
await amplifyPushUpdate(projRoot);
validateMeta();
validateRestApiMeta(projRoot);
});

it('init a project, create lambda and attach multiple rest apis', async () => {
Expand Down Expand Up @@ -159,60 +113,7 @@ describe('amplify add api (REST)', () => {
expect(PolicyName).toMatch(/PolicyAPIGWUnauth\d/);
}

validateMeta(amplifyMeta);
validateRestApiMeta(projRoot, amplifyMeta);
});

it('adds a rest api and then adds a path to the existing api', async () => {
await initJSProjectWithProfile(projRoot, {});
await addFunction(projRoot, { functionTemplate: 'Hello World' }, 'nodejs');
await addRestApi(projRoot, { existingLambda: true });
await addRestApi(projRoot, { isFirstRestApi: false, existingLambda: true, path: '/newpath' });
await amplifyPushUpdate(projRoot);
validateMeta();
});

it('migrates malformed project files during push', async () => {
await initJSProjectWithProfile(projRoot, {});
await addFunction(projRoot, { functionTemplate: 'Hello World' }, 'nodejs');
await addRestApi(projRoot, { existingLambda: true, restrictAccess: true });

const apisDirectory = path.join(projRoot, 'amplify', 'backend', 'api');
const apis = readdirSync(apisDirectory);
const apiName = apis[0];
const apiDirectory = path.join(apisDirectory, apiName);
const cfnTemplateFile = path.join(apiDirectory, 'build', `${apiName}-cloudformation-template.json`);
const cfnTemplate = JSON.parse(readFileSync(cfnTemplateFile, 'utf8'));

// The ApiId output is required
expect(cfnTemplate.Outputs.ApiId).toBeDefined();

await amplifyPushUpdate(projRoot);
validateMeta();
});

it('amplify push prompt for cognito configuration if auth mode is missing', async () => {
const envName = 'devtest';
const projName = 'lambdaauthmode';
await initJSProjectWithProfile(projRoot, { name: projName, envName });
await addFeatureFlag(projRoot, 'graphqltransformer', 'useexperimentalpipelinedtransformer', true);
await addFeatureFlag(projRoot, 'graphqltransformer', 'transformerversion', 2);
await addApiWithoutSchema(projRoot, { transformerVersion: 1 });
await addFunction(projRoot, { functionTemplate: 'Hello World' }, 'nodejs');
await updateApiSchema(projRoot, projName, 'cognito_simple_model.graphql');
await amplifyPushGraphQlWithCognitoPrompt(projRoot);

const meta = getProjectMeta(projRoot);
const region = meta.providers.awscloudformation.Region;
const { output } = meta.api.lambdaauthmode;
const { GraphQLAPIIdOutput, GraphQLAPIEndpointOutput, GraphQLAPIKeyOutput } = output;
const { graphqlApi } = await getAppSyncApi(GraphQLAPIIdOutput, region);

expect(GraphQLAPIIdOutput).toBeDefined();
expect(GraphQLAPIEndpointOutput).toBeDefined();
expect(GraphQLAPIKeyOutput).toBeDefined();

expect(graphqlApi).toBeDefined();
expect(graphqlApi.apiId).toEqual(GraphQLAPIIdOutput);
validateMeta(meta);
});
});
74 changes: 74 additions & 0 deletions packages/amplify-e2e-tests/src/__tests__/api_8.test.ts
@@ -0,0 +1,74 @@
import {
addApiWithoutSchema,
addFeatureFlag,
addFunction,
addRestApi,
amplifyPushGraphQlWithCognitoPrompt,
amplifyPushUpdate,
createNewProjectDir,
deleteProject,
deleteProjectDir,
getAppSyncApi,
getProjectMeta,
initJSProjectWithProfile,
updateApiSchema,
validateRestApiMeta,
} from 'amplify-e2e-core';
import { readdirSync, readFileSync } from 'fs';
import * as path from 'path';

describe('amplify add api (REST and GRAPHQL)', () => {
let projRoot: string;
beforeEach(async () => {
projRoot = await createNewProjectDir('rest-api');
});

afterEach(async () => {
await deleteProject(projRoot);
deleteProjectDir(projRoot);
});

it('adds a rest api and then adds a path to the existing api', async () => {
await initJSProjectWithProfile(projRoot, {});
await addFunction(projRoot, { functionTemplate: 'Hello World' }, 'nodejs');
await addRestApi(projRoot, { existingLambda: true });
await addRestApi(projRoot, { isFirstRestApi: false, existingLambda: true, path: '/newpath' });
await amplifyPushUpdate(projRoot);
validateRestApiMeta(projRoot);

const apisDirectory = path.join(projRoot, 'amplify', 'backend', 'api');
const apis = readdirSync(apisDirectory);
const apiName = apis[0];
const apiDirectory = path.join(apisDirectory, apiName);
const cfnTemplateFile = path.join(apiDirectory, 'build', `${apiName}-cloudformation-template.json`);
const cfnTemplate = JSON.parse(readFileSync(cfnTemplateFile, 'utf8'));
// The ApiId output is required
expect(cfnTemplate.Outputs.ApiId).toBeDefined();
});

it('amplify push prompt for cognito configuration if auth mode is missing', async () => {
const envName = 'devtest';
const projName = 'lambdaauthmode';
await initJSProjectWithProfile(projRoot, { name: projName, envName });
await addFeatureFlag(projRoot, 'graphqltransformer', 'useexperimentalpipelinedtransformer', true);
await addFeatureFlag(projRoot, 'graphqltransformer', 'transformerversion', 2);
await addApiWithoutSchema(projRoot, { transformerVersion: 1 });
await addFunction(projRoot, { functionTemplate: 'Hello World' }, 'nodejs');
await updateApiSchema(projRoot, projName, 'cognito_simple_model.graphql');
await amplifyPushGraphQlWithCognitoPrompt(projRoot);

const meta = getProjectMeta(projRoot);
const region = meta.providers.awscloudformation.Region;
const { output } = meta.api.lambdaauthmode;
const { GraphQLAPIIdOutput, GraphQLAPIEndpointOutput, GraphQLAPIKeyOutput } = output;
const { graphqlApi } = await getAppSyncApi(GraphQLAPIIdOutput, region);

expect(GraphQLAPIIdOutput).toBeDefined();
expect(GraphQLAPIEndpointOutput).toBeDefined();
expect(GraphQLAPIKeyOutput).toBeDefined();

expect(graphqlApi).toBeDefined();
expect(graphqlApi.apiId).toEqual(GraphQLAPIIdOutput);
validateRestApiMeta(projRoot, meta);
});
});

0 comments on commit 2bd4172

Please sign in to comment.