-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreateApp.test.ts
More file actions
201 lines (170 loc) · 7.32 KB
/
createApp.test.ts
File metadata and controls
201 lines (170 loc) · 7.32 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/**
* Tests for createApp function
*
* This test suite covers the createApp function which creates a CDK App with
* standard configuration including:
* - Environment variable validation
* - App creation with proper aspects and tags
* - Stack properties configuration
* - Pull request detection and drift detection group modification
* - Regional configuration
*
* Note: The getBooleanConfigFromEnvVar function uses Boolean() which converts
* any non-empty string (including "false", "0", etc.) to true. Tests account
* for this behaviour.
*/
import {App, Aspects, Tags} from "aws-cdk-lib"
import {
describe,
test,
beforeEach,
afterEach,
expect,
vi
} from "vitest"
import {createApp, type CreateAppParams} from "../../src/apps/createApp"
import {AwsSolutionsChecks} from "cdk-nag"
import {CDK_ENV_PREFIX} from "../../src/constants"
describe("createApp", () => {
const originalEnv = process.env
const defaultParams: CreateAppParams = {
productName: "testProduct",
appName: "testApp",
repoName: "testRepo",
driftDetectionGroup: "test-drift-group"
}
const buildParams = (overrides: Partial<CreateAppParams> = {}): CreateAppParams => ({
...defaultParams,
...overrides
})
beforeEach(() => {
// Reset environment before each test
vi.resetModules()
process.env = {...originalEnv}
})
afterEach(() => {
// Restore environment after each test
process.env = originalEnv
})
describe("when all environment variables are set", () => {
beforeEach(() => {
process.env[`${CDK_ENV_PREFIX}versionNumber`] = "1.2.3"
process.env[`${CDK_ENV_PREFIX}commitId`] = "abc123def456"
process.env[`${CDK_ENV_PREFIX}isPullRequest`] = "false"
process.env[`${CDK_ENV_PREFIX}environment`] = "test-environment"
})
test("creates an App with correct configuration", () => {
const {app, props} = createApp(buildParams())
expect(app).toBeInstanceOf(App)
expect(props.version).toBe("1.2.3")
expect(props.commitId).toBe("abc123def456")
expect(props.isPullRequest).toBe(false)
expect(props.env?.region).toBe("eu-west-2")
})
test("uses custom region when provided", () => {
const {props} = createApp(buildParams({region: "us-east-1"}))
expect(props.env?.region).toBe("us-east-1")
})
test("applies correct tags to the app", () => {
// Spy on Tags.of(app).add to verify tag calls
const addTagSpy = vi.fn()
const tagsOfSpy = vi.spyOn(Tags, "of").mockReturnValue({
add: addTagSpy
} as unknown as Tags)
const {app} = createApp(buildParams())
// Verify Tags.of was called with the app
expect(tagsOfSpy).toHaveBeenCalledWith(app)
// Verify all expected tags were added with correct values
expect(addTagSpy).toHaveBeenCalledWith("TagVersion", "1")
expect(addTagSpy).toHaveBeenCalledWith("Programme", "EPS")
expect(addTagSpy).toHaveBeenCalledWith("Product", "testProduct")
expect(addTagSpy).toHaveBeenCalledWith("Owner", "england.epssupport@nhs.net")
expect(addTagSpy).toHaveBeenCalledWith("Product", "testProduct")
expect(addTagSpy).toHaveBeenCalledWith("CostCentre", "128997")
expect(addTagSpy).toHaveBeenCalledWith("Customer", "NHS England")
expect(addTagSpy).toHaveBeenCalledWith("data_classification", "5")
expect(addTagSpy).toHaveBeenCalledWith("DataType", "PII")
expect(addTagSpy).toHaveBeenCalledWith("Environment", "test-environment")
expect(addTagSpy).toHaveBeenCalledWith("ProjectType", "Production")
expect(addTagSpy).toHaveBeenCalledWith("PublicFacing", "Y")
expect(addTagSpy).toHaveBeenCalledWith("ServiceCategory", "Platinum")
expect(addTagSpy).toHaveBeenCalledWith("OnOffPattern", "AlwaysOn")
expect(addTagSpy).toHaveBeenCalledWith("DeploymentTool", "CDK")
expect(addTagSpy).toHaveBeenCalledWith("version", "1.2.3")
expect(addTagSpy).toHaveBeenCalledWith("commit", "abc123def456")
expect(addTagSpy).toHaveBeenCalledWith("cdkApp", "testApp")
expect(addTagSpy).toHaveBeenCalledWith("repo", "testRepo")
expect(addTagSpy).toHaveBeenCalledWith("cfnDriftDetectionGroup", "test-drift-group")
// Verify exactly 19 tags were added
expect(addTagSpy).toHaveBeenCalledTimes(19)
// Restore the spy
tagsOfSpy.mockRestore()
})
test("adds AwsSolutionsChecks aspect", () => {
const {app} = createApp(buildParams())
const aspects = Aspects.of(app).all
expect(aspects).toContainEqual(new AwsSolutionsChecks({verbose: true}))
})
})
describe("when isPullRequest is true", () => {
beforeEach(() => {
process.env[`${CDK_ENV_PREFIX}versionNumber`] = "0.0.1-pr"
process.env[`${CDK_ENV_PREFIX}commitId`] = "pr123"
process.env[`${CDK_ENV_PREFIX}isPullRequest`] = "true"
process.env[`${CDK_ENV_PREFIX}environment`] = "test-environment"
})
test("correctly modifies props", () => {
const {props} = createApp(buildParams())
expect(props.version).toBe("0.0.1-pr")
expect(props.commitId).toBe("pr123")
expect(props.isPullRequest).toBe(true)
expect(props.environment).toBe("test-environment")
})
test("modifies drift detection group with -pull-request suffix", () => {
// Spy on Tags.of(app).add to verify tag calls
const addTagSpy = vi.fn()
const tagsOfSpy = vi.spyOn(Tags, "of").mockReturnValue({
add: addTagSpy
} as unknown as Tags)
const {app} = createApp(buildParams())
// Verify Tags.of was called with the app
expect(tagsOfSpy).toHaveBeenCalledWith(app)
// Verify all expected tags were added with correct values
expect(addTagSpy).toHaveBeenCalledWith("cfnDriftDetectionGroup", "test-drift-group-pull-request")
})
})
describe("when environment variables are missing", () => {
test("throws error when versionNumber is not set", () => {
process.env[`${CDK_ENV_PREFIX}commitId`] = "abc123"
process.env[`${CDK_ENV_PREFIX}isPullRequest`] = "false"
process.env[`${CDK_ENV_PREFIX}environment`] = "test-environment"
expect(() => {
createApp(buildParams())
}).toThrow(`Environment variable ${CDK_ENV_PREFIX}versionNumber is not set`)
})
test("throws error when commitId is not set", () => {
process.env[`${CDK_ENV_PREFIX}versionNumber`] = "1.0.0"
process.env[`${CDK_ENV_PREFIX}isPullRequest`] = "false"
process.env[`${CDK_ENV_PREFIX}environment`] = "test-environment"
expect(() => {
createApp(buildParams())
}).toThrow(`Environment variable ${CDK_ENV_PREFIX}commitId is not set`)
})
test("throws error when isPullRequest is not set", () => {
process.env[`${CDK_ENV_PREFIX}versionNumber`] = "1.0.0"
process.env[`${CDK_ENV_PREFIX}commitId`] = "abc123"
process.env[`${CDK_ENV_PREFIX}environment`] = "test-environment"
expect(() => {
createApp(buildParams())
}).toThrow(`Environment variable ${CDK_ENV_PREFIX}isPullRequest is not set`)
})
test("throws error when environment is not set", () => {
process.env[`${CDK_ENV_PREFIX}versionNumber`] = "1.0.0"
process.env[`${CDK_ENV_PREFIX}commitId`] = "abc123"
process.env[`${CDK_ENV_PREFIX}isPullRequest`] = "false"
expect(() => {
createApp(buildParams())
}).toThrow(`Environment variable ${CDK_ENV_PREFIX}environment is not set`)
})
})
})