|
| 1 | +import {App, Stack} from "aws-cdk-lib" |
| 2 | +import {RestApi} from "aws-cdk-lib/aws-apigateway" |
| 3 | +import {Role, ServicePrincipal} from "aws-cdk-lib/aws-iam" |
| 4 | +import {Template, Match} from "aws-cdk-lib/assertions" |
| 5 | +import {Architecture, Function as LambdaFunction, Runtime} from "aws-cdk-lib/aws-lambda" |
| 6 | +import { |
| 7 | + describe, |
| 8 | + test, |
| 9 | + beforeAll, |
| 10 | + expect |
| 11 | +} from "vitest" |
| 12 | +import {HttpMethod} from "aws-cdk-lib/aws-lambda" |
| 13 | + |
| 14 | +import {LambdaEndpoint} from "../../../src/constructs/RestApiGateway/LambdaEndpoint.js" |
| 15 | + |
| 16 | +describe("LambdaEndpoint construct", () => { |
| 17 | + let stack: Stack |
| 18 | + let template: Template |
| 19 | + let construct: LambdaEndpoint |
| 20 | + |
| 21 | + beforeAll(() => { |
| 22 | + const app = new App() |
| 23 | + stack = new Stack(app, "LambdaEndpointStack") |
| 24 | + |
| 25 | + const api = new RestApi(stack, "TestApi") |
| 26 | + |
| 27 | + const credentialsRole = new Role(stack, "ApiGwRole", { |
| 28 | + assumedBy: new ServicePrincipal("apigateway.amazonaws.com") |
| 29 | + }) |
| 30 | + |
| 31 | + // Minimal lambda function stub that satisfies LambdaFunctionHolder interface |
| 32 | + const lambdaFn = new LambdaFunction(stack, "DummyFn", { |
| 33 | + runtime: Runtime.NODEJS_22_X, |
| 34 | + handler: "index.handler", |
| 35 | + code: { |
| 36 | + bind: () => ({ |
| 37 | + s3Location: {bucketName: "dummy", objectKey: "dummy.zip"} |
| 38 | + }), |
| 39 | + bindToResource: () => undefined, |
| 40 | + isInline: false |
| 41 | + } as unknown as never, |
| 42 | + architecture: Architecture.X86_64 |
| 43 | + }) |
| 44 | + |
| 45 | + construct = new LambdaEndpoint(stack, "TestLambdaEndpoint", { |
| 46 | + parentResource: api.root, |
| 47 | + resourceName: "test-resource", |
| 48 | + method: HttpMethod.GET, |
| 49 | + restApiGatewayRole: credentialsRole, |
| 50 | + lambdaFunction: {function: lambdaFn} |
| 51 | + }) |
| 52 | + |
| 53 | + template = Template.fromStack(stack) |
| 54 | + }) |
| 55 | + |
| 56 | + test("creates an API Gateway resource with the correct path part", () => { |
| 57 | + template.hasResourceProperties("AWS::ApiGateway::Resource", { |
| 58 | + PathPart: "test-resource" |
| 59 | + }) |
| 60 | + }) |
| 61 | + |
| 62 | + test("creates a GET method on the resource", () => { |
| 63 | + template.hasResourceProperties("AWS::ApiGateway::Method", { |
| 64 | + HttpMethod: "GET" |
| 65 | + }) |
| 66 | + }) |
| 67 | + |
| 68 | + test("exposes the resource as a public property", () => { |
| 69 | + expect(construct.resource).toBeDefined() |
| 70 | + }) |
| 71 | + |
| 72 | + test("uses credentials role on the Lambda integration", () => { |
| 73 | + template.hasResourceProperties("AWS::ApiGateway::Method", { |
| 74 | + HttpMethod: "GET", |
| 75 | + Integration: Match.objectLike({ |
| 76 | + Type: "AWS_PROXY" |
| 77 | + }) |
| 78 | + }) |
| 79 | + }) |
| 80 | +}) |
| 81 | + |
| 82 | +describe("LambdaEndpoint accepts TypescriptLambdaFunction via structural typing", () => { |
| 83 | + test("LambdaFunctionHolder interface is satisfied by any object with a function property", () => { |
| 84 | + // This is a compile-time check verified by the build step. Here we just |
| 85 | + // assert the interface shape is correct at runtime. |
| 86 | + const holder = {function: {} as unknown as never} |
| 87 | + expect(holder.function).toBeDefined() |
| 88 | + }) |
| 89 | +}) |
0 commit comments