131 lines
4.9 KiB
TypeScript
131 lines
4.9 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi, type Mock } from "vitest";
|
|
|
|
// Mock the AWS SDK so the *real* S3Client wrapper (and the default-client /
|
|
// default-quarantine-factory construction paths) can be exercised without any
|
|
// network round-trip. Each mocked command class records its constructor input.
|
|
vi.mock("@aws-sdk/client-s3", () => {
|
|
const send = vi.fn(async () => ({
|
|
UploadId: "aws-upload-1",
|
|
CopyPartResult: { ETag: "aws-etag" },
|
|
ok: true,
|
|
}));
|
|
class AwsS3Client {
|
|
config: unknown;
|
|
send = send;
|
|
constructor(config: unknown) {
|
|
this.config = config;
|
|
}
|
|
}
|
|
class Cmd {
|
|
input: unknown;
|
|
constructor(input: unknown) {
|
|
this.input = input;
|
|
}
|
|
}
|
|
return {
|
|
__send: send,
|
|
S3Client: AwsS3Client,
|
|
PutObjectCommand: class extends Cmd {},
|
|
DeleteObjectCommand: class extends Cmd {},
|
|
CreateMultipartUploadCommand: class extends Cmd {},
|
|
UploadPartCopyCommand: class extends Cmd {},
|
|
CompleteMultipartUploadCommand: class extends Cmd {},
|
|
AbortMultipartUploadCommand: class extends Cmd {},
|
|
CopyObjectCommand: class extends Cmd {},
|
|
};
|
|
});
|
|
|
|
import * as awsS3 from "@aws-sdk/client-s3";
|
|
import {
|
|
AbortMultipartUploadCommand,
|
|
CompleteMultipartUploadCommand,
|
|
CreateMultipartUploadCommand,
|
|
DeleteObjectCommand,
|
|
PutObjectCommand,
|
|
S3Client,
|
|
UploadPartCopyCommand,
|
|
createS3StorageAdapter,
|
|
type S3StorageAdapterConfig,
|
|
} from "./index.js";
|
|
import { createS3StorageQuarantineAdapter } from "./scan/index.js";
|
|
|
|
const awsSend = (awsS3 as unknown as { __send: Mock }).__send;
|
|
|
|
const clientConfig = {
|
|
endpoint: "https://s3.example.test",
|
|
region: "us-east-1",
|
|
credentials: { accessKeyId: "ak", secretAccessKey: "sk" },
|
|
};
|
|
|
|
const adapterConfig: S3StorageAdapterConfig = {
|
|
endpoint: "https://s3.example.test",
|
|
region: "us-east-1",
|
|
bucket: "uploads",
|
|
accessKeyId: "ak",
|
|
secretAccessKey: "sk",
|
|
};
|
|
|
|
describe("S3Client.send command dispatch", () => {
|
|
beforeEach(() => {
|
|
awsSend.mockClear();
|
|
});
|
|
afterEach(() => {
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
it("maps a PutObjectCommand (normalizing ArrayBuffer bodies) to the AWS command", async () => {
|
|
const client = new S3Client(clientConfig);
|
|
await client.send(new PutObjectCommand({ Bucket: "u", Key: "k", Body: new ArrayBuffer(4) }));
|
|
|
|
expect(awsSend).toHaveBeenCalledOnce();
|
|
const sent = awsSend.mock.calls[0]?.[0] as { input: { Body: unknown } };
|
|
expect(sent).toBeInstanceOf(awsS3.PutObjectCommand);
|
|
// ArrayBuffer is normalized to a Uint8Array before reaching the AWS SDK.
|
|
expect(sent.input.Body).toBeInstanceOf(Uint8Array);
|
|
});
|
|
|
|
it("maps a DeleteObjectCommand to the AWS delete command (fallback branch)", async () => {
|
|
const client = new S3Client(clientConfig);
|
|
await client.send(new DeleteObjectCommand({ Bucket: "u", Key: "k" }));
|
|
expect(awsSend.mock.calls[0]?.[0]).toBeInstanceOf(awsS3.DeleteObjectCommand);
|
|
});
|
|
|
|
it("maps each multipart command to its AWS equivalent", async () => {
|
|
const client = new S3Client(clientConfig);
|
|
await client.send(new CreateMultipartUploadCommand({ Bucket: "u", Key: "k" }));
|
|
await client.send(
|
|
new UploadPartCopyCommand({ Bucket: "u", Key: "k", UploadId: "u1", PartNumber: 1, CopySource: "u/src" }),
|
|
);
|
|
await client.send(
|
|
new CompleteMultipartUploadCommand({ Bucket: "u", Key: "k", UploadId: "u1", MultipartUpload: { Parts: [] } }),
|
|
);
|
|
await client.send(new AbortMultipartUploadCommand({ Bucket: "u", Key: "k", UploadId: "u1" }));
|
|
|
|
expect(awsSend.mock.calls[0]?.[0]).toBeInstanceOf(awsS3.CreateMultipartUploadCommand);
|
|
expect(awsSend.mock.calls[1]?.[0]).toBeInstanceOf(awsS3.UploadPartCopyCommand);
|
|
expect(awsSend.mock.calls[2]?.[0]).toBeInstanceOf(awsS3.CompleteMultipartUploadCommand);
|
|
expect(awsSend.mock.calls[3]?.[0]).toBeInstanceOf(awsS3.AbortMultipartUploadCommand);
|
|
});
|
|
|
|
it("createS3StorageAdapter builds a default S3Client when none is injected", async () => {
|
|
const adapter = createS3StorageAdapter(adapterConfig);
|
|
await expect(adapter.putObject({ key: "k.txt", body: "x" })).resolves.toMatchObject({
|
|
bucket: "uploads",
|
|
key: "k.txt",
|
|
});
|
|
expect(awsSend).toHaveBeenCalledOnce();
|
|
expect(awsSend.mock.calls[0]?.[0]).toBeInstanceOf(awsS3.PutObjectCommand);
|
|
});
|
|
|
|
it("createS3StorageQuarantineAdapter builds a default AWS client when no factory is given", async () => {
|
|
const adapter = createS3StorageQuarantineAdapter({ region: "us-east-1", accessKeyId: "ak", secretAccessKey: "sk" });
|
|
const result = await adapter.quarantineObject({ bucket: "uploads", key: "bad.exe", signature: "Win.Test" });
|
|
|
|
expect(result).toMatchObject({ bucket: "uploads", key: "quarantine/bad.exe", action: "moved" });
|
|
// Copy into quarantine + delete original.
|
|
expect(awsSend).toHaveBeenCalledTimes(2);
|
|
expect(awsSend.mock.calls[0]?.[0]).toBeInstanceOf(awsS3.CopyObjectCommand);
|
|
expect(awsSend.mock.calls[1]?.[0]).toBeInstanceOf(awsS3.DeleteObjectCommand);
|
|
});
|
|
});
|