2023-04-19 12:31:05 -07:00
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2024-01-16 10:41:11 -05:00
// SPDX-License-Identifier: Apache-2.0
2023-04-19 12:31:05 -07:00
import { describe , it , expect , vi } from "vitest" ;
const mockSendFn = vi . fn ( ) ;
vi . doMock ( "@aws-sdk/client-s3" , ( ) => {
return {
S3Client : class {
send = mockSendFn ;
} ,
ListObjectsCommand : class { } ,
} ;
} ) ;
2023-05-26 11:04:21 -04:00
const { handler } = await import (
"../guide_supplements/guide-cdk-lambda-function.js"
) ;
2023-04-19 12:31:05 -07:00
describe ( "guide-cdk-lambda-function" , ( ) => {
it ( "should return a list of object names if S3 responds successfully" , async ( ) => {
process . env . BUCKET = "test-bucket" ;
mockSendFn . mockImplementationOnce ( ( ) =>
Promise . resolve ( {
Contents : [ { Key : "test-key" } ] ,
2023-10-02 12:43:28 -04:00
} ) ,
2023-04-19 12:31:05 -07:00
) ;
const response = await handler ( { httpMethod : "GET" , path : "/" } ) ;
expect ( response . statusCode ) . toBe ( 200 ) ;
expect ( response . body ) . toEqual ( [ "test-key" ] ) ;
} ) ;
it ( "should return a 204 if the bucket is empty" , async ( ) => {
process . env . BUCKET = "test-bucket" ;
mockSendFn . mockImplementationOnce ( ( ) => Promise . resolve ( { Contents : [ ] } ) ) ;
const response = await handler ( { httpMethod : "GET" , path : "/" } ) ;
expect ( response . statusCode ) . toBe ( 204 ) ;
} ) ;
it ( "should return a 400 if there is no bucket name" , async ( ) => {
process . env . BUCKET = undefined ;
mockSendFn . mockImplementationOnce ( ( ) =>
2023-10-02 12:43:28 -04:00
Promise . resolve ( { Contents : [ { Key : "test-key" } ] } ) ,
2023-04-19 12:31:05 -07:00
) ;
const response = await handler ( { httpMethod : "GET" , path : "/" } ) ;
expect ( response . statusCode ) . toBe ( 400 ) ;
} ) ;
2023-05-26 11:04:21 -04:00
it ( "should return a 400 if an unsupported http method is called" , async ( ) => {
const response = await handler ( { httpMethod : "PATCH" , path : "/" } ) ;
2023-04-19 12:31:05 -07:00
expect ( response . statusCode ) . toBe ( 400 ) ;
2023-05-26 11:04:21 -04:00
} ) ;
2023-04-19 12:31:05 -07:00
} ) ;