|
1 | 1 | import { describe, it } from 'node:test';
|
2 | 2 | import { RestApiConstruct } from './construct.js';
|
3 | 3 | import { App, Stack } from 'aws-cdk-lib';
|
4 |
| -import { Template } from 'aws-cdk-lib/assertions'; |
5 |
| -import * as lambda from 'aws-cdk-lib/aws-lambda'; |
6 |
| -import { strictEqual } from 'assert'; |
7 |
| -import * as fs from 'fs'; |
| 4 | +import { defineFunction } from '@aws-amplify/backend'; |
| 5 | +import { StackMetadataBackendOutputStorageStrategy } from '@aws-amplify/backend-output-storage'; |
| 6 | +import { |
| 7 | + ConstructContainerStub, |
| 8 | + ResourceNameValidatorStub, |
| 9 | + StackResolverStub, |
| 10 | +} from '@aws-amplify/backend-platform-test-stubs'; |
| 11 | +import { ConstructFactoryGetInstanceProps } from '@aws-amplify/plugin-types'; |
8 | 12 |
|
9 | 13 | void describe('RestApiConstruct Lambda Handling', () => {
|
10 |
| - void it('loads an existing local lambda function if the directory is specified', () => { |
| 14 | + void it('integrates the result of defineFunction into the api', () => { |
11 | 15 | const app = new App();
|
12 | 16 | const stack = new Stack(app);
|
13 |
| - new RestApiConstruct(stack, 'RestApiLambdaTest', { |
14 |
| - apiName: 'RestApiLambdaTest', |
15 |
| - apiProps: [ |
16 |
| - { |
17 |
| - path: 'items', |
18 |
| - defaultAuthorizer: { type: 'none' }, |
19 |
| - methods: [ |
20 |
| - { |
21 |
| - method: 'GET', |
22 |
| - authorizer: { type: 'none' }, |
23 |
| - }, |
24 |
| - ], |
25 |
| - lambdaEntry: { |
26 |
| - runtime: lambda.Runtime.NODEJS_18_X, |
27 |
| - source: { path: 'packages/rest-api-construct/lib/test-assets' }, |
28 |
| - }, |
29 |
| - }, |
30 |
| - ], |
31 |
| - }); |
32 |
| - const template = Template.fromStack(stack); |
33 |
| - template.hasResourceProperties('AWS::Lambda::Function', { |
34 |
| - Handler: 'index.handler', |
35 |
| - }); |
36 |
| - }); |
37 |
| - |
38 |
| - void it('creates a new lambda function if the code is specified', () => { |
39 |
| - const app = new App(); |
40 |
| - const stack = new Stack(app); |
41 |
| - new RestApiConstruct(stack, 'RestApiNewLambdaTest', { |
42 |
| - apiName: 'RestApiNewLambda', |
43 |
| - apiProps: [ |
44 |
| - { |
45 |
| - path: 'items', |
46 |
| - defaultAuthorizer: { type: 'none' }, |
47 |
| - methods: [ |
48 |
| - { |
49 |
| - method: 'GET', |
50 |
| - authorizer: { type: 'none' }, |
51 |
| - }, |
52 |
| - ], |
53 |
| - lambdaEntry: { |
54 |
| - runtime: lambda.Runtime.NODEJS_22_X, |
55 |
| - source: { |
56 |
| - functionName: 'MyFunction1', |
57 |
| - code: "export const handler = () => {return 'Hello World! This is a new lambda function.';};", |
58 |
| - }, |
59 |
| - }, |
60 |
| - }, |
61 |
| - ], |
62 |
| - }); |
63 |
| - const template = Template.fromStack(stack); |
64 |
| - template.hasResourceProperties('AWS::Lambda::Function', { |
65 |
| - Handler: 'index.handler', |
66 |
| - }); |
67 |
| - }); |
68 |
| - |
69 |
| - void it('creates local files for a new function if the code is specified', () => { |
70 |
| - //delete folder amplify/functions/MyFunction2 first if the test has failed |
71 |
| - |
72 |
| - //function files should not originally exist |
73 |
| - let src = process.cwd(); |
74 |
| - src += '/amplify/functions/MyFunction2/'; |
75 |
| - strictEqual(fs.existsSync(src + 'resource.ts'), false); |
76 |
| - strictEqual(fs.existsSync(src + 'handler.ts'), false); |
77 |
| - |
78 |
| - //create the api containing a new lambda from source code |
79 |
| - const app = new App(); |
80 |
| - const stack = new Stack(app); |
81 |
| - new RestApiConstruct(stack, 'RestApiNewLambda2', { |
82 |
| - apiName: 'RestApiNewLambda2', |
83 |
| - apiProps: [ |
84 |
| - { |
85 |
| - path: 'items', |
86 |
| - methods: ['GET'], |
87 |
| - lambdaEntry: { |
88 |
| - runtime: lambda.Runtime.NODEJS_22_X, |
89 |
| - source: { |
90 |
| - functionName: 'MyFunction2', |
91 |
| - code: "export const handler = () => {return 'Hello World! This is a new lambda function.';};", |
92 |
| - }, |
93 |
| - }, |
94 |
| - }, |
95 |
| - ], |
96 |
| - }); |
97 |
| - |
98 |
| - //function files should have been created |
99 |
| - strictEqual(fs.existsSync(src + 'resource.ts'), true); |
100 |
| - strictEqual(fs.existsSync(src + 'handler.ts'), true); |
101 |
| - }); |
102 |
| - |
103 |
| - void it('loads a function from aws if the id and name are specified', () => { |
104 |
| - const app = new App(); |
105 |
| - const funcStack = new Stack(app, 'funcStack'); |
106 |
| - |
107 |
| - const func = new lambda.Function(funcStack, 'testFunc', { |
108 |
| - runtime: lambda.Runtime.NODEJS_22_X, |
109 |
| - handler: 'index.handler', |
110 |
| - code: lambda.Code.fromInline( |
111 |
| - "export const handler = () => {return 'Hello World! This is an existing lambda function.';};", |
112 |
| - ), |
| 17 | + const factory = defineFunction({ |
| 18 | + name: 'Test Function', |
| 19 | + entry: './test-assets/handler.js', |
113 | 20 | });
|
| 21 | + const func = factory.getInstance; |
114 | 22 |
|
115 |
| - const stack = new Stack(app, 'stack'); |
116 |
| - new RestApiConstruct(stack, 'RestApiLoadFunc', { |
117 |
| - apiName: 'RestApiLoad', |
118 |
| - apiProps: [ |
119 |
| - { |
120 |
| - path: 'stuff', |
121 |
| - methods: ['GET'], |
122 |
| - lambdaEntry: { |
123 |
| - runtime: lambda.Runtime.NODEJS_22_X, |
124 |
| - source: { id: func.functionArn, name: func.functionName }, |
125 |
| - }, |
126 |
| - }, |
127 |
| - ], |
128 |
| - }); |
129 |
| - |
130 |
| - const template = Template.fromStack(stack); |
131 |
| - //there should be no functions in the stack |
132 |
| - template.resourcePropertiesCountIs( |
133 |
| - 'AWS::Lambda::Function', |
134 |
| - { Arn: func.functionArn }, |
135 |
| - 0, |
| 23 | + //stubs for the instance props |
| 24 | + const constructContainer = new ConstructContainerStub( |
| 25 | + new StackResolverStub(stack), |
136 | 26 | );
|
137 |
| - //should be allowed to call the function |
138 |
| - template.hasResourceProperties('AWS::Lambda::Permission', { |
139 |
| - Action: 'lambda:InvokeFunction', |
140 |
| - Principal: 'apigateway.amazonaws.com', |
141 |
| - FunctionName: { 'Fn::GetAtt': [func.functionName, 'Arn'] }, |
142 |
| - }); |
143 |
| - //api should reference ARN |
144 |
| - template.hasResourceProperties('AWS::ApiGateway::Method', { |
145 |
| - Integration: { |
146 |
| - Uri: { |
147 |
| - 'Fn::Sub': [ |
148 |
| - 'arn:aws:apigateway:${AWS::REGION}:lambda:path/2015-03-31/functions/${LambdaArn}/invocations', |
149 |
| - { LambdaArn: func.functionArn }, |
150 |
| - ], |
151 |
| - }, |
152 |
| - }, |
153 |
| - }); |
154 |
| - }); |
155 |
| -}); |
| 27 | + const outputStorageStrategy = new StackMetadataBackendOutputStorageStrategy( |
| 28 | + stack, |
| 29 | + ); |
| 30 | + const resourceNameValidator = new ResourceNameValidatorStub(); |
| 31 | + const getInstanceProps: ConstructFactoryGetInstanceProps = { |
| 32 | + constructContainer, |
| 33 | + outputStorageStrategy, |
| 34 | + resourceNameValidator, |
| 35 | + }; |
| 36 | + |
| 37 | + const f = func(getInstanceProps).resources.lambda; |
156 | 38 |
|
157 |
| -void describe('RestApiConstruct', () => { |
158 |
| - void it('creates a queue if specified', () => { |
159 |
| - const app = new App(); |
160 |
| - const stack = new Stack(app); |
161 | 39 | new RestApiConstruct(stack, 'RestApiTest', {
|
162 | 40 | apiName: 'RestApiTest',
|
163 | 41 | apiProps: [
|
164 | 42 | {
|
165 |
| - path: '/test', |
| 43 | + path: 'items', |
| 44 | + lambdaEntry: f, |
166 | 45 | methods: [
|
167 | 46 | {
|
168 | 47 | method: 'GET',
|
169 | 48 | authorizer: { type: 'none' },
|
170 | 49 | },
|
171 | 50 | ],
|
172 |
| - lambdaEntry: { |
173 |
| - runtime: lambda.Runtime.NODEJS_18_X, |
174 |
| - source: { |
175 |
| - path: './test-lambda', |
176 |
| - }, |
177 |
| - }, |
178 |
| - }, |
179 |
| - { |
180 |
| - path: '/blog', |
181 |
| - methods: [ |
182 |
| - { |
183 |
| - method: 'POST', |
184 |
| - authorizer: { type: 'userPool', groups: ['Admins'] }, |
185 |
| - }, |
186 |
| - { |
187 |
| - method: 'GET', |
188 |
| - authorizer: { type: 'userPool' }, |
189 |
| - }, |
190 |
| - ], |
191 |
| - defaultAuthorizer: { type: 'userPool' }, |
192 |
| - lambdaEntry: { |
193 |
| - runtime: lambda.Runtime.NODEJS_18_X, |
194 |
| - source: { |
195 |
| - path: './blog-lambda', |
196 |
| - }, |
197 |
| - }, |
198 | 51 | },
|
199 | 52 | ],
|
200 | 53 | });
|
201 |
| - const template = Template.fromStack(stack); |
202 |
| - template.resourceCountIs('AWS::AGW::RestApi', 1); |
203 | 54 | });
|
204 | 55 | });
|
| 56 | + |
| 57 | +//test needs to be updated to new lambda handling |
| 58 | +// void describe('RestApiConstruct', () => { |
| 59 | +// void it('creates a queue if specified', () => { |
| 60 | +// const app = new App(); |
| 61 | +// const stack = new Stack(app); |
| 62 | +// new RestApiConstruct(stack, 'RestApiTest', { |
| 63 | +// apiName: 'RestApiTest', |
| 64 | +// apiProps: [ |
| 65 | +// { |
| 66 | +// path: '/test', |
| 67 | +// methods: [ |
| 68 | +// { |
| 69 | +// method: 'GET', |
| 70 | +// authorizer: { type: 'none' }, |
| 71 | +// }, |
| 72 | +// ], |
| 73 | +// lambdaEntry: { |
| 74 | +// runtime: lambda.Runtime.NODEJS_18_X, |
| 75 | +// source: { |
| 76 | +// path: './test-lambda', |
| 77 | +// }, |
| 78 | +// }, |
| 79 | +// }, |
| 80 | +// { |
| 81 | +// path: '/blog', |
| 82 | +// methods: [ |
| 83 | +// { |
| 84 | +// method: 'POST', |
| 85 | +// authorizer: { type: 'userPool', groups: ['Admins'] }, |
| 86 | +// }, |
| 87 | +// { |
| 88 | +// method: 'GET', |
| 89 | +// authorizer: { type: 'userPool' }, |
| 90 | +// }, |
| 91 | +// ], |
| 92 | +// defaultAuthorizer: { type: 'userPool' }, |
| 93 | +// lambdaEntry: { |
| 94 | +// runtime: lambda.Runtime.NODEJS_18_X, |
| 95 | +// source: { |
| 96 | +// path: './blog-lambda', |
| 97 | +// }, |
| 98 | +// }, |
| 99 | +// }, |
| 100 | +// ], |
| 101 | +// }); |
| 102 | +// const template = Template.fromStack(stack); |
| 103 | +// template.resourceCountIs('AWS::AGW::RestApi', 1); |
| 104 | +// }); |
| 105 | +// }); |
0 commit comments