Skip to content

Commit

Permalink
test: account-type
Browse files Browse the repository at this point in the history
  • Loading branch information
aripalo committed Nov 21, 2024
1 parent c46deaa commit 6210cf9
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 17 deletions.
15 changes: 15 additions & 0 deletions src/__test__/expectErrorMetadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Construct } from "constructs";

export function expectErrorMetadata(scope: Construct, matcher?: jest.Expect) {
if (matcher === undefined) {
expect(scope.node.metadata).toEqual([]);
} else {
expect(scope.node.metadata).toEqual(
expect.arrayContaining([
expect.objectContaining({
data: matcher,
}),
]),
);
}
}
18 changes: 1 addition & 17 deletions src/context/environment.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,8 @@
import { EnvironmentContext } from "./environment";
import { expectErrorMetadata } from "../__test__/expectErrorMetadata";
import { TestableProjectStack } from "../__test__/TestableProjectStack";
import { AccountStrategy, AccountType } from "../configurations/accounts";

function expectErrorMetadata(
stack: TestableProjectStack,
matcher?: jest.Expect,
) {
if (matcher === undefined) {
expect(stack.node.metadata).toEqual([]);
} else {
expect(stack.node.metadata).toEqual(
expect.arrayContaining([
expect.objectContaining({
data: matcher,
}),
]),
);
}
}

describe("getName", () => {
test("returns environment name", () => {
const stack = new TestableProjectStack({
Expand Down
102 changes: 102 additions & 0 deletions src/project/account-type.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import * as cdk from "aws-cdk-lib";
import { AccountType } from "./account-type";
import { expectErrorMetadata } from "../__test__/expectErrorMetadata";
import { TestableProjectStack } from "../__test__/TestableProjectStack";
import { TestableResource } from "../__test__/TestableResource";
import { AccountStrategy } from "../configurations";

describe("set", () => {
const accountType = "dev";
const stack = new cdk.Stack();
AccountType.set(stack, accountType);
const testable = new TestableResource({ scope: stack });

test("context: account-type", () => {
expect(testable.node.getContext("account-type")).toBe(accountType);
});

test("context: account", () => {
expect(testable.node.getContext("account")).toBe(accountType);
});
});

describe("get", () => {
test("returns empty string and sets error metadata if no account type defined", () => {
const stack = new cdk.Stack();
const testable = new TestableResource({ scope: stack });
expect(AccountType.get(testable)).toBe("");
expectErrorMetadata(
testable,
expect.stringContaining("Account Type not specified"),
);
});

test("set via account-type context", () => {
const stack = new cdk.Stack();
stack.node.setContext("account-type", "dev");
const testable = new TestableResource({ scope: stack });
expect(AccountType.get(testable)).toBe("dev");
});

test("set via account context", () => {
const stack = new cdk.Stack();
stack.node.setContext("account", "dev");
const testable = new TestableResource({ scope: stack });
expect(AccountType.get(testable)).toBe("dev");
});

test("prefer account-type context over account context", () => {
const stack = new cdk.Stack();
stack.node.setContext("account-type", "dev");
stack.node.setContext("account", "prod");
const testable = new TestableResource({ scope: stack });
expect(AccountType.get(testable)).toBe("dev");
});
});

describe("matchFromEnvironment", () => {
test("returns matching account type for environment", () => {
const accounts = AccountStrategy.three({
dev: { id: "111111111111" },
preprod: { id: "222222222222" },
prod: { id: "333333333333" },
});

const stack = new TestableProjectStack({
accounts,
accountType: "dev",
});

const testable = new TestableResource({ scope: stack });

expect(
AccountType.matchFromEnvironment(testable, accounts, "development"),
).toBe("dev");
expectErrorMetadata(testable, undefined);
});

test("returns empty string and sets error metadata if no matching account found", () => {
const accounts = AccountStrategy.three({
dev: { id: "111111111111" },
preprod: { id: "222222222222" },
prod: { id: "333333333333" },
});

const stack = new TestableProjectStack({
accounts,
accountType: "dev",
});

const testable = new TestableResource({ scope: stack });

expect(
AccountType.matchFromEnvironment(testable, accounts, "preprod"),
).toBe("");
expectErrorMetadata(
testable,
expect.stringContaining(
"Could not find matching account type for given environment preprod",
),
);
});
});

0 comments on commit 6210cf9

Please sign in to comment.