104 lines
3.3 KiB
TypeScript
104 lines
3.3 KiB
TypeScript
import { Hono } from "hono";
|
|
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import { signAdminEmailProviderEvent } from "@hivegrid/integrations/admin-email-provider-events";
|
|
import type { ApiEnv } from "../api-types.js";
|
|
import { postAdminEmailProviderEvent } from "./admin-email-routes.js";
|
|
|
|
const secret = "admin-email-provider-events".padEnd(40, "x");
|
|
|
|
function buildApp(record = vi.fn(async () => ({ status: "recorded" as const, eventId: "event-1", messageId: "message-1" }))) {
|
|
const app = new Hono<ApiEnv>();
|
|
app.post("/v1/admin-sales/email/provider-events", (c) =>
|
|
postAdminEmailProviderEvent(c, { webhookSecret: secret, record })
|
|
);
|
|
return { app, record };
|
|
}
|
|
|
|
describe("admin email provider event API", () => {
|
|
it("authenticates a normalized event and binds it to the admin owner", async () => {
|
|
const now = new Date();
|
|
const body = JSON.stringify({
|
|
adminUserId: "admin-1",
|
|
scheduledEmailId: "scheduled-1",
|
|
type: "bounced",
|
|
eventId: "provider-event-1",
|
|
occurredAt: now.toISOString(),
|
|
recipient: "buyer@example.com",
|
|
});
|
|
const { app, record } = buildApp();
|
|
const response = await app.request("/v1/admin-sales/email/provider-events", {
|
|
method: "POST",
|
|
body,
|
|
headers: {
|
|
"content-type": "application/json",
|
|
"x-hivegrid-timestamp": String(now.getTime()),
|
|
"x-hivegrid-signature": signAdminEmailProviderEvent({
|
|
body,
|
|
timestamp: String(now.getTime()),
|
|
secret,
|
|
}),
|
|
},
|
|
});
|
|
|
|
expect(response.status).toBe(201);
|
|
await expect(response.json()).resolves.toEqual({
|
|
accepted: true,
|
|
status: "recorded",
|
|
});
|
|
expect(record).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
adminUserId: "admin-1",
|
|
scheduledEmailId: "scheduled-1",
|
|
type: "bounced",
|
|
eventKey: "provider-event-1",
|
|
})
|
|
);
|
|
});
|
|
|
|
it("rejects unsigned input before any database access", async () => {
|
|
const { app, record } = buildApp();
|
|
const response = await app.request("/v1/admin-sales/email/provider-events", {
|
|
method: "POST",
|
|
body: "{}",
|
|
headers: {
|
|
"content-type": "application/json",
|
|
"x-hivegrid-timestamp": String(Date.now()),
|
|
},
|
|
});
|
|
expect(response.status).toBe(401);
|
|
expect(record).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("rejects an authenticated but invalid provider payload as a bad request", async () => {
|
|
const now = new Date();
|
|
const body = JSON.stringify({
|
|
adminUserId: "admin-1",
|
|
scheduledEmailId: "scheduled-1",
|
|
type: "opened",
|
|
eventId: "provider-event-1",
|
|
occurredAt: now.toISOString(),
|
|
});
|
|
const { app, record } = buildApp();
|
|
const response = await app.request("/v1/admin-sales/email/provider-events", {
|
|
method: "POST",
|
|
body,
|
|
headers: {
|
|
"content-type": "application/json",
|
|
"x-hivegrid-timestamp": String(now.getTime()),
|
|
"x-hivegrid-signature": signAdminEmailProviderEvent({
|
|
body,
|
|
timestamp: String(now.getTime()),
|
|
secret,
|
|
}),
|
|
},
|
|
});
|
|
|
|
expect(response.status).toBe(400);
|
|
await expect(response.json()).resolves.toMatchObject({
|
|
error: { code: "invalid_request" },
|
|
});
|
|
expect(record).not.toHaveBeenCalled();
|
|
});
|
|
});
|