11// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22// SPDX-License-Identifier: Apache-2.0
33
4+ import { S3ServiceException } from "@aws-sdk/client-s3" ;
45import { describe , it , expect , vi } from "vitest" ;
56
67const send = vi . fn ( ) ;
@@ -19,28 +20,56 @@ const { main } = await import("../actions/get-bucket-website.js");
1920
2021describe ( "get-bucket-website" , ( ) => {
2122 it ( "should log the response from the service" , async ( ) => {
22- send . mockResolvedValue ( {
23+ const mockResponse = {
2324 IndexDocument : { Suffix : "foo" } ,
2425 ErrorDocument : { Key : "bar" } ,
25- } ) ;
26+ } ;
27+ send . mockResolvedValue ( mockResponse ) ;
28+ const bucketName = "amzn-s3-demo-bucket" ;
2629
2730 const spy = vi . spyOn ( console , "log" ) ;
2831
29- await main ( ) ;
32+ await main ( { bucketName } ) ;
3033
3134 expect ( spy ) . toHaveBeenCalledWith (
32- "Your bucket is set up to host a website. It has an error document:" ,
33- "bar, and an index document: foo." ,
35+ `Your bucket is set up to host a website with the following configuration:\n${ JSON . stringify ( mockResponse , null , 2 ) } ` ,
3436 ) ;
3537 } ) ;
3638
37- it ( "should log errors" , async ( ) => {
38- send . mockRejectedValue ( "foo" ) ;
39+ it ( "should log a relevant error when the bucket isn't configured as a website." , async ( ) => {
40+ const error = new S3ServiceException ( "Not such website configuration." ) ;
41+ error . name = "NoSuchWebsiteConfiguration" ;
42+ const bucketName = "amzn-s3-demo-bucket" ;
43+ send . mockRejectedValueOnce ( error ) ;
3944
4045 const spy = vi . spyOn ( console , "error" ) ;
4146
42- await main ( ) ;
47+ await main ( { bucketName } ) ;
4348
44- expect ( spy ) . toHaveBeenCalledWith ( "foo" ) ;
49+ expect ( spy ) . toHaveBeenCalledWith (
50+ `Error from S3 while getting website configuration for ${ bucketName } . The bucket isn't configured as a website.` ,
51+ ) ;
52+ } ) ;
53+
54+ it ( "should indicate a failure came from S3 when the error isn't generic" , async ( ) => {
55+ const error = new S3ServiceException ( "Some S3 service exception." ) ;
56+ error . name = "ServiceException" ;
57+ const bucketName = "amzn-s3-demo-bucket" ;
58+ send . mockRejectedValueOnce ( error ) ;
59+
60+ const spy = vi . spyOn ( console , "error" ) ;
61+
62+ await main ( { bucketName } ) ;
63+
64+ expect ( spy ) . toHaveBeenCalledWith (
65+ `Error from S3 while getting website configuration for ${ bucketName } . ${ error . name } : ${ error . message } ` ,
66+ ) ;
67+ } ) ;
68+
69+ it ( "should throw errors that are not S3 specific" , async ( ) => {
70+ const bucketName = "amzn-s3-demo-bucket" ;
71+ send . mockRejectedValueOnce ( new Error ( ) ) ;
72+
73+ await expect ( ( ) => main ( { bucketName } ) ) . rejects . toBeTruthy ( ) ;
4574 } ) ;
4675} ) ;
0 commit comments