@@ -12,20 +12,19 @@ limitations under the License.
1212*/
1313
1414import { handler } from "./proxy" ;
15- import axios , { AxiosRequestHeaders , AxiosResponse } from "axios" ;
15+ import type { AxiosRequestHeaders , AxiosResponse } from "axios" ;
1616import { readFileSync } from "fs" ;
1717import { Agent } from "https" ;
18- import { readFileFromLayer } from "./file-readers" ;
1918import { APIGatewayProxyWithLambdaAuthorizerEvent } from "aws-lambda" ;
2019import { VALID_PING_EVENT } from "../fixtures/valid-ping-payload" ;
2120import { VALID_PUSH_PAYLOAD } from "../fixtures/valid-push-payload" ;
2221import { VALID_PUSH_PAYLOAD_USER_REPO } from "../fixtures/valid-push-payload-user-repo" ;
22+ import { afterEach , beforeEach , describe , expect , it , mock } from "bun:test" ;
23+
2324const urlencodedPayload = readFileSync (
2425 "fixtures/invalid-payload-urlencoded.txt" ,
2526) . toString ( ) ;
2627
27- jest . mock ( "axios" ) ;
28- jest . mock ( "./file-readers" ) ;
2928const axiosResponse : AxiosResponse = {
3029 status : 200 ,
3130 data : {
@@ -39,7 +38,26 @@ const axiosResponse: AxiosResponse = {
3938 headers : { } as AxiosRequestHeaders ,
4039 } ,
4140} ;
42- ( axios . post as jest . Mock ) . mockResolvedValue ( axiosResponse ) ;
41+ const axiosPostMock = mock ( ( ) => axiosResponse ) ;
42+ mock . module ( "axios" , ( ) => ( {
43+ default : {
44+ post : axiosPostMock ,
45+ } ,
46+ } ) ) ;
47+
48+ const fileMap : Record < string , string > = {
49+ "allowed-destination-hosts.json" : JSON . stringify ( [
50+ "approved.host" ,
51+ "another.approved.host" ,
52+ "a.wildcard.*.host" ,
53+ ] ) ,
54+ } ;
55+ const readFileFromLayerMock = mock ( ( fileName : string ) => fileMap [ fileName ] ) ;
56+ mock . module ( "./file-readers" , ( ) => ( {
57+ readFileFromLayer : readFileFromLayerMock ,
58+ getPublicCerts : mock ( ) ,
59+ } ) ) ;
60+
4361const expectedResponseObject = {
4462 statusCode : 200 ,
4563 body : '{"some":"data"}' ,
@@ -66,23 +84,17 @@ const baseEvent: APIGatewayProxyWithLambdaAuthorizerEvent<any> = {
6684 requestContext : { } as any ,
6785 resource : "" ,
6886} ;
69- const fileMap : Record < string , string > = {
70- "allowed-destination-hosts.json" : JSON . stringify ( [
71- "approved.host" ,
72- "another.approved.host" ,
73- "a.wildcard.*.host" ,
74- ] ) ,
75- } ;
76- ( readFileFromLayer as jest . Mock ) . mockImplementation (
77- ( fileName : string ) => fileMap [ fileName ] ,
78- ) ;
7987
8088describe ( "proxy" , ( ) => {
8189 beforeEach ( ( ) => {
8290 process . env . ENTERPRISE_SLUG = "some_enterprise" ;
8391 process . env . ENTERPRISE_MANAGED_USER_SUFFIX = "" ;
8492 } ) ;
8593
94+ afterEach ( ( ) => {
95+ mock . clearAllMocks ( ) ;
96+ } ) ;
97+
8698 it ( "should reject a request with an invalid urlencoded payload" , async ( ) => {
8799 const event : APIGatewayProxyWithLambdaAuthorizerEvent < any > = {
88100 ...baseEvent ,
@@ -94,7 +106,7 @@ describe("proxy", () => {
94106 } ;
95107 const result = await handler ( event ) ;
96108 expect ( result ) . toEqual ( { statusCode : 403 , body : "Access denied" } ) ;
97- expect ( axios . post ) . not . toHaveBeenCalled ( ) ;
109+ expect ( axiosPostMock ) . not . toHaveBeenCalled ( ) ;
98110 } ) ;
99111
100112 it ( "should reject a request with an endpointId which is not an encoded URL" , async ( ) => {
@@ -106,7 +118,7 @@ describe("proxy", () => {
106118 } ;
107119 const result = await handler ( event ) ;
108120 expect ( result ) . toEqual ( { statusCode : 403 , body : "Access denied" } ) ;
109- expect ( axios . post ) . not . toHaveBeenCalled ( ) ;
121+ expect ( axiosPostMock ) . not . toHaveBeenCalled ( ) ;
110122 } ) ;
111123
112124 it ( "should allow a request from a managed user suffix when supplied" , async ( ) => {
@@ -122,7 +134,7 @@ describe("proxy", () => {
122134 } ;
123135 const result = await handler ( event ) ;
124136 expect ( result ) . toEqual ( expectedResponseObject ) ;
125- expect ( axios . post ) . toHaveBeenCalled ( ) ;
137+ expect ( axiosPostMock ) . toHaveBeenCalled ( ) ;
126138 } ) ;
127139
128140 it ( "should forward a request when header is Content-Type" , async ( ) => {
@@ -142,7 +154,7 @@ describe("proxy", () => {
142154 } ;
143155 const result = await handler ( event ) ;
144156 expect ( result ) . toEqual ( expectedResponseObject ) ;
145- expect ( axios . post ) . toHaveBeenCalled ( ) ;
157+ expect ( axiosPostMock ) . toHaveBeenCalled ( ) ;
146158 } ) ;
147159
148160 it ( "should not forward a request that does not come from an enterprise or managed user suffix" , async ( ) => {
@@ -161,7 +173,7 @@ describe("proxy", () => {
161173 } ;
162174 const result = await handler ( event ) ;
163175 expect ( result ) . toEqual ( { statusCode : 403 , body : "Access denied" } ) ;
164- expect ( axios . post ) . not . toHaveBeenCalled ( ) ;
176+ expect ( axiosPostMock ) . not . toHaveBeenCalled ( ) ;
165177 } ) ;
166178
167179 it ( "should not forward a request that does not have an approved host" , async ( ) => {
@@ -175,7 +187,7 @@ describe("proxy", () => {
175187 } ;
176188 const result = await handler ( event ) ;
177189 expect ( result ) . toEqual ( { statusCode : 403 , body : "Access denied" } ) ;
178- expect ( axios . post ) . not . toHaveBeenCalled ( ) ;
190+ expect ( axiosPostMock ) . not . toHaveBeenCalled ( ) ;
179191 } ) ;
180192
181193 it ( "should forward a request that has an approved host which matches a wildcard" , async ( ) => {
@@ -201,7 +213,7 @@ describe("proxy", () => {
201213 } ,
202214 } ;
203215 const result = await handler ( event ) ;
204- expect ( axios . post ) . toHaveBeenCalledWith (
216+ expect ( axiosPostMock ) . toHaveBeenCalledWith (
205217 destinationUrl ,
206218 stringifiedPayload ,
207219 {
@@ -220,7 +232,7 @@ describe("proxy", () => {
220232 "ca.pem" : "some ca" ,
221233 "cert.pem" : "some cert" ,
222234 } ;
223- ( readFileFromLayer as jest . Mock ) . mockImplementation (
235+ readFileFromLayerMock . mockImplementation (
224236 ( fileName : string ) => newFileMap [ fileName ] ,
225237 ) ;
226238 const destinationUrl = "https://approved.host/github-webhook/" ;
@@ -232,7 +244,7 @@ describe("proxy", () => {
232244 } ,
233245 } ;
234246 const result = await handler ( event ) ;
235- expect ( axios . post ) . toHaveBeenCalledWith (
247+ expect ( axiosPostMock ) . toHaveBeenCalledWith (
236248 destinationUrl ,
237249 stringifiedPayload ,
238250 {
@@ -253,7 +265,7 @@ describe("proxy", () => {
253265 } ;
254266 const result = await handler ( event ) ;
255267 expect ( result ) . toEqual ( { statusCode : 404 , body : "Not found" } ) ;
256- expect ( axios . post ) . not . toHaveBeenCalled ( ) ;
268+ expect ( axiosPostMock ) . not . toHaveBeenCalled ( ) ;
257269 } ) ;
258270
259271 it ( "should forward a ping event from a managed user suffix when supplied" , async ( ) => {
@@ -269,7 +281,7 @@ describe("proxy", () => {
269281 } ;
270282 const result = await handler ( event ) ;
271283 expect ( result ) . toEqual ( expectedResponseObject ) ;
272- expect ( axios . post ) . toHaveBeenCalled ( ) ;
284+ expect ( axiosPostMock ) . toHaveBeenCalled ( ) ;
273285 } ) ;
274286
275287 it ( "should return error response from axios" , async ( ) => {
@@ -286,9 +298,7 @@ describe("proxy", () => {
286298 headers : { } as AxiosRequestHeaders ,
287299 } ,
288300 } ;
289- ( axios . post as jest . Mock ) . mockRejectedValue ( {
290- response : axiosErrorResponse ,
291- } ) ;
301+ axiosPostMock . mockImplementationOnce ( ( ) => axiosErrorResponse ) ;
292302
293303 process . env . ENTERPRISE_MANAGED_USER_SUFFIX = "suffix" ;
294304 const destinationUrl = "https://approved.host/github-webhook/" ;
@@ -306,6 +316,6 @@ describe("proxy", () => {
306316 body : '{"some":"error"}' ,
307317 headers : { response : "headers" } ,
308318 } ) ;
309- expect ( axios . post ) . toHaveBeenCalled ( ) ;
319+ expect ( axiosPostMock ) . toHaveBeenCalled ( ) ;
310320 } ) ;
311321} ) ;
0 commit comments