Skip to content

Commit

Permalink
fix: getUploadUrl testing woe
Browse files Browse the repository at this point in the history
  • Loading branch information
asharonbaltazar committed Aug 5, 2024
1 parent 728aec0 commit 22e2dbc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
17 changes: 11 additions & 6 deletions lib/lambda/getUploadUrl.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
import { describe, it, expect, vi, beforeEach, Mock } from "vitest";
import { APIGatewayEvent } from "aws-lambda";
import { handler } from "./getUploadUrl";
import { response } from "libs/handler-lib";
Expand Down Expand Up @@ -27,7 +27,7 @@ describe("Handler for generating signed URL", () => {
vi.clearAllMocks();
process.env.attachmentsBucketName = "test-bucket";
process.env.attachmentsBucketRegion = "test-region";
(uuidv4 as vi.Mock).mockReturnValue("123e4567-e89b-12d3-a456-426614174000");
(uuidv4 as Mock).mockReturnValue("123e4567-e89b-12d3-a456-426614174000");
});

it("should return 400 if event body is missing", async () => {
Expand All @@ -43,7 +43,7 @@ describe("Handler for generating signed URL", () => {

it("should return 200 with signed URL, bucket, and key", async () => {
const mockUrl = "https://example.com/signed-url";
(getSignedUrl as vi.Mock).mockResolvedValueOnce(mockUrl);
(getSignedUrl as Mock).mockResolvedValueOnce(mockUrl);

const event = {
body: JSON.stringify({ fileName: "test-file.pdf" }),
Expand All @@ -63,7 +63,7 @@ describe("Handler for generating signed URL", () => {
});

it("should return 500 if an error occurs during processing", async () => {
(getSignedUrl as vi.Mock).mockRejectedValueOnce(new Error("Test error"));
(getSignedUrl as Mock).mockRejectedValueOnce(new Error("Test error"));

const event = {
body: JSON.stringify({ fileName: "test-file.pdf" }),
Expand All @@ -77,9 +77,14 @@ describe("Handler for generating signed URL", () => {
});
});

it("should throw an error if required environment variables are missing", () => {
it("should throw an error if required environment variables are missing", async () => {
delete process.env.attachmentsBucketName;

expect(() => handler({} as APIGatewayEvent)).toThrowError();
await handler({} as APIGatewayEvent);

expect(response).toHaveBeenCalledWith({
statusCode: 500,
body: { message: "Internal server error" },
});
});
});
5 changes: 3 additions & 2 deletions lib/lambda/getUploadUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ const s3 = new S3Client({
});

export const handler = async (event: APIGatewayEvent) => {
validateEnvVariable("attachmentsBucketName");
validateEnvVariable("attachmentsBucketRegion");
try {
validateEnvVariable("attachmentsBucketName");
validateEnvVariable("attachmentsBucketRegion");

if (!event.body) {
return response({
statusCode: 400,
Expand Down

0 comments on commit 22e2dbc

Please sign in to comment.