@bernierllc/email-webhook-events 1.4.0 ā 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/EmailWebhookEventsService.js +4 -0
- package/dist/EmailWebhookEventsService.js.map +1 -1
- package/dist/analytics/AnalyticsEngine.js +2 -1
- package/dist/analytics/AnalyticsEngine.js.map +1 -1
- package/dist/errors.d.ts +12 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +25 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/persistence/InMemoryPersistenceAdapter.js +2 -4
- package/dist/persistence/InMemoryPersistenceAdapter.js.map +1 -1
- package/package.json +6 -2
- package/.eslintrc.cjs +0 -30
- package/CHANGELOG.md +0 -131
- package/__tests__/EmailWebhookEventsService.test.ts +0 -251
- package/__tests__/WebhookEventCallbacks.test.ts +0 -343
- package/__tests__/analytics/AnalyticsEngine.test.ts +0 -250
- package/__tests__/fixtures/sendgrid-webhooks/.gitkeep +0 -0
- package/__tests__/fixtures/sendgrid-webhooks/README.md +0 -63
- package/__tests__/fixtures/sendgrid-webhooks/deferred.json +0 -31
- package/__tests__/fixtures/sendgrid-webhooks/delivered.json +0 -83
- package/__tests__/fixtures/sendgrid-webhooks/open.json +0 -83
- package/__tests__/integration/ngrok-tunnel-manager.ts +0 -88
- package/__tests__/integration/sendgrid-webhook-integration.test.ts +0 -272
- package/__tests__/integration/webhook-server.ts +0 -188
- package/__tests__/mocks/README.md +0 -172
- package/__tests__/mocks/index.ts +0 -94
- package/__tests__/mocks/sendgrid-api-responses.ts +0 -391
- package/__tests__/mocks/sendgrid-webhook-payloads.ts +0 -463
- package/__tests__/normalizers/MailgunNormalizer.test.ts +0 -149
- package/__tests__/normalizers/PostmarkNormalizer.test.ts +0 -112
- package/__tests__/normalizers/SESNormalizer.test.ts +0 -168
- package/__tests__/normalizers/SMTP2GONormalizer.test.ts +0 -83
- package/__tests__/normalizers/SendGridNormalizer.test.ts +0 -181
- package/__tests__/persistence/PersistenceAdapter.test.ts +0 -103
- package/jest.config.cjs +0 -38
- package/src/EmailWebhookEventsService.ts +0 -290
- package/src/analytics/AnalyticsEngine.ts +0 -230
- package/src/index.ts +0 -40
- package/src/normalizers/EventNormalizer.ts +0 -13
- package/src/normalizers/MailgunNormalizer.ts +0 -72
- package/src/normalizers/PostmarkNormalizer.ts +0 -72
- package/src/normalizers/SESNormalizer.ts +0 -117
- package/src/normalizers/SMTP2GONormalizer.ts +0 -54
- package/src/normalizers/SendGridNormalizer.ts +0 -63
- package/src/persistence/InMemoryPersistenceAdapter.ts +0 -68
- package/src/persistence/PersistenceAdapter.ts +0 -15
- package/src/types.ts +0 -261
- package/tsconfig.json +0 -31
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
[
|
|
2
|
-
{
|
|
3
|
-
"headers": {
|
|
4
|
-
"host": "example-webhook.ngrok.app",
|
|
5
|
-
"user-agent": "SendGrid Event API",
|
|
6
|
-
"content-length": "405",
|
|
7
|
-
"accept-encoding": "gzip",
|
|
8
|
-
"content-type": "application/json;charset=utf-8",
|
|
9
|
-
"x-forwarded-for": "192.0.2.1",
|
|
10
|
-
"x-forwarded-host": "example-webhook.ngrok.app",
|
|
11
|
-
"x-forwarded-proto": "https",
|
|
12
|
-
"x-twilio-email-event-webhook-signature": "FAKE_SIGNATURE_MEUCIB5bSBgVWU_DO_NOT_USE_IN_PRODUCTION=",
|
|
13
|
-
"x-twilio-email-event-webhook-timestamp": "1700000000"
|
|
14
|
-
},
|
|
15
|
-
"body": {
|
|
16
|
-
"email": "test-recipient@example.com",
|
|
17
|
-
"event": "delivered",
|
|
18
|
-
"ip": "192.0.2.10",
|
|
19
|
-
"response": "250 2.0.0 OK 1700000000 example-mail-server.example.com - gsmtp",
|
|
20
|
-
"sg_event_id": "FAKE_DELIVERED_EVENT_001",
|
|
21
|
-
"sg_message_id": "FAKE_MSG_001.recvd-1234567890-abcde-1-12345678-37.0",
|
|
22
|
-
"smtp-id": "<FAKE_MSG_001@geopod-ismtpd-test>",
|
|
23
|
-
"timestamp": 1700000000,
|
|
24
|
-
"tls": 1
|
|
25
|
-
},
|
|
26
|
-
"timestamp": "2025-01-01T12:00:00.000Z",
|
|
27
|
-
"eventType": "delivered"
|
|
28
|
-
},
|
|
29
|
-
{
|
|
30
|
-
"headers": {
|
|
31
|
-
"host": "example-webhook.ngrok.app",
|
|
32
|
-
"user-agent": "SendGrid Event API",
|
|
33
|
-
"content-length": "405",
|
|
34
|
-
"accept-encoding": "gzip",
|
|
35
|
-
"content-type": "application/json;charset=utf-8",
|
|
36
|
-
"x-forwarded-for": "192.0.2.2",
|
|
37
|
-
"x-forwarded-host": "example-webhook.ngrok.app",
|
|
38
|
-
"x-forwarded-proto": "https",
|
|
39
|
-
"x-twilio-email-event-webhook-signature": "FAKE_SIGNATURE_MEUCICpeBdSY_DO_NOT_USE_IN_PRODUCTION=",
|
|
40
|
-
"x-twilio-email-event-webhook-timestamp": "1700000100"
|
|
41
|
-
},
|
|
42
|
-
"body": {
|
|
43
|
-
"email": "test-recipient@example.com",
|
|
44
|
-
"event": "delivered",
|
|
45
|
-
"ip": "192.0.2.11",
|
|
46
|
-
"response": "250 2.0.0 OK 1700000100 example-mail-server.example.com - gsmtp",
|
|
47
|
-
"sg_event_id": "FAKE_DELIVERED_EVENT_002",
|
|
48
|
-
"sg_message_id": "FAKE_MSG_002.recvd-1234567890-fghij-1-12345679-10.0",
|
|
49
|
-
"smtp-id": "<FAKE_MSG_002@geopod-ismtpd-test>",
|
|
50
|
-
"timestamp": 1700000100,
|
|
51
|
-
"tls": 1
|
|
52
|
-
},
|
|
53
|
-
"timestamp": "2025-01-01T12:01:40.000Z",
|
|
54
|
-
"eventType": "delivered"
|
|
55
|
-
},
|
|
56
|
-
{
|
|
57
|
-
"headers": {
|
|
58
|
-
"host": "example-webhook.ngrok.app",
|
|
59
|
-
"user-agent": "SendGrid Event API",
|
|
60
|
-
"content-length": "405",
|
|
61
|
-
"accept-encoding": "gzip",
|
|
62
|
-
"content-type": "application/json;charset=utf-8",
|
|
63
|
-
"x-forwarded-for": "192.0.2.3",
|
|
64
|
-
"x-forwarded-host": "example-webhook.ngrok.app",
|
|
65
|
-
"x-forwarded-proto": "https",
|
|
66
|
-
"x-twilio-email-event-webhook-signature": "FAKE_SIGNATURE_MEQCIAektsAA_DO_NOT_USE_IN_PRODUCTION=",
|
|
67
|
-
"x-twilio-email-event-webhook-timestamp": "1700000200"
|
|
68
|
-
},
|
|
69
|
-
"body": {
|
|
70
|
-
"email": "another-recipient@example.com",
|
|
71
|
-
"event": "delivered",
|
|
72
|
-
"ip": "192.0.2.12",
|
|
73
|
-
"response": "250 2.0.0 OK 1700000200 example-mail-server.example.com - gsmtp",
|
|
74
|
-
"sg_event_id": "FAKE_DELIVERED_EVENT_003",
|
|
75
|
-
"sg_message_id": "FAKE_MSG_003.recvd-1234567890-klmno-1-1234567A-10.0",
|
|
76
|
-
"smtp-id": "<FAKE_MSG_003@geopod-ismtpd-test>",
|
|
77
|
-
"timestamp": 1700000200,
|
|
78
|
-
"tls": 1
|
|
79
|
-
},
|
|
80
|
-
"timestamp": "2025-01-01T12:03:20.000Z",
|
|
81
|
-
"eventType": "delivered"
|
|
82
|
-
}
|
|
83
|
-
]
|
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
[
|
|
2
|
-
{
|
|
3
|
-
"headers": {
|
|
4
|
-
"host": "example-webhook.ngrok.app",
|
|
5
|
-
"user-agent": "SendGrid Event API",
|
|
6
|
-
"content-length": "416",
|
|
7
|
-
"accept-encoding": "gzip",
|
|
8
|
-
"content-type": "application/json;charset=utf-8",
|
|
9
|
-
"x-forwarded-for": "192.0.2.20",
|
|
10
|
-
"x-forwarded-host": "example-webhook.ngrok.app",
|
|
11
|
-
"x-forwarded-proto": "https",
|
|
12
|
-
"x-twilio-email-event-webhook-signature": "FAKE_SIGNATURE_MEQCIHN5S5nZ_DO_NOT_USE_IN_PRODUCTION=",
|
|
13
|
-
"x-twilio-email-event-webhook-timestamp": "1700000050"
|
|
14
|
-
},
|
|
15
|
-
"body": {
|
|
16
|
-
"email": "test-recipient@example.com",
|
|
17
|
-
"event": "open",
|
|
18
|
-
"ip": "192.0.2.100",
|
|
19
|
-
"sg_content_type": "html",
|
|
20
|
-
"sg_event_id": "FAKE_OPEN_EVENT_001",
|
|
21
|
-
"sg_machine_open": false,
|
|
22
|
-
"sg_message_id": "FAKE_MSG_001.recvd-1234567890-abcde-1-12345678-1B.0",
|
|
23
|
-
"timestamp": 1700000050,
|
|
24
|
-
"useragent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
|
|
25
|
-
},
|
|
26
|
-
"timestamp": "2025-01-01T12:00:50.000Z",
|
|
27
|
-
"eventType": "open"
|
|
28
|
-
},
|
|
29
|
-
{
|
|
30
|
-
"headers": {
|
|
31
|
-
"host": "example-webhook.ngrok.app",
|
|
32
|
-
"user-agent": "SendGrid Event API",
|
|
33
|
-
"content-length": "416",
|
|
34
|
-
"accept-encoding": "gzip",
|
|
35
|
-
"content-type": "application/json;charset=utf-8",
|
|
36
|
-
"x-forwarded-for": "192.0.2.21",
|
|
37
|
-
"x-forwarded-host": "example-webhook.ngrok.app",
|
|
38
|
-
"x-forwarded-proto": "https",
|
|
39
|
-
"x-twilio-email-event-webhook-signature": "FAKE_SIGNATURE_MEQCIFFHHjr5_DO_NOT_USE_IN_PRODUCTION=",
|
|
40
|
-
"x-twilio-email-event-webhook-timestamp": "1700000150"
|
|
41
|
-
},
|
|
42
|
-
"body": {
|
|
43
|
-
"email": "test-recipient@example.com",
|
|
44
|
-
"event": "open",
|
|
45
|
-
"ip": "192.0.2.101",
|
|
46
|
-
"sg_content_type": "html",
|
|
47
|
-
"sg_event_id": "FAKE_OPEN_EVENT_002",
|
|
48
|
-
"sg_machine_open": false,
|
|
49
|
-
"sg_message_id": "FAKE_MSG_002.recvd-1234567890-fghij-1-12345679-10.0",
|
|
50
|
-
"timestamp": 1700000150,
|
|
51
|
-
"useragent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
|
|
52
|
-
},
|
|
53
|
-
"timestamp": "2025-01-01T12:02:30.000Z",
|
|
54
|
-
"eventType": "open"
|
|
55
|
-
},
|
|
56
|
-
{
|
|
57
|
-
"headers": {
|
|
58
|
-
"host": "example-webhook.ngrok.app",
|
|
59
|
-
"user-agent": "SendGrid Event API",
|
|
60
|
-
"content-length": "366",
|
|
61
|
-
"accept-encoding": "gzip",
|
|
62
|
-
"content-type": "application/json;charset=utf-8",
|
|
63
|
-
"x-forwarded-for": "192.0.2.22",
|
|
64
|
-
"x-forwarded-host": "example-webhook.ngrok.app",
|
|
65
|
-
"x-forwarded-proto": "https",
|
|
66
|
-
"x-twilio-email-event-webhook-signature": "FAKE_SIGNATURE_MEUCIQC7bY6P_DO_NOT_USE_IN_PRODUCTION=",
|
|
67
|
-
"x-twilio-email-event-webhook-timestamp": "1700000250"
|
|
68
|
-
},
|
|
69
|
-
"body": {
|
|
70
|
-
"email": "another-recipient@example.com",
|
|
71
|
-
"event": "open",
|
|
72
|
-
"ip": "192.0.2.102",
|
|
73
|
-
"sg_content_type": "html",
|
|
74
|
-
"sg_event_id": "FAKE_OPEN_EVENT_003",
|
|
75
|
-
"sg_machine_open": true,
|
|
76
|
-
"sg_message_id": "FAKE_MSG_003.recvd-1234567890-klmno-1-1234567A-10.0",
|
|
77
|
-
"timestamp": 1700000250,
|
|
78
|
-
"useragent": "Mozilla/5.0 (Windows NT 5.1; rv:11.0) Gecko Firefox/11.0 (via ggpht.com GoogleImageProxy)"
|
|
79
|
-
},
|
|
80
|
-
"timestamp": "2025-01-01T12:04:10.000Z",
|
|
81
|
-
"eventType": "open"
|
|
82
|
-
}
|
|
83
|
-
]
|
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright (c) 2025 Bernier LLC
|
|
3
|
-
|
|
4
|
-
This file is licensed to the client under a limited-use license.
|
|
5
|
-
The client may use and modify this code *only within the scope of the project it was delivered for*.
|
|
6
|
-
Redistribution or use in other products or commercial offerings is not permitted without written consent from Bernier LLC.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import { connect, disconnect } from '@ngrok/ngrok';
|
|
10
|
-
|
|
11
|
-
export interface NgrokTunnelConfig {
|
|
12
|
-
port: number;
|
|
13
|
-
domain?: string; // Reserved domain (paid account feature)
|
|
14
|
-
authtoken?: string;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export class NgrokTunnelManager {
|
|
18
|
-
private listener: any;
|
|
19
|
-
private publicUrl: string | null = null;
|
|
20
|
-
|
|
21
|
-
async start(config: NgrokTunnelConfig): Promise<string> {
|
|
22
|
-
const connectOptions: any = {
|
|
23
|
-
addr: config.port,
|
|
24
|
-
authtoken: config.authtoken || process.env.NGROK_AUTH_TOKEN
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
// Use reserved domain if available (paid account feature)
|
|
28
|
-
if (config.domain) {
|
|
29
|
-
connectOptions.domain = config.domain;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
try {
|
|
33
|
-
// Disconnect any existing tunnels first (in case of previous failed runs)
|
|
34
|
-
try {
|
|
35
|
-
await disconnect();
|
|
36
|
-
console.log('Disconnected existing ngrok tunnels');
|
|
37
|
-
} catch {
|
|
38
|
-
// Ignore errors if no tunnels exist
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
this.listener = await connect(connectOptions);
|
|
42
|
-
this.publicUrl = this.listener.url();
|
|
43
|
-
|
|
44
|
-
if (!this.publicUrl) {
|
|
45
|
-
throw new Error('Failed to get ngrok tunnel URL');
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
console.log(`ngrok tunnel established: ${this.publicUrl}`);
|
|
49
|
-
return this.publicUrl;
|
|
50
|
-
} catch (error) {
|
|
51
|
-
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
52
|
-
throw new Error(`Failed to start ngrok tunnel: ${errorMessage}`);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
async stop(): Promise<void> {
|
|
57
|
-
if (this.listener) {
|
|
58
|
-
try {
|
|
59
|
-
// Use disconnect with the URL, or close the listener directly
|
|
60
|
-
const url = this.listener.url();
|
|
61
|
-
if (url) {
|
|
62
|
-
const { disconnect } = await import('@ngrok/ngrok');
|
|
63
|
-
await disconnect(url);
|
|
64
|
-
}
|
|
65
|
-
this.listener = null;
|
|
66
|
-
this.publicUrl = null;
|
|
67
|
-
console.log('ngrok tunnel closed');
|
|
68
|
-
} catch (error) {
|
|
69
|
-
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
70
|
-
console.warn(`Error closing ngrok tunnel: ${errorMessage}`);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
getUrl(): string | null {
|
|
76
|
-
return this.publicUrl;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
// Get tunnel statistics (paid account feature)
|
|
80
|
-
async getStats(): Promise<any> {
|
|
81
|
-
if (!this.listener) {
|
|
82
|
-
throw new Error('Tunnel not started');
|
|
83
|
-
}
|
|
84
|
-
// Access tunnel metrics via ngrok API
|
|
85
|
-
return this.listener;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
@@ -1,272 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright (c) 2025 Bernier LLC
|
|
3
|
-
|
|
4
|
-
This file is licensed to the client under a limited-use license.
|
|
5
|
-
The client may use and modify this code *only within the scope of the project it was delivered for*.
|
|
6
|
-
Redistribution or use in other products or commercial offerings is not permitted without written consent from Bernier LLC.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* SendGrid Webhook Integration Tests
|
|
11
|
-
*
|
|
12
|
-
* These tests use REAL SendGrid API and webhooks to:
|
|
13
|
-
* - Send emails via @bernierllc/email-manager
|
|
14
|
-
* - Receive webhooks from SendGrid via ngrok tunnel
|
|
15
|
-
* - Process webhooks through @bernierllc/email-webhook-events
|
|
16
|
-
* - Capture real webhook payloads for mock generation
|
|
17
|
-
*
|
|
18
|
-
* Prerequisites:
|
|
19
|
-
* - .env.test file with valid API keys and configuration
|
|
20
|
-
* - SendGrid webhook URL configured to ngrok URL
|
|
21
|
-
* - RUN_INTEGRATION_TESTS=true environment variable set
|
|
22
|
-
*
|
|
23
|
-
* Run with: npm test -- sendgrid-webhook-integration
|
|
24
|
-
*/
|
|
25
|
-
|
|
26
|
-
import * as dotenv from 'dotenv';
|
|
27
|
-
import * as path from 'path';
|
|
28
|
-
// Jest provides describe, it, expect, beforeAll, afterAll, beforeEach as globals
|
|
29
|
-
import { EmailManager } from '@bernierllc/email-manager';
|
|
30
|
-
import { EmailWebhookEventsService, EmailProvider, EmailEventType } from '../../src/index';
|
|
31
|
-
import { WebhookTestServer, CapturedWebhook } from './webhook-server';
|
|
32
|
-
import { NgrokTunnelManager } from './ngrok-tunnel-manager';
|
|
33
|
-
|
|
34
|
-
// Load test environment variables from .env.test (at repo root)
|
|
35
|
-
dotenv.config({ path: path.join(__dirname, '..', '..', '..', '..', '..', '.env.test') });
|
|
36
|
-
|
|
37
|
-
// Skip these tests if not explicitly enabled or if required env vars are missing
|
|
38
|
-
const SKIP_INTEGRATION_TESTS =
|
|
39
|
-
!process.env.RUN_INTEGRATION_TESTS ||
|
|
40
|
-
!process.env.SENDGRID_API_KEY ||
|
|
41
|
-
!process.env.SENDGRID_FROM_EMAIL ||
|
|
42
|
-
!process.env.NGROK_AUTH_TOKEN ||
|
|
43
|
-
!process.env.TEST_EMAIL;
|
|
44
|
-
|
|
45
|
-
const describeOrSkip = SKIP_INTEGRATION_TESTS ? describe.skip : describe;
|
|
46
|
-
|
|
47
|
-
describeOrSkip('SendGrid Webhook Integration Tests', () => {
|
|
48
|
-
let webhookServer: WebhookTestServer;
|
|
49
|
-
let ngrokTunnel: NgrokTunnelManager;
|
|
50
|
-
let webhookUrl: string;
|
|
51
|
-
let emailManager: EmailManager;
|
|
52
|
-
let webhookService: EmailWebhookEventsService;
|
|
53
|
-
|
|
54
|
-
beforeAll(async () => {
|
|
55
|
-
// Start webhook server
|
|
56
|
-
const port = parseInt(process.env.WEBHOOK_SERVER_PORT || '3000', 10);
|
|
57
|
-
webhookServer = new WebhookTestServer(port);
|
|
58
|
-
await webhookServer.start();
|
|
59
|
-
|
|
60
|
-
// Start ngrok tunnel programmatically
|
|
61
|
-
ngrokTunnel = new NgrokTunnelManager();
|
|
62
|
-
webhookUrl = await ngrokTunnel.start({
|
|
63
|
-
port,
|
|
64
|
-
domain: process.env.NGROK_RESERVED_DOMAIN,
|
|
65
|
-
authtoken: process.env.NGROK_AUTH_TOKEN
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
console.log(`\nš” Webhook URL: ${webhookUrl}/webhooks/sendgrid`);
|
|
69
|
-
console.log(`š Configure SendGrid webhook URL to: ${webhookUrl}/webhooks/sendgrid\n`);
|
|
70
|
-
|
|
71
|
-
// Initialize email manager
|
|
72
|
-
emailManager = new EmailManager({
|
|
73
|
-
providers: [],
|
|
74
|
-
scheduling: {
|
|
75
|
-
enabled: false,
|
|
76
|
-
checkIntervalMs: 60000,
|
|
77
|
-
maxRetries: 3,
|
|
78
|
-
retryDelayMs: 1000
|
|
79
|
-
} // Disable scheduler for tests
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
// Manually add provider and await to ensure it's registered before sending
|
|
83
|
-
const addResult = await emailManager.addProvider({
|
|
84
|
-
id: 'sendgrid-test',
|
|
85
|
-
name: 'SendGrid Test',
|
|
86
|
-
type: 'sendgrid',
|
|
87
|
-
config: {
|
|
88
|
-
apiKey: process.env.SENDGRID_API_KEY!,
|
|
89
|
-
fromEmail: process.env.SENDGRID_FROM_EMAIL!,
|
|
90
|
-
fromName: 'Webhook Integration Test'
|
|
91
|
-
},
|
|
92
|
-
isActive: true,
|
|
93
|
-
priority: 1
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
if (!addResult.success) {
|
|
97
|
-
throw new Error(`Failed to add SendGrid provider: ${addResult.errors?.join(', ')}`);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
console.log('ā
SendGrid provider added successfully');
|
|
101
|
-
|
|
102
|
-
// Initialize webhook service
|
|
103
|
-
webhookService = new EmailWebhookEventsService({
|
|
104
|
-
providers: [{
|
|
105
|
-
provider: EmailProvider.SENDGRID,
|
|
106
|
-
enabled: true,
|
|
107
|
-
webhookUrl: '/webhooks/sendgrid',
|
|
108
|
-
secretKey: process.env.SENDGRID_WEBHOOK_SECRET || '',
|
|
109
|
-
signatureHeader: 'X-Twilio-Email-Event-Webhook-Signature',
|
|
110
|
-
signatureAlgorithm: 'sha256'
|
|
111
|
-
}],
|
|
112
|
-
persistence: {
|
|
113
|
-
enabled: true,
|
|
114
|
-
adapter: 'memory'
|
|
115
|
-
},
|
|
116
|
-
analytics: {
|
|
117
|
-
realTimeEnabled: true,
|
|
118
|
-
aggregationInterval: 5000,
|
|
119
|
-
retentionDays: 90
|
|
120
|
-
}
|
|
121
|
-
});
|
|
122
|
-
await webhookService.initialize();
|
|
123
|
-
}, 60000); // 60 second timeout for setup
|
|
124
|
-
|
|
125
|
-
afterAll(async () => {
|
|
126
|
-
await ngrokTunnel.stop();
|
|
127
|
-
await webhookServer.stop();
|
|
128
|
-
}, 30000);
|
|
129
|
-
|
|
130
|
-
beforeEach(() => {
|
|
131
|
-
webhookServer.clearCapturedWebhooks();
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
// Helper function to wait for webhook
|
|
135
|
-
async function waitForWebhook(
|
|
136
|
-
eventType: string,
|
|
137
|
-
timeout: number = 60000
|
|
138
|
-
): Promise<CapturedWebhook | null> {
|
|
139
|
-
const startTime = Date.now();
|
|
140
|
-
|
|
141
|
-
while (Date.now() - startTime < timeout) {
|
|
142
|
-
const webhooks = webhookServer.getCapturedWebhooks();
|
|
143
|
-
const matching = webhooks.find(w => w.body.event === eventType);
|
|
144
|
-
|
|
145
|
-
if (matching) {
|
|
146
|
-
return matching;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
return null;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
it('should receive and process delivered webhook', async () => {
|
|
156
|
-
const testEmail = process.env.TEST_EMAIL!;
|
|
157
|
-
|
|
158
|
-
// Send email
|
|
159
|
-
const sendResult = await emailManager.sendEmail({
|
|
160
|
-
to: testEmail,
|
|
161
|
-
subject: 'Test Email for Webhook Testing - Delivered',
|
|
162
|
-
html: '<h1>Test</h1><p>This is a test email for delivered webhook testing.</p>',
|
|
163
|
-
text: 'Test: This is a test email for delivered webhook testing.'
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
expect(sendResult.success).toBe(true);
|
|
167
|
-
expect(sendResult.messageId).toBeDefined();
|
|
168
|
-
|
|
169
|
-
// Wait for webhook (with timeout)
|
|
170
|
-
const deliveredWebhook = await waitForWebhook('delivered', 60000);
|
|
171
|
-
|
|
172
|
-
expect(deliveredWebhook).toBeDefined();
|
|
173
|
-
expect(deliveredWebhook?.body.event).toBe('delivered');
|
|
174
|
-
expect(deliveredWebhook?.body.email).toBe(testEmail);
|
|
175
|
-
|
|
176
|
-
// Process webhook through service
|
|
177
|
-
const result = await webhookService.processWebhook({
|
|
178
|
-
provider: EmailProvider.SENDGRID,
|
|
179
|
-
signature: deliveredWebhook!.headers['x-twilio-email-event-webhook-signature'] || '',
|
|
180
|
-
payload: deliveredWebhook!.body,
|
|
181
|
-
headers: deliveredWebhook!.headers
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
expect(result.success).toBe(true);
|
|
185
|
-
expect(result.event).toBeDefined();
|
|
186
|
-
expect(result.event!.type).toBe(EmailEventType.DELIVERED);
|
|
187
|
-
expect(result.event!.recipient).toBe(testEmail);
|
|
188
|
-
}, 120000); // 2 minute timeout for email delivery
|
|
189
|
-
|
|
190
|
-
it('should capture webhook payload structure', async () => {
|
|
191
|
-
const testEmail = process.env.TEST_EMAIL!;
|
|
192
|
-
|
|
193
|
-
// Send email
|
|
194
|
-
await emailManager.sendEmail({
|
|
195
|
-
to: testEmail,
|
|
196
|
-
subject: 'Test Email for Payload Capture',
|
|
197
|
-
html: '<h1>Payload Test</h1><p>Capturing webhook payload structure.</p>',
|
|
198
|
-
text: 'Payload Test: Capturing webhook payload structure.'
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
// Wait for delivered webhook
|
|
202
|
-
const deliveredWebhook = await waitForWebhook('delivered', 60000);
|
|
203
|
-
|
|
204
|
-
expect(deliveredWebhook).toBeDefined();
|
|
205
|
-
|
|
206
|
-
// Verify payload structure
|
|
207
|
-
expect(deliveredWebhook!.body).toHaveProperty('event');
|
|
208
|
-
expect(deliveredWebhook!.body).toHaveProperty('email');
|
|
209
|
-
expect(deliveredWebhook!.body).toHaveProperty('timestamp');
|
|
210
|
-
expect(deliveredWebhook!.body).toHaveProperty('sg_message_id');
|
|
211
|
-
|
|
212
|
-
// Verify headers
|
|
213
|
-
expect(deliveredWebhook!.headers).toBeDefined();
|
|
214
|
-
|
|
215
|
-
console.log('\nš¦ Captured webhook payload structure:');
|
|
216
|
-
console.log(JSON.stringify(deliveredWebhook!.body, null, 2));
|
|
217
|
-
}, 120000);
|
|
218
|
-
|
|
219
|
-
it('should handle multiple events in single webhook', async () => {
|
|
220
|
-
const testEmail = process.env.TEST_EMAIL!;
|
|
221
|
-
|
|
222
|
-
// Send email
|
|
223
|
-
await emailManager.sendEmail({
|
|
224
|
-
to: testEmail,
|
|
225
|
-
subject: 'Test Email for Multiple Events',
|
|
226
|
-
html: '<h1>Multiple Events</h1><p>Testing multiple webhook events.</p>',
|
|
227
|
-
text: 'Multiple Events: Testing multiple webhook events.'
|
|
228
|
-
});
|
|
229
|
-
|
|
230
|
-
// Wait for delivered webhook
|
|
231
|
-
const deliveredWebhook = await waitForWebhook('delivered', 60000);
|
|
232
|
-
|
|
233
|
-
expect(deliveredWebhook).toBeDefined();
|
|
234
|
-
|
|
235
|
-
// SendGrid may send multiple events in one webhook
|
|
236
|
-
// Verify we can handle both single and multiple events
|
|
237
|
-
const allWebhooks = webhookServer.getCapturedWebhooks();
|
|
238
|
-
expect(allWebhooks.length).toBeGreaterThan(0);
|
|
239
|
-
}, 120000);
|
|
240
|
-
|
|
241
|
-
// Note: Open, click, bounce, spamreport, and unsubscribe events
|
|
242
|
-
// require manual interaction or specific test conditions.
|
|
243
|
-
// These can be tested manually or with email client automation.
|
|
244
|
-
|
|
245
|
-
it('should process webhook with signature validation', async () => {
|
|
246
|
-
const testEmail = process.env.TEST_EMAIL!;
|
|
247
|
-
|
|
248
|
-
// Send email
|
|
249
|
-
await emailManager.sendEmail({
|
|
250
|
-
to: testEmail,
|
|
251
|
-
subject: 'Test Email for Signature Validation',
|
|
252
|
-
html: '<h1>Signature Test</h1><p>Testing webhook signature validation.</p>',
|
|
253
|
-
text: 'Signature Test: Testing webhook signature validation.'
|
|
254
|
-
});
|
|
255
|
-
|
|
256
|
-
// Wait for delivered webhook
|
|
257
|
-
const deliveredWebhook = await waitForWebhook('delivered', 60000);
|
|
258
|
-
|
|
259
|
-
expect(deliveredWebhook).toBeDefined();
|
|
260
|
-
|
|
261
|
-
// Verify signature header is present (if SendGrid provides it)
|
|
262
|
-
const signatureHeader = deliveredWebhook!.headers['x-twilio-email-event-webhook-signature'];
|
|
263
|
-
|
|
264
|
-
if (signatureHeader) {
|
|
265
|
-
expect(signatureHeader).toBeDefined();
|
|
266
|
-
console.log('\nš Webhook signature header present:', signatureHeader.substring(0, 20) + '...');
|
|
267
|
-
} else {
|
|
268
|
-
console.log('\nā ļø Webhook signature header not present (may not be configured in SendGrid)');
|
|
269
|
-
}
|
|
270
|
-
}, 120000);
|
|
271
|
-
});
|
|
272
|
-
|