From 485818e0318ae3a7e0fcac04338063f0d308357e Mon Sep 17 00:00:00 2001 From: guiyom-e Date: Tue, 30 Jan 2024 10:23:19 +0100 Subject: [PATCH] fix tests --- .env.test | 7 ++ jest.config.js | 8 -- jest.config.ts | 9 ++ jest.setup.ts | 3 + lib/common.ts | 9 +- lib/helpers.ts | 7 ++ scripts/domain-auto-update/update-ip.sh | 2 +- test/auto-update-ip-aws.test.ts | 126 +++++++++++++++++++++++- 8 files changed, 150 insertions(+), 21 deletions(-) create mode 100644 .env.test delete mode 100644 jest.config.js create mode 100644 jest.config.ts create mode 100644 jest.setup.ts create mode 100644 lib/helpers.ts diff --git a/.env.test b/.env.test new file mode 100644 index 0000000..adc4cb6 --- /dev/null +++ b/.env.test @@ -0,0 +1,7 @@ +REGION=eu-west-1 +DOMAIN_NAME=toto.example.com + +HOSTED_ZONE_ID=ABC + +API_INTEGRATION_PATH=/start-update-ip-sfn +FETCH_IP_API_PATH=/get-ip diff --git a/jest.config.js b/jest.config.js deleted file mode 100644 index 08263b8..0000000 --- a/jest.config.js +++ /dev/null @@ -1,8 +0,0 @@ -module.exports = { - testEnvironment: 'node', - roots: ['/test'], - testMatch: ['**/*.test.ts'], - transform: { - '^.+\\.tsx?$': 'ts-jest' - } -}; diff --git a/jest.config.ts b/jest.config.ts new file mode 100644 index 0000000..2a5499d --- /dev/null +++ b/jest.config.ts @@ -0,0 +1,9 @@ +module.exports = { + testEnvironment: "node", + roots: ["/test"], + testMatch: ["**/*.test.ts"], + transform: { + "^.+\\.tsx?$": "ts-jest", + }, + setupFiles: ["./jest.setup.ts"], +}; diff --git a/jest.setup.ts b/jest.setup.ts new file mode 100644 index 0000000..573cefb --- /dev/null +++ b/jest.setup.ts @@ -0,0 +1,3 @@ +import * as dotenv from "dotenv"; + +dotenv.config({ path: ".env.test", override: true }); diff --git a/lib/common.ts b/lib/common.ts index f1ce827..f72dca6 100644 --- a/lib/common.ts +++ b/lib/common.ts @@ -1,15 +1,8 @@ import * as dotenv from "dotenv"; +import { getEnvVar } from "./helpers"; dotenv.config(); -const getEnvVar = (name: string): string => { - const value = process.env[name]; - if (value === undefined) { - throw new Error(`${name} is undefined`); - } - return value; -}; - export const REGION = getEnvVar("REGION"); export const DOMAIN_NAME = getEnvVar("DOMAIN_NAME"); diff --git a/lib/helpers.ts b/lib/helpers.ts new file mode 100644 index 0000000..d423a63 --- /dev/null +++ b/lib/helpers.ts @@ -0,0 +1,7 @@ +export const getEnvVar = (name: string): string => { + const value = process.env[name]; + if (value === undefined) { + throw new Error(`${name} is undefined`); + } + return value; +}; diff --git a/scripts/domain-auto-update/update-ip.sh b/scripts/domain-auto-update/update-ip.sh index 4755ba2..63d6b3c 100755 --- a/scripts/domain-auto-update/update-ip.sh +++ b/scripts/domain-auto-update/update-ip.sh @@ -26,7 +26,7 @@ OLD_IP=$(tail -n 1 $SCRIPT_DIR/current_ip) IP=$(curl -s $GET_CURRENT_IP_API_URL | jq -r .ip) echo $NOW >$SCRIPT_DIR/latest_script_execution.log -echo "METHOD: api integartion" | tee -a $SCRIPT_DIR/latest_script_execution.log +echo "METHOD: api integration" | tee -a $SCRIPT_DIR/latest_script_execution.log # Update ip if it has changed since last update (or if it's the first time the script is run) if [ "$OLD_IP" = "$IP" ]; then diff --git a/test/auto-update-ip-aws.test.ts b/test/auto-update-ip-aws.test.ts index 3eeb851..e11ddb9 100644 --- a/test/auto-update-ip-aws.test.ts +++ b/test/auto-update-ip-aws.test.ts @@ -1,18 +1,136 @@ import * as cdk from "aws-cdk-lib"; import { Template } from "aws-cdk-lib/assertions"; import { AutoUpdateIpStack } from "../lib/auto-update-ip-aws-stack"; +import { getEnvVar } from "../lib/helpers"; test("State machine created", () => { const app = new cdk.App(); - // WHEN + const stack = new AutoUpdateIpStack(app, "MyTestStack", { - hostedZoneId: "123456789", - domaineName: "example.com", + hostedZoneId: getEnvVar("HOSTED_ZONE_ID"), + domaineName: getEnvVar("DOMAIN_NAME"), }); - // THEN + const template = Template.fromStack(stack); + template.hasResourceProperties("AWS::IAM::Role", { + Policies: [ + { + PolicyDocument: { + Statement: [ + { + Action: "route53:ChangeResourceRecordSets", + Condition: { + "ForAllValues:StringEquals": { + "route53:ChangeResourceRecordSetsActions": "UPSERT", + "route53:ChangeResourceRecordSetsNormalizedRecordNames": + "toto.example.com", + "route53:ChangeResourceRecordSetsRecordTypes": "A", + }, + }, + Effect: "Allow", + Resource: "arn:aws:route53:::hostedzone/ABC", + }, + ], + Version: "2012-10-17", + }, + PolicyName: "allowARecordChange", + }, + { + PolicyDocument: { + Statement: [ + { + Action: [ + "logs:CreateLogDelivery", + "logs:DeleteLogDelivery", + "logs:DescribeLogGroups", + "logs:DescribeResourcePolicies", + "logs:GetLogDelivery", + "logs:ListLogDeliveries", + "logs:PutResourcePolicy", + "logs:UpdateLogDelivery", + ], + Effect: "Allow", + Resource: "*", + }, + ], + Version: "2012-10-17", + }, + PolicyName: "logs", + }, + ], + }); + template.hasResourceProperties("AWS::StepFunctions::StateMachine", { StateMachineType: "EXPRESS", }); }); + +test("REST API for integration created", () => { + const app = new cdk.App(); + const region = getEnvVar("REGION"); + + const stack = new AutoUpdateIpStack(app, "MyTestStack", { + hostedZoneId: getEnvVar("HOSTED_ZONE_ID"), + domaineName: getEnvVar("DOMAIN_NAME"), + apiIntegration: { methodPath: getEnvVar("API_INTEGRATION_PATH") }, + }); + + const template = Template.fromStack(stack); + + template.hasResourceProperties("AWS::ApiGateway::RestApi", { + ApiKeySourceType: "HEADER", + EndpointConfiguration: { + Types: ["REGIONAL"], + }, + }); + template.hasResourceProperties("AWS::ApiGateway::Stage", { + StageName: "prod", + }); + + template.hasResourceProperties("AWS::ApiGateway::Method", { + ApiKeyRequired: true, + AuthorizationType: "NONE", + HttpMethod: "POST", + Integration: { + IntegrationHttpMethod: "POST", + Type: "AWS", + Uri: { + "Fn::Join": [ + "", + [ + "arn:", + { Ref: "AWS::Partition" }, + `:apigateway:${region}:states:action/StartSyncExecution`, + ], + ], + }, + }, + MethodResponses: [{ StatusCode: "200" }], + }); + + template.hasResourceProperties("AWS::ApiGateway::ApiKey", { Enabled: true }); +}); + +test("HTTP API for fetch IP created", () => { + const app = new cdk.App(); + + const stack = new AutoUpdateIpStack(app, "MyTestStack", { + hostedZoneId: getEnvVar("HOSTED_ZONE_ID"), + domaineName: getEnvVar("DOMAIN_NAME"), + fetchIpApi: { methodPath: getEnvVar("FETCH_IP_API_PATH") }, + }); + + const template = Template.fromStack(stack); + + template.hasResourceProperties("AWS::ApiGatewayV2::Api", { + ProtocolType: "HTTP", + }); + + template.hasResourceProperties("AWS::Lambda::Function", { + Handler: "index.handler", + Runtime: "nodejs20.x", + Architectures: ["arm64"], + Timeout: 10, + }); +});