195 lines
5.9 KiB
TypeScript
195 lines
5.9 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
import {
|
|
createAdminMailProviderGateway,
|
|
type AdminMailProviderPorts,
|
|
} from "./admin-mail-provider.server.js";
|
|
import {
|
|
GMAIL_SCOPES,
|
|
} from "./google/gmailApi.js";
|
|
import {
|
|
MICROSOFT_MAIL_SCOPES,
|
|
} from "./microsoft/graphMailApi.js";
|
|
|
|
function ports(): AdminMailProviderPorts {
|
|
return {
|
|
gmail: {
|
|
configured: () => true,
|
|
authorizationUrl: (state) => `https://google.test?state=${state}`,
|
|
exchange: vi.fn().mockResolvedValue({
|
|
accessToken: "google-access",
|
|
refreshToken: "google-refresh",
|
|
expiresAt: new Date("2026-07-24T12:00:00.000Z"),
|
|
scopes: GMAIL_SCOPES,
|
|
}),
|
|
profile: vi.fn().mockResolvedValue({
|
|
externalAccountId: "google-account",
|
|
email: "SALES@HIVEGRID.DE",
|
|
name: "Sales",
|
|
}),
|
|
refresh: vi.fn().mockResolvedValue({
|
|
accessToken: "google-next",
|
|
expiresAt: new Date("2026-07-24T13:00:00.000Z"),
|
|
scopes: GMAIL_SCOPES,
|
|
}),
|
|
revoke: vi.fn().mockResolvedValue(undefined),
|
|
send: vi.fn().mockResolvedValue({
|
|
id: "google-message-1",
|
|
threadId: "google-thread-1",
|
|
}),
|
|
},
|
|
microsoft: {
|
|
configured: () => true,
|
|
authorizationUrl: (state) => `https://microsoft.test?state=${state}`,
|
|
exchange: vi.fn().mockResolvedValue({
|
|
accessToken: "microsoft-access",
|
|
refreshToken: "microsoft-refresh",
|
|
expiresAt: new Date("2026-07-24T12:00:00.000Z"),
|
|
scopes: MICROSOFT_MAIL_SCOPES,
|
|
}),
|
|
profile: vi.fn().mockResolvedValue({
|
|
externalAccountId: "microsoft-account",
|
|
email: "sales@hivegrid.de",
|
|
displayName: "Sales",
|
|
}),
|
|
refresh: vi.fn().mockResolvedValue({
|
|
accessToken: "microsoft-next",
|
|
refreshToken: "microsoft-next-refresh",
|
|
expiresAt: new Date("2026-07-24T13:00:00.000Z"),
|
|
scopes: MICROSOFT_MAIL_SCOPES,
|
|
}),
|
|
send: vi.fn().mockResolvedValue(undefined),
|
|
},
|
|
};
|
|
}
|
|
|
|
describe("admin mail provider gateway", () => {
|
|
it.each([
|
|
["google_mail", "google-account"],
|
|
["microsoft_mail", "microsoft-account"],
|
|
] as const)("exchanges %s grants into one normalized contract", async (provider, accountId) => {
|
|
const gateway = createAdminMailProviderGateway(ports());
|
|
await expect(gateway.exchange(provider, "oauth-code")).resolves.toMatchObject({
|
|
account: {
|
|
externalAccountId: accountId,
|
|
emailAddress: "sales@hivegrid.de",
|
|
},
|
|
credentials: {
|
|
expiresAt: "2026-07-24T12:00:00.000Z",
|
|
},
|
|
});
|
|
});
|
|
|
|
it("preserves a Google refresh token when refresh rotates only access", async () => {
|
|
const gateway = createAdminMailProviderGateway(ports());
|
|
await expect(
|
|
gateway.refresh("google_mail", {
|
|
accessToken: "google-access",
|
|
refreshToken: "google-refresh",
|
|
expiresAt: "2026-07-24T12:00:00.000Z",
|
|
scopes: GMAIL_SCOPES,
|
|
})
|
|
).resolves.toMatchObject({
|
|
accessToken: "google-next",
|
|
refreshToken: "google-refresh",
|
|
});
|
|
});
|
|
|
|
it("revokes Google at the provider and shreds Microsoft only locally", async () => {
|
|
const providerPorts = ports();
|
|
const gateway = createAdminMailProviderGateway(providerPorts);
|
|
const googleCredentials = {
|
|
accessToken: "google-access",
|
|
refreshToken: "google-refresh",
|
|
expiresAt: "2026-07-24T12:00:00.000Z",
|
|
scopes: GMAIL_SCOPES,
|
|
};
|
|
|
|
await gateway.revoke("google_mail", googleCredentials);
|
|
expect(providerPorts.gmail.revoke).toHaveBeenCalledWith("google-refresh");
|
|
await expect(
|
|
gateway.revoke("microsoft_mail", {
|
|
...googleCredentials,
|
|
scopes: MICROSOFT_MAIL_SCOPES,
|
|
})
|
|
).resolves.toBeUndefined();
|
|
});
|
|
|
|
it("sends a threaded RFC-2822 message through Gmail", async () => {
|
|
const providerPorts = ports();
|
|
const gateway = createAdminMailProviderGateway(providerPorts);
|
|
|
|
await expect(
|
|
gateway.send(
|
|
"google_mail",
|
|
{
|
|
accessToken: "google-access",
|
|
refreshToken: "google-refresh",
|
|
expiresAt: "2026-07-24T12:00:00.000Z",
|
|
scopes: GMAIL_SCOPES,
|
|
},
|
|
{
|
|
fromAddress: "sales@hivegrid.de",
|
|
fromName: "Sales Team",
|
|
to: [{ address: "lead@example.com", name: "Lead" }],
|
|
subject: "Demo",
|
|
bodyHtml: "<p>Hallo</p>",
|
|
providerThreadId: "google-thread-1",
|
|
inReplyToInternetMessageId: "previous@example.com",
|
|
}
|
|
)
|
|
).resolves.toEqual({
|
|
providerMessageId: "google-message-1",
|
|
providerThreadId: "google-thread-1",
|
|
});
|
|
|
|
expect(providerPorts.gmail.send).toHaveBeenCalledWith(
|
|
"google-access",
|
|
expect.any(String),
|
|
"google-thread-1"
|
|
);
|
|
const raw = Buffer.from(
|
|
vi.mocked(providerPorts.gmail.send).mock.calls[0]![1],
|
|
"base64url"
|
|
).toString("utf8");
|
|
expect(raw).toContain("Subject: Demo");
|
|
expect(raw).toContain("In-Reply-To: <previous@example.com>");
|
|
});
|
|
|
|
it("sends the neutral message contract through Microsoft", async () => {
|
|
const providerPorts = ports();
|
|
const gateway = createAdminMailProviderGateway(providerPorts);
|
|
|
|
await expect(
|
|
gateway.send(
|
|
"microsoft_mail",
|
|
{
|
|
accessToken: "microsoft-access",
|
|
refreshToken: "microsoft-refresh",
|
|
expiresAt: "2026-07-24T12:00:00.000Z",
|
|
scopes: MICROSOFT_MAIL_SCOPES,
|
|
},
|
|
{
|
|
fromAddress: "sales@hivegrid.de",
|
|
to: [{ address: "lead@example.com" }],
|
|
subject: "Demo",
|
|
bodyHtml: "<p>Hallo</p>",
|
|
}
|
|
)
|
|
).resolves.toEqual({
|
|
providerMessageId: null,
|
|
providerThreadId: null,
|
|
});
|
|
|
|
expect(providerPorts.microsoft.send).toHaveBeenCalledWith(
|
|
"microsoft-access",
|
|
{
|
|
subject: "Demo",
|
|
bodyHtml: "<p>Hallo</p>",
|
|
to: [{ address: "lead@example.com" }],
|
|
cc: undefined,
|
|
bcc: undefined,
|
|
}
|
|
);
|
|
});
|
|
});
|