Skip to content

Commit

Permalink
test: check-fallback-image.spec.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed May 24, 2024
1 parent f16e1c7 commit bfbf098
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 28 deletions.
38 changes: 13 additions & 25 deletions source/custom-resource/test/check-fallback-image.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,7 @@ describe("CHECK_FALLBACK_IMAGE", () => {
});

it("Should return success when the default fallback image exists", async () => {
mockAwsS3.headObject.mockImplementation(() => ({
promise() {
return Promise.resolve(head);
},
}));
mockAwsS3.headObject.mockImplementation(() => Promise.resolve(head));

const result = await handler(event, mockContext);

Expand Down Expand Up @@ -107,11 +103,7 @@ describe("CHECK_FALLBACK_IMAGE", () => {
});

it("Should return failed when the default fallback image does not exist", async () => {
mockAwsS3.headObject.mockImplementation(() => ({
promise() {
return Promise.reject(new CustomResourceError("NotFound", null));
},
}));
mockAwsS3.headObject.mockImplementation(() => Promise.reject(new CustomResourceError("NotFound", null)));
(event.ResourceProperties as CheckFallbackImageRequestProperties).FallbackImageS3Key = "fallback-image.jpg";

const result = await handler(event, mockContext);
Expand All @@ -134,21 +126,17 @@ describe("CHECK_FALLBACK_IMAGE", () => {
});

it("Should retry and return success when IAM policy is not ready so S3 API returns AccessDenied or Forbidden", async () => {
mockAwsS3.headObject.mockImplementationOnce(() => ({
promise() {
return Promise.reject(new CustomResourceError(ErrorCodes.ACCESS_DENIED, null));
},
}));
mockAwsS3.headObject.mockImplementationOnce(() => ({
promise() {
return Promise.reject(new CustomResourceError(ErrorCodes.FORBIDDEN, null));
},
}));
mockAwsS3.headObject.mockImplementationOnce(() => ({
promise() {
return Promise.resolve(head);
},
}));
mockAwsS3.headObject.mockImplementationOnce(() => {
const error = new Error(ErrorCodes.ACCESS_DENIED);
error.name = ErrorCodes.ACCESS_DENIED;
return Promise.reject(error);
});
mockAwsS3.headObject.mockImplementationOnce(() => {
const error = new Error(ErrorCodes.FORBIDDEN);
error.name = ErrorCodes.FORBIDDEN;
return Promise.reject(error);
});
mockAwsS3.headObject.mockImplementationOnce(() => Promise.resolve(head));

const result = await handler(event, mockContext);

Expand Down
12 changes: 9 additions & 3 deletions source/custom-resource/test/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ export const mockAwsEc2 = {
describeRegions: jest.fn(),
};

jest.mock("aws-sdk/clients/ec2", () => jest.fn(() => ({ ...mockAwsEc2 })));
jest.mock("@aws-sdk/client-ec2", () => ({
EC2: jest.fn(() => ({ ...mockAwsEc2 }))
}));

export const mockAwsS3 = {
headObject: jest.fn(),
Expand All @@ -20,13 +22,17 @@ export const mockAwsS3 = {
putBucketPolicy: jest.fn(),
};

jest.mock("aws-sdk/clients/s3", () => jest.fn(() => ({ ...mockAwsS3 })));
jest.mock("@aws-sdk/client-s3", () => ({
S3: jest.fn(() => ({ ...mockAwsS3 }))
}));

export const mockAwsSecretManager = {
getSecretValue: jest.fn(),
};

jest.mock("aws-sdk/clients/secretsmanager", () => jest.fn(() => ({ ...mockAwsSecretManager })));
jest.mock("@aws-sdk/client-secrets-manager", () => ({
SecretsManager: jest.fn(() => ({ ...mockAwsSecretManager }))
}));

export const mockAxios = {
put: jest.fn(),
Expand Down

0 comments on commit bfbf098

Please sign in to comment.