Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/db/src/admin-email-domain.server.test.ts
T

265 lines
8.1 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from "vitest";
const { writeAdminSalesAudit } = vi.hoisted(() => ({
writeAdminSalesAudit: vi.fn(),
}));
vi.mock("./admin-sales-core.server.js", async (importOriginal) => {
const original =
await importOriginal<typeof import("./admin-sales-core.server.js")>();
return { ...original, writeAdminSalesAudit };
});
import {
applyAdminSendingDomainCheck,
ingestAdminBccEmail,
saveAdminBccAddress,
saveAdminSendingDomain,
} from "./admin-email-domain.server.js";
beforeEach(() => {
vi.clearAllMocks();
});
function actor() {
return {
adminUserId: "admin-user-1",
email: "raffael@hivegrid.de",
reason: "Admin-E-Mail-Konfiguration aktualisiert",
};
}
describe("admin sending domains", () => {
it("stores exact expected records and resets verification after changes", async () => {
const domain = {
id: "domain-1",
domain: "hivegrid.de",
spfRecordName: "hivegrid.de",
dkimRecordName: "mailjet._domainkey.hivegrid.de",
status: "pending",
};
const tx = {
adminSendingDomain: {
findUnique: vi.fn().mockResolvedValue(null),
upsert: vi.fn().mockResolvedValue(domain),
},
};
const client = { $transaction: vi.fn((callback) => callback(tx)) };
await expect(
saveAdminSendingDomain(client as never, {
adminUserId: "admin-user-1",
domain: "HiveGrid.de.",
spfRecordName: "hivegrid.de",
spfExpectedValue: "v=spf1 include:spf.mailjet.com ~all",
dkimRecordName: "mailjet._domainkey.hivegrid.de",
dkimExpectedValue: "k=rsa; p=key",
actor: actor(),
})
).resolves.toEqual(domain);
expect(tx.adminSendingDomain.upsert).toHaveBeenCalledWith(
expect.objectContaining({
create: expect.objectContaining({
domain: "hivegrid.de",
spfRecordName: "hivegrid.de",
dkimRecordName: "mailjet._domainkey.hivegrid.de",
}),
update: expect.objectContaining({
status: "pending",
lastCheckedAt: null,
}),
})
);
expect(writeAdminSalesAudit).toHaveBeenCalledOnce();
});
it("does not transfer an existing domain to another admin", async () => {
const tx = {
adminSendingDomain: {
findUnique: vi
.fn()
.mockResolvedValue({ adminUserId: "admin-user-2" }),
upsert: vi.fn(),
},
};
const client = { $transaction: vi.fn((callback) => callback(tx)) };
await expect(
saveAdminSendingDomain(client as never, {
adminUserId: "admin-user-1",
domain: "hivegrid.de",
spfRecordName: "hivegrid.de",
spfExpectedValue: "v=spf1 -all",
dkimRecordName: "mail._domainkey.hivegrid.de",
dkimExpectedValue: "k=rsa; p=key",
actor: actor(),
})
).rejects.toThrow("admin_email_domain_owned_by_another_admin");
expect(tx.adminSendingDomain.upsert).not.toHaveBeenCalled();
});
it("persists a worker DNS result with a clear error key", async () => {
const update = vi.fn().mockResolvedValue({ id: "domain-1" });
await applyAdminSendingDomainCheck(
{ adminSendingDomain: { update } } as never,
{
domainId: "domain-1",
checkedAt: new Date("2026-07-24T10:00:00.000Z"),
check: {
status: "degraded",
spfStatus: "error",
dkimStatus: "verified",
},
}
);
expect(update).toHaveBeenCalledWith({
where: { id: "domain-1" },
data: expect.objectContaining({
status: "degraded",
lastErrorMessageKey: "admin.sales.email.domain.dnsCheckFailed",
}),
});
});
});
describe("admin BCC persistence", () => {
it("archives the prior capability before exposing a replacement", async () => {
const address = {
id: "bcc-1",
mailboxId: "mailbox-1",
};
const tx = {
adminMailbox: {
findFirst: vi.fn().mockResolvedValue({ id: "mailbox-1" }),
},
adminBccAddress: {
updateMany: vi.fn().mockResolvedValue({ count: 1 }),
upsert: vi.fn().mockResolvedValue(address),
},
};
const client = { $transaction: vi.fn((callback) => callback(tx)) };
await expect(
saveAdminBccAddress(client as never, {
adminUserId: "admin-user-1",
mailboxId: "mailbox-1",
localPartHash: "a".repeat(64),
displayAddress: "signed@bcc.hivegrid.de",
actor: actor(),
})
).resolves.toEqual(address);
expect(tx.adminBccAddress.updateMany).toHaveBeenCalledWith(
expect.objectContaining({
where: expect.objectContaining({ archivedAt: null }),
data: expect.objectContaining({ archivedAt: expect.any(Date) }),
})
);
expect(writeAdminSalesAudit).toHaveBeenCalledOnce();
});
it("logs an authenticated external send once with activity and audit", async () => {
const tx = {
adminBccAddress: {
findFirst: vi.fn().mockResolvedValue({
id: "bcc-1",
mailboxId: "mailbox-1",
mailbox: {
emailAddress: "sales@hivegrid.de",
status: "active",
archivedAt: null,
},
}),
},
adminEmailMessage: {
findUnique: vi.fn().mockResolvedValue(null),
createManyAndReturn: vi.fn().mockResolvedValue([{ id: "message-1" }]),
findUniqueOrThrow: vi.fn(),
},
adminEmailThread: {
upsert: vi.fn().mockResolvedValue({ id: "thread-1" }),
update: vi.fn().mockResolvedValue({ id: "thread-1" }),
},
adminEmailParticipant: {
create: vi.fn().mockResolvedValue({}),
},
adminActivity: {
create: vi.fn().mockResolvedValue({ id: "activity-1" }),
},
};
const client = { $transaction: vi.fn((callback) => callback(tx)) };
await expect(
ingestAdminBccEmail(client as never, {
adminUserId: "admin-user-1",
localPartHash: "b".repeat(64),
providerMessageId: "provider-message-1",
fromAddress: "sales@hivegrid.de",
toAddresses: ["buyer@example.com"],
subject: "Demo",
bodyText: "Hallo",
occurredAt: new Date("2026-07-24T10:00:00.000Z"),
match: {
status: "matched",
contactId: "contact-1",
leadId: null,
dealId: "deal-1",
reason: "single_open_deal",
candidates: [{ id: "contact-1", kind: "contact" }],
},
})
).resolves.toEqual({ status: "created", messageId: "message-1" });
expect(tx.adminEmailMessage.createManyAndReturn).toHaveBeenCalledWith(
expect.objectContaining({
skipDuplicates: true,
data: expect.objectContaining({
direction: "outbound",
providerLabels: ["BCC_LOGGED"],
matchedDealId: "deal-1",
}),
})
);
expect(tx.adminActivity.create).toHaveBeenCalledOnce();
expect(writeAdminSalesAudit).toHaveBeenCalledOnce();
});
it("rejects a spoofed sender before creating a thread", async () => {
const tx = {
adminBccAddress: {
findFirst: vi.fn().mockResolvedValue({
id: "bcc-1",
mailboxId: "mailbox-1",
mailbox: {
emailAddress: "sales@hivegrid.de",
status: "active",
archivedAt: null,
},
}),
},
adminEmailThread: { upsert: vi.fn() },
};
const client = { $transaction: vi.fn((callback) => callback(tx)) };
await expect(
ingestAdminBccEmail(client as never, {
adminUserId: "admin-user-1",
localPartHash: "c".repeat(64),
providerMessageId: "provider-message-2",
fromAddress: "attacker@example.com",
toAddresses: ["buyer@example.com"],
subject: "Spoof",
bodyText: "Nicht protokollieren",
occurredAt: new Date(),
match: {
status: "unmatched",
contactId: null,
leadId: null,
dealId: null,
reason: "no_candidate",
candidates: [],
},
})
).resolves.toEqual({ status: "ignored", reason: "sender_mismatch" });
expect(tx.adminEmailThread.upsert).not.toHaveBeenCalled();
});
});