Files
Zumri-Backend/tests/unit/storage.service.test.js
T
Sathira Sri Sathara b6b345f245 feat: Implement Phase 2 cross-cutting services with email and notification enhancements
- Refactor email verification and password reset utilities to use new email service.
- Introduce email delivery queue and notification delivery model for better tracking.
- Enhance file validation and storage services for improved security and ownership management.
- Add cron job for cleaning inactive notifications with retention policy.
- Update document worker to handle document generation and storage more efficiently.
- Implement logging improvements in activity and log workers.
- Create comprehensive documentation for new API endpoints and services.
- Add unit tests for file validation and notification policies to ensure robustness.
2026-09-03 14:22:34 +05:30

22 lines
1.5 KiB
JavaScript

describe("Phase 2 file validation", () => {
beforeEach(() => jest.resetModules());
test("detects a valid PNG and creates checksum/safe name", () => {
const { validateUpload } = require("../../app/services/storage/file-validation.service");
const buffer = Buffer.concat([Buffer.from([0x89,0x50,0x4e,0x47,0x0d,0x0a,0x1a,0x0a]), Buffer.from("payload")]);
const result = validateUpload({ buffer, mimetype: "image/png", originalname: "../unsafe name.svg" });
expect(result.mimeType).toBe("image/png"); expect(result.safeName).toBe("unsafe_name.png"); expect(result.checksum).toMatch(/^[a-f0-9]{64}$/);
});
test("rejects claimed MIME that disagrees with magic bytes", () => {
const { validateUpload } = require("../../app/services/storage/file-validation.service");
expect(() => validateUpload({ buffer: Buffer.from("not an image"), mimetype: "image/png", originalname: "x.png" })).toThrow("does not match");
});
test("rejects empty files", () => {
const { validateUpload } = require("../../app/services/storage/file-validation.service");
expect(() => validateUpload({ buffer: Buffer.alloc(0), mimetype: "image/png", originalname: "x.png" })).toThrow("Empty");
});
test("builds controlled owner/date keys without original filenames", () => {
const { createObjectKey } = require("../../app/services/storage/storage.service");
expect(createObjectKey({ ownerId: "usr_1", mimeType: "application/pdf", purpose: "document", now: new Date("2026-09-03T00:00:00Z"), id: "fixed" })).toBe("documents/usr_1/2026/09/fixed.pdf");
});
});