From e6e131bdb4005a29e82ca3389302592c624eef5b 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 | 8 ++++ lib/common.ts | 9 +--- lib/helpers.ts | 7 +++ scripts/domain-auto-update/update-ip.sh | 2 +- test/auto-update-ip-aws.test.ts | 60 +++++++++++++++++++++++-- 7 files changed, 80 insertions(+), 21 deletions(-) create mode 100644 .env.test delete mode 100644 jest.config.js create mode 100644 jest.config.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..a8f075a --- /dev/null +++ b/jest.config.ts @@ -0,0 +1,8 @@ +module.exports = { + testEnvironment: "node", + roots: ["/test"], + testMatch: ["**/*.test.ts"], + transform: { + "^.+\\.tsx?$": "ts-jest", + }, +}; 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..f698f5f 100644 --- a/test/auto-update-ip-aws.test.ts +++ b/test/auto-update-ip-aws.test.ts @@ -1,17 +1,69 @@ 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"; +import * as dotenv from "dotenv"; + +dotenv.config({ path: ".env.test", override: true }); 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", });