102 lines
3.1 KiB
TypeScript
102 lines
3.1 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, test } from "vitest";
|
|
|
|
import { resetApiRuntimeConfigForTests, setApiRuntimeConfigForTests } from "./config.js";
|
|
import { createStorageSignedDownloadUrl, resetStorageCachesForTests } from "./storage.js";
|
|
|
|
const baseConfig = {
|
|
appName: "Test API",
|
|
nodeEnv: "test",
|
|
revision: "unknown",
|
|
listen: { port: 3001 },
|
|
routing: {
|
|
appBaseDomain: "localhost:3000",
|
|
webAppHost: "app.localhost:3000",
|
|
adminAppHost: "admin.localhost:3000",
|
|
apiHost: "api.localhost:3000",
|
|
protocol: "http",
|
|
},
|
|
origins: {},
|
|
apiAccessToken: "service-token",
|
|
jwtSecret: "jwt-secret",
|
|
analytics: {},
|
|
mailjet: {},
|
|
lago: { timeoutMs: 10000 },
|
|
mollie: { timeoutMs: 10000 },
|
|
apiKeys: { allowLegacyPlaintextFallback: true },
|
|
storage: {
|
|
endpoint: "https://storage.example.com",
|
|
region: "us-east-1",
|
|
accessKeyId: "test-access-key",
|
|
secretAccessKey: "test-secret-key",
|
|
forcePathStyle: "true",
|
|
},
|
|
} as const;
|
|
|
|
beforeEach(() => {
|
|
resetStorageCachesForTests();
|
|
});
|
|
|
|
afterEach(() => {
|
|
resetApiRuntimeConfigForTests();
|
|
});
|
|
|
|
describe("createStorageSignedDownloadUrl edge cases", () => {
|
|
test("surfaces a config_missing diagnostic when storage credentials are absent", async () => {
|
|
setApiRuntimeConfigForTests({ ...baseConfig, storage: {} });
|
|
|
|
await expect(
|
|
createStorageSignedDownloadUrl("exports", { key: "a/b.csv", expiresInSeconds: 300 }),
|
|
).rejects.toMatchObject({
|
|
code: "signed_download_failed",
|
|
details: {
|
|
upstream: {
|
|
name: "StorageAdapterError",
|
|
// The signer enumerates every missing field before throwing.
|
|
message: expect.stringContaining("Missing signed URL storage configuration"),
|
|
},
|
|
},
|
|
});
|
|
|
|
// The error must name each missing credential to be actionable.
|
|
await expect(
|
|
createStorageSignedDownloadUrl("exports", { key: "a/b.csv", expiresInSeconds: 300 }),
|
|
).rejects.toMatchObject({
|
|
details: {
|
|
upstream: {
|
|
message: expect.stringMatching(/endpoint.*region.*accessKeyId.*secretAccessKey/),
|
|
},
|
|
},
|
|
});
|
|
});
|
|
|
|
test("embeds the session token as X-Amz-Security-Token when configured", async () => {
|
|
setApiRuntimeConfigForTests({
|
|
...baseConfig,
|
|
storage: { ...baseConfig.storage, sessionToken: "temp-session-token-123" },
|
|
});
|
|
|
|
const result = await createStorageSignedDownloadUrl("exports", {
|
|
key: "tenants/t1/file.csv",
|
|
expiresInSeconds: 300,
|
|
});
|
|
|
|
expect(result.url).toContain("X-Amz-Security-Token=temp-session-token-123");
|
|
});
|
|
|
|
test("uses virtual-hosted-style host when forcePathStyle is not enabled", async () => {
|
|
setApiRuntimeConfigForTests({
|
|
...baseConfig,
|
|
storage: { ...baseConfig.storage, forcePathStyle: "false" },
|
|
});
|
|
|
|
const result = await createStorageSignedDownloadUrl("exports", {
|
|
key: "tenants/t1/file.csv",
|
|
expiresInSeconds: 300,
|
|
});
|
|
|
|
// Bucket moves into the host; the path no longer carries the bucket segment.
|
|
expect(result.url).toContain("https://exports.storage.example.com/tenants/t1/file.csv");
|
|
expect(result.url).not.toContain("/exports/tenants/t1/file.csv");
|
|
});
|
|
});
|