52 lines
3.7 KiB
TypeScript
52 lines
3.7 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
||
import { GmailSender, buildMime } from "./gmail.js";
|
||
import { longDate, newRef, renderStartEmail } from "./template.js";
|
||
|
||
const P = { ref: "KHB-261116-ABCD", contractor: "Tampa General", address: "1807 Pass a Grille Way, St. Pete Beach, FL 33706", date: "2026-11-16", managerName: "Walt Walker", managerPhone: "313-400-0090", managerEmail: "walter@khbuild.pro" };
|
||
|
||
describe("письмо подрядчику", () => {
|
||
it("шаблон Артура: дата, адрес, подпись, код в теме и мелким шрифтом в конце", () => {
|
||
const m = renderStartEmail(P);
|
||
expect(m.subject).toBe("[KHB-261116-ABCD] Start of work at 1807 Pass a Grille Way, St. Pete Beach, FL 33706 on Monday, November 16, 2026");
|
||
expect(m.text).toContain("Dear Tampa General Team,");
|
||
expect(m.text).toContain("on Monday, November 16, 2026.");
|
||
expect(m.text).toContain("Send invoices to invoice@khbuild.pro.");
|
||
expect(m.text).toContain("For any questions, call me at 313-400-0090 or email walter@khbuild.pro.");
|
||
expect(m.text.trim().endsWith("Ref: KHB-261116-ABCD")).toBe(true);
|
||
expect(m.html).toContain("font-size:10px");
|
||
expect(renderStartEmail({ ...P, work: "foundation pour" }).text).toContain("Scope: foundation pour.");
|
||
});
|
||
it("нет данных менеджера - письмо не строится", () => {
|
||
expect(() => renderStartEmail({ ...P, managerPhone: "" })).toThrow();
|
||
expect(() => longDate("16.11.2026")).toThrow();
|
||
});
|
||
it("код письма: KHB-ГГММДД-XXXX", () => {
|
||
expect(newRef(new Date("2026-11-10T12:00:00Z"), () => "7F3A")).toBe("KHB-261110-7F3A");
|
||
});
|
||
it("MIME: заголовки, Cc, кодирование не-ASCII имени, защита от инъекции заголовков", () => {
|
||
const base = { from: { email: "walter@khbuild.pro", name: "Walter Wálker" }, to: [{ email: "a@x.com", name: "A" }], cc: [{ email: "agents@khbuild.pro" }], subject: "s", text: "t", html: "<p>t</p>" };
|
||
const mime = buildMime(base, "id1@khbuild.pro", new Date("2026-11-10T12:00:00Z"));
|
||
expect(mime).toContain("Cc: agents@khbuild.pro");
|
||
expect(mime).toContain("Message-ID: <id1@khbuild.pro>");
|
||
expect(mime).toMatch(/From: =\?UTF-8\?B\?.+\?= <walter@khbuild.pro>/);
|
||
expect(() => buildMime({ ...base, subject: "x\r\nBcc: evil@x.com" }, "i", new Date())).toThrow();
|
||
});
|
||
it("отправка: токен от имени отправителя (sub), запрос send с raw", async () => {
|
||
const calls: { url: string; body: string }[] = [];
|
||
const fetchFn = (async (url: string, init?: { body?: unknown }) => {
|
||
const body = String(init?.body ?? "");
|
||
calls.push({ url: String(url), body });
|
||
if (String(url).includes("oauth2")) return new Response(JSON.stringify({ access_token: "tok", expires_in: 3600 }));
|
||
return new Response(JSON.stringify({ id: "g1", threadId: "t1" }));
|
||
}) as unknown as typeof fetch;
|
||
const { generateKeyPairSync } = await import("node:crypto");
|
||
const pem = generateKeyPairSync("rsa", { modulusLength: 2048 }).privateKey.export({ type: "pkcs8", format: "pem" }) as string;
|
||
const s = new GmailSender({ key: { client_email: "sa@p.iam", private_key: pem }, fetchFn });
|
||
const r = await s.send({ from: { email: "walter@khbuild.pro" }, to: [{ email: "a@x.com" }], subject: "s", text: "t", html: "h" });
|
||
expect(r).toMatchObject({ gmailId: "g1", threadId: "t1" });
|
||
const jwt = new URLSearchParams(calls[0]!.body).get("assertion")!.split(".")[1]!;
|
||
expect(JSON.parse(Buffer.from(jwt, "base64url").toString()).sub).toBe("walter@khbuild.pro");
|
||
expect(calls[1]!.url).toContain("/messages/send");
|
||
});
|
||
});
|