-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.test.ts
More file actions
169 lines (133 loc) · 4.98 KB
/
index.test.ts
File metadata and controls
169 lines (133 loc) · 4.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import {
describe,
test,
beforeEach,
afterAll,
expect,
vi
} from "vitest"
import {
getConfigFromEnvVar,
getBooleanConfigFromEnvVar,
getNumberConfigFromEnvVar,
getTrustStoreVersion
} from "../../src/config/index"
const mockCloudFormationSend = vi.fn()
const mockS3Send = vi.fn()
const createdCfnClients: Array<{region?: string}> = []
const createdS3Clients: Array<{region?: string}> = []
vi.mock("@aws-sdk/client-cloudformation", () => {
class CloudFormationClient {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
config: any
constructor(config: {region: string}) {
this.config = config
createdCfnClients.push({region: config.region})
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
send(command: any) {
return mockCloudFormationSend(command)
}
}
class DescribeStacksCommand {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
input: any
// eslint-disable-next-line @typescript-eslint/no-explicit-any
constructor(input: any) {
this.input = input
}
}
return {CloudFormationClient, DescribeStacksCommand}
})
vi.mock("@aws-sdk/client-s3", () => {
class S3Client {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
config: any
constructor(config: {region: string}) {
this.config = config
createdS3Clients.push({region: config.region})
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
send(command: any) {
return mockS3Send(command)
}
}
class HeadObjectCommand {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
input: any
// eslint-disable-next-line @typescript-eslint/no-explicit-any
constructor(input: any) {
this.input = input
}
}
return {S3Client, HeadObjectCommand}
})
const ORIGINAL_ENV = process.env
describe("config helpers", () => {
beforeEach(() => {
process.env = {...ORIGINAL_ENV}
mockCloudFormationSend.mockReset()
mockS3Send.mockReset()
createdCfnClients.length = 0
createdS3Clients.length = 0
})
afterAll(() => {
process.env = ORIGINAL_ENV
})
test("getConfigFromEnvVar returns the configured value", () => {
process.env.CDK_CONFIG_STACK_NAME = "primary"
expect(getConfigFromEnvVar("STACK_NAME")).toBe("primary")
})
test("getConfigFromEnvVar returns the default value when env var is not set", () => {
delete process.env.CDK_CONFIG_MISSING
expect(getConfigFromEnvVar("MISSING", "fallback")).toBe("fallback")
})
test("getConfigFromEnvVar throws when value is missing", () => {
delete process.env.CDK_CONFIG_MISSING
expect(() => getConfigFromEnvVar("MISSING"))
.toThrow("Environment variable CDK_CONFIG_MISSING is not set")
})
test("getConfigFromEnvVar supports alternate prefixes", () => {
process.env.APP_CUSTOM_VALUE = "alt"
expect(getConfigFromEnvVar("CUSTOM_VALUE", undefined, "APP_")).toBe("alt")
})
test("getBooleanConfigFromEnvVar maps string booleans", () => {
process.env.CDK_CONFIG_FEATURE_FLAG = "true "
process.env.CDK_CONFIG_OTHER_FLAG = " false"
expect(getBooleanConfigFromEnvVar("FEATURE_FLAG")).toBe(true)
expect(getBooleanConfigFromEnvVar("OTHER_FLAG")).toBe(false)
})
test("getBooleanConfigFromEnvVar uses default value when env var is not set", () => {
delete process.env.CDK_CONFIG_BOOL_MISSING
expect(getBooleanConfigFromEnvVar("BOOL_MISSING", "true")).toBe(true)
expect(getBooleanConfigFromEnvVar("BOOL_MISSING", "false")).toBe(false)
})
test("getNumberConfigFromEnvVar parses numeric strings", () => {
process.env.CDK_CONFIG_TIMEOUT = "45"
expect(getNumberConfigFromEnvVar("TIMEOUT")).toBe(45)
})
test("getNumberConfigFromEnvVar uses default value when env var is not set", () => {
delete process.env.CDK_CONFIG_NUM_MISSING
expect(getNumberConfigFromEnvVar("NUM_MISSING", "99")).toBe(99)
})
test("getConfigFromEnvVar ignores default value when env var is set", () => {
process.env.CDK_CONFIG_STACK_NAME = "primary"
expect(getConfigFromEnvVar("STACK_NAME", "ignored")).toBe("primary")
})
test("getTrustStoreVersion returns the version ID from S3", async () => {
mockCloudFormationSend.mockResolvedValueOnce({
Stacks: [{
Outputs: [{OutputKey: "TrustStoreBucket", OutputValue: "arn:aws:s3:::nhs-trust"}]
}]
})
mockS3Send.mockResolvedValueOnce({VersionId: "abc123"})
const version = await getTrustStoreVersion("truststore.pem", "eu-central-1")
expect(version).toBe("abc123")
expect(createdCfnClients.at(-1)?.region).toBe("eu-central-1")
expect(createdS3Clients.at(-1)?.region).toBe("eu-central-1")
const describeCommand = mockCloudFormationSend.mock.calls[0][0] as {input: {StackName: string}}
expect(describeCommand.input.StackName).toBe("account-resources")
const headCommand = mockS3Send.mock.calls[0][0] as {input: {Bucket: string, Key: string}}
expect(headCommand.input).toEqual({Bucket: "nhs-trust", Key: "truststore.pem"})
})
})