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 33878a8 commit 485818e
Show file tree
Hide file tree
Showing 8 changed files with 150 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.

9 changes: 9 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
testEnvironment: "node",
roots: ["<rootDir>/test"],
testMatch: ["**/*.test.ts"],
transform: {
"^.+\\.tsx?$": "ts-jest",
},
setupFiles: ["./jest.setup.ts"],
};
3 changes: 3 additions & 0 deletions jest.setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import * as dotenv from "dotenv";

dotenv.config({ path: ".env.test", override: true });
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
126 changes: 122 additions & 4 deletions test/auto-update-ip-aws.test.ts
Original file line number Diff line number Diff line change
@@ -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,
});
});

0 comments on commit 485818e

Please sign in to comment.