66 vi
77} from "vitest"
88import { deployApi } from "../../src/specifications/deployApi"
9+ import type { ApiConfig } from "../../src/specifications/deployApi"
910
1011const lambdaSendMock = vi . fn ( )
1112
@@ -20,8 +21,6 @@ vi.mock("@aws-sdk/client-lambda", () => {
2021 }
2122
2223 class LambdaClient {
23- constructor ( ) { }
24-
2524 // eslint-disable-next-line @typescript-eslint/no-explicit-any
2625 send ( command : any ) {
2726 return lambdaSendMock ( command )
@@ -64,39 +63,49 @@ const defaultExportsMap = {
6463 "account-resources:proxygenKey" : "arn:proxygen-key"
6564}
6665
66+ function buildConfig ( overrides : Partial < ApiConfig > = { } ) : ApiConfig {
67+ return {
68+ specification : JSON . stringify ( createSpec ( ) ) ,
69+ apiName : "eps" ,
70+ version : "1.0.0" ,
71+ apigeeEnvironment : "internal-dev" ,
72+ isPullRequest : false ,
73+ awsEnvironment : "nonprod" ,
74+ stackName : "eps-stack-001" ,
75+ mtlsSecretName : "mtls/secret" ,
76+ clientCertExportName : "clientCert" ,
77+ clientPrivateKeyExportName : "clientKey" ,
78+ proxygenPrivateKeyExportName : "proxygenKey" ,
79+ proxygenKid : "kid-123" ,
80+ ...overrides
81+ }
82+ }
83+
84+ function payloadFromCall ( callIndex : number ) {
85+ const command = lambdaSendMock . mock . calls [ callIndex ] [ 0 ] as { input : { Payload : Buffer } }
86+ return JSON . parse ( command . input . Payload . toString ( ) )
87+ }
88+
89+ function functionNameFromCall ( callIndex : number ) {
90+ const command = lambdaSendMock . mock . calls [ callIndex ] [ 0 ] as { input : { FunctionName : string } }
91+ return command . input . FunctionName
92+ }
93+
6794describe ( "deployApi" , ( ) => {
6895 beforeEach ( ( ) => {
6996 lambdaSendMock . mockReset ( ) . mockResolvedValue ( { Payload : Buffer . from ( '"ok"' ) } )
7097 getCloudFormationExportsMock . mockReset ( )
7198 } )
7299
73- function payloadFromCall ( callIndex : number ) {
74- const command = lambdaSendMock . mock . calls [ callIndex ] [ 0 ] as { input : { Payload : Buffer } }
75- return JSON . parse ( command . input . Payload . toString ( ) )
76- }
77-
78- function functionNameFromCall ( callIndex : number ) {
79- const command = lambdaSendMock . mock . calls [ callIndex ] [ 0 ] as { input : { FunctionName : string } }
80- return command . input . FunctionName
81- }
82-
83100 test ( "stores secrets, deploys instance and publishes spec for internal-dev" , async ( ) => {
84- const spec = JSON . stringify ( createSpec ( ) )
85101 getCloudFormationExportsMock . mockResolvedValue ( defaultExportsMap )
86102
87103 await deployApi (
88- spec ,
89- "eps" ,
90- "2.0.0" ,
91- "internal-dev" ,
92- false ,
93- "nonprod" ,
94- "eps-stack-001" ,
95- "mtls/secret" ,
96- "clientCert" ,
97- "clientKey" ,
98- "proxygenKey" ,
99- "kid-123" ,
104+ buildConfig ( {
105+ version : "2.0.0" ,
106+ apigeeEnvironment : "internal-dev" ,
107+ stackName : "eps-stack-001"
108+ } ) ,
100109 false
101110 )
102111
@@ -135,22 +144,16 @@ describe("deployApi", () => {
135144 } )
136145
137146 test ( "handles pull requests in sandbox without storing secrets" , async ( ) => {
138- const spec = JSON . stringify ( createSpec ( ) )
139147 getCloudFormationExportsMock . mockResolvedValue ( defaultExportsMap )
140148
141149 await deployApi (
142- spec ,
143- "eps" ,
144- "3.1.4" ,
145- "sandbox" ,
146- true ,
147- "nonprod" ,
148- "eps-pr-stack-456" ,
149- "mtls/secret" ,
150- "clientCert" ,
151- "clientKey" ,
152- "proxygenKey" ,
153- "kid-789" ,
150+ buildConfig ( {
151+ version : "3.1.4" ,
152+ apigeeEnvironment : "sandbox" ,
153+ isPullRequest : true ,
154+ stackName : "eps-pr-stack-456" ,
155+ proxygenKid : "kid-789"
156+ } ) ,
154157 false
155158 )
156159
@@ -168,22 +171,16 @@ describe("deployApi", () => {
168171 } )
169172
170173 test ( "uses prod lambdas and prod security scheme refs" , async ( ) => {
171- const spec = JSON . stringify ( createSpec ( ) )
172174 getCloudFormationExportsMock . mockResolvedValue ( defaultExportsMap )
173175
174176 await deployApi (
175- spec ,
176- "eps" ,
177- "4.0.0" ,
178- "prod" ,
179- false ,
180- "prod" ,
181- "eps-prod-stack" ,
182- "mtls/secret" ,
183- "clientCert" ,
184- "clientKey" ,
185- "proxygenKey" ,
186- "kid-prod" ,
177+ buildConfig ( {
178+ version : "4.0.0" ,
179+ apigeeEnvironment : "prod" ,
180+ awsEnvironment : "prod" ,
181+ stackName : "eps-prod-stack" ,
182+ proxygenKid : "kid-prod"
183+ } ) ,
187184 false
188185 )
189186
@@ -199,22 +196,15 @@ describe("deployApi", () => {
199196 } )
200197
201198 test ( "publishes spec to prod catalogue for int environment" , async ( ) => {
202- const spec = JSON . stringify ( createSpec ( ) )
203199 getCloudFormationExportsMock . mockResolvedValue ( defaultExportsMap )
204200
205201 await deployApi (
206- spec ,
207- "eps" ,
208- "5.0.0" ,
209- "int" ,
210- false ,
211- "nonprod" ,
212- "eps-int-stack" ,
213- "mtls/secret" ,
214- "clientCert" ,
215- "clientKey" ,
216- "proxygenKey" ,
217- "kid-int" ,
202+ buildConfig ( {
203+ version : "5.0.0" ,
204+ apigeeEnvironment : "int" ,
205+ stackName : "eps-int-stack" ,
206+ proxygenKid : "kid-int"
207+ } ) ,
218208 false
219209 )
220210
@@ -227,23 +217,15 @@ describe("deployApi", () => {
227217 } )
228218
229219 test ( "dry run only logs intended invocations" , async ( ) => {
230- const spec = JSON . stringify ( createSpec ( ) )
231220 getCloudFormationExportsMock . mockResolvedValue ( defaultExportsMap )
232221 const logSpy = vi . spyOn ( console , "log" ) . mockImplementation ( ( ) => undefined )
233222
234223 await deployApi (
235- spec ,
236- "eps" ,
237- "1.0.0" ,
238- "int" ,
239- false ,
240- "nonprod" ,
241- "eps-int-stack" ,
242- "mtls/secret" ,
243- "clientCert" ,
244- "clientKey" ,
245- "proxygenKey" ,
246- "kid-int" ,
224+ buildConfig ( {
225+ apigeeEnvironment : "int" ,
226+ stackName : "eps-int-stack" ,
227+ proxygenKid : "kid-int"
228+ } ) ,
247229 true
248230 )
249231
@@ -255,24 +237,16 @@ describe("deployApi", () => {
255237 } )
256238
257239 test ( "throws when lambda invocation returns a FunctionError" , async ( ) => {
258- const spec = JSON . stringify ( createSpec ( ) )
259240 getCloudFormationExportsMock . mockResolvedValue ( defaultExportsMap )
260241 lambdaSendMock
261242 . mockResolvedValueOnce ( { FunctionError : "Handled" , Payload : Buffer . from ( '"bad"' ) } )
262243
263244 await expect ( deployApi (
264- spec ,
265- "eps" ,
266- "1.2.3" ,
267- "int" ,
268- false ,
269- "nonprod" ,
270- "eps-stack" ,
271- "mtls/secret" ,
272- "clientCert" ,
273- "clientKey" ,
274- "proxygenKey" ,
275- "kid" ,
245+ buildConfig ( {
246+ version : "1.2.3" ,
247+ apigeeEnvironment : "int" ,
248+ stackName : "eps-stack"
249+ } ) ,
276250 false
277251 ) ) . rejects . toThrow ( "Error calling lambda lambda-resources-ProxygenProdMTLSSecretPut: \"bad\"" )
278252 } )
0 commit comments