Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
guiyom-e committed Jan 30, 2024
1 parent 45e2006 commit e6e131b
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 21 deletions.
7 changes: 7 additions & 0 deletions .env.test
Original file line number Diff line number Diff line change
@@ -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
8 changes: 0 additions & 8 deletions jest.config.js

This file was deleted.

8 changes: 8 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
testEnvironment: "node",
roots: ["<rootDir>/test"],
testMatch: ["**/*.test.ts"],
transform: {
"^.+\\.tsx?$": "ts-jest",
},
};
9 changes: 1 addition & 8 deletions lib/common.ts
Original file line number Diff line number Diff line change
@@ -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");

Expand Down
7 changes: 7 additions & 0 deletions lib/helpers.ts
Original file line number Diff line number Diff line change
@@ -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;
};
2 changes: 1 addition & 1 deletion scripts/domain-auto-update/update-ip.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
60 changes: 56 additions & 4 deletions test/auto-update-ip-aws.test.ts
Original file line number Diff line number Diff line change
@@ -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",
});
Expand Down

0 comments on commit e6e131b

Please sign in to comment.