|
| 1 | +import { Stack } from "aws-cdk-lib"; |
| 2 | +import { Match, Template } from "aws-cdk-lib/assertions"; |
| 3 | +import { Code, Runtime } from "aws-cdk-lib/aws-lambda"; |
| 4 | +import { NodejsFunction } from "aws-cdk-lib/aws-lambda-nodejs"; |
| 5 | +import { join as pathJoin } from "path"; |
| 6 | +import { StepFunctionFromFile } from "./step-function-from-file-construct"; |
| 7 | + |
| 8 | +const snapshotMessage = |
| 9 | + "Rerun tests with the -u flag to update snapshots if changes are expected"; |
| 10 | + |
| 11 | +describe("StepFunctionFromFile", () => { |
| 12 | + let stack: Stack; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + stack = new Stack(); |
| 16 | + new StepFunctionFromFile(stack, "MyStateMachine", { |
| 17 | + filepath: pathJoin(__dirname, "__data__", "test-machine.asl.yaml"), |
| 18 | + }); |
| 19 | + }); |
| 20 | + |
| 21 | + test("creates a state machine from a file", () => { |
| 22 | + const template = Template.fromStack(stack); |
| 23 | + |
| 24 | + const stateMachines = template.findResources( |
| 25 | + "AWS::StepFunctions::StateMachine" |
| 26 | + ); |
| 27 | + expect(stateMachines).toMatchSnapshot(snapshotMessage); |
| 28 | + }); |
| 29 | + |
| 30 | + test("creates a state machine with lambda functions", () => { |
| 31 | + // Create test lambda functions |
| 32 | + const lambda = new NodejsFunction(stack, "TestFunction", { |
| 33 | + runtime: Runtime.NODEJS_22_X, |
| 34 | + handler: "index.handler", |
| 35 | + code: Code.fromInline( |
| 36 | + "exports.handler = async () => ({ statusCode: 200 });" |
| 37 | + ), |
| 38 | + }); |
| 39 | + |
| 40 | + const otherLambda = new NodejsFunction(stack, "OtherTestFunction", { |
| 41 | + runtime: Runtime.NODEJS_22_X, |
| 42 | + handler: "index.handler", |
| 43 | + code: Code.fromInline( |
| 44 | + "exports.handler = async () => ({ statusCode: 200 });" |
| 45 | + ), |
| 46 | + }); |
| 47 | + |
| 48 | + new StepFunctionFromFile(stack, "LambdaStateMachine", { |
| 49 | + filepath: pathJoin(__dirname, "__data__", "test-machine.asl.yaml"), |
| 50 | + lambdaFunctions: [lambda, otherLambda], |
| 51 | + definitionSubstitutions: { |
| 52 | + ExtraParam: "ExtraValue", |
| 53 | + }, |
| 54 | + }); |
| 55 | + |
| 56 | + const template = Template.fromStack(stack); |
| 57 | + |
| 58 | + template.hasResourceProperties("AWS::StepFunctions::StateMachine", { |
| 59 | + DefinitionSubstitutions: { |
| 60 | + ExtraParam: "ExtraValue", |
| 61 | + TestFunction: { |
| 62 | + "Fn::GetAtt": [Match.stringLikeRegexp("TestFunction.*"), "Arn"], |
| 63 | + }, |
| 64 | + OtherTestFunction: { |
| 65 | + "Fn::GetAtt": [Match.stringLikeRegexp("OtherTestFunction.*"), "Arn"], |
| 66 | + }, |
| 67 | + }, |
| 68 | + }); |
| 69 | + |
| 70 | + // Check that IAM policies grant invoke permissions (there should be 2 policies, one for each lambda) |
| 71 | + template.hasResourceProperties("AWS::IAM::Policy", { |
| 72 | + PolicyDocument: { |
| 73 | + Statement: Match.arrayWith([ |
| 74 | + Match.objectLike({ |
| 75 | + Action: "lambda:InvokeFunction", |
| 76 | + Effect: "Allow", |
| 77 | + }), |
| 78 | + ]), |
| 79 | + }, |
| 80 | + }); |
| 81 | + }); |
| 82 | +}); |
0 commit comments