72 lines
3.4 KiB
TypeScript
72 lines
3.4 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import { createMailjetDnsProvisioner } from "./mailjet-dns.js";
|
|
import { MailProviderError } from "./types.js";
|
|
|
|
function jsonResponse(body: unknown, status = 200): Response {
|
|
return new Response(JSON.stringify(body), { status, headers: { "Content-Type": "application/json" } });
|
|
}
|
|
|
|
const DNS_PAYLOAD = {
|
|
Count: 1,
|
|
Data: [
|
|
{
|
|
ID: 12345,
|
|
DKIMRecordName: "mailjet._domainkey.acme.test",
|
|
DKIMRecordValue: "v=DKIM1; k=rsa; p=MIIB...",
|
|
SPFRecordValue: "v=spf1 include:spf.mailjet.com ~all",
|
|
DKIMStatus: "OK",
|
|
SPFStatus: "Error",
|
|
},
|
|
],
|
|
};
|
|
|
|
describe("createMailjetDnsProvisioner", () => {
|
|
it("sends Basic auth and reads back normalized DKIM/SPF records", async () => {
|
|
const fetchImpl = vi.fn<(url: string, init?: RequestInit) => Promise<Response>>(async () => jsonResponse(DNS_PAYLOAD));
|
|
const provisioner = createMailjetDnsProvisioner({ apiKey: "key", secretKey: "secret", fetch: fetchImpl as unknown as typeof fetch });
|
|
|
|
const records = await provisioner.getRecords("acme.test");
|
|
|
|
expect(records).toEqual({
|
|
dnsId: "12345",
|
|
dkimSelector: "mailjet._domainkey.acme.test",
|
|
dkimRecord: "v=DKIM1; k=rsa; p=MIIB...",
|
|
spfRecord: "v=spf1 include:spf.mailjet.com ~all",
|
|
dkimStatus: "ok",
|
|
spfStatus: "error",
|
|
});
|
|
const [url, init] = fetchImpl.mock.calls[0]!;
|
|
expect(url).toBe("https://api.mailjet.com/v3/REST/dns/acme.test");
|
|
expect(init?.headers).toMatchObject({ Authorization: `Basic ${Buffer.from("key:secret").toString("base64")}` });
|
|
});
|
|
|
|
it("registers a wildcard sender for the domain and tolerates a 400 (already exists)", async () => {
|
|
const fetchImpl = vi.fn<(url: string, init?: RequestInit) => Promise<Response>>(async () => jsonResponse({ ErrorMessage: "already exists" }, 400));
|
|
const provisioner = createMailjetDnsProvisioner({ apiKey: "k", secretKey: "s", fetch: fetchImpl as unknown as typeof fetch });
|
|
|
|
await expect(provisioner.registerDomain("acme.test")).resolves.toEqual({ ok: true });
|
|
const [url, init] = fetchImpl.mock.calls[0]!;
|
|
expect(url).toBe("https://api.mailjet.com/v3/REST/sender");
|
|
expect(JSON.parse(init?.body as string)).toEqual({ Email: "*@acme.test", IsDefaultSender: false });
|
|
});
|
|
|
|
it("maps a check response to per-record status", async () => {
|
|
const fetchImpl = vi.fn(async () => jsonResponse(DNS_PAYLOAD));
|
|
const provisioner = createMailjetDnsProvisioner({ apiKey: "k", secretKey: "s", fetch: fetchImpl as unknown as typeof fetch });
|
|
await expect(provisioner.check("acme.test")).resolves.toEqual({ dkimStatus: "ok", spfStatus: "error" });
|
|
});
|
|
|
|
it("throws a MailProviderError on a non-400 failure", async () => {
|
|
const fetchImpl = vi.fn(async () => jsonResponse({ error: "server" }, 500));
|
|
const provisioner = createMailjetDnsProvisioner({ apiKey: "k", secretKey: "s", fetch: fetchImpl as unknown as typeof fetch });
|
|
await expect(provisioner.getRecords("acme.test")).rejects.toBeInstanceOf(MailProviderError);
|
|
});
|
|
|
|
it("returns pending defaults when Mailjet returns no data rows", async () => {
|
|
const fetchImpl = vi.fn(async () => jsonResponse({ Count: 0, Data: [] }));
|
|
const provisioner = createMailjetDnsProvisioner({ apiKey: "k", secretKey: "s", fetch: fetchImpl as unknown as typeof fetch });
|
|
await expect(provisioner.getRecords("acme.test")).resolves.toMatchObject({ dkimStatus: "pending", spfStatus: "pending", dkimRecord: null });
|
|
});
|
|
});
|