webhook-db.test.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. import assert from 'node:assert/strict';
  2. import { mkdtempSync } from 'node:fs';
  3. import { tmpdir } from 'node:os';
  4. import path from 'node:path';
  5. import { DatabaseSync } from 'node:sqlite';
  6. import { test } from 'node:test';
  7. import {
  8. claimWebhookDeliveries,
  9. completeWebhookDeliveryFailure,
  10. completeWebhookDeliverySuccess,
  11. createDomain,
  12. createInboundMailbox,
  13. createInboundMessage,
  14. createUser,
  15. createWebhook,
  16. deleteWebhook,
  17. enqueueWebhookDeliveries,
  18. enqueueInboundWebhookDeliveries,
  19. enqueueWebhookTestDelivery,
  20. getWebhook,
  21. initDatabase,
  22. listWebhookDeliveries,
  23. listWebhooks,
  24. logSendEvent,
  25. replayWebhookDelivery,
  26. rotateWebhookSecret,
  27. updateSendEventDelivery,
  28. updateWebhook
  29. } from '../src/db.js';
  30. import { MAX_WEBHOOK_ATTEMPTS } from '../src/webhook-model.js';
  31. test('isolates webhooks by user and supports domain scope filter', () => {
  32. initDatabase(tempDataDir(), 'test-secret');
  33. const alice = createUser({ username: 'alice', email: 'alice@example.com', password: 'password123' });
  34. const bob = createUser({ username: 'bob', email: 'bob@example.com', password: 'password123' });
  35. const aliceDomain = createDomain(alice.id, domainFixture('alice.example'));
  36. createWebhook(alice.id, {
  37. name: 'Alice account',
  38. url: 'https://hooks.alice.example/account',
  39. events: ['sent']
  40. });
  41. createWebhook(alice.id, {
  42. name: 'Alice domain',
  43. url: 'https://hooks.alice.example/domain',
  44. events: ['failed'],
  45. domainId: aliceDomain.id
  46. });
  47. createWebhook(bob.id, {
  48. name: 'Bob account',
  49. url: 'https://hooks.bob.example/account',
  50. events: ['sent', 'bounced']
  51. });
  52. assert.equal(listWebhooks(alice.id).length, 2);
  53. assert.equal(listWebhooks(bob.id).length, 1);
  54. assert.equal(listWebhooks(alice.id, { domainId: null }).length, 1);
  55. assert.equal(listWebhooks(alice.id, { domainId: aliceDomain.id }).length, 1);
  56. assert.equal(listWebhooks(alice.id, { domainId: aliceDomain.id })[0].name, 'Alice domain');
  57. assert.equal(listWebhooks(bob.id, { domainId: aliceDomain.id }).length, 0);
  58. });
  59. test('migrates legacy webhook deliveries without losing send-event records', () => {
  60. const dataDir = tempDataDir();
  61. const database = new DatabaseSync(path.join(dataDir, 'mailhub.sqlite'));
  62. database.exec(`
  63. CREATE TABLE users (
  64. id INTEGER PRIMARY KEY AUTOINCREMENT,
  65. username TEXT NOT NULL UNIQUE,
  66. email TEXT NOT NULL UNIQUE,
  67. password_hash TEXT NOT NULL,
  68. role TEXT NOT NULL DEFAULT 'user',
  69. status TEXT NOT NULL DEFAULT 'active',
  70. created_at TEXT NOT NULL,
  71. updated_at TEXT NOT NULL
  72. );
  73. CREATE TABLE webhooks (
  74. id INTEGER PRIMARY KEY AUTOINCREMENT,
  75. user_id INTEGER NOT NULL,
  76. domain_id INTEGER,
  77. name TEXT NOT NULL,
  78. url TEXT NOT NULL,
  79. secret_ciphertext TEXT NOT NULL,
  80. secret_prefix TEXT NOT NULL,
  81. events_json TEXT NOT NULL,
  82. enabled TEXT NOT NULL DEFAULT 'true',
  83. created_at TEXT NOT NULL,
  84. updated_at TEXT NOT NULL
  85. );
  86. CREATE TABLE webhook_deliveries (
  87. id INTEGER PRIMARY KEY AUTOINCREMENT,
  88. webhook_id INTEGER NOT NULL,
  89. user_id INTEGER NOT NULL,
  90. send_event_id INTEGER NOT NULL,
  91. event_type TEXT NOT NULL,
  92. payload_json TEXT NOT NULL,
  93. status TEXT NOT NULL,
  94. attempt_count INTEGER NOT NULL DEFAULT 0,
  95. next_attempt_at TEXT NOT NULL,
  96. last_attempt_at TEXT,
  97. response_status INTEGER,
  98. response_body_preview TEXT NOT NULL DEFAULT '',
  99. error TEXT NOT NULL DEFAULT '',
  100. created_at TEXT NOT NULL,
  101. FOREIGN KEY(webhook_id) REFERENCES webhooks(id) ON DELETE CASCADE,
  102. UNIQUE(webhook_id, send_event_id, event_type)
  103. );
  104. INSERT INTO users (id, username, email, password_hash, role, status, created_at, updated_at)
  105. VALUES (1, 'legacy', 'legacy@example.com', 'hash', 'user', 'active', '2026-07-14T00:00:00.000Z', '2026-07-14T00:00:00.000Z');
  106. INSERT INTO webhooks (id, user_id, name, url, secret_ciphertext, secret_prefix, events_json, enabled, created_at, updated_at)
  107. VALUES (1, 1, 'Legacy', 'https://hooks.example.com/legacy', 'secret', 'whsec_12', '["sent"]', 'true', '2026-07-14T00:00:00.000Z', '2026-07-14T00:00:00.000Z');
  108. INSERT INTO webhook_deliveries (id, webhook_id, user_id, send_event_id, event_type, payload_json, status, attempt_count, next_attempt_at, response_body_preview, error, created_at)
  109. VALUES (1, 1, 1, 42, 'sent', '{}', 'success', 1, '2026-07-14T00:00:00.000Z', '', '', '2026-07-14T00:00:00.000Z');
  110. `);
  111. database.close();
  112. initDatabase(dataDir, 'test-secret');
  113. const [delivery] = listWebhookDeliveries(1);
  114. assert.equal(delivery.sendEventId, 42);
  115. assert.equal(delivery.inboundMessageId, null);
  116. assert.equal(listWebhooks(1)[0].mailboxId, null);
  117. });
  118. test('create returns secret once; list and get omit secret', () => {
  119. initDatabase(tempDataDir(), 'test-secret');
  120. const alice = createUser({ username: 'alice', email: 'alice@example.com', password: 'password123' });
  121. const created = createWebhook(alice.id, {
  122. name: 'Primary',
  123. url: 'https://hooks.example.com/mail',
  124. events: ['sent', 'failed']
  125. });
  126. assert.ok(created.secret);
  127. assert.match(created.secret, /^whsec_/);
  128. assert.equal(created.secretPrefix, created.secret.slice(0, 8));
  129. assert.deepEqual(created.events, ['sent', 'failed']);
  130. assert.equal(created.enabled, true);
  131. assert.equal('secret' in listWebhooks(alice.id)[0], false);
  132. assert.equal('secret' in getWebhook(created.id, alice.id), false);
  133. assert.equal(listWebhooks(alice.id)[0].secretPrefix, created.secretPrefix);
  134. const rotated = rotateWebhookSecret(alice.id, created.id);
  135. assert.ok(rotated.secret);
  136. assert.notEqual(rotated.secret, created.secret);
  137. assert.equal(rotated.secretPrefix, rotated.secret.slice(0, 8));
  138. assert.equal('secret' in getWebhook(created.id, alice.id), false);
  139. const updated = updateWebhook(alice.id, created.id, {
  140. name: 'Renamed',
  141. enabled: false,
  142. events: ['bounced']
  143. });
  144. assert.equal(updated.name, 'Renamed');
  145. assert.equal(updated.enabled, false);
  146. assert.deepEqual(updated.events, ['bounced']);
  147. assert.equal(deleteWebhook(alice.id, created.id), true);
  148. assert.equal(getWebhook(created.id, alice.id), null);
  149. });
  150. test('enqueueWebhookDeliveries is idempotent per webhook+event+send_event', () => {
  151. initDatabase(tempDataDir(), 'test-secret');
  152. const alice = createUser({ username: 'alice', email: 'alice@example.com', password: 'password123' });
  153. const domain = createDomain(alice.id, domainFixture('alice.example'));
  154. const webhook = createWebhook(alice.id, {
  155. name: 'Account',
  156. url: 'https://hooks.example.com/a',
  157. events: ['sent', 'failed']
  158. });
  159. const sendEvent = {
  160. id: 42,
  161. userId: alice.id,
  162. domainId: domain.id,
  163. status: 'sent',
  164. sender: 'noreply@alice.example',
  165. recipients: ['user@example.com'],
  166. subject: 'Hello',
  167. detail: 'ok',
  168. queueId: 'QUEUE42',
  169. deliveredAt: '2026-07-09T12:00:01.000Z'
  170. };
  171. const first = enqueueWebhookDeliveries(sendEvent);
  172. const second = enqueueWebhookDeliveries(sendEvent);
  173. assert.equal(first.length, 1);
  174. assert.equal(second.length, 0);
  175. const deliveries = listWebhookDeliveries(alice.id);
  176. assert.equal(deliveries.length, 1);
  177. assert.equal(deliveries[0].webhookId, webhook.id);
  178. assert.equal(deliveries[0].sendEventId, 42);
  179. assert.equal(deliveries[0].eventType, 'sent');
  180. assert.equal(deliveries[0].status, 'pending');
  181. assert.equal(deliveries[0].attemptCount, 0);
  182. const payload = JSON.parse(deliveries[0].payloadJson);
  183. assert.equal(payload.id, `whd_${deliveries[0].id}`);
  184. assert.equal(payload.type, 'email.sent');
  185. assert.equal(payload.data.message_id, 'mh-42');
  186. assert.equal(payload.data.domain, 'alice.example');
  187. assert.equal(payload.data.queue_id, 'QUEUE42');
  188. });
  189. test('mailbox webhooks only enqueue idempotent receipt callbacks for their stored mail', () => {
  190. initDatabase(tempDataDir(), 'test-secret');
  191. const alice = createUser({ username: 'inbound-alice', email: 'inbound-alice@example.com', password: 'password123' });
  192. const bob = createUser({ username: 'inbound-bob', email: 'inbound-bob@example.com', password: 'password123' });
  193. const domain = createDomain(alice.id, domainFixture('inbound-hook.example'));
  194. const mailbox = createInboundMailbox(alice.id, { address: 'support@inbound-hook.example' });
  195. const otherMailbox = createInboundMailbox(alice.id, { address: 'sales@inbound-hook.example' });
  196. const webhook = createWebhook(alice.id, {
  197. name: 'Support receipt',
  198. url: 'https://hooks.example.com/inbound',
  199. events: ['received'],
  200. mailboxId: mailbox.id
  201. });
  202. const sendWebhook = createWebhook(alice.id, {
  203. name: 'Sending only',
  204. url: 'https://hooks.example.com/send',
  205. events: ['sent']
  206. });
  207. assert.equal(webhook.domainId, null);
  208. assert.equal(webhook.mailboxId, mailbox.id);
  209. assert.equal(listWebhooks(alice.id, { mailboxId: mailbox.id }).length, 1);
  210. assert.equal(listWebhooks(alice.id, { domainId: null }).length, 1);
  211. assert.throws(() => createWebhook(alice.id, {
  212. name: 'Invalid account receipt',
  213. url: 'https://hooks.example.com/invalid-account',
  214. events: ['received']
  215. }), /received/);
  216. assert.throws(() => createWebhook(alice.id, {
  217. name: 'Invalid mailbox send',
  218. url: 'https://hooks.example.com/invalid-mailbox',
  219. events: ['sent'],
  220. mailboxId: mailbox.id
  221. }), /邮箱 Webhook/);
  222. assert.throws(() => createWebhook(bob.id, {
  223. name: 'Other user mailbox',
  224. url: 'https://hooks.example.com/other-user',
  225. events: ['received'],
  226. mailboxId: mailbox.id
  227. }), /收信邮箱/);
  228. const inboundMessage = createInboundMessage(mailbox, {
  229. sender: 'sender@example.net',
  230. recipients: ['support@inbound-hook.example'],
  231. subject: 'Receipt callback',
  232. messageId: '<inbound-message@example.net>',
  233. textBody: 'Plain text',
  234. htmlBody: '<p>HTML</p>'
  235. });
  236. const unrelatedMessage = createInboundMessage(otherMailbox, {
  237. sender: 'sender@example.net',
  238. recipients: ['sales@inbound-hook.example'],
  239. subject: 'Other mailbox'
  240. });
  241. assert.equal(enqueueInboundWebhookDeliveries(inboundMessage).length, 1);
  242. assert.equal(enqueueInboundWebhookDeliveries(inboundMessage).length, 0);
  243. assert.equal(enqueueInboundWebhookDeliveries(unrelatedMessage).length, 0);
  244. enqueueWebhookDeliveries({
  245. id: 99,
  246. userId: alice.id,
  247. domainId: domain.id,
  248. status: 'sent',
  249. sender: 'noreply@inbound-hook.example',
  250. recipients: ['reader@example.net'],
  251. subject: 'Sending path'
  252. });
  253. const deliveries = listWebhookDeliveries(alice.id);
  254. const received = deliveries.find((delivery) => delivery.eventType === 'received');
  255. assert.ok(received);
  256. assert.equal(received.webhookId, webhook.id);
  257. assert.equal(received.sendEventId, 0);
  258. assert.equal(received.inboundMessageId, inboundMessage.id);
  259. const payload = JSON.parse(received.payloadJson);
  260. assert.equal(payload.type, 'email.received');
  261. assert.equal(payload.data.mailbox, 'support@inbound-hook.example');
  262. assert.equal(payload.data.rfc_message_id, '<inbound-message@example.net>');
  263. assert.equal(payload.data.text, 'Plain text');
  264. assert.equal(payload.data.html, '<p>HTML</p>');
  265. assert.equal('raw_message' in payload.data, false);
  266. assert.equal(deliveries.find((delivery) => delivery.eventType === 'sent')?.webhookId, sendWebhook.id);
  267. const testDelivery = enqueueWebhookTestDelivery(alice.id, webhook.id);
  268. assert.equal(testDelivery.inboundMessageId, 0);
  269. assert.equal(JSON.parse(testDelivery.payloadJson).type, 'email.received');
  270. updateWebhook(alice.id, webhook.id, { enabled: false });
  271. const disabledMessage = createInboundMessage(mailbox, {
  272. sender: 'sender@example.net',
  273. recipients: ['support@inbound-hook.example'],
  274. subject: 'Disabled callback'
  275. });
  276. assert.equal(enqueueInboundWebhookDeliveries(disabledMessage).length, 0);
  277. });
  278. test('domain override skips account webhooks for that event', () => {
  279. initDatabase(tempDataDir(), 'test-secret');
  280. const alice = createUser({ username: 'alice', email: 'alice@example.com', password: 'password123' });
  281. const domain = createDomain(alice.id, domainFixture('override.example'));
  282. const account = createWebhook(alice.id, {
  283. name: 'Account',
  284. url: 'https://hooks.example.com/account',
  285. events: ['sent', 'failed']
  286. });
  287. const domainHook = createWebhook(alice.id, {
  288. name: 'Domain',
  289. url: 'https://hooks.example.com/domain',
  290. events: ['sent'],
  291. domainId: domain.id
  292. });
  293. enqueueWebhookDeliveries({
  294. id: 7,
  295. userId: alice.id,
  296. domainId: domain.id,
  297. domain: 'override.example',
  298. status: 'sent',
  299. sender: 'noreply@override.example',
  300. recipients: ['a@example.com'],
  301. subject: 'Override',
  302. detail: '',
  303. queueId: 'Q7'
  304. });
  305. const sent = listWebhookDeliveries(alice.id);
  306. assert.equal(sent.length, 1);
  307. assert.equal(sent[0].webhookId, domainHook.id);
  308. enqueueWebhookDeliveries({
  309. id: 8,
  310. userId: alice.id,
  311. domainId: domain.id,
  312. domain: 'override.example',
  313. status: 'failed',
  314. sender: 'noreply@override.example',
  315. recipients: ['a@example.com'],
  316. subject: 'Fallback',
  317. detail: 'error',
  318. queueId: 'Q8'
  319. });
  320. const failed = listWebhookDeliveries(alice.id, { eventType: 'failed' });
  321. assert.equal(failed.length, 1);
  322. assert.equal(failed[0].webhookId, account.id);
  323. });
  324. test('logSendEvent with failed status creates webhook delivery', () => {
  325. initDatabase(tempDataDir(), 'test-secret');
  326. const alice = createUser({ username: 'alice', email: 'alice@example.com', password: 'password123' });
  327. const domain = createDomain(alice.id, domainFixture('fail.example'));
  328. createWebhook(alice.id, {
  329. name: 'Failures',
  330. url: 'https://hooks.example.com/failed',
  331. events: ['failed']
  332. });
  333. const eventId = logSendEvent({
  334. userId: alice.id,
  335. domainId: domain.id,
  336. sender: 'noreply@fail.example',
  337. recipients: ['user@example.com'],
  338. subject: 'Boom',
  339. status: 'failed',
  340. detail: 'SMTP rejected'
  341. });
  342. const deliveries = listWebhookDeliveries(alice.id);
  343. assert.equal(deliveries.length, 1);
  344. assert.equal(deliveries[0].sendEventId, eventId);
  345. assert.equal(deliveries[0].eventType, 'failed');
  346. const payload = JSON.parse(deliveries[0].payloadJson);
  347. assert.equal(payload.type, 'email.failed');
  348. assert.equal(payload.data.domain, 'fail.example');
  349. assert.equal(payload.data.detail, 'SMTP rejected');
  350. });
  351. test('updateSendEventDelivery terminal status change enqueues delivery', () => {
  352. initDatabase(tempDataDir(), 'test-secret');
  353. const alice = createUser({ username: 'alice', email: 'alice@example.com', password: 'password123' });
  354. const domain = createDomain(alice.id, domainFixture('track.example'));
  355. createWebhook(alice.id, {
  356. name: 'Sent',
  357. url: 'https://hooks.example.com/sent',
  358. events: ['sent']
  359. });
  360. const eventId = logSendEvent({
  361. userId: alice.id,
  362. domainId: domain.id,
  363. sender: 'noreply@track.example',
  364. recipients: ['recipient@example.net'],
  365. subject: 'Tracked',
  366. status: 'queued',
  367. detail: '250 2.0.0 Ok: queued as 1DAEBC3EC8'
  368. });
  369. assert.equal(listWebhookDeliveries(alice.id).length, 0);
  370. assert.equal(
  371. updateSendEventDelivery('1DAEBC3EC8', {
  372. at: '2026-07-08T04:15:21.000Z',
  373. queueId: '1DAEBC3EC8',
  374. recipient: 'recipient@example.net',
  375. relay: 'mx.example.net[203.0.113.25]:25',
  376. dsn: '2.0.0',
  377. status: 'sent',
  378. response: '250 OK',
  379. raw: 'raw postfix line'
  380. }),
  381. true
  382. );
  383. const deliveries = listWebhookDeliveries(alice.id);
  384. assert.equal(deliveries.length, 1);
  385. assert.equal(deliveries[0].sendEventId, eventId);
  386. assert.equal(deliveries[0].eventType, 'sent');
  387. const payload = JSON.parse(deliveries[0].payloadJson);
  388. assert.equal(payload.type, 'email.sent');
  389. assert.equal(payload.data.domain, 'track.example');
  390. assert.equal(payload.data.queue_id, '1DAEBC3EC8');
  391. assert.equal(payload.data.delivered_at, '2026-07-08T04:15:21.000Z');
  392. // Same terminal status again (duplicate attempt ignored) must not create another delivery.
  393. updateSendEventDelivery('1DAEBC3EC8', {
  394. at: '2026-07-08T04:15:21.000Z',
  395. queueId: '1DAEBC3EC8',
  396. recipient: 'recipient@example.net',
  397. relay: 'mx.example.net[203.0.113.25]:25',
  398. dsn: '2.0.0',
  399. status: 'sent',
  400. response: '250 OK',
  401. raw: 'raw postfix line'
  402. });
  403. assert.equal(listWebhookDeliveries(alice.id).length, 1);
  404. });
  405. test('claim, complete success/failure, replay, test delivery, and dead path', () => {
  406. initDatabase(tempDataDir(), 'test-secret');
  407. const alice = createUser({ username: 'alice', email: 'alice@example.com', password: 'password123' });
  408. createDomain(alice.id, domainFixture('worker.example'));
  409. const webhook = createWebhook(alice.id, {
  410. name: 'Worker',
  411. url: 'https://hooks.example.com/worker',
  412. events: ['sent', 'failed']
  413. });
  414. enqueueWebhookDeliveries({
  415. id: 99,
  416. userId: alice.id,
  417. domainId: null,
  418. status: 'sent',
  419. sender: 'noreply@worker.example',
  420. recipients: ['a@example.com'],
  421. subject: 'Work',
  422. detail: '',
  423. queueId: 'W99'
  424. });
  425. const claimed = claimWebhookDeliveries(5);
  426. assert.equal(claimed.length, 1);
  427. assert.equal(claimed[0].webhook.id, webhook.id);
  428. assert.equal(claimed[0].webhook.url, 'https://hooks.example.com/worker');
  429. assert.ok(claimed[0].webhook.secret);
  430. assert.equal(claimed[0].delivery.status, 'processing');
  431. assert.match(claimed[0].webhook.secret, /^whsec_/);
  432. const success = completeWebhookDeliverySuccess(claimed[0].delivery.id, {
  433. responseStatus: 200,
  434. bodyPreview: 'ok'
  435. });
  436. assert.equal(success.status, 'success');
  437. assert.equal(success.attemptCount, 1);
  438. assert.equal(success.responseStatus, 200);
  439. const testDelivery = enqueueWebhookTestDelivery(alice.id, webhook.id);
  440. assert.equal(testDelivery.sendEventId, 0);
  441. assert.equal(testDelivery.status, 'pending');
  442. const testPayload = JSON.parse(testDelivery.payloadJson);
  443. assert.equal(testPayload.data.test, true);
  444. assert.equal(testPayload.data.message_id, 'mh-test');
  445. assert.equal(testPayload.id, `whd_${testDelivery.id}`);
  446. const reused = enqueueWebhookTestDelivery(alice.id, webhook.id);
  447. assert.equal(reused.id, testDelivery.id);
  448. assert.equal(reused.status, 'pending');
  449. assert.equal(reused.attemptCount, 0);
  450. const claimedTest = claimWebhookDeliveries(5);
  451. assert.equal(claimedTest.length, 1);
  452. const failed = completeWebhookDeliveryFailure(claimedTest[0].delivery.id, {
  453. responseStatus: 500,
  454. bodyPreview: 'err',
  455. error: 'server error'
  456. });
  457. assert.equal(failed.status, 'pending');
  458. assert.equal(failed.attemptCount, 1);
  459. assert.ok(failed.nextAttemptAt > failed.lastAttemptAt);
  460. const replayed = replayWebhookDelivery(alice.id, failed.id);
  461. assert.equal(replayed.status, 'pending');
  462. assert.equal(replayed.attemptCount, 0);
  463. assert.equal(replayed.error, '');
  464. assert.equal(replayed.responseBodyPreview, '');
  465. const processing = claimWebhookDeliveries(1)[0];
  466. assert.throws(() => replayWebhookDelivery(alice.id, processing.delivery.id), /投递中|租约/);
  467. enqueueWebhookDeliveries({
  468. id: 100,
  469. userId: alice.id,
  470. status: 'failed',
  471. sender: 'noreply@worker.example',
  472. recipients: ['b@example.com'],
  473. subject: 'Dead path',
  474. detail: 'x'
  475. });
  476. let row = listWebhookDeliveries(alice.id, { eventType: 'failed' }).find((d) => d.sendEventId === 100);
  477. assert.ok(row);
  478. while (row.status !== 'dead') {
  479. row = completeWebhookDeliveryFailure(row.id, { responseStatus: 502, error: 'down' });
  480. }
  481. assert.equal(row.status, 'dead');
  482. assert.equal(row.attemptCount, MAX_WEBHOOK_ATTEMPTS);
  483. const reset = replayWebhookDelivery(alice.id, row.id);
  484. assert.equal(reset.status, 'pending');
  485. assert.equal(reset.attemptCount, 0);
  486. });
  487. function domainFixture(domain) {
  488. return {
  489. domain,
  490. selector: 'mh202607',
  491. verificationToken: 'token',
  492. dkimPublic: 'public',
  493. dkimPrivate: 'private',
  494. senderHost: `mail.${domain}`,
  495. sendingIp: '127.0.0.1',
  496. spfExtra: '',
  497. dmarcPolicy: 'none',
  498. dmarcRua: ''
  499. };
  500. }
  501. function tempDataDir() {
  502. return mkdtempSync(path.join(tmpdir(), 'mailhub-webhook-db-'));
  503. }