feat: centralize mail credential and sandbox policy
Quality / quality (ubuntu-latest) (push) Failing after 9m52s
Quality / quality (windows-latest) (push) Canceled after 0s

This commit is contained in:
2026-08-23 21:51:37 +05:30
parent fe44bc2091
commit f52e1d50e4
4 changed files with 118 additions and 14 deletions
+27 -1
View File
@@ -1,5 +1,11 @@
import { expect, test } from "bun:test";
import { defineMail, defineMailTemplate, sealedMailDriver } from "../src/index.ts";
import {
defineMail,
defineMailTemplate,
defineSealedMailCredentials,
mailSandboxDecision,
sealedMailDriver,
} from "../src/index.ts";
test("mail templates render and sandbox blocks accidental recipients", async () => {
const sent: unknown[] = [];
@@ -17,6 +23,26 @@ test("mail templates render and sandbox blocks accidental recipients", async ()
await expect(mail.send({ to: "real@example.com", subject: "no" })).rejects.toThrow("SANDBOX");
});
test("mail owns fail-closed sandbox decisions and sealed credential setup", async () => {
expect(
mailSandboxDecision({ enabled: 1, allowlist: '["Test@Example.com"]' }, "test@example.com"),
).toEqual({ allowed: true });
expect(
mailSandboxDecision({ enabled: true, allowlist: "not-json" }, "x@example.com").allowed,
).toBe(false);
const previous = process.env.TEST_MAIL_ENCRYPTION_KEY;
process.env.TEST_MAIL_ENCRYPTION_KEY = btoa(String.fromCharCode(...new Uint8Array(32).fill(7)));
try {
const credentials = defineSealedMailCredentials({ env: "TEST_MAIL_ENCRYPTION_KEY" });
const sealed = await credentials.seal("smtp-secret");
expect(sealed).not.toContain("smtp-secret");
expect(await credentials.open(sealed)).toBe("smtp-secret");
} finally {
if (previous === undefined) delete process.env.TEST_MAIL_ENCRYPTION_KEY;
else process.env.TEST_MAIL_ENCRYPTION_KEY = previous;
}
});
test("sealed drivers decrypt lazily once and delegate send/test", async () => {
let opens = 0;
const sent: string[] = [];