From 36eaa20649bf24f34bca6910926cd1dccd1a5496 Mon Sep 17 00:00:00 2001 From: ykethan Date: Thu, 4 Aug 2022 22:38:23 +0000 Subject: [PATCH] added nodejs appsync test --- .../src/categories/lambda-function.ts | 6 ++ packages/amplify-e2e-tests/Readme.md | 10 +-- .../src/__tests__/function_11.test.ts | 85 ++++++++++--------- 3 files changed, 58 insertions(+), 43 deletions(-) diff --git a/packages/amplify-e2e-core/src/categories/lambda-function.ts b/packages/amplify-e2e-core/src/categories/lambda-function.ts index a4347b9a7e8..73046ecf808 100644 --- a/packages/amplify-e2e-core/src/categories/lambda-function.ts +++ b/packages/amplify-e2e-core/src/categories/lambda-function.ts @@ -32,6 +32,7 @@ const nodeJSTemplateChoices = [ 'Hello World', 'Lambda trigger', 'Serverless ExpressJS function (Integration with API Gateway)', + 'AppSync Todo', ]; const pythonTemplateChoices = ['Hello World']; @@ -358,6 +359,11 @@ export const selectTemplate = (chain: ExecutionContext, functionTemplate: string moveUp(chain, templateChoices.indexOf('Hello World')); singleSelect(chain, functionTemplate, templateChoices); + + if(functionTemplate === 'AppSync Todo'){ + chain.wait('Pick a Auth type'); + singleSelect(chain, 'API_KEY', ['API_KEY', 'IAM']); + } }; export const removeFunction = (cwd: string, funcName: string) => diff --git a/packages/amplify-e2e-tests/Readme.md b/packages/amplify-e2e-tests/Readme.md index 2e6412f5f9b..246a2714e17 100644 --- a/packages/amplify-e2e-tests/Readme.md +++ b/packages/amplify-e2e-tests/Readme.md @@ -35,10 +35,10 @@ env VERBOSE_LOGGING_DO_NOT_USE_IN_CI_OR_YOU_WILL_BE_FIRED=true yarn e2e ``` ```typescript -import { amplifyPush, deleteProject, initJSProjectWithProfile } from "../init"; -import { createNewProjectDir, deleteProjectDir, getProjectMeta } from "../utils"; +import { amplifyPush, deleteProject, initJSProjectWithProfile } from '../init'; +import { createNewProjectDir, deleteProjectDir, getProjectMeta } from '../utils'; -describe("amplify your test", () => { +describe('amplify your test', () => { let projRoot: string; beforeEach(() => { projRoot = createNewProjectDir(); // create a new project for each test @@ -50,8 +50,8 @@ describe("amplify your test", () => { deleteProjectDir(projRoot); // delete the project directory }); - it("", async () => { - await initJSProjectWithProfile(projRoot, { name: "" }); + it('', async () => { + await initJSProjectWithProfile(projRoot, { name: '' }); // add resources that you want to test await amplifyPush(projRoot); // Push it to the cloud const { output } = getProjectMeta(projRoot).api.simplemodel; diff --git a/packages/amplify-e2e-tests/src/__tests__/function_11.test.ts b/packages/amplify-e2e-tests/src/__tests__/function_11.test.ts index 2fac3aa0d4d..eb8ebd1a049 100644 --- a/packages/amplify-e2e-tests/src/__tests__/function_11.test.ts +++ b/packages/amplify-e2e-tests/src/__tests__/function_11.test.ts @@ -8,7 +8,11 @@ import { getBackendConfig, initJSProjectWithProfile, getProjectMeta, - getFunction + getFunction, + functionBuild, + amplifyPushAuth, + invokeFunction + } from "@aws-amplify/amplify-e2e-core"; import { v4 as uuid } from "uuid"; import path from "path"; @@ -30,54 +34,59 @@ describe("Lambda AppSync nodejs", () => { }); it.only("add nodejs appsync function", async () => { + await initJSProjectWithProfile(projRoot, {}); + + await addApi(projRoot, { + 'API key': {}, + transformerVersion: 2 + }); + await amplifyPush(projRoot); - // await addApi(projRoot, { - // "API key": {}, - // transformerVersion: 2 - // }); - // await amplifyPush(projRoot); - - // expect(getBackendConfig(projRoot)).toBeDefined(); + expect(getBackendConfig(projRoot)).toBeDefined(); - // var beforeMeta = getBackendConfig(projRoot); - // console.log(beforeMeta); - // const apiName = Object.keys(beforeMeta.api)[0]; - // console.log(apiName); - - // expect(apiName + " abc").toContain("lambdaappsyncnodejs4"); + var beforeMeta = getBackendConfig(projRoot); + console.log(beforeMeta); + console.log(Object.keys(beforeMeta.api.lambdaappsyncnodejsa.output)) + const apiName = Object.keys(beforeMeta.api)[0]; + console.log(apiName); - const [shortId] = uuid().split("-"); - const funcName = `lambdaappsync${shortId}`; + expect(apiName).toBeDefined(); + await addFunction( - projRoot, - { - name: funcName, - functionTemplate: "AppSync Todo" - // additionalPermissions: { - // permissions: ["api"], - // choices: ["api"], - // resources: ["api"], - // operations: ["Query"], - // actions: ["API_KEY"] - // } - }, - "nodejs" - ); - - const beforeMeta = getBackendConfig(projRoot); - console.log(beforeMeta); + projRoot, + { + functionTemplate: 'AppSync Todo', + additionalPermissions: { + permissions: ['api'], + choices: ['api'], + resources: [apiName], + operations: ['Query'], + }, + }, + 'nodejs' + ); + await functionBuild(projRoot, {}); + await amplifyPushAuth(projRoot); const meta = getProjectMeta(projRoot); - const { Arn: functionArn, Name: functionName, Region: region, CloudWatchEventRule: ruleName } = Object.keys(meta.function).map( - key => meta.function[key] - )[0].output; + const { Arn: functionArn, Name: functionName, Region: region } = Object.keys(meta.function).map(key => meta.function[key])[0].output; expect(functionArn).toBeDefined(); expect(functionName).toBeDefined(); expect(region).toBeDefined(); - expect(ruleName).toBeUndefined(); - const cloudFunction = await getFunction(functionName, region); expect(cloudFunction.Configuration.FunctionArn).toEqual(functionArn); + + + const payloadObj = {test1: 'hello' }; + const fnResponse = await invokeFunction(functionName, JSON.stringify(payloadObj), region); + + console.log(fnResponse); + expect(fnResponse.StatusCode).toBe(200); + // expect(fnResponse.Payload).toBeDefined(); + // const gqlResponse = JSON.parse(fnResponse.Payload as string); + + // expect(gqlResponse.data).toBeDefined(); + // expect(gqlResponse.data.createTodo.name).toEqual('todo'); }); });