mirror of
https://github.com/formbricks/formbricks.git
synced 2026-05-07 14:21:52 -05:00
adds tests
This commit is contained in:
@@ -6,6 +6,7 @@ import {
|
||||
validateSingleFile,
|
||||
} from "@/modules/storage/utils";
|
||||
import { beforeEach, describe, expect, test, vi } from "vitest";
|
||||
import { StorageErrorCode } from "@formbricks/storage";
|
||||
import { TResponseData } from "@formbricks/types/responses";
|
||||
import { ZAllowedFileExtension } from "@formbricks/types/storage";
|
||||
import { TSurveyQuestion } from "@formbricks/types/surveys/types";
|
||||
@@ -35,6 +36,76 @@ describe("storage utils", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("getErrorResponseFromStorageError", () => {
|
||||
test("returns appropriate responses for each storage error code", async () => {
|
||||
// Mock responses helper to observe calls and provide Response objects
|
||||
vi.doMock("@/app/lib/api/response", () => ({
|
||||
responses: {
|
||||
notFoundResponse: vi.fn(
|
||||
(_entity: string, _id?: string | null, _public?: boolean) => new Response(null, { status: 404 })
|
||||
),
|
||||
badRequestResponse: vi.fn(
|
||||
(_msg: string, _details?: unknown, _public?: boolean) => new Response(null, { status: 400 })
|
||||
),
|
||||
internalServerErrorResponse: vi.fn(
|
||||
(_msg: string, _public?: boolean) => new Response(null, { status: 500 })
|
||||
),
|
||||
},
|
||||
}));
|
||||
|
||||
const { getErrorResponseFromStorageError } = await import("@/modules/storage/utils");
|
||||
|
||||
// FileNotFoundError uses notFoundResponse with details.fileName or null
|
||||
const r404 = getErrorResponseFromStorageError(
|
||||
{ code: StorageErrorCode.FileNotFoundError },
|
||||
{
|
||||
fileName: "file.png",
|
||||
}
|
||||
);
|
||||
expect(r404.status).toBe(404);
|
||||
|
||||
// InvalidInput -> 400
|
||||
const r400 = getErrorResponseFromStorageError({ code: StorageErrorCode.InvalidInput }, {
|
||||
reason: "bad",
|
||||
} as any);
|
||||
expect(r400.status).toBe(400);
|
||||
|
||||
// S3 related and Unknown -> 500
|
||||
const r500a = getErrorResponseFromStorageError({ code: StorageErrorCode.S3ClientError });
|
||||
expect(r500a.status).toBe(500);
|
||||
const r500b = getErrorResponseFromStorageError({ code: StorageErrorCode.S3CredentialsError });
|
||||
expect(r500b.status).toBe(500);
|
||||
const r500c = getErrorResponseFromStorageError({ code: StorageErrorCode.Unknown });
|
||||
expect(r500c.status).toBe(500);
|
||||
|
||||
// Default branch (unknown string) -> 500
|
||||
const r500d = getErrorResponseFromStorageError({ code: "something_else" as any });
|
||||
expect(r500d.status).toBe(500);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getOriginalFileNameFromUrl (actual)", () => {
|
||||
test("extracts original name from full URL with fid and extension", async () => {
|
||||
const { getOriginalFileNameFromUrl } =
|
||||
await vi.importActual<typeof import("@/modules/storage/utils")>("@/modules/storage/utils");
|
||||
const url = "https://cdn.example.com/storage/env/public/photo--fid--12345.png?x=1#hash";
|
||||
expect(getOriginalFileNameFromUrl(url)).toBe("photo.png");
|
||||
});
|
||||
|
||||
test("handles /storage/ relative path and missing fid", async () => {
|
||||
const { getOriginalFileNameFromUrl } =
|
||||
await vi.importActual<typeof import("@/modules/storage/utils")>("@/modules/storage/utils");
|
||||
const path = "/storage/env/public/Document%20Name.pdf";
|
||||
expect(getOriginalFileNameFromUrl(path)).toBe("/storage/env/public/Document Name.pdf");
|
||||
});
|
||||
|
||||
test("returns empty string on invalid URL input", async () => {
|
||||
const { getOriginalFileNameFromUrl } =
|
||||
await vi.importActual<typeof import("@/modules/storage/utils")>("@/modules/storage/utils");
|
||||
expect(getOriginalFileNameFromUrl("ht!tp://%$^&")).toBe("");
|
||||
});
|
||||
});
|
||||
|
||||
describe("isAllowedFileExtension", () => {
|
||||
test("should return false for a file with no extension", () => {
|
||||
expect(isAllowedFileExtension("filename")).toBe(false);
|
||||
|
||||
@@ -196,6 +196,27 @@ describe("client.ts", () => {
|
||||
expect(result.data).toBeDefined();
|
||||
}
|
||||
});
|
||||
|
||||
test("should return unknown error when S3Client constructor throws", async () => {
|
||||
// Provide valid credentials so we reach the constructor path
|
||||
vi.doMock("./constants", () => ({
|
||||
...mockConstants,
|
||||
}));
|
||||
|
||||
// Make the mocked S3Client throw on construction for this test only
|
||||
mockS3Client.mockImplementationOnce((..._args: [S3ClientConfig] | []): S3Client => {
|
||||
throw new Error("constructor failed");
|
||||
});
|
||||
|
||||
const { createS3ClientFromEnv } = await import("./client");
|
||||
|
||||
const result = createS3ClientFromEnv();
|
||||
|
||||
expect(result.ok).toBe(false);
|
||||
if (!result.ok) {
|
||||
expect(result.error.code).toBe("unknown");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("createS3Client", () => {
|
||||
|
||||
Reference in New Issue
Block a user